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..a7b13564258452ff6bfb8fd7902de03f7fbc9509 100644 |
--- a/chrome/browser/android/webapk/webapk_installer_unittest.cc |
+++ b/chrome/browser/android/webapk/webapk_installer_unittest.cc |
@@ -132,13 +132,16 @@ 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(); |
} |
@@ -248,6 +251,30 @@ class WebApkInstallerTest : public ::testing::Test { |
new WebApkInstallerRunner(best_icon_url_)); |
} |
+ void BuildWebApkProto( |
pkotwicz
2016/12/19 21:59:53
Sorry for being a pain. Can you move this function
|
+ const GURL& best_icon_url, |
+ bool stale_manifest, |
+ const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { |
+ ShortcutInfo info(GURL::EmptyGURL()); |
+ info.best_icon_url = best_icon_url; |
+ |
+ // WebApkInstaller owns itself. |
+ WebApkInstaller* installer = |
+ new TestWebApkInstaller(info, SkBitmap(), false); |
+ installer->SetTimeoutMs(100); |
+ installer->BuildWebApkProtoInBackgroundForTesting( |
+ base::Bind(&WebApkInstallerTest::OnBuiltWebApkProto, |
+ base::Unretained(this)), |
+ stale_manifest, |
+ icon_url_to_murmur2_hash_map); |
+ |
+ base::RunLoop run_loop; |
+ on_completed_callback_ = run_loop.QuitClosure(); |
+ run_loop.Run(); |
+ } |
+ |
+ webapk::WebApk* GetWebApkRequest() { return webapk_request_.get(); } |
+ |
net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } |
private: |
@@ -269,6 +296,12 @@ class WebApkInstallerTest : public ::testing::Test { |
: std::unique_ptr<net::test_server::HttpResponse>(); |
} |
+ // Called when the |webapk| request is populated. |
+ void OnBuiltWebApkProto(std::unique_ptr<webapk::WebApk> webapk) { |
+ webapk_request_ = std::move(webapk); |
+ on_completed_callback_.Run(); |
+ } |
+ |
content::TestBrowserThreadBundle thread_bundle_; |
net::EmbeddedTestServer test_server_; |
@@ -278,6 +311,12 @@ class WebApkInstallerTest : public ::testing::Test { |
// Builds response to the WebAPK creation request. |
WebApkResponseBuilder webapk_response_builder_; |
+ // The populated webapk::WebApk. |
+ std::unique_ptr<webapk::WebApk> webapk_request_; |
+ |
+ // Called after the installation process has succeeded or failed. |
+ base::Closure on_completed_callback_; |
+ |
DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); |
}; |
@@ -368,3 +407,71 @@ 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; |
+ |
+ BuildWebApkProto(GURL(""), true, icon_url_to_murmur2_hash_map); |
+ webapk::WebApk* webapk_request = 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); |
+ |
+ 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; |
+ |
+ BuildWebApkProto(best_icon_url, false, icon_url_to_murmur2_hash_map); |
+ webapk::WebApk* webapk_request = 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_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()); |
+} |