OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/webapk/webapk_installer.h" | 5 #include "chrome/browser/android/webapk/webapk_installer.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
31 #include "url/gurl.h" | 31 #include "url/gurl.h" |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 const base::FilePath::CharType kTestDataDir[] = | 35 const base::FilePath::CharType kTestDataDir[] = |
36 FILE_PATH_LITERAL("chrome/test/data"); | 36 FILE_PATH_LITERAL("chrome/test/data"); |
37 | 37 |
38 // URL of mock WebAPK server. | 38 // URL of mock WebAPK server. |
39 const std::string kServerUrl = "/webapkserver"; | 39 const char* kServerUrl = "/webapkserver"; |
| 40 |
| 41 // Icon URL from Web Manifest. We use a random file in the test data directory. |
| 42 // Since WebApkInstaller does not try to decode the file as an image it is OK |
| 43 // that the file is not an image. |
| 44 const char* kIconUrl = "/simple.html"; |
40 | 45 |
41 // URL of file to download from the WebAPK server. We use a random file in the | 46 // URL of file to download from the WebAPK server. We use a random file in the |
42 // test data directory. | 47 // test data directory. |
43 const std::string kDownloadUrl = "/simple.html"; | 48 const char* kDownloadUrl = "/simple.html"; |
44 | 49 |
45 // The package name of the downloaded WebAPK. | 50 // The package name of the downloaded WebAPK. |
46 const std::string kDownloadedWebApkPackageName = "party.unicode"; | 51 const char* kDownloadedWebApkPackageName = "party.unicode"; |
47 | 52 |
48 // WebApkInstaller subclass where | 53 // WebApkInstaller subclass where |
49 // WebApkInstaller::StartDownloadedWebApkInstall() is stubbed out. | 54 // WebApkInstaller::StartDownloadedWebApkInstall() is stubbed out. |
50 class TestWebApkInstaller : public WebApkInstaller { | 55 class TestWebApkInstaller : public WebApkInstaller { |
51 public: | 56 public: |
52 TestWebApkInstaller(const ShortcutInfo& shortcut_info, | 57 TestWebApkInstaller(const ShortcutInfo& shortcut_info, |
53 const SkBitmap& shortcut_icon) | 58 const SkBitmap& shortcut_icon) |
54 : WebApkInstaller(shortcut_info, shortcut_icon) {} | 59 : WebApkInstaller(shortcut_info, shortcut_icon) {} |
55 | 60 |
56 bool StartDownloadedWebApkInstall( | 61 bool StartDownloadedWebApkInstall( |
57 JNIEnv* env, | 62 JNIEnv* env, |
58 const base::android::ScopedJavaLocalRef<jstring>& file_path, | 63 const base::android::ScopedJavaLocalRef<jstring>& file_path, |
59 const base::android::ScopedJavaLocalRef<jstring>& package_name) override { | 64 const base::android::ScopedJavaLocalRef<jstring>& package_name) override { |
60 return true; | 65 return true; |
61 } | 66 } |
62 | 67 |
63 private: | 68 private: |
64 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); | 69 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); |
65 }; | 70 }; |
66 | 71 |
67 // Runs the WebApkInstaller installation process and blocks till done. | 72 // Runs the WebApkInstaller installation process and blocks till done. |
68 class WebApkInstallerRunner { | 73 class WebApkInstallerRunner { |
69 public: | 74 public: |
70 WebApkInstallerRunner() | 75 explicit WebApkInstallerRunner(const GURL& icon_url) |
71 : url_request_context_getter_(new net::TestURLRequestContextGetter( | 76 : url_request_context_getter_(new net::TestURLRequestContextGetter( |
72 base::ThreadTaskRunnerHandle::Get())) {} | 77 base::ThreadTaskRunnerHandle::Get())), |
| 78 icon_url_(icon_url) {} |
73 ~WebApkInstallerRunner() {} | 79 ~WebApkInstallerRunner() {} |
74 | 80 |
75 void Run() { | 81 void Run() { |
76 ShortcutInfo info(GURL::EmptyGURL()); | 82 ShortcutInfo info(GURL::EmptyGURL()); |
| 83 info.icon_url = icon_url_; |
77 | 84 |
78 // WebApkInstaller owns itself. | 85 // WebApkInstaller owns itself. |
79 WebApkInstaller* installer = new TestWebApkInstaller(info, SkBitmap()); | 86 WebApkInstaller* installer = new TestWebApkInstaller(info, SkBitmap()); |
80 | 87 |
81 installer->SetTimeoutMs(100); | 88 installer->SetTimeoutMs(100); |
| 89 |
| 90 base::RunLoop run_loop; |
| 91 on_completed_callback_ = run_loop.QuitClosure(); |
82 installer->InstallAsyncWithURLRequestContextGetter( | 92 installer->InstallAsyncWithURLRequestContextGetter( |
83 url_request_context_getter_.get(), | 93 url_request_context_getter_.get(), |
84 base::Bind(&WebApkInstallerRunner::OnCompleted, | 94 base::Bind(&WebApkInstallerRunner::OnCompleted, |
85 base::Unretained(this))); | 95 base::Unretained(this))); |
86 | |
87 base::RunLoop run_loop; | |
88 on_completed_callback_ = run_loop.QuitClosure(); | |
89 run_loop.Run(); | 96 run_loop.Run(); |
90 } | 97 } |
91 | 98 |
92 bool success() { return success_; } | 99 bool success() { return success_; } |
93 | 100 |
94 private: | 101 private: |
95 void OnCompleted(bool success) { | 102 void OnCompleted(bool success) { |
96 success_ = success; | 103 success_ = success; |
97 on_completed_callback_.Run(); | 104 on_completed_callback_.Run(); |
98 } | 105 } |
99 | 106 |
100 scoped_refptr<net::TestURLRequestContextGetter> | 107 scoped_refptr<net::TestURLRequestContextGetter> |
101 url_request_context_getter_; | 108 url_request_context_getter_; |
102 | 109 |
| 110 // The Web Manifest's icon URL. |
| 111 const GURL icon_url_; |
| 112 |
103 // Called after the installation process has succeeded or failed. | 113 // Called after the installation process has succeeded or failed. |
104 base::Closure on_completed_callback_; | 114 base::Closure on_completed_callback_; |
105 | 115 |
106 // Whether the installation process succeeded. | 116 // Whether the installation process succeeded. |
107 bool success_; | 117 bool success_; |
108 | 118 |
109 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); | 119 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); |
110 }; | 120 }; |
111 | 121 |
112 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download | 122 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download |
(...skipping 28 matching lines...) Expand all Loading... |
141 void SetUp() override { | 151 void SetUp() override { |
142 test_server_.AddDefaultHandlers(base::FilePath(kTestDataDir)); | 152 test_server_.AddDefaultHandlers(base::FilePath(kTestDataDir)); |
143 test_server_.RegisterRequestHandler( | 153 test_server_.RegisterRequestHandler( |
144 base::Bind(&WebApkInstallerTest::HandleCreateWebApkRequest, | 154 base::Bind(&WebApkInstallerTest::HandleCreateWebApkRequest, |
145 base::Unretained(this))); | 155 base::Unretained(this))); |
146 ASSERT_TRUE(test_server_.Start()); | 156 ASSERT_TRUE(test_server_.Start()); |
147 | 157 |
148 SetDefaults(); | 158 SetDefaults(); |
149 } | 159 } |
150 | 160 |
| 161 // Sets the Web Manifest's icon URL. |
| 162 void SetIconUrl(const GURL& icon_url) { icon_url_ = icon_url; } |
| 163 |
151 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller | 164 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller |
152 // should fail if the URL is not |kServerUrl|. | 165 // should fail if the URL is not |kServerUrl|. |
153 void SetWebApkServerUrl(const GURL& server_url) { | 166 void SetWebApkServerUrl(const GURL& server_url) { |
154 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 167 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
155 switches::kWebApkServerUrl, server_url.spec()); | 168 switches::kWebApkServerUrl, server_url.spec()); |
156 } | 169 } |
157 | 170 |
158 // Sets the function that should be used to build the response to the | 171 // Sets the function that should be used to build the response to the |
159 // WebAPK creation request. | 172 // WebAPK creation request. |
160 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { | 173 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { |
161 webapk_response_builder_ = builder; | 174 webapk_response_builder_ = builder; |
162 } | 175 } |
163 | 176 |
| 177 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { |
| 178 return std::unique_ptr<WebApkInstallerRunner>( |
| 179 new WebApkInstallerRunner(icon_url_)); |
| 180 } |
| 181 |
164 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } | 182 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } |
165 | 183 |
166 private: | 184 private: |
167 // Sets default configuration for running WebApkInstaller. | 185 // Sets default configuration for running WebApkInstaller. |
168 void SetDefaults() { | 186 void SetDefaults() { |
| 187 GURL icon_url = test_server_.GetURL(kIconUrl); |
| 188 SetIconUrl(icon_url); |
169 GURL server_url = test_server_.GetURL(kServerUrl); | 189 GURL server_url = test_server_.GetURL(kServerUrl); |
170 SetWebApkServerUrl(server_url); | 190 SetWebApkServerUrl(server_url); |
171 GURL download_url = test_server_.GetURL(kDownloadUrl); | 191 GURL download_url = test_server_.GetURL(kDownloadUrl); |
172 SetWebApkResponseBuilder( | 192 SetWebApkResponseBuilder( |
173 base::Bind(&BuildValidWebApkResponse, download_url)); | 193 base::Bind(&BuildValidWebApkResponse, download_url)); |
174 } | 194 } |
175 | 195 |
176 std::unique_ptr<net::test_server::HttpResponse> HandleCreateWebApkRequest( | 196 std::unique_ptr<net::test_server::HttpResponse> HandleCreateWebApkRequest( |
177 const net::test_server::HttpRequest& request) { | 197 const net::test_server::HttpRequest& request) { |
178 return (request.relative_url == kServerUrl) | 198 return (request.relative_url == kServerUrl) |
179 ? webapk_response_builder_.Run() | 199 ? webapk_response_builder_.Run() |
180 : std::unique_ptr<net::test_server::HttpResponse>(); | 200 : std::unique_ptr<net::test_server::HttpResponse>(); |
181 } | 201 } |
182 | 202 |
183 content::TestBrowserThreadBundle thread_bundle_; | 203 content::TestBrowserThreadBundle thread_bundle_; |
184 net::EmbeddedTestServer test_server_; | 204 net::EmbeddedTestServer test_server_; |
185 | 205 |
| 206 // Web Manifest's icon URL. |
| 207 GURL icon_url_; |
| 208 |
186 // Builds response to the WebAPK creation request. | 209 // Builds response to the WebAPK creation request. |
187 WebApkResponseBuilder webapk_response_builder_; | 210 WebApkResponseBuilder webapk_response_builder_; |
188 | 211 |
189 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); | 212 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); |
190 }; | 213 }; |
191 | 214 |
192 // Test installation succeeding. | 215 // Test installation succeeding. |
193 TEST_F(WebApkInstallerTest, Success) { | 216 TEST_F(WebApkInstallerTest, Success) { |
194 WebApkInstallerRunner runner; | 217 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
195 runner.Run(); | 218 runner->Run(); |
196 EXPECT_TRUE(runner.success()); | 219 EXPECT_TRUE(runner->success()); |
| 220 } |
| 221 |
| 222 // Test that installation fails if fetching the bitmap at the icon URL times |
| 223 // out. In a perfect world the fetch would never time out because the bitmap at |
| 224 // the icon URL should be in the HTTP cache. |
| 225 TEST_F(WebApkInstallerTest, IconUrlDownloadTimesOut) { |
| 226 GURL icon_url = test_server()->GetURL("/slow?1000"); |
| 227 SetIconUrl(icon_url); |
| 228 |
| 229 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 230 runner->Run(); |
| 231 EXPECT_FALSE(runner->success()); |
197 } | 232 } |
198 | 233 |
199 // Test that installation fails if the WebAPK creation request times out. | 234 // Test that installation fails if the WebAPK creation request times out. |
200 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { | 235 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { |
201 GURL server_url = test_server()->GetURL("/slow?1000"); | 236 GURL server_url = test_server()->GetURL("/slow?1000"); |
202 SetWebApkServerUrl(server_url); | 237 SetWebApkServerUrl(server_url); |
203 | 238 |
204 WebApkInstallerRunner runner; | 239 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
205 runner.Run(); | 240 runner->Run(); |
206 EXPECT_FALSE(runner.success()); | 241 EXPECT_FALSE(runner->success()); |
207 } | 242 } |
208 | 243 |
209 // Test that installation fails if the WebAPK download times out. | 244 // Test that installation fails if the WebAPK download times out. |
210 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { | 245 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { |
211 GURL download_url = test_server()->GetURL("/slow?1000"); | 246 GURL download_url = test_server()->GetURL("/slow?1000"); |
212 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); | 247 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); |
213 | 248 |
214 WebApkInstallerRunner runner; | 249 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
215 runner.Run(); | 250 runner->Run(); |
216 EXPECT_FALSE(runner.success()); | 251 EXPECT_FALSE(runner->success()); |
217 } | 252 } |
218 | 253 |
219 // Test that installation fails if the WebAPK download fails. | 254 // Test that installation fails if the WebAPK download fails. |
220 TEST_F(WebApkInstallerTest, WebApkDownloadFails) { | 255 TEST_F(WebApkInstallerTest, WebApkDownloadFails) { |
221 GURL download_url = test_server()->GetURL("/nocontent"); | 256 GURL download_url = test_server()->GetURL("/nocontent"); |
222 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); | 257 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); |
223 | 258 |
224 WebApkInstallerRunner runner; | 259 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
225 runner.Run(); | 260 runner->Run(); |
226 EXPECT_FALSE(runner.success()); | 261 EXPECT_FALSE(runner->success()); |
227 } | 262 } |
228 | 263 |
229 namespace { | 264 namespace { |
230 | 265 |
231 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. | 266 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. |
232 std::unique_ptr<net::test_server::HttpResponse> | 267 std::unique_ptr<net::test_server::HttpResponse> |
233 BuildUnparsableWebApkResponse() { | 268 BuildUnparsableWebApkResponse() { |
234 std::unique_ptr<net::test_server::BasicHttpResponse> response( | 269 std::unique_ptr<net::test_server::BasicHttpResponse> response( |
235 new net::test_server::BasicHttpResponse()); | 270 new net::test_server::BasicHttpResponse()); |
236 response->set_code(net::HTTP_OK); | 271 response->set_code(net::HTTP_OK); |
237 response->set_content("😀"); | 272 response->set_content("😀"); |
238 return std::move(response); | 273 return std::move(response); |
239 } | 274 } |
240 | 275 |
241 } // anonymous namespace | 276 } // anonymous namespace |
242 | 277 |
243 // Test that an HTTP response which cannot be parsed as a webapk::WebApkResponse | 278 // Test that an HTTP response which cannot be parsed as a webapk::WebApkResponse |
244 // is handled properly. | 279 // is handled properly. |
245 TEST_F(WebApkInstallerTest, UnparsableCreateWebApkResponse) { | 280 TEST_F(WebApkInstallerTest, UnparsableCreateWebApkResponse) { |
246 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse)); | 281 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse)); |
247 | 282 |
248 WebApkInstallerRunner runner; | 283 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
249 runner.Run(); | 284 runner->Run(); |
250 EXPECT_FALSE(runner.success()); | 285 EXPECT_FALSE(runner->success()); |
251 } | 286 } |
OLD | NEW |