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 7a12d954f3351ed2ee9ef3185600f90fb6b2caa1..ea9bd55cf553cd6312f27e5788acd25b34add1a6 100644 |
| --- a/chrome/browser/android/webapk/webapk_installer_unittest.cc |
| +++ b/chrome/browser/android/webapk/webapk_installer_unittest.cc |
| @@ -40,10 +40,11 @@ const base::FilePath::CharType kTestDataDir[] = |
| // URL of mock WebAPK server. |
| const char* kServerUrl = "/webapkserver/"; |
| -// The best primary icon URL from Web Manifest. We use a random file in the test |
| +// The URLs of best icons from Web Manifest. We use a random file in the test |
| // data directory. Since WebApkInstaller does not try to decode the file as an |
| // image it is OK that the file is not an image. |
| const char* kBestPrimaryIconUrl = "/simple.html"; |
| +const char* kBestBadgeIconUrl = "/nostore.html"; |
| // URL of file to download from the WebAPK server. We use a random file in the |
| // test data directory. |
| @@ -61,9 +62,13 @@ class TestWebApkInstaller : public WebApkInstaller { |
| public: |
| TestWebApkInstaller(content::BrowserContext* browser_context, |
| const ShortcutInfo& shortcut_info, |
| - const SkBitmap& shortcut_icon, |
| + const SkBitmap& primary_icon, |
| + const SkBitmap& badge_icon, |
| bool can_use_google_play_install_service) |
| - : WebApkInstaller(browser_context, shortcut_info, shortcut_icon), |
| + : WebApkInstaller(browser_context, |
| + shortcut_info, |
| + primary_icon, |
| + badge_icon), |
| can_use_google_play_install_service_( |
| can_use_google_play_install_service) {} |
| @@ -109,9 +114,11 @@ class TestWebApkInstaller : public WebApkInstaller { |
| class WebApkInstallerRunner { |
| public: |
| WebApkInstallerRunner(content::BrowserContext* browser_context, |
| - const GURL& best_primary_icon_url) |
| + const GURL& best_primary_icon_url, |
| + const GURL& best_badge_icon_url) |
| : browser_context_(browser_context), |
| best_primary_icon_url_(best_primary_icon_url), |
| + best_badge_icon_url_(best_badge_icon_url), |
| can_use_google_play_install_service_(false) {} |
| ~WebApkInstallerRunner() {} |
| @@ -132,7 +139,8 @@ class WebApkInstallerRunner { |
| const int kWebApkVersion = 1; |
| std::map<std::string, std::string> icon_url_to_murmur2_hash{ |
| - {best_primary_icon_url_.spec(), "0"}}; |
| + {best_primary_icon_url_.spec(), "0"}, |
| + {best_badge_icon_url_.spec(), "0"}}; |
| WebApkInstaller::UpdateAsyncForTesting( |
| CreateWebApkInstaller(), kDownloadedWebApkPackageName, kWebApkVersion, |
| @@ -145,10 +153,11 @@ class WebApkInstallerRunner { |
| WebApkInstaller* CreateWebApkInstaller() { |
| ShortcutInfo info(GURL::EmptyGURL()); |
| info.best_primary_icon_url = best_primary_icon_url_; |
| + info.best_badge_icon_url = best_badge_icon_url_; |
| // WebApkInstaller owns itself. |
| WebApkInstaller* installer = |
| - new TestWebApkInstaller(browser_context_, info, SkBitmap(), |
| + new TestWebApkInstaller(browser_context_, info, SkBitmap(), SkBitmap(), |
| can_use_google_play_install_service_); |
| installer->SetTimeoutMs(100); |
| return installer; |
| @@ -172,8 +181,9 @@ class WebApkInstallerRunner { |
| content::BrowserContext* browser_context_; |
| - // The Web Manifest's icon URL. |
| + // The Web Manifest's icon URLs. |
| const GURL best_primary_icon_url_; |
| + const GURL best_badge_icon_url_; |
| // Called after the installation process has succeeded or failed. |
| base::Closure on_completed_callback_; |
| @@ -216,14 +226,16 @@ class BuildProtoRunner { |
| void BuildSync( |
| const GURL& best_primary_icon_url, |
| + const GURL& best_badge_icon_url, |
| const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| bool is_manifest_stale) { |
| ShortcutInfo info(GURL::EmptyGURL()); |
| info.best_primary_icon_url = best_primary_icon_url; |
| + info.best_badge_icon_url = best_badge_icon_url; |
| // WebApkInstaller owns itself. |
| - WebApkInstaller* installer = |
| - new TestWebApkInstaller(browser_context_, info, SkBitmap(), false); |
| + WebApkInstaller* installer = new TestWebApkInstaller( |
| + browser_context_, info, SkBitmap(), SkBitmap(), false); |
| installer->BuildWebApkProtoInBackgroundForTesting( |
| base::Bind(&BuildProtoRunner::OnBuiltWebApkProto, |
| base::Unretained(this)), |
| @@ -286,6 +298,11 @@ class WebApkInstallerTest : public ::testing::Test { |
| best_primary_icon_url_ = best_primary_icon_url; |
| } |
| + // Sets the best Web Manifest's badge icon URL. |
| + void SetBestBadgeIconUrl(const GURL& best_badge_icon_url) { |
| + best_badge_icon_url_ = best_badge_icon_url; |
| + } |
| + |
| // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller |
| // should fail if the URL is not |kServerUrl|. |
| void SetWebApkServerUrl(const GURL& server_url) { |
| @@ -300,8 +317,8 @@ class WebApkInstallerTest : public ::testing::Test { |
| } |
| std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { |
| - return std::unique_ptr<WebApkInstallerRunner>( |
| - new WebApkInstallerRunner(profile_.get(), best_primary_icon_url_)); |
| + return std::unique_ptr<WebApkInstallerRunner>(new WebApkInstallerRunner( |
| + profile_.get(), best_primary_icon_url_, best_badge_icon_url_)); |
| } |
| std::unique_ptr<BuildProtoRunner> CreateBuildProtoRunner() { |
| @@ -314,13 +331,11 @@ class WebApkInstallerTest : public ::testing::Test { |
| private: |
| // Sets default configuration for running WebApkInstaller. |
| void SetDefaults() { |
| - GURL best_primary_icon_url = test_server_.GetURL(kBestPrimaryIconUrl); |
| - SetBestPrimaryIconUrl(best_primary_icon_url); |
| - GURL server_url = test_server_.GetURL(kServerUrl); |
| - SetWebApkServerUrl(server_url); |
| - GURL download_url = test_server_.GetURL(kDownloadUrl); |
| - SetWebApkResponseBuilder( |
| - base::Bind(&BuildValidWebApkResponse, download_url)); |
| + SetBestPrimaryIconUrl(test_server_.GetURL(kBestPrimaryIconUrl)); |
| + SetBestBadgeIconUrl(test_server_.GetURL(kBestBadgeIconUrl)); |
| + SetWebApkServerUrl(test_server_.GetURL(kServerUrl)); |
| + SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, |
| + test_server_.GetURL(kDownloadUrl))); |
| } |
| std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( |
| @@ -334,8 +349,9 @@ class WebApkInstallerTest : public ::testing::Test { |
| content::TestBrowserThreadBundle thread_bundle_; |
| net::EmbeddedTestServer test_server_; |
| - // Web Manifest's icon URL. |
| + // Web Manifest's icon URLs. |
| GURL best_primary_icon_url_; |
| + GURL best_badge_icon_url_; |
| // Builds response to the WebAPK creation request. |
| WebApkResponseBuilder webapk_response_builder_; |
| @@ -354,8 +370,18 @@ TEST_F(WebApkInstallerTest, Success) { |
| // URL times out. In a perfect world the fetch would never time out because the |
| // bitmap at the best primary icon URL should be in the HTTP cache. |
| TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) { |
| - GURL best_primary_icon_url = test_server()->GetURL("/slow?1000"); |
| - SetBestPrimaryIconUrl(best_primary_icon_url); |
| + SetBestPrimaryIconUrl(test_server()->GetURL("/slow?1000")); |
| + |
| + std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| + runner->RunInstallWebApk(); |
| + EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
| +} |
| + |
| +// Test that installation fails if fetching the bitmap at the best badge icon |
| +// URL times out. In a perfect world the fetch would never time out because the |
| +// bitmap at the best badge icon URL should be in the HTTP cache. |
| +TEST_F(WebApkInstallerTest, BestBadgeIconUrlDownloadTimesOut) { |
| + SetBestBadgeIconUrl(test_server()->GetURL("/slow?1000")); |
| std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| runner->RunInstallWebApk(); |
| @@ -364,8 +390,7 @@ TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) { |
| // Test that installation fails if the WebAPK creation request times out. |
| TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { |
| - GURL server_url = test_server()->GetURL("/slow?1000"); |
| - SetWebApkServerUrl(server_url); |
| + SetWebApkServerUrl(test_server()->GetURL("/slow?1000")); |
| std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| runner->RunInstallWebApk(); |
| @@ -374,8 +399,8 @@ TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { |
| // Test that installation fails if the WebAPK download times out. |
| TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { |
| - GURL download_url = test_server()->GetURL("/slow?1000"); |
| - SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); |
| + SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, |
| + test_server()->GetURL("/slow?1000"))); |
| std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| runner->RunInstallWebApk(); |
| @@ -384,8 +409,8 @@ TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { |
| // Test that installation fails if the WebAPK download fails. |
| TEST_F(WebApkInstallerTest, WebApkDownloadFails) { |
| - GURL download_url = test_server()->GetURL("/nocontent"); |
| - SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); |
| + SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, |
| + test_server()->GetURL("/nocontent"))); |
| std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| runner->RunInstallWebApk(); |
| @@ -459,7 +484,7 @@ TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { |
| icon_url_to_murmur2_hash[icon_url_2.spec()] = icon_murmur2_hash_2; |
| std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
| - runner->BuildSync(GURL(""), icon_url_to_murmur2_hash, |
| + runner->BuildSync(GURL(), GURL(), icon_url_to_murmur2_hash, |
| true /* is_manifest_stale*/); |
| webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| ASSERT_NE(nullptr, webapk_request); |
| @@ -487,33 +512,95 @@ TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { |
| // 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) { |
|
pkotwicz
2017/04/06 18:58:31
Please split this test up into two separate tests
F
2017/04/06 20:33:24
Done.
|
| - GURL icon_url_1 = test_server()->GetURL("/icon.png"); |
| - GURL best_primary_icon_url = test_server()->GetURL(kBestPrimaryIconUrl); |
| - std::string icon_murmur2_hash_1 = "1"; |
| - std::string best_primary_icon_murmur2_hash = "0"; |
| - std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| - icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; |
| - icon_url_to_murmur2_hash[best_primary_icon_url.spec()] = |
| - best_primary_icon_murmur2_hash; |
| - |
| - std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
| - runner->BuildSync(best_primary_icon_url, icon_url_to_murmur2_hash, |
| - false /* is_manifest_stale*/); |
| - webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| - ASSERT_NE(nullptr, webapk_request); |
| - |
| - webapk::WebAppManifest manifest = webapk_request->manifest(); |
| - ASSERT_EQ(2, manifest.icons_size()); |
| - |
| - webapk::Image icons[2]; |
| - for (int i = 0; i < 2; ++i) |
| - icons[i] = manifest.icons(i); |
| - |
| - EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src()); |
| - EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash()); |
| - EXPECT_TRUE(icons[0].has_image_data()); |
| + // Use different icons for primary icon and badge icon. |
| + { |
| + GURL icon_url_1 = test_server()->GetURL("/icon.png"); |
| + GURL best_primary_icon_url = test_server()->GetURL(kBestPrimaryIconUrl); |
| + GURL best_badge_icon_url = test_server()->GetURL(kBestBadgeIconUrl); |
| + std::string icon_murmur2_hash_1 = "0"; |
| + std::string best_primary_icon_murmur2_hash = "1"; |
| + std::string best_badge_icon_murmur2_hash = "2"; |
| + std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| + icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; |
|
pkotwicz
2017/04/06 18:58:31
How about:
std::string icon_url_1 = test_server()-
F
2017/04/06 20:33:24
Done.
pkotwicz
2017/04/06 22:08:52
Please make the same change for |best_primary_icon
F
2017/04/06 23:42:46
Done.
|
| + icon_url_to_murmur2_hash[best_primary_icon_url.spec()] = |
| + best_primary_icon_murmur2_hash; |
| + icon_url_to_murmur2_hash[best_badge_icon_url.spec()] = |
| + best_badge_icon_murmur2_hash; |
| + |
| + std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
| + runner->BuildSync(best_primary_icon_url, best_badge_icon_url, |
| + icon_url_to_murmur2_hash, false /* is_manifest_stale*/); |
| + webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| + ASSERT_NE(nullptr, webapk_request); |
| + |
| + webapk::WebAppManifest manifest = webapk_request->manifest(); |
| + ASSERT_EQ(3, manifest.icons_size()); |
| + |
| + webapk::Image icons[3]; |
| + for (int i = 0; i < 3; ++i) |
| + icons[i] = manifest.icons(i); |
| + |
| + // Check protobuf fields for /icon.png. |
| + EXPECT_EQ(icon_url_1.spec(), icons[0].src()); |
| + EXPECT_EQ(icon_murmur2_hash_1, icons[0].hash()); |
| + EXPECT_FALSE(icons[0].has_image_data()); |
| + |
| + // Check protobuf fields for kBestBadgeIconUrl. |
| + EXPECT_EQ(best_badge_icon_url.spec(), icons[1].src()); |
| + EXPECT_EQ(best_badge_icon_murmur2_hash, icons[1].hash()); |
| + ASSERT_EQ(1, icons[1].usages_size()); |
| + EXPECT_EQ(webapk::Image::BADGE_ICON, icons[1].usages(0)); |
| + EXPECT_TRUE(icons[1].has_image_data()); |
| + |
| + // Check protobuf fields for kBestPrimaryIconUrl. |
| + EXPECT_EQ(best_primary_icon_url.spec(), icons[2].src()); |
| + EXPECT_EQ(best_primary_icon_murmur2_hash, icons[2].hash()); |
| + ASSERT_EQ(1, icons[2].usages_size()); |
| + EXPECT_EQ(webapk::Image::PRIMARY_ICON, icons[2].usages(0)); |
| + EXPECT_TRUE(icons[2].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()); |
| + // Use one icon for both primary icon and badge icon. |
| + { |
| + GURL icon_url_1 = test_server()->GetURL("/icon.png"); |
| + GURL best_primary_icon_url = test_server()->GetURL(kBestPrimaryIconUrl); |
| + GURL best_badge_icon_url = test_server()->GetURL(kBestPrimaryIconUrl); |
| + std::string icon_murmur2_hash_1 = "1"; |
| + std::string best_primary_icon_murmur2_hash = "0"; |
| + std::string best_badge_icon_murmur2_hash = "0"; |
| + std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| + icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; |
| + icon_url_to_murmur2_hash[best_primary_icon_url.spec()] = |
| + best_primary_icon_murmur2_hash; |
| + icon_url_to_murmur2_hash[best_badge_icon_url.spec()] = |
| + best_badge_icon_murmur2_hash; |
| + |
| + std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
| + runner->BuildSync(best_primary_icon_url, best_badge_icon_url, |
| + icon_url_to_murmur2_hash, false /* is_manifest_stale*/); |
| + webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| + ASSERT_NE(nullptr, webapk_request); |
| + |
| + webapk::WebAppManifest manifest = webapk_request->manifest(); |
| + ASSERT_EQ(2, manifest.icons_size()); |
| + |
| + webapk::Image icons[2]; |
| + for (int i = 0; i < 2; ++i) |
| + icons[i] = manifest.icons(i); |
| + |
| + // Check protobuf fields for /icon.png. |
| + EXPECT_EQ(icon_url_1.spec(), icons[0].src()); |
| + EXPECT_EQ(icon_murmur2_hash_1, icons[0].hash()); |
| + EXPECT_FALSE(icons[0].has_image_data()); |
| + |
| + // Check protobuf fields for kBestPrimaryIconUrl. |
| + EXPECT_EQ(best_primary_icon_url.spec(), icons[1].src()); |
| + EXPECT_EQ(best_primary_icon_murmur2_hash, icons[1].hash()); |
| + EXPECT_EQ(best_badge_icon_url.spec(), icons[1].src()); |
| + EXPECT_EQ(best_badge_icon_murmur2_hash, icons[1].hash()); |
| + ASSERT_EQ(2, icons[1].usages_size()); |
| + EXPECT_EQ(webapk::Image::PRIMARY_ICON, icons[1].usages(0)); |
| + EXPECT_EQ(webapk::Image::BADGE_ICON, icons[1].usages(1)); |
| + EXPECT_TRUE(icons[1].has_image_data()); |
| + } |
| } |