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.
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"
21 namespace resource_provider
{
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
);
30 return std::string(buffer
.get(), read
);
33 std::vector
<std::string
> VectorWithString(const std::string
& contents
) {
34 std::vector
<std::string
> result
;
35 result
.push_back(contents
);
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
);
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
{
52 using ResourceMap
= std::map
<std::string
, std::string
>;
54 explicit ResourceFetcher(ResourceProvider
* provider
)
55 : provider_(provider
), waiting_count_(0u) {}
58 ResourceMap
* resources() { return &resources_
; }
60 void WaitForResults() {
61 ASSERT_NE(0u, waiting_count_
);
65 void GetResources(const std::vector
<std::string
>& paths
) {
67 provider_
->GetResources(mojo::Array
<mojo::String
>::From(paths
),
68 base::Bind(&ResourceFetcher::OnGotResources
,
69 base::Unretained(this), paths
));
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
) {
80 if (resources
[i
].is_valid()) {
81 MojoPlatformHandle platform_handle
;
82 ASSERT_EQ(MOJO_RESULT_OK
,
83 MojoExtractPlatformHandle(resources
[i
].release().value(),
85 base::File
file(platform_handle
);
86 contents
= ReadFile(&file
);
88 resources_
[paths
[i
]] = contents
;
91 if (waiting_count_
> 0) {
93 if (waiting_count_
== 0)
98 ResourceProvider
* provider_
;
99 ResourceMap resources_
;
100 // Number of resources we're waiting on.
101 size_t waiting_count_
;
102 base::RunLoop run_loop_
;
104 DISALLOW_COPY_AND_ASSIGN(ResourceFetcher
);
107 class ResourceProviderApplicationTest
: public mojo::test::ApplicationTestBase
{
109 ResourceProviderApplicationTest() {}
110 ~ResourceProviderApplicationTest() override
{}
113 // ApplicationTestBase:
114 void SetUp() override
{
115 ApplicationTestBase::SetUp();
116 application_impl()->ConnectToService("mojo:resource_provider",
117 &resource_provider_
);
120 ResourceProviderPtr resource_provider_
;
123 MOJO_DISALLOW_COPY_AND_ASSIGN(ResourceProviderApplicationTest
);
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"]);
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"]);
141 ASSERT_TRUE(fetcher
.resources()->count("dir/sample2") > 0u);
142 EXPECT_EQ("xxyy\n", (*fetcher
.resources())["dir/sample2"]);
146 } // namespace resource_provider