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 "mojo/shell/fetcher/about_fetcher.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <utility> | |
10 | |
11 #include "base/at_exit.h" | |
12 #include "base/command_line.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/path_service.h" | |
17 #include "base/run_loop.h" | |
18 #include "mojo/public/cpp/bindings/weak_binding_set.h" | |
19 #include "mojo/shell/application_loader.h" | |
20 #include "mojo/shell/application_manager.h" | |
21 #include "mojo/shell/package_manager/package_manager_impl.h" | |
22 #include "mojo/shell/public/cpp/interface_factory.h" | |
23 #include "mojo/shell/public/cpp/shell_client.h" | |
24 #include "mojo/shell/public/cpp/shell_connection.h" | |
25 #include "mojo/shell/public/interfaces/content_handler.mojom.h" | |
26 #include "mojo/shell/switches.h" | |
27 #include "mojo/util/filename_util.h" | |
28 #include "testing/gtest/include/gtest/gtest.h" | |
29 | |
30 namespace mojo { | |
31 namespace shell { | |
32 namespace { | |
33 | |
34 class TestContentHandler : public ShellClient, | |
35 public InterfaceFactory<mojom::ContentHandler>, | |
36 public mojom::ContentHandler { | |
37 public: | |
38 TestContentHandler() : response_number_(0) {} | |
39 ~TestContentHandler() override {} | |
40 | |
41 size_t response_number() const { return response_number_; } | |
42 const URLResponse* latest_response() const { return latest_response_.get(); } | |
43 | |
44 private: | |
45 // Overridden from ShellClient: | |
46 void Initialize(Shell* shell, const std::string& url, uint32_t id) override {} | |
47 bool AcceptConnection(Connection* connection) override { | |
48 connection->AddInterface<mojom::ContentHandler>(this); | |
49 return true; | |
50 } | |
51 | |
52 // Overridden from InterfaceFactory<mojom::ContentHandler>: | |
53 void Create(Connection* connection, | |
54 InterfaceRequest<mojom::ContentHandler> request) override { | |
55 bindings_.AddBinding(this, std::move(request)); | |
56 } | |
57 | |
58 // Overridden from mojom::ContentHandler: | |
59 void StartApplication( | |
60 InterfaceRequest<mojom::ShellClient> request, | |
61 URLResponsePtr response, | |
62 const Callback<void()>& destruct_callback) override { | |
63 response_number_++; | |
64 latest_response_ = std::move(response); | |
65 destruct_callback.Run(); | |
66 | |
67 // Drop |application| request. This results in the application manager | |
68 // dropping the InterfaceProvider interface request provided by the client | |
69 // who made the ConnectToApplication() call. Therefore the client could | |
70 // listen for connection error of the InterfaceProvider interface to learn | |
71 // that StartApplication() has been called. | |
72 } | |
73 | |
74 size_t response_number_; | |
75 URLResponsePtr latest_response_; | |
76 WeakBindingSet<mojom::ContentHandler> bindings_; | |
77 | |
78 DISALLOW_COPY_AND_ASSIGN(TestContentHandler); | |
79 }; | |
80 | |
81 class TestLoader : public ApplicationLoader { | |
82 public: | |
83 explicit TestLoader(ShellClient* delegate) : delegate_(delegate) {} | |
84 ~TestLoader() override {} | |
85 | |
86 private: | |
87 // Overridden from ApplicationLoader: | |
88 void Load(const GURL& url, | |
89 InterfaceRequest<mojom::ShellClient> request) override { | |
90 app_.reset(new ShellConnection(delegate_, std::move(request))); | |
91 } | |
92 | |
93 ShellClient* delegate_; | |
94 scoped_ptr<ShellConnection> app_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(TestLoader); | |
97 }; | |
98 | |
99 class AboutFetcherTest : public testing::Test { | |
100 public: | |
101 AboutFetcherTest() {} | |
102 ~AboutFetcherTest() override {} | |
103 | |
104 protected: | |
105 const TestContentHandler* html_content_handler() const { | |
106 return &html_content_handler_; | |
107 } | |
108 | |
109 void ConnectAndWait(const std::string& url) { | |
110 base::RunLoop run_loop; | |
111 | |
112 shell::mojom::InterfaceProviderPtr remote_interfaces; | |
113 shell::mojom::InterfaceProviderRequest remote_request = | |
114 GetProxy(&remote_interfaces); | |
115 // This connection error handler will be called when: | |
116 // - TestContentHandler::StartApplication() has been called (please see | |
117 // comments in that method); or | |
118 // - the application manager fails to fetch the requested URL. | |
119 remote_interfaces.set_connection_error_handler( | |
120 [&run_loop]() { run_loop.Quit(); }); | |
121 | |
122 scoped_ptr<ConnectToApplicationParams> params( | |
123 new ConnectToApplicationParams); | |
124 params->SetTargetURL(GURL(url)); | |
125 params->set_remote_interfaces(std::move(remote_request)); | |
126 application_manager_->ConnectToApplication(std::move(params)); | |
127 | |
128 run_loop.Run(); | |
129 } | |
130 | |
131 // Overridden from testing::Test: | |
132 void SetUp() override { | |
133 if (!ShouldRunTest()) | |
134 return; | |
135 base::FilePath shell_dir; | |
136 PathService::Get(base::DIR_MODULE, &shell_dir); | |
137 scoped_ptr<PackageManagerImpl> package_manager( | |
138 new PackageManagerImpl(shell_dir, nullptr, nullptr)); | |
139 package_manager->RegisterContentHandler( | |
140 "text/html", GURL("test:html_content_handler")); | |
141 application_manager_.reset( | |
142 new ApplicationManager(std::move(package_manager), true)); | |
143 application_manager_->SetLoaderForURL( | |
144 make_scoped_ptr(new TestLoader(&html_content_handler_)), | |
145 GURL("test:html_content_handler")); | |
146 } | |
147 | |
148 void TearDown() override { application_manager_.reset(); } | |
149 | |
150 bool ShouldRunTest() const { | |
151 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
152 switches::kDontUseRemotePackageManager); | |
153 } | |
154 | |
155 private: | |
156 base::ShadowingAtExitManager at_exit_; | |
157 TestContentHandler html_content_handler_; | |
158 base::MessageLoop loop_; | |
159 scoped_ptr<ApplicationManager> application_manager_; | |
160 | |
161 DISALLOW_COPY_AND_ASSIGN(AboutFetcherTest); | |
162 }; | |
163 | |
164 TEST_F(AboutFetcherTest, AboutBlank) { | |
165 if (!ShouldRunTest()) | |
166 return; | |
167 | |
168 ConnectAndWait("about:blank"); | |
169 | |
170 ASSERT_EQ(1u, html_content_handler()->response_number()); | |
171 | |
172 const URLResponse* response = html_content_handler()->latest_response(); | |
173 EXPECT_EQ("about:blank", response->url); | |
174 EXPECT_EQ(200u, response->status_code); | |
175 EXPECT_EQ("text/html", response->mime_type); | |
176 EXPECT_FALSE(response->body.is_valid()); | |
177 } | |
178 | |
179 TEST_F(AboutFetcherTest, UnrecognizedURL) { | |
180 if (!ShouldRunTest()) | |
181 return; | |
182 | |
183 ConnectAndWait("about:some_unrecognized_url"); | |
184 | |
185 ASSERT_EQ(1u, html_content_handler()->response_number()); | |
186 | |
187 const URLResponse* response = html_content_handler()->latest_response(); | |
188 EXPECT_EQ("about:some_unrecognized_url", response->url); | |
189 EXPECT_EQ(404u, response->status_code); | |
190 EXPECT_EQ("text/html", response->mime_type); | |
191 EXPECT_FALSE(response->body.is_valid()); | |
192 } | |
193 | |
194 } // namespace | |
195 } // namespace shell | |
196 } // namespace mojo | |
OLD | NEW |