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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 base::Bind(&WebApkInstallerRunner::OnCompleted, | 125 base::Bind(&WebApkInstallerRunner::OnCompleted, |
126 base::Unretained(this))); | 126 base::Unretained(this))); |
127 Run(); | 127 Run(); |
128 } | 128 } |
129 | 129 |
130 void RunUpdateWebApk() { | 130 void RunUpdateWebApk() { |
131 const std::string kIconMurmur2Hash = "0"; | 131 const std::string kIconMurmur2Hash = "0"; |
132 const int kWebApkVersion = 1; | 132 const int kWebApkVersion = 1; |
133 | 133 |
134 WebApkInstaller* installer = CreateWebApkInstaller(); | 134 WebApkInstaller* installer = CreateWebApkInstaller(); |
135 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
136 icon_url_to_murmur2_hash_map[best_icon_url_.spec()] = kIconMurmur2Hash; | |
135 | 137 |
136 installer->UpdateAsyncWithURLRequestContextGetter( | 138 installer->UpdateAsyncWithURLRequestContextGetter( |
137 url_request_context_getter_.get(), | 139 url_request_context_getter_.get(), |
138 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), | 140 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), |
139 kIconMurmur2Hash, | |
140 kDownloadedWebApkPackageName, | 141 kDownloadedWebApkPackageName, |
141 kWebApkVersion); | 142 kWebApkVersion, |
143 false /* stale_manifest */, | |
144 icon_url_to_murmur2_hash_map); | |
142 | 145 |
143 Run(); | 146 Run(); |
144 } | 147 } |
145 | 148 |
149 void BuildWebApkProto( | |
pkotwicz
2016/12/19 15:28:00
Can you move this to a different class. I know tha
Xi Han
2016/12/19 18:37:44
Done.
| |
150 const GURL& best_icon_url, | |
151 bool stale_manifest, | |
152 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { | |
153 best_icon_url_ = best_icon_url; | |
154 WebApkInstaller* installer = CreateWebApkInstaller(); | |
155 installer->BuildWebApkProtoInBackgroundForTesting( | |
156 base::Bind(&WebApkInstallerRunner::OnBuiltWebApkProto, | |
157 base::Unretained(this)), | |
158 stale_manifest, | |
159 icon_url_to_murmur2_hash_map); | |
160 Run(); | |
161 } | |
162 | |
146 WebApkInstaller* CreateWebApkInstaller() { | 163 WebApkInstaller* CreateWebApkInstaller() { |
147 ShortcutInfo info(GURL::EmptyGURL()); | 164 ShortcutInfo info(GURL::EmptyGURL()); |
148 info.best_icon_url = best_icon_url_; | 165 info.best_icon_url = best_icon_url_; |
149 | 166 |
150 // WebApkInstaller owns itself. | 167 // WebApkInstaller owns itself. |
151 WebApkInstaller* installer = new TestWebApkInstaller( | 168 WebApkInstaller* installer = new TestWebApkInstaller( |
152 info, SkBitmap(), has_google_play_webapk_install_delegate_); | 169 info, SkBitmap(), has_google_play_webapk_install_delegate_); |
153 installer->SetTimeoutMs(100); | 170 installer->SetTimeoutMs(100); |
154 return installer; | 171 return installer; |
155 } | 172 } |
156 | 173 |
157 void Run() { | 174 void Run() { |
158 base::RunLoop run_loop; | 175 base::RunLoop run_loop; |
159 on_completed_callback_ = run_loop.QuitClosure(); | 176 on_completed_callback_ = run_loop.QuitClosure(); |
160 run_loop.Run(); | 177 run_loop.Run(); |
161 } | 178 } |
162 | 179 |
163 bool success() { return success_; } | 180 bool success() { return success_; } |
164 | 181 |
182 webapk::WebApk* GetWebApkRequest() { return webapk_request_.get(); } | |
183 | |
165 private: | 184 private: |
166 void OnCompleted(bool success, const std::string& webapk_package) { | 185 void OnCompleted(bool success, const std::string& webapk_package) { |
167 success_ = success; | 186 success_ = success; |
168 on_completed_callback_.Run(); | 187 on_completed_callback_.Run(); |
169 } | 188 } |
170 | 189 |
190 | |
191 // Called when the |webapk| request is populated. | |
192 void OnBuiltWebApkProto(std::unique_ptr<webapk::WebApk> webapk) { | |
193 webapk_request_ = std::move(webapk); | |
194 on_completed_callback_.Run(); | |
195 } | |
196 | |
171 scoped_refptr<net::TestURLRequestContextGetter> | 197 scoped_refptr<net::TestURLRequestContextGetter> |
172 url_request_context_getter_; | 198 url_request_context_getter_; |
173 | 199 |
174 // The Web Manifest's icon URL. | 200 // The Web Manifest's icon URL. |
175 const GURL best_icon_url_; | 201 GURL best_icon_url_; |
176 | 202 |
177 // Called after the installation process has succeeded or failed. | 203 // Called after the installation process has succeeded or failed. |
178 base::Closure on_completed_callback_; | 204 base::Closure on_completed_callback_; |
179 | 205 |
180 // Whether the installation process succeeded. | 206 // Whether the installation process succeeded. |
181 bool success_; | 207 bool success_; |
182 | 208 |
183 // Whether the Google Play install delegate is available. | 209 // Whether the Google Play install delegate is available. |
184 bool has_google_play_webapk_install_delegate_; | 210 bool has_google_play_webapk_install_delegate_; |
185 | 211 |
212 // The populated webapk::WebApk. | |
213 std::unique_ptr<webapk::WebApk> webapk_request_; | |
214 | |
186 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); | 215 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); |
187 }; | 216 }; |
188 | 217 |
189 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download | 218 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download |
190 // URL. | 219 // URL. |
191 std::unique_ptr<net::test_server::HttpResponse> BuildValidWebApkResponse( | 220 std::unique_ptr<net::test_server::HttpResponse> BuildValidWebApkResponse( |
192 const GURL& download_url) { | 221 const GURL& download_url) { |
193 std::unique_ptr<webapk::WebApkResponse> response_proto( | 222 std::unique_ptr<webapk::WebApkResponse> response_proto( |
194 new webapk::WebApkResponse); | 223 new webapk::WebApkResponse); |
195 response_proto->set_package_name(kDownloadedWebApkPackageName); | 224 response_proto->set_package_name(kDownloadedWebApkPackageName); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 EXPECT_TRUE(runner->success()); | 390 EXPECT_TRUE(runner->success()); |
362 } | 391 } |
363 | 392 |
364 // Test installation succeeds using Google Play. | 393 // Test installation succeeds using Google Play. |
365 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { | 394 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { |
366 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 395 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
367 runner->SetHasGooglePlayWebApkInstallDelegate(true); | 396 runner->SetHasGooglePlayWebApkInstallDelegate(true); |
368 runner->RunInstallWebApk(); | 397 runner->RunInstallWebApk(); |
369 EXPECT_TRUE(runner->success()); | 398 EXPECT_TRUE(runner->success()); |
370 } | 399 } |
400 | |
401 // When there is no Web Manifest available for a site, an empty |best_icon_url| | |
402 // is used to build a WebApk update request. Tests the request can be built | |
403 // properly. | |
404 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { | |
405 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); | |
406 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); | |
407 std::string icon_murmur2_hash_1 = "1"; | |
408 std::string icon_murmur2_hash_2 = "2"; | |
409 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
410 icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; | |
411 icon_url_to_murmur2_hash_map[icon_url_2.spec()] = icon_murmur2_hash_2; | |
412 | |
413 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
414 runner->BuildWebApkProto(GURL(""), true, icon_url_to_murmur2_hash_map); | |
415 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | |
416 EXPECT_NE(nullptr, webapk_request); | |
pkotwicz
2016/12/19 15:28:00
If this EXPECT_NE() fails the test will crash. If
Xi Han
2016/12/19 18:37:44
Thanks for catching these, I updated all of them.
| |
417 | |
418 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
419 EXPECT_EQ(3, manifest.icons_size()); | |
pkotwicz
2016/12/19 15:28:00
ASSERT_EQ()
Xi Han
2016/12/19 18:37:44
Done.
| |
420 | |
421 webapk::Image icons[3]; | |
422 for (int i = 0; i < 3; ++i) | |
423 icons[i] = manifest.icons(i); | |
424 | |
425 EXPECT_EQ("", icons[0].src()); | |
426 EXPECT_FALSE(icons[0].has_hash()); | |
427 EXPECT_TRUE(icons[0].has_image_data()); | |
428 | |
429 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | |
430 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | |
431 EXPECT_FALSE(icons[1].has_image_data()); | |
432 | |
433 EXPECT_EQ(icon_url_2.spec(), icons[2].src()); | |
434 EXPECT_EQ(icon_murmur2_hash_2, icons[2].hash()); | |
435 EXPECT_FALSE(icons[2].has_image_data()); | |
436 } | |
437 | |
438 // Tests a WebApk install or update request is built properly when the Chrome | |
439 // knows the best icon URL of a site after fetching its Web Manifest. | |
440 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { | |
441 GURL icon_url_1 = test_server()->GetURL("/icon.png"); | |
442 GURL best_icon_url = test_server()->GetURL(kBestIconUrl); | |
443 std::string icon_murmur2_hash_1 = "1"; | |
444 std::string best_icon_murmur2_hash = "0"; | |
445 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
446 icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; | |
447 icon_url_to_murmur2_hash_map[best_icon_url.spec()] = | |
448 best_icon_murmur2_hash; | |
449 | |
450 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
451 runner->BuildWebApkProto(best_icon_url, false, icon_url_to_murmur2_hash_map); | |
452 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | |
453 EXPECT_NE(nullptr, webapk_request); | |
pkotwicz
2016/12/19 15:28:00
ASSERT_NE()
Xi Han
2016/12/19 18:37:44
Done.
| |
454 | |
455 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
456 EXPECT_EQ(2, manifest.icons_size()); | |
pkotwicz
2016/12/19 15:28:00
ASSERT_EQ()
Xi Han
2016/12/19 18:37:44
Done.
| |
457 | |
458 webapk::Image icons[2]; | |
459 for (int i = 0; i < 2; ++i) | |
460 icons[i] = manifest.icons(i); | |
461 | |
462 EXPECT_EQ(best_icon_url.spec(), icons[0].src()); | |
463 EXPECT_EQ(best_icon_murmur2_hash, icons[0].hash()); | |
464 EXPECT_TRUE(icons[0].has_image_data()); | |
465 | |
466 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | |
467 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | |
468 EXPECT_FALSE(icons[1].has_image_data()); | |
469 } | |
OLD | NEW |