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