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..e3393b365c803c8f327d431ac6d5613ad16c443d 100644 |
| --- a/chrome/browser/android/webapk/webapk_installer_unittest.cc |
| +++ b/chrome/browser/android/webapk/webapk_installer_unittest.cc |
| @@ -102,6 +102,8 @@ class TestWebApkInstaller : public WebApkInstaller { |
| DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); |
| }; |
| +} // anonymous namespace |
| + |
| // Runs the WebApkInstaller installation process/update and blocks till done. |
| class WebApkInstallerRunner { |
| public: |
| @@ -132,13 +134,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(); |
| } |
| @@ -154,6 +159,25 @@ class WebApkInstallerRunner { |
| return installer; |
| } |
| + // Populates a webapk::WebApk. |
| + void BuildWebApkProto( |
| + const GURL& best_icon_url, |
| + bool stale_manifest, |
| + const std::map<std::string, std::string> icon_url_to_murmur2_hash_map) { |
| + ShortcutInfo shortcut_info(GURL::EmptyGURL()); |
| + shortcut_info.best_icon_url = best_icon_url; |
| + |
| + base::PostTaskAndReplyWithResult( |
| + GetBackgroundTaskRunner().get(), FROM_HERE, |
| + base::Bind(&WebApkInstaller::BuildWebApkProtoInBackground, |
| + shortcut_info, SkBitmap(), stale_manifest, |
| + icon_url_to_murmur2_hash_map), |
| + base::Bind(&WebApkInstallerRunner::OnBuiltWebApkProto, |
| + base::Unretained(this))); |
| + |
| + Run(); |
|
pkotwicz
2016/12/14 04:05:41
Can you do this:
base::RunLoop run_loop;
base::Po
Xi Han
2016/12/14 18:02:23
We can't use run_loop.QuitClosure() directly. This
pkotwicz
2016/12/15 02:38:53
Can you use PostTaskAndReply() instead? https://cs
Xi Han
2016/12/16 22:39:20
Since the WebApkInstaller::BuildWebApkProtoInBackg
|
| + } |
| + |
| void Run() { |
| base::RunLoop run_loop; |
| on_completed_callback_ = run_loop.QuitClosure(); |
| @@ -162,12 +186,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(); |
| } |
| + scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
| + return content::BrowserThread::GetBlockingPool() |
| + ->GetTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| + } |
| + |
| + // 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_; |
| @@ -183,6 +221,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); |
| }; |
| @@ -204,7 +245,6 @@ std::unique_ptr<net::test_server::HttpResponse> BuildValidWebApkResponse( |
| return std::move(response); |
| } |
| -} // anonymous namespace |
| class WebApkInstallerTest : public ::testing::Test { |
| public: |
| @@ -368,3 +408,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); |
| + |
| + webapk::WebAppManifest manifest = webapk_request->manifest(); |
| + EXPECT_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; |
| + |
| + 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); |
| + |
| + webapk::WebAppManifest manifest = webapk_request->manifest(); |
| + EXPECT_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()); |
| +} |