| 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 <stddef.h> | |
| 6 #include <stdint.h> | |
| 7 | |
| 8 #include "base/containers/scoped_ptr_hash_map.h" | |
| 9 #include "base/files/file.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "components/resource_provider/public/cpp/resource_loader.h" | |
| 13 #include "components/resource_provider/public/interfaces/resource_provider.mojom
.h" | |
| 14 #include "mojo/common/common_type_converters.h" | |
| 15 #include "mojo/platform_handle/platform_handle_functions.h" | |
| 16 #include "mojo/public/cpp/bindings/array.h" | |
| 17 #include "mojo/shell/public/cpp/application_test_base.h" | |
| 18 | |
| 19 namespace resource_provider { | |
| 20 namespace { | |
| 21 | |
| 22 std::string ReadFile(base::File* file) { | |
| 23 const size_t kBufferSize = 1 << 16; | |
| 24 scoped_ptr<char[]> buffer(new char[kBufferSize]); | |
| 25 const int read = file->ReadAtCurrentPos(buffer.get(), kBufferSize); | |
| 26 if (read == -1) | |
| 27 return std::string(); | |
| 28 return std::string(buffer.get(), read); | |
| 29 } | |
| 30 | |
| 31 std::set<std::string> SetWithString(const std::string& contents) { | |
| 32 std::set<std::string> result; | |
| 33 result.insert(contents); | |
| 34 return result; | |
| 35 } | |
| 36 | |
| 37 std::set<std::string> SetWithStrings(const std::string& contents1, | |
| 38 const std::string& contents2) { | |
| 39 std::set<std::string> result; | |
| 40 result.insert(contents1); | |
| 41 result.insert(contents2); | |
| 42 return result; | |
| 43 } | |
| 44 | |
| 45 class ResourceProviderApplicationTest : public mojo::test::ApplicationTestBase { | |
| 46 public: | |
| 47 ResourceProviderApplicationTest() {} | |
| 48 ~ResourceProviderApplicationTest() override {} | |
| 49 | |
| 50 protected: | |
| 51 using ResourceContentsMap = std::map<std::string, std::string>; | |
| 52 | |
| 53 // Queries ResourceProvider for the specified resources, blocking until the | |
| 54 // resources are returned. The return map maps from the path to the contents | |
| 55 // of the file at the specified path. | |
| 56 ResourceContentsMap GetResources(const std::set<std::string>& paths) { | |
| 57 ResourceLoader loader(connector(), paths); | |
| 58 loader.BlockUntilLoaded(); | |
| 59 | |
| 60 // Load the contents of each of the handles. | |
| 61 ResourceContentsMap results; | |
| 62 for (auto& path : paths) { | |
| 63 base::File file(loader.ReleaseFile(path)); | |
| 64 results[path] = ReadFile(&file); | |
| 65 } | |
| 66 return results; | |
| 67 } | |
| 68 | |
| 69 // ApplicationTestBase: | |
| 70 void SetUp() override { | |
| 71 ApplicationTestBase::SetUp(); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 DISALLOW_COPY_AND_ASSIGN(ResourceProviderApplicationTest); | |
| 76 }; | |
| 77 | |
| 78 TEST_F(ResourceProviderApplicationTest, FetchOneResource) { | |
| 79 ResourceContentsMap results(GetResources(SetWithString("sample"))); | |
| 80 ASSERT_TRUE(results.count("sample") > 0u); | |
| 81 EXPECT_EQ("test data\n", results["sample"]); | |
| 82 } | |
| 83 | |
| 84 TEST_F(ResourceProviderApplicationTest, FetchTwoResources) { | |
| 85 ResourceContentsMap results( | |
| 86 GetResources(SetWithStrings("sample", "dir/sample2"))); | |
| 87 ASSERT_TRUE(results.count("sample") > 0u); | |
| 88 EXPECT_EQ("test data\n", results["sample"]); | |
| 89 | |
| 90 ASSERT_TRUE(results.count("dir/sample2") > 0u); | |
| 91 EXPECT_EQ("xxyy\n", results["dir/sample2"]); | |
| 92 } | |
| 93 | |
| 94 } // namespace | |
| 95 } // namespace resource_provider | |
| OLD | NEW |