Chromium Code Reviews| Index: chrome/browser/android/webapk/webapk_installer_unittest.cc |
| diff --git a/chrome/browser/android/webapk/webapk_installer_unittest.cc b/chrome/browser/android/webapk/webapk_installer_unittest.cc |
| index 0734459e9e6c4e76a8bf1dd1f2427afed6999d6c..a22c85bc31703aa7763f97dab486514e4ac7155b 100644 |
| --- a/chrome/browser/android/webapk/webapk_installer_unittest.cc |
| +++ b/chrome/browser/android/webapk/webapk_installer_unittest.cc |
| @@ -132,17 +132,34 @@ class WebApkInstallerRunner { |
| const int kWebApkVersion = 1; |
| WebApkInstaller* installer = CreateWebApkInstaller(); |
| + std::map<std::string, std::string> icon_url_to_murmur2_hash_map; |
| + icon_url_to_murmur2_hash_map[best_icon_url_.spec()] = kIconMurmur2Hash; |
| installer->UpdateAsyncWithURLRequestContextGetter( |
| url_request_context_getter_.get(), |
| base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), |
| - kIconMurmur2Hash, |
| kDownloadedWebApkPackageName, |
| - kWebApkVersion); |
| + kWebApkVersion, |
| + false /* stale_manifest */, |
| + icon_url_to_murmur2_hash_map); |
| Run(); |
| } |
| + 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.
|
| + const GURL& best_icon_url, |
| + bool stale_manifest, |
| + const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { |
| + best_icon_url_ = best_icon_url; |
| + WebApkInstaller* installer = CreateWebApkInstaller(); |
| + installer->BuildWebApkProtoInBackgroundForTesting( |
| + base::Bind(&WebApkInstallerRunner::OnBuiltWebApkProto, |
| + base::Unretained(this)), |
| + stale_manifest, |
| + icon_url_to_murmur2_hash_map); |
| + Run(); |
| + } |
| + |
| WebApkInstaller* CreateWebApkInstaller() { |
| ShortcutInfo info(GURL::EmptyGURL()); |
| info.best_icon_url = best_icon_url_; |
| @@ -162,17 +179,26 @@ class WebApkInstallerRunner { |
| bool success() { return success_; } |
| + webapk::WebApk* GetWebApkRequest() { return webapk_request_.get(); } |
| + |
| private: |
| void OnCompleted(bool success, const std::string& webapk_package) { |
| success_ = success; |
| on_completed_callback_.Run(); |
| } |
| + |
| + // Called when the |webapk| request is populated. |
| + void OnBuiltWebApkProto(std::unique_ptr<webapk::WebApk> webapk) { |
| + webapk_request_ = std::move(webapk); |
| + on_completed_callback_.Run(); |
| + } |
| + |
| scoped_refptr<net::TestURLRequestContextGetter> |
| url_request_context_getter_; |
| // The Web Manifest's icon URL. |
| - const GURL best_icon_url_; |
| + GURL best_icon_url_; |
| // Called after the installation process has succeeded or failed. |
| base::Closure on_completed_callback_; |
| @@ -183,6 +209,9 @@ class WebApkInstallerRunner { |
| // Whether the Google Play install delegate is available. |
| bool has_google_play_webapk_install_delegate_; |
| + // The populated webapk::WebApk. |
| + std::unique_ptr<webapk::WebApk> webapk_request_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); |
| }; |
| @@ -368,3 +397,73 @@ TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { |
| runner->RunInstallWebApk(); |
| EXPECT_TRUE(runner->success()); |
| } |
| + |
| +// When there is no Web Manifest available for a site, an empty |best_icon_url| |
| +// is used to build a WebApk update request. Tests the request can be built |
| +// properly. |
| +TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { |
| + GURL icon_url_1 = test_server()->GetURL("/icon1.png"); |
| + GURL icon_url_2 = test_server()->GetURL("/icon2.png"); |
| + std::string icon_murmur2_hash_1 = "1"; |
| + std::string icon_murmur2_hash_2 = "2"; |
| + std::map<std::string, std::string> icon_url_to_murmur2_hash_map; |
| + icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; |
| + icon_url_to_murmur2_hash_map[icon_url_2.spec()] = icon_murmur2_hash_2; |
| + |
| + std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| + runner->BuildWebApkProto(GURL(""), true, icon_url_to_murmur2_hash_map); |
| + webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| + 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.
|
| + |
| + webapk::WebAppManifest manifest = webapk_request->manifest(); |
| + EXPECT_EQ(3, manifest.icons_size()); |
|
pkotwicz
2016/12/19 15:28:00
ASSERT_EQ()
Xi Han
2016/12/19 18:37:44
Done.
|
| + |
| + webapk::Image icons[3]; |
| + for (int i = 0; i < 3; ++i) |
| + icons[i] = manifest.icons(i); |
| + |
| + EXPECT_EQ("", icons[0].src()); |
| + EXPECT_FALSE(icons[0].has_hash()); |
| + EXPECT_TRUE(icons[0].has_image_data()); |
| + |
| + EXPECT_EQ(icon_url_1.spec(), icons[1].src()); |
| + EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); |
| + EXPECT_FALSE(icons[1].has_image_data()); |
| + |
| + EXPECT_EQ(icon_url_2.spec(), icons[2].src()); |
| + EXPECT_EQ(icon_murmur2_hash_2, icons[2].hash()); |
| + EXPECT_FALSE(icons[2].has_image_data()); |
| +} |
| + |
| +// Tests a WebApk install or update request is built properly when the Chrome |
| +// knows the best icon URL of a site after fetching its Web Manifest. |
| +TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { |
| + GURL icon_url_1 = test_server()->GetURL("/icon.png"); |
| + GURL best_icon_url = test_server()->GetURL(kBestIconUrl); |
| + std::string icon_murmur2_hash_1 = "1"; |
| + std::string best_icon_murmur2_hash = "0"; |
| + std::map<std::string, std::string> icon_url_to_murmur2_hash_map; |
| + icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; |
| + icon_url_to_murmur2_hash_map[best_icon_url.spec()] = |
| + best_icon_murmur2_hash; |
| + |
| + std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| + runner->BuildWebApkProto(best_icon_url, false, icon_url_to_murmur2_hash_map); |
| + webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| + EXPECT_NE(nullptr, webapk_request); |
|
pkotwicz
2016/12/19 15:28:00
ASSERT_NE()
Xi Han
2016/12/19 18:37:44
Done.
|
| + |
| + webapk::WebAppManifest manifest = webapk_request->manifest(); |
| + EXPECT_EQ(2, manifest.icons_size()); |
|
pkotwicz
2016/12/19 15:28:00
ASSERT_EQ()
Xi Han
2016/12/19 18:37:44
Done.
|
| + |
| + webapk::Image icons[2]; |
| + for (int i = 0; i < 2; ++i) |
| + icons[i] = manifest.icons(i); |
| + |
| + EXPECT_EQ(best_icon_url.spec(), icons[0].src()); |
| + EXPECT_EQ(best_icon_murmur2_hash, icons[0].hash()); |
| + EXPECT_TRUE(icons[0].has_image_data()); |
| + |
| + EXPECT_EQ(icon_url_1.spec(), icons[1].src()); |
| + EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); |
| + EXPECT_FALSE(icons[1].has_image_data()); |
| +} |