Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(481)

Side by Side Diff: components/resource_provider/resource_provider_apptest.cc

Issue 1108403008: Adds resource_provider::ResourceProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/run_loop.h"
10 #include "components/resource_provider/public/interfaces/resource_provider.mojom .h"
11 #include "mojo/application/application_test_base_chromium.h"
12 #include "mojo/common/common_type_converters.h"
13 #include "third_party/mojo/src/mojo/public/cpp/application/application_delegate. h"
14 #include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h"
15 #include "third_party/mojo/src/mojo/public/cpp/application/service_provider_impl .h"
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
17 #include "third_party/mojo/src/mojo/public/cpp/system/macros.h"
18
19 namespace resource_provider {
20 namespace {
21
22 // ResourceFetcher fetches resources from a ResourceProvider, storing the
23 // results as well as running
24 // a message loop until the resource has been obtained.
25 class ResourceFetcher {
26 public:
27 using ResourceMap =
28 base::ScopedPtrHashMap<std::string, scoped_ptr<mojo::Array<uint8_t>>>;
29
30 explicit ResourceFetcher(ResourceProvider* provider)
31 : provider_(provider), waiting_count_(0u) {}
32 ~ResourceFetcher() {}
33
34 ResourceMap* resources() { return &resources_; }
35
36 void WaitForResults() {
37 ASSERT_NE(0u, waiting_count_);
38 run_loop_.Run();
39 }
40
41 void GetResource(const std::string& path) {
42 waiting_count_++;
43 provider_->GetResource(path, base::Bind(&ResourceFetcher::OnGotResource,
44 base::Unretained(this), path));
45 }
46
47 private:
48 // Callback when a resource has been fetched.
49 void OnGotResource(const std::string& path, mojo::Array<uint8_t> data) {
50 scoped_ptr<mojo::Array<uint8_t>> resource;
51 EXPECT_FALSE(resources_.contains(path));
52 if (!data.is_null())
53 resource.reset(new mojo::Array<uint8_t>(data.Pass()));
54 resources_.add(path, resource.Pass());
55
56 if (waiting_count_ > 0) {
57 waiting_count_--;
58 if (waiting_count_ == 0)
59 run_loop_.Quit();
60 }
61 }
62
63 ResourceProvider* provider_;
64 ResourceMap resources_;
65 // Number of resources we're waiting on.
66 size_t waiting_count_;
67 base::RunLoop run_loop_;
68
69 DISALLOW_COPY_AND_ASSIGN(ResourceFetcher);
70 };
71
72 class ResourceProviderApplicationTest : public mojo::test::ApplicationTestBase {
73 public:
74 ResourceProviderApplicationTest() {}
75 ~ResourceProviderApplicationTest() override {}
76
77 protected:
78 // ApplicationTestBase:
79 void SetUp() override {
80 ApplicationTestBase::SetUp();
81 application_impl()->ConnectToService("mojo:resource_provider",
82 &resource_provider_);
83 }
84
85 ResourceProviderPtr resource_provider_;
86
87 private:
88 MOJO_DISALLOW_COPY_AND_ASSIGN(ResourceProviderApplicationTest);
89 };
90
91 TEST_F(ResourceProviderApplicationTest, FetchOneResource) {
92 ResourceFetcher fetcher(resource_provider_.get());
93 fetcher.GetResource("sample");
94 fetcher.WaitForResults();
95 ASSERT_TRUE(fetcher.resources()->contains("sample"));
96 ASSERT_FALSE(fetcher.resources()->get("sample")->is_null());
97 EXPECT_EQ("test data\n",
98 fetcher.resources()->get("sample")->To<std::string>());
99 }
100
101 TEST_F(ResourceProviderApplicationTest, FetchTwoResources) {
102 ResourceFetcher fetcher(resource_provider_.get());
103 fetcher.GetResource("sample");
104 fetcher.GetResource("dir/sample2");
105 fetcher.WaitForResults();
106 ASSERT_TRUE(fetcher.resources()->contains("sample"));
107 ASSERT_TRUE(fetcher.resources()->get("sample"));
108 ASSERT_FALSE(fetcher.resources()->get("sample")->is_null());
109 EXPECT_EQ("test data\n",
110 fetcher.resources()->get("sample")->To<std::string>());
111
112 ASSERT_TRUE(fetcher.resources()->contains("dir/sample2"));
113 ASSERT_TRUE(fetcher.resources()->get("dir/sample2"));
114 ASSERT_FALSE(fetcher.resources()->get("dir/sample2")->is_null());
115 EXPECT_EQ("xxyy\n",
116 fetcher.resources()->get("dir/sample2")->To<std::string>());
117 }
118
119 } // namespace
120 } // namespace resource_provider
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698