Chromium Code Reviews

Side by Side Diff: mojo/package_manager/content_handler_unittest.cc

Issue 1358533004: Move more of ContentHandler handling into PackageManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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 "base/at_exit.h"
6 #include "base/bind.h"
7 #include "base/macros.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "mojo/application/public/cpp/application_connection.h"
13 #include "mojo/application/public/cpp/application_delegate.h"
14 #include "mojo/application/public/cpp/application_impl.h"
15 #include "mojo/application/public/cpp/interface_factory.h"
16 #include "mojo/application/public/interfaces/content_handler.mojom.h"
17 #include "mojo/application/public/interfaces/service_provider.mojom.h"
18 #include "mojo/package_manager/package_manager_impl.h"
19 #include "mojo/public/cpp/bindings/strong_binding.h"
20 #include "mojo/shell/application_loader.h"
21 #include "mojo/shell/application_manager.h"
22 #include "mojo/shell/connect_util.h"
23 #include "mojo/shell/fetcher.h"
24 #include "mojo/shell/test_package_manager.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 namespace mojo {
28 namespace package_manager {
29 namespace test {
30 namespace {
31
32 const char kTestURLString[] = "test:testService";
yzshen1 2015/09/23 16:53:46 These three urls are not used.
33 const char kTestAURLString[] = "test:TestA";
34 const char kTestBURLString[] = "test:TestB";
35
36 const char kTestMimeType[] = "test/mime-type";
37
38 class TestFetcher : public shell::Fetcher {
39 public:
40 TestFetcher(const FetchCallback& fetch_callback,
41 const GURL& url,
42 const std::string& mime_type)
43 : Fetcher(fetch_callback), url_(url), mime_type_(mime_type) {
44 loader_callback_.Run(make_scoped_ptr(this));
45 }
46 ~TestFetcher() override {}
47
48 // Fetcher:
49 const GURL& GetURL() const override { return url_; }
50 GURL GetRedirectURL() const override { return GURL("yyy"); }
51 GURL GetRedirectReferer() const override { return GURL(); }
52 URLResponsePtr AsURLResponse(base::TaskRunner* task_runner,
53 uint32_t skip) override {
54 return URLResponse::New().Pass();
55 }
56 void AsPath(
57 base::TaskRunner* task_runner,
58 base::Callback<void(const base::FilePath&, bool)> callback) override {}
59 std::string MimeType() override { return mime_type_; }
60 bool HasMojoMagic() override { return false; }
61 bool PeekFirstLine(std::string* line) override { return false; }
62
63 private:
64 const GURL url_;
65 const std::string mime_type_;
66
67 DISALLOW_COPY_AND_ASSIGN(TestFetcher);
68 };
69
70 void QuitClosure(bool* value) {
71 *value = true;
72 base::MessageLoop::current()->QuitWhenIdle();
73 }
74
75 class TestContentHandler : public ContentHandler, public ApplicationDelegate {
76 public:
77 TestContentHandler(ApplicationConnection* connection,
78 InterfaceRequest<ContentHandler> request)
79 : binding_(this, request.Pass()) {}
80
81 // ContentHandler:
82 void StartApplication(InterfaceRequest<Application> application_request,
83 URLResponsePtr response) override {
84 apps_.push_back(new ApplicationImpl(this, application_request.Pass()));
85 }
86
87 private:
88 StrongBinding<ContentHandler> binding_;
89 ScopedVector<ApplicationImpl> apps_;
90
91 DISALLOW_COPY_AND_ASSIGN(TestContentHandler);
92 };
93
94 class TestApplicationLoader : public shell::ApplicationLoader,
95 public ApplicationDelegate,
96 public InterfaceFactory<ContentHandler> {
97 public:
98 TestApplicationLoader() : num_loads_(0) {}
99
100 ~TestApplicationLoader() override {
101 test_app_.reset();
yzshen1 2015/09/23 16:53:46 nit: this line seems unnecessary.
102 }
103
104 int num_loads() const { return num_loads_; }
105 const GURL& last_requestor_url() const { return last_requestor_url_; }
106
107 private:
108 // ApplicationLoader implementation.
109 void Load(const GURL& url,
110 InterfaceRequest<Application> application_request) override {
111 ++num_loads_;
112 test_app_.reset(new ApplicationImpl(this, application_request.Pass()));
113 }
114
115 // ApplicationDelegate implementation.
116 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
117 connection->AddService<ContentHandler>(this);
118 last_requestor_url_ = GURL(connection->GetRemoteApplicationURL());
119 return true;
120 }
121 // InterfaceFactory<ContentHandler> implementation.
122 void Create(ApplicationConnection* connection,
123 InterfaceRequest<ContentHandler> request) override {
124 new TestContentHandler(connection, request.Pass());
125 }
126
127 scoped_ptr<ApplicationImpl> test_app_;
128 int num_loads_;
129 GURL last_requestor_url_;
130
131 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader);
132 };
133
134 class TestPackageManager : public PackageManagerImpl {
135 public:
136 explicit TestPackageManager(const base::FilePath& package_path)
137 : PackageManagerImpl(package_path, nullptr),
138 mime_type_(kTestMimeType) {}
139 ~TestPackageManager() override {}
140
141 void set_mime_type(const std::string& mime_type) {
142 mime_type_ = mime_type;
143 }
144
145 // PackageManagerImpl:
146 void FetchRequest(
147 URLRequestPtr request,
148 const shell::Fetcher::FetchCallback& loader_callback) override {
149 new TestFetcher(loader_callback, GURL(request->url), mime_type_);
150 }
151
152 private:
153 std::string mime_type_;
154
155 DISALLOW_COPY_AND_ASSIGN(TestPackageManager);
156 };
157
158 } // namespace
159
160 class ContentHandlerTest : public testing::Test {
161 public:
162 ContentHandlerTest()
163 : content_handler_url_("http://test.content.handler"),
164 requestor_url_("http://requestor.url") {}
165 ~ContentHandlerTest() override {}
166
167 void SetUp() override {
168 base::FilePath shell_dir;
169 PathService::Get(base::DIR_MODULE, &shell_dir);
170 test_package_manager_ = new TestPackageManager(shell_dir);
171 test_package_manager_->RegisterContentHandler(kTestMimeType,
172 content_handler_url_);
173 application_manager_.reset(new shell::ApplicationManager(
174 make_scoped_ptr(test_package_manager_)));
175 }
176
177 void TearDown() override {
178 test_package_manager_ = nullptr;
179 application_manager_.reset();
180 }
181
182 protected:
183 const GURL content_handler_url_;
184 const GURL requestor_url_;
185
186 base::MessageLoop loop_;
187 scoped_ptr<shell::ApplicationManager> application_manager_;
188 // Owned by ApplicationManager.
189 TestPackageManager* test_package_manager_;
190
191 DISALLOW_COPY_AND_ASSIGN(ContentHandlerTest);
192 };
193
194 TEST_F(ContentHandlerTest, ContentHandlerConnectionGetsRequestorURL) {
195 TestApplicationLoader* loader = new TestApplicationLoader;
196 application_manager_->SetLoaderForURL(
197 scoped_ptr<shell::ApplicationLoader>(loader),
198 content_handler_url_);
199
200 bool called = false;
201 scoped_ptr<shell::ConnectToApplicationParams> params(
202 new shell::ConnectToApplicationParams);
203 params->set_source(shell::Identity(requestor_url_));
204 params->SetTargetURL(GURL("test:test"));
205 params->set_on_application_end(
206 base::Bind(&QuitClosure, base::Unretained(&called)));
207 application_manager_->ConnectToApplication(params.Pass());
208 loop_.Run();
209 EXPECT_TRUE(called);
210
211 ASSERT_EQ(1, loader->num_loads());
212 EXPECT_EQ(requestor_url_, loader->last_requestor_url());
213 }
214
215 TEST_F(ContentHandlerTest,
216 MultipleConnectionsToContentHandlerGetSameContentHandlerId) {
217 TestApplicationLoader* content_handler_loader = new TestApplicationLoader;
218 application_manager_->SetLoaderForURL(
219 scoped_ptr<shell::ApplicationLoader>(content_handler_loader),
220 content_handler_url_);
221
222 uint32_t content_handler_id;
223 {
224 base::RunLoop run_loop;
225 scoped_ptr<shell::ConnectToApplicationParams> params(
226 new shell::ConnectToApplicationParams);
227 params->set_source(shell::Identity(requestor_url_));
228 params->SetTargetURL(GURL("test:test"));
229 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) {
230 content_handler_id = t;
231 run_loop.Quit();
232 });
233 application_manager_->ConnectToApplication(params.Pass());
234 run_loop.Run();
235 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id);
236 }
237
238 uint32_t content_handler_id2;
239 {
240 base::RunLoop run_loop;
241 scoped_ptr<shell::ConnectToApplicationParams> params(
242 new shell::ConnectToApplicationParams);
243 params->set_source(shell::Identity(requestor_url_));
244 params->SetTargetURL(GURL("test:test"));
245 params->set_connect_callback([&content_handler_id2, &run_loop](uint32_t t) {
246 content_handler_id2 = t;
247 run_loop.Quit();
248 });
249 application_manager_->ConnectToApplication(params.Pass());
250 run_loop.Run();
251 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2);
252 }
253 EXPECT_EQ(content_handler_id, content_handler_id2);
254 }
255
256 TEST_F(ContentHandlerTest, DifferedContentHandlersGetDifferentIDs) {
257 TestApplicationLoader* content_handler_loader = new TestApplicationLoader;
258 application_manager_->SetLoaderForURL(
259 scoped_ptr<shell::ApplicationLoader>(content_handler_loader),
260 content_handler_url_);
261
262 uint32_t content_handler_id;
263 {
264 base::RunLoop run_loop;
265 scoped_ptr<shell::ConnectToApplicationParams> params(
266 new shell::ConnectToApplicationParams);
267 params->set_source(shell::Identity(requestor_url_));
268 params->SetTargetURL(GURL("test:test"));
269 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) {
270 content_handler_id = t;
271 run_loop.Quit();
272 });
273 application_manager_->ConnectToApplication(params.Pass());
274 run_loop.Run();
275 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id);
276 }
277
278 const std::string mime_type2 = "test/mime-type2";
279 const GURL content_handler_url2("http://test.content.handler2");
280 test_package_manager_->set_mime_type(mime_type2);
281 test_package_manager_->RegisterContentHandler(mime_type2,
282 content_handler_url2);
283
284 TestApplicationLoader* content_handler_loader2 = new TestApplicationLoader;
285 application_manager_->SetLoaderForURL(
286 scoped_ptr<shell::ApplicationLoader>(content_handler_loader2),
287 content_handler_url2);
288
289 uint32_t content_handler_id2;
290 {
291 base::RunLoop run_loop;
292 scoped_ptr<shell::ConnectToApplicationParams> params(
293 new shell::ConnectToApplicationParams);
294 params->set_source(shell::Identity(requestor_url_));
295 params->SetTargetURL(GURL("test2:test2"));
296 params->set_connect_callback([&content_handler_id2, &run_loop](uint32_t t) {
297 content_handler_id2 = t;
298 run_loop.Quit();
299 });
300 application_manager_->ConnectToApplication(params.Pass());
301 run_loop.Run();
302 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2);
303 }
304 EXPECT_NE(content_handler_id, content_handler_id2);
305 }
306
307 TEST_F(ContentHandlerTest,
308 ConnectWithNoContentHandlerGetsInvalidContentHandlerId) {
309 application_manager_->SetLoaderForURL(
310 scoped_ptr<shell::ApplicationLoader>(new TestApplicationLoader),
311 GURL("test:test"));
312
313 uint32_t content_handler_id = 1u;
314 scoped_ptr<shell::ConnectToApplicationParams> params(
315 new shell::ConnectToApplicationParams);
316 params->SetTargetURL(GURL("test:test"));
317 params->set_connect_callback(
318 [&content_handler_id](uint32_t t) { content_handler_id = t; });
319 application_manager_->ConnectToApplication(params.Pass());
320 EXPECT_EQ(0u, content_handler_id);
321 }
322
323 } // namespace test
324 } // namespace package_manager
325 } // namespace mojo
OLDNEW

Powered by Google App Engine