1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include "base/bind.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/files/file.h"
#include "base/run_loop.h"
#include "components/resource_provider/public/interfaces/resource_provider.mojom.h"
#include "mojo/application/application_test_base_chromium.h"
#include "mojo/common/common_type_converters.h"
#include "mojo/platform_handle/platform_handle_functions.h"
#include "third_party/mojo/src/mojo/public/cpp/application/application_delegate.h"
#include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h"
#include "third_party/mojo/src/mojo/public/cpp/application/service_provider_impl.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
#include "third_party/mojo/src/mojo/public/cpp/system/macros.h"
namespace resource_provider {
namespace {
std::string ReadFile(base::File* file) {
const size_t kBufferSize = 1 << 16;
scoped_ptr<char[]> buffer(new char[kBufferSize]);
const int read = file->ReadAtCurrentPos(buffer.get(), kBufferSize);
if (read == -1)
return std::string();
return std::string(buffer.get(), read);
}
std::vector<std::string> VectorWithString(const std::string& contents) {
std::vector<std::string> result;
result.push_back(contents);
return result;
}
std::vector<std::string> VectorWithStrings(const std::string& contents1,
const std::string& contents2) {
std::vector<std::string> result;
result.push_back(contents1);
result.push_back(contents2);
return result;
}
// ResourceFetcher fetches resources from a ResourceProvider, storing the
// results as well as running
// a message loop until the resource has been obtained.
class ResourceFetcher {
public:
using ResourceMap = std::map<std::string, std::string>;
explicit ResourceFetcher(ResourceProvider* provider)
: provider_(provider), waiting_count_(0u) {}
~ResourceFetcher() {}
ResourceMap* resources() { return &resources_; }
void WaitForResults() {
ASSERT_NE(0u, waiting_count_);
run_loop_.Run();
}
void GetResources(const std::vector<std::string>& paths) {
waiting_count_++;
provider_->GetResources(mojo::Array<mojo::String>::From(paths),
base::Bind(&ResourceFetcher::OnGotResources,
base::Unretained(this), paths));
}
private:
// Callback when a resource has been fetched.
void OnGotResources(const std::vector<std::string>& paths,
mojo::Array<mojo::ScopedHandle> resources) {
ASSERT_FALSE(resources.is_null());
ASSERT_EQ(paths.size(), resources.size());
for (size_t i = 0; i < paths.size(); ++i) {
std::string contents;
if (resources[i].is_valid()) {
MojoPlatformHandle platform_handle;
ASSERT_EQ(MOJO_RESULT_OK,
MojoExtractPlatformHandle(resources[i].release().value(),
&platform_handle));
base::File file(platform_handle);
contents = ReadFile(&file);
}
resources_[paths[i]] = contents;
}
if (waiting_count_ > 0) {
waiting_count_--;
if (waiting_count_ == 0)
run_loop_.Quit();
}
}
ResourceProvider* provider_;
ResourceMap resources_;
// Number of resources we're waiting on.
size_t waiting_count_;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(ResourceFetcher);
};
class ResourceProviderApplicationTest : public mojo::test::ApplicationTestBase {
public:
ResourceProviderApplicationTest() {}
~ResourceProviderApplicationTest() override {}
protected:
// ApplicationTestBase:
void SetUp() override {
ApplicationTestBase::SetUp();
application_impl()->ConnectToService("mojo:resource_provider",
&resource_provider_);
}
ResourceProviderPtr resource_provider_;
private:
MOJO_DISALLOW_COPY_AND_ASSIGN(ResourceProviderApplicationTest);
};
TEST_F(ResourceProviderApplicationTest, FetchOneResource) {
ResourceFetcher fetcher(resource_provider_.get());
fetcher.GetResources(VectorWithString("sample"));
fetcher.WaitForResults();
ASSERT_TRUE(fetcher.resources()->count("sample") > 0u);
EXPECT_EQ("test data\n", (*fetcher.resources())["sample"]);
}
TEST_F(ResourceProviderApplicationTest, FetchTwoResources) {
ResourceFetcher fetcher(resource_provider_.get());
fetcher.GetResources(VectorWithStrings("sample", "dir/sample2"));
fetcher.WaitForResults();
ASSERT_TRUE(fetcher.resources()->count("sample") > 0u);
EXPECT_EQ("test data\n", (*fetcher.resources())["sample"]);
ASSERT_TRUE(fetcher.resources()->count("dir/sample2") > 0u);
EXPECT_EQ("xxyy\n", (*fetcher.resources())["dir/sample2"]);
}
} // namespace
} // namespace resource_provider
|