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