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