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.
7 #include "base/containers/scoped_ptr_hash_map.h"
8 #include "base/files/file.h"
9 #include "base/run_loop.h"
10 #include "components/resource_provider/public/cpp/resource_loader.h"
11 #include "components/resource_provider/public/interfaces/resource_provider.mojom.h"
12 #include "mojo/application/public/cpp/application_delegate.h"
13 #include "mojo/application/public/cpp/application_impl.h"
14 #include "mojo/application/public/cpp/application_test_base.h"
15 #include "mojo/application/public/cpp/service_provider_impl.h"
16 #include "mojo/common/common_type_converters.h"
17 #include "mojo/platform_handle/platform_handle_functions.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::set
<std::string
> SetWithString(const std::string
& contents
) {
34 std::set
<std::string
> result
;
35 result
.insert(contents
);
39 std::set
<std::string
> SetWithStrings(const std::string
& contents1
,
40 const std::string
& contents2
) {
41 std::set
<std::string
> result
;
42 result
.insert(contents1
);
43 result
.insert(contents2
);
47 class ResourceProviderApplicationTest
: public mojo::test::ApplicationTestBase
{
49 ResourceProviderApplicationTest() {}
50 ~ResourceProviderApplicationTest() override
{}
53 using ResourceContentsMap
= std::map
<std::string
, std::string
>;
55 // Queries ResourceProvider for the specified resources, blocking until the
56 // resources are returned. The return map maps from the path to the contents
57 // of the file at the specified path.
58 ResourceContentsMap
GetResources(const std::set
<std::string
>& paths
) {
59 ResourceLoader
loader(application_impl()->shell(), paths
);
60 loader
.BlockUntilLoaded();
62 // Load the contents of each of the handles.
63 ResourceContentsMap results
;
64 for (auto& path
: paths
) {
65 base::File
file(loader
.ReleaseFile(path
));
66 results
[path
] = ReadFile(&file
);
71 // ApplicationTestBase:
72 void SetUp() override
{
73 ApplicationTestBase::SetUp();
77 MOJO_DISALLOW_COPY_AND_ASSIGN(ResourceProviderApplicationTest
);
80 TEST_F(ResourceProviderApplicationTest
, FetchOneResource
) {
81 ResourceContentsMap
results(GetResources(SetWithString("sample")));
82 ASSERT_TRUE(results
.count("sample") > 0u);
83 EXPECT_EQ("test data\n", results
["sample"]);
86 TEST_F(ResourceProviderApplicationTest
, FetchTwoResources
) {
87 ResourceContentsMap
results(
88 GetResources(SetWithStrings("sample", "dir/sample2")));
89 ASSERT_TRUE(results
.count("sample") > 0u);
90 EXPECT_EQ("test data\n", results
["sample"]);
92 ASSERT_TRUE(results
.count("dir/sample2") > 0u);
93 EXPECT_EQ("xxyy\n", results
["dir/sample2"]);
97 } // namespace resource_provider