| 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 10 matching lines...) Expand all Loading... |
| 21 #include "chrome/browser/android/webapk/webapk.pb.h" | 21 #include "chrome/browser/android/webapk/webapk.pb.h" |
| 22 #include "chrome/browser/android/webapk/webapk_install_service.h" | 22 #include "chrome/browser/android/webapk/webapk_install_service.h" |
| 23 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 27 #include "net/test/embedded_test_server/embedded_test_server.h" | 27 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 28 #include "net/test/embedded_test_server/http_request.h" | 28 #include "net/test/embedded_test_server/http_request.h" |
| 29 #include "net/test/embedded_test_server/http_response.h" | 29 #include "net/test/embedded_test_server/http_response.h" |
| 30 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "third_party/skia/include/core/SkBitmap.h" | 33 #include "third_party/skia/include/core/SkBitmap.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 const base::FilePath::CharType kTestDataDir[] = | 38 const base::FilePath::CharType kTestDataDir[] = |
| 38 FILE_PATH_LITERAL("chrome/test/data"); | 39 FILE_PATH_LITERAL("chrome/test/data"); |
| 39 | 40 |
| 40 // URL of mock WebAPK server. | 41 // URL of mock WebAPK server. |
| 41 const char* kServerUrl = "/webapkserver/"; | 42 const char* kServerUrl = "/webapkserver/"; |
| 42 | 43 |
| 43 // The best primary icon URL from Web Manifest. We use a random file in the test | 44 // The URLs of best icons from Web Manifest. We use a random file in the test |
| 44 // data directory. Since WebApkInstaller does not try to decode the file as an | 45 // data directory. Since WebApkInstaller does not try to decode the file as an |
| 45 // image it is OK that the file is not an image. | 46 // image it is OK that the file is not an image. |
| 46 const char* kBestPrimaryIconUrl = "/simple.html"; | 47 const char* kBestPrimaryIconUrl = "/simple.html"; |
| 48 const char* kBestBadgeIconUrl = "/nostore.html"; |
| 47 | 49 |
| 48 // URL of file to download from the WebAPK server. We use a random file in the | 50 // URL of file to download from the WebAPK server. We use a random file in the |
| 49 // test data directory. | 51 // test data directory. |
| 50 const char* kDownloadUrl = "/simple.html"; | 52 const char* kDownloadUrl = "/simple.html"; |
| 51 | 53 |
| 52 // The package name of the downloaded WebAPK. | 54 // The package name of the downloaded WebAPK. |
| 53 const char* kDownloadedWebApkPackageName = "party.unicode"; | 55 const char* kDownloadedWebApkPackageName = "party.unicode"; |
| 54 | 56 |
| 55 // WebApkInstaller subclass where | 57 // WebApkInstaller subclass where |
| 56 // WebApkInstaller::StartInstallingDownloadedWebApk() and | 58 // WebApkInstaller::StartInstallingDownloadedWebApk() and |
| 57 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() and | 59 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() and |
| 58 // WebApkInstaller::CanUseGooglePlayInstallService() and | 60 // WebApkInstaller::CanUseGooglePlayInstallService() and |
| 59 // WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay() are stubbed out. | 61 // WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay() are stubbed out. |
| 60 class TestWebApkInstaller : public WebApkInstaller { | 62 class TestWebApkInstaller : public WebApkInstaller { |
| 61 public: | 63 public: |
| 62 TestWebApkInstaller(content::BrowserContext* browser_context, | 64 TestWebApkInstaller(content::BrowserContext* browser_context, |
| 63 const ShortcutInfo& shortcut_info, | 65 const ShortcutInfo& shortcut_info, |
| 64 const SkBitmap& shortcut_icon, | 66 const SkBitmap& primary_icon, |
| 67 const SkBitmap& badge_icon, |
| 65 bool can_install_webapks) | 68 bool can_install_webapks) |
| 66 : WebApkInstaller(browser_context, shortcut_info, shortcut_icon), | 69 : WebApkInstaller(browser_context, |
| 70 shortcut_info, |
| 71 primary_icon, |
| 72 badge_icon), |
| 67 can_install_webapks_(can_install_webapks) {} | 73 can_install_webapks_(can_install_webapks) {} |
| 68 | 74 |
| 69 bool CanInstallWebApks() override { return can_install_webapks_; } | 75 bool CanInstallWebApks() override { return can_install_webapks_; } |
| 70 | 76 |
| 71 void InstallOrUpdateWebApk(const std::string& package_name, | 77 void InstallOrUpdateWebApk(const std::string& package_name, |
| 72 int version, | 78 int version, |
| 73 const std::string& token) override { | 79 const std::string& token) override { |
| 74 PostTaskToRunSuccessCallback(); | 80 PostTaskToRunSuccessCallback(); |
| 75 } | 81 } |
| 76 | 82 |
| 77 void PostTaskToRunSuccessCallback() { | 83 void PostTaskToRunSuccessCallback() { |
| 78 base::ThreadTaskRunnerHandle::Get()->PostTask( | 84 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 79 FROM_HERE, | 85 FROM_HERE, |
| 80 base::Bind(&TestWebApkInstaller::OnResult, base::Unretained(this), | 86 base::Bind(&TestWebApkInstaller::OnResult, base::Unretained(this), |
| 81 WebApkInstallResult::SUCCESS)); | 87 WebApkInstallResult::SUCCESS)); |
| 82 } | 88 } |
| 83 | 89 |
| 84 private: | 90 private: |
| 85 // Whether the Google Play Services can be used and the install delegate is | 91 // Whether the Google Play Services can be used and the install delegate is |
| 86 // available. | 92 // available. |
| 87 bool can_install_webapks_; | 93 bool can_install_webapks_; |
| 88 | 94 |
| 89 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); | 95 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); |
| 90 }; | 96 }; |
| 91 | 97 |
| 92 // Runs the WebApkInstaller installation process/update and blocks till done. | 98 // Runs the WebApkInstaller installation process/update and blocks till done. |
| 93 class WebApkInstallerRunner { | 99 class WebApkInstallerRunner { |
| 94 public: | 100 public: |
| 95 WebApkInstallerRunner(content::BrowserContext* browser_context, | 101 WebApkInstallerRunner(content::BrowserContext* browser_context, |
| 96 const GURL& best_primary_icon_url) | 102 const GURL& best_primary_icon_url, |
| 103 const GURL& best_badge_icon_url) |
| 97 : browser_context_(browser_context), | 104 : browser_context_(browser_context), |
| 98 best_primary_icon_url_(best_primary_icon_url), | 105 best_primary_icon_url_(best_primary_icon_url), |
| 106 best_badge_icon_url_(best_badge_icon_url), |
| 99 can_install_webapks_(true) {} | 107 can_install_webapks_(true) {} |
| 100 | 108 |
| 101 ~WebApkInstallerRunner() {} | 109 ~WebApkInstallerRunner() {} |
| 102 | 110 |
| 103 void SetCanInstallWebApks(bool can_install_webapks) { | 111 void SetCanInstallWebApks(bool can_install_webapks) { |
| 104 can_install_webapks_ = can_install_webapks; | 112 can_install_webapks_ = can_install_webapks; |
| 105 } | 113 } |
| 106 | 114 |
| 107 void RunInstallWebApk() { | 115 void RunInstallWebApk() { |
| 108 WebApkInstaller::InstallAsyncForTesting( | 116 WebApkInstaller::InstallAsyncForTesting( |
| 109 CreateWebApkInstaller(), base::Bind(&WebApkInstallerRunner::OnCompleted, | 117 CreateWebApkInstaller(), base::Bind(&WebApkInstallerRunner::OnCompleted, |
| 110 base::Unretained(this))); | 118 base::Unretained(this))); |
| 111 Run(); | 119 Run(); |
| 112 } | 120 } |
| 113 | 121 |
| 114 void RunUpdateWebApk() { | 122 void RunUpdateWebApk() { |
| 115 const int kWebApkVersion = 1; | 123 const int kWebApkVersion = 1; |
| 116 | 124 |
| 117 std::map<std::string, std::string> icon_url_to_murmur2_hash{ | 125 std::map<std::string, std::string> icon_url_to_murmur2_hash{ |
| 118 {best_primary_icon_url_.spec(), "0"}}; | 126 {best_primary_icon_url_.spec(), "0"}, |
| 127 {best_badge_icon_url_.spec(), "0"}}; |
| 119 | 128 |
| 120 WebApkInstaller::UpdateAsyncForTesting( | 129 WebApkInstaller::UpdateAsyncForTesting( |
| 121 CreateWebApkInstaller(), kDownloadedWebApkPackageName, kWebApkVersion, | 130 CreateWebApkInstaller(), kDownloadedWebApkPackageName, kWebApkVersion, |
| 122 icon_url_to_murmur2_hash, false /* is_manifest_stale */, | 131 icon_url_to_murmur2_hash, false /* is_manifest_stale */, |
| 123 base::Bind(&WebApkInstallerRunner::OnCompleted, | 132 base::Bind(&WebApkInstallerRunner::OnCompleted, |
| 124 base::Unretained(this))); | 133 base::Unretained(this))); |
| 125 Run(); | 134 Run(); |
| 126 } | 135 } |
| 127 | 136 |
| 128 WebApkInstaller* CreateWebApkInstaller() { | 137 WebApkInstaller* CreateWebApkInstaller() { |
| 129 ShortcutInfo info(GURL::EmptyGURL()); | 138 ShortcutInfo info(GURL::EmptyGURL()); |
| 130 info.best_primary_icon_url = best_primary_icon_url_; | 139 info.best_primary_icon_url = best_primary_icon_url_; |
| 140 info.best_badge_icon_url = best_badge_icon_url_; |
| 131 | 141 |
| 132 // WebApkInstaller owns itself. | 142 // WebApkInstaller owns itself. |
| 133 WebApkInstaller* installer = new TestWebApkInstaller( | 143 WebApkInstaller* installer = new TestWebApkInstaller( |
| 134 browser_context_, info, SkBitmap(), can_install_webapks_); | 144 browser_context_, info, SkBitmap(), SkBitmap(), can_install_webapks_); |
| 135 installer->SetTimeoutMs(100); | 145 installer->SetTimeoutMs(100); |
| 136 return installer; | 146 return installer; |
| 137 } | 147 } |
| 138 | 148 |
| 139 void Run() { | 149 void Run() { |
| 140 base::RunLoop run_loop; | 150 base::RunLoop run_loop; |
| 141 on_completed_callback_ = run_loop.QuitClosure(); | 151 on_completed_callback_ = run_loop.QuitClosure(); |
| 142 run_loop.Run(); | 152 run_loop.Run(); |
| 143 } | 153 } |
| 144 | 154 |
| 145 WebApkInstallResult result() { return result_; } | 155 WebApkInstallResult result() { return result_; } |
| 146 | 156 |
| 147 private: | 157 private: |
| 148 void OnCompleted(WebApkInstallResult result, | 158 void OnCompleted(WebApkInstallResult result, |
| 149 bool relax_updates, | 159 bool relax_updates, |
| 150 const std::string& webapk_package) { | 160 const std::string& webapk_package) { |
| 151 result_ = result; | 161 result_ = result; |
| 152 on_completed_callback_.Run(); | 162 on_completed_callback_.Run(); |
| 153 } | 163 } |
| 154 | 164 |
| 155 content::BrowserContext* browser_context_; | 165 content::BrowserContext* browser_context_; |
| 156 | 166 |
| 157 // The Web Manifest's icon URL. | 167 // The Web Manifest's icon URLs. |
| 158 const GURL best_primary_icon_url_; | 168 const GURL best_primary_icon_url_; |
| 169 const GURL best_badge_icon_url_; |
| 159 | 170 |
| 160 // Called after the installation process has succeeded or failed. | 171 // Called after the installation process has succeeded or failed. |
| 161 base::Closure on_completed_callback_; | 172 base::Closure on_completed_callback_; |
| 162 | 173 |
| 163 // The result of the installation process. | 174 // The result of the installation process. |
| 164 WebApkInstallResult result_; | 175 WebApkInstallResult result_; |
| 165 | 176 |
| 166 // Whether the device supports installation of WebApks. | 177 // Whether the device supports installation of WebApks. |
| 167 bool can_install_webapks_; | 178 bool can_install_webapks_; |
| 168 | 179 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 190 // Builds WebApk proto and blocks till done. | 201 // Builds WebApk proto and blocks till done. |
| 191 class BuildProtoRunner { | 202 class BuildProtoRunner { |
| 192 public: | 203 public: |
| 193 explicit BuildProtoRunner(content::BrowserContext* browser_context) | 204 explicit BuildProtoRunner(content::BrowserContext* browser_context) |
| 194 : browser_context_(browser_context) {} | 205 : browser_context_(browser_context) {} |
| 195 | 206 |
| 196 ~BuildProtoRunner() {} | 207 ~BuildProtoRunner() {} |
| 197 | 208 |
| 198 void BuildSync( | 209 void BuildSync( |
| 199 const GURL& best_primary_icon_url, | 210 const GURL& best_primary_icon_url, |
| 211 const GURL& best_badge_icon_url, |
| 200 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 212 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 201 bool is_manifest_stale) { | 213 bool is_manifest_stale) { |
| 202 ShortcutInfo info(GURL::EmptyGURL()); | 214 ShortcutInfo info(GURL::EmptyGURL()); |
| 203 info.best_primary_icon_url = best_primary_icon_url; | 215 info.best_primary_icon_url = best_primary_icon_url; |
| 216 info.best_badge_icon_url = best_badge_icon_url; |
| 204 | 217 |
| 205 // WebApkInstaller owns itself. | 218 // WebApkInstaller owns itself. |
| 206 WebApkInstaller* installer = | 219 WebApkInstaller* installer = new TestWebApkInstaller( |
| 207 new TestWebApkInstaller(browser_context_, info, SkBitmap(), false); | 220 browser_context_, info, SkBitmap(), SkBitmap(), false); |
| 208 installer->BuildWebApkProtoInBackgroundForTesting( | 221 installer->BuildWebApkProtoInBackgroundForTesting( |
| 209 base::Bind(&BuildProtoRunner::OnBuiltWebApkProto, | 222 base::Bind(&BuildProtoRunner::OnBuiltWebApkProto, |
| 210 base::Unretained(this)), | 223 base::Unretained(this)), |
| 211 icon_url_to_murmur2_hash, is_manifest_stale); | 224 icon_url_to_murmur2_hash, is_manifest_stale); |
| 212 | 225 |
| 213 base::RunLoop run_loop; | 226 base::RunLoop run_loop; |
| 214 on_completed_callback_ = run_loop.QuitClosure(); | 227 on_completed_callback_ = run_loop.QuitClosure(); |
| 215 run_loop.Run(); | 228 run_loop.Run(); |
| 216 } | 229 } |
| 217 | 230 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 void TearDown() override { | 273 void TearDown() override { |
| 261 profile_.reset(); | 274 profile_.reset(); |
| 262 base::RunLoop().RunUntilIdle(); | 275 base::RunLoop().RunUntilIdle(); |
| 263 } | 276 } |
| 264 | 277 |
| 265 // Sets the best Web Manifest's primary icon URL. | 278 // Sets the best Web Manifest's primary icon URL. |
| 266 void SetBestPrimaryIconUrl(const GURL& best_primary_icon_url) { | 279 void SetBestPrimaryIconUrl(const GURL& best_primary_icon_url) { |
| 267 best_primary_icon_url_ = best_primary_icon_url; | 280 best_primary_icon_url_ = best_primary_icon_url; |
| 268 } | 281 } |
| 269 | 282 |
| 283 // Sets the best Web Manifest's badge icon URL. |
| 284 void SetBestBadgeIconUrl(const GURL& best_badge_icon_url) { |
| 285 best_badge_icon_url_ = best_badge_icon_url; |
| 286 } |
| 287 |
| 270 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller | 288 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller |
| 271 // should fail if the URL is not |kServerUrl|. | 289 // should fail if the URL is not |kServerUrl|. |
| 272 void SetWebApkServerUrl(const GURL& server_url) { | 290 void SetWebApkServerUrl(const GURL& server_url) { |
| 273 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 291 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 274 switches::kWebApkServerUrl, server_url.spec()); | 292 switches::kWebApkServerUrl, server_url.spec()); |
| 275 } | 293 } |
| 276 | 294 |
| 277 // Sets the function that should be used to build the response to the | 295 // Sets the function that should be used to build the response to the |
| 278 // WebAPK creation request. | 296 // WebAPK creation request. |
| 279 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { | 297 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { |
| 280 webapk_response_builder_ = builder; | 298 webapk_response_builder_ = builder; |
| 281 } | 299 } |
| 282 | 300 |
| 283 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { | 301 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { |
| 284 return std::unique_ptr<WebApkInstallerRunner>( | 302 return std::unique_ptr<WebApkInstallerRunner>(new WebApkInstallerRunner( |
| 285 new WebApkInstallerRunner(profile_.get(), best_primary_icon_url_)); | 303 profile_.get(), best_primary_icon_url_, best_badge_icon_url_)); |
| 286 } | 304 } |
| 287 | 305 |
| 288 std::unique_ptr<BuildProtoRunner> CreateBuildProtoRunner() { | 306 std::unique_ptr<BuildProtoRunner> CreateBuildProtoRunner() { |
| 289 return std::unique_ptr<BuildProtoRunner>( | 307 return std::unique_ptr<BuildProtoRunner>( |
| 290 new BuildProtoRunner(profile_.get())); | 308 new BuildProtoRunner(profile_.get())); |
| 291 } | 309 } |
| 292 | 310 |
| 293 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } | 311 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } |
| 294 | 312 |
| 295 private: | 313 private: |
| 296 // Sets default configuration for running WebApkInstaller. | 314 // Sets default configuration for running WebApkInstaller. |
| 297 void SetDefaults() { | 315 void SetDefaults() { |
| 298 GURL best_primary_icon_url = test_server_.GetURL(kBestPrimaryIconUrl); | 316 SetBestPrimaryIconUrl(test_server_.GetURL(kBestPrimaryIconUrl)); |
| 299 SetBestPrimaryIconUrl(best_primary_icon_url); | 317 SetBestBadgeIconUrl(test_server_.GetURL(kBestBadgeIconUrl)); |
| 300 GURL server_url = test_server_.GetURL(kServerUrl); | 318 SetWebApkServerUrl(test_server_.GetURL(kServerUrl)); |
| 301 SetWebApkServerUrl(server_url); | 319 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, |
| 302 GURL download_url = test_server_.GetURL(kDownloadUrl); | 320 test_server_.GetURL(kDownloadUrl))); |
| 303 SetWebApkResponseBuilder( | |
| 304 base::Bind(&BuildValidWebApkResponse, download_url)); | |
| 305 } | 321 } |
| 306 | 322 |
| 307 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( | 323 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( |
| 308 const net::test_server::HttpRequest& request) { | 324 const net::test_server::HttpRequest& request) { |
| 309 return (request.relative_url == kServerUrl) | 325 return (request.relative_url == kServerUrl) |
| 310 ? webapk_response_builder_.Run() | 326 ? webapk_response_builder_.Run() |
| 311 : std::unique_ptr<net::test_server::HttpResponse>(); | 327 : std::unique_ptr<net::test_server::HttpResponse>(); |
| 312 } | 328 } |
| 313 | 329 |
| 314 std::unique_ptr<TestingProfile> profile_; | 330 std::unique_ptr<TestingProfile> profile_; |
| 315 content::TestBrowserThreadBundle thread_bundle_; | 331 content::TestBrowserThreadBundle thread_bundle_; |
| 316 net::EmbeddedTestServer test_server_; | 332 net::EmbeddedTestServer test_server_; |
| 317 | 333 |
| 318 // Web Manifest's icon URL. | 334 // Web Manifest's icon URLs. |
| 319 GURL best_primary_icon_url_; | 335 GURL best_primary_icon_url_; |
| 336 GURL best_badge_icon_url_; |
| 320 | 337 |
| 321 // Builds response to the WebAPK creation request. | 338 // Builds response to the WebAPK creation request. |
| 322 WebApkResponseBuilder webapk_response_builder_; | 339 WebApkResponseBuilder webapk_response_builder_; |
| 323 | 340 |
| 324 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); | 341 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); |
| 325 }; | 342 }; |
| 326 | 343 |
| 327 // Test installation succeeding. | 344 // Test installation succeeding. |
| 328 TEST_F(WebApkInstallerTest, Success) { | 345 TEST_F(WebApkInstallerTest, Success) { |
| 329 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 346 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 330 runner->RunInstallWebApk(); | 347 runner->RunInstallWebApk(); |
| 331 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); | 348 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); |
| 332 } | 349 } |
| 333 | 350 |
| 334 // Test that installation fails if fetching the bitmap at the best primary icon | 351 // Test that installation fails if fetching the bitmap at the best primary icon |
| 335 // URL times out. In a perfect world the fetch would never time out because the | 352 // URL times out. In a perfect world the fetch would never time out because the |
| 336 // bitmap at the best primary icon URL should be in the HTTP cache. | 353 // bitmap at the best primary icon URL should be in the HTTP cache. |
| 337 TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) { | 354 TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) { |
| 338 GURL best_primary_icon_url = test_server()->GetURL("/slow?1000"); | 355 SetBestPrimaryIconUrl(test_server()->GetURL("/slow?1000")); |
| 339 SetBestPrimaryIconUrl(best_primary_icon_url); | |
| 340 | 356 |
| 341 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 357 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 342 runner->RunInstallWebApk(); | 358 runner->RunInstallWebApk(); |
| 359 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
| 360 } |
| 361 |
| 362 // Test that installation fails if fetching the bitmap at the best badge icon |
| 363 // URL times out. In a perfect world the fetch would never time out because the |
| 364 // bitmap at the best badge icon URL should be in the HTTP cache. |
| 365 TEST_F(WebApkInstallerTest, BestBadgeIconUrlDownloadTimesOut) { |
| 366 SetBestBadgeIconUrl(test_server()->GetURL("/slow?1000")); |
| 367 |
| 368 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 369 runner->RunInstallWebApk(); |
| 343 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | 370 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
| 344 } | 371 } |
| 345 | 372 |
| 346 // Test that installation fails if the WebAPK creation request times out. | 373 // Test that installation fails if the WebAPK creation request times out. |
| 347 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { | 374 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { |
| 348 GURL server_url = test_server()->GetURL("/slow?1000"); | 375 SetWebApkServerUrl(test_server()->GetURL("/slow?1000")); |
| 349 SetWebApkServerUrl(server_url); | |
| 350 | 376 |
| 351 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 377 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 352 runner->RunInstallWebApk(); | 378 runner->RunInstallWebApk(); |
| 353 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | 379 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
| 354 } | 380 } |
| 355 | 381 |
| 356 namespace { | 382 namespace { |
| 357 | 383 |
| 358 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. | 384 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. |
| 359 std::unique_ptr<net::test_server::HttpResponse> | 385 std::unique_ptr<net::test_server::HttpResponse> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 | 422 |
| 397 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 423 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 398 runner->RunUpdateWebApk(); | 424 runner->RunUpdateWebApk(); |
| 399 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); | 425 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); |
| 400 } | 426 } |
| 401 | 427 |
| 402 // When there is no Web Manifest available for a site, an empty | 428 // When there is no Web Manifest available for a site, an empty |
| 403 // |best_primary_icon_url| is used to build a WebApk update request. Tests the | 429 // |best_primary_icon_url| is used to build a WebApk update request. Tests the |
| 404 // request can be built properly. | 430 // request can be built properly. |
| 405 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { | 431 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { |
| 406 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); | 432 std::string icon_url_1 = test_server()->GetURL("/icon1.png").spec(); |
| 407 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); | 433 std::string icon_url_2 = test_server()->GetURL("/icon2.png").spec(); |
| 408 std::string icon_murmur2_hash_1 = "1"; | |
| 409 std::string icon_murmur2_hash_2 = "2"; | |
| 410 std::map<std::string, std::string> icon_url_to_murmur2_hash; | 434 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| 411 icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; | 435 icon_url_to_murmur2_hash[icon_url_1] = "1"; |
| 412 icon_url_to_murmur2_hash[icon_url_2.spec()] = icon_murmur2_hash_2; | 436 icon_url_to_murmur2_hash[icon_url_2] = "2"; |
| 413 | 437 |
| 414 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); | 438 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
| 415 runner->BuildSync(GURL(""), icon_url_to_murmur2_hash, | 439 runner->BuildSync(GURL(), GURL(), icon_url_to_murmur2_hash, |
| 416 true /* is_manifest_stale*/); | 440 true /* is_manifest_stale*/); |
| 417 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | 441 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| 418 ASSERT_NE(nullptr, webapk_request); | 442 ASSERT_NE(nullptr, webapk_request); |
| 419 | 443 |
| 420 webapk::WebAppManifest manifest = webapk_request->manifest(); | 444 webapk::WebAppManifest manifest = webapk_request->manifest(); |
| 421 ASSERT_EQ(3, manifest.icons_size()); | 445 ASSERT_EQ(3, manifest.icons_size()); |
| 422 | 446 |
| 423 webapk::Image icons[3]; | 447 webapk::Image icons[3]; |
| 424 for (int i = 0; i < 3; ++i) | 448 for (int i = 0; i < 3; ++i) |
| 425 icons[i] = manifest.icons(i); | 449 icons[i] = manifest.icons(i); |
| 426 | 450 |
| 427 EXPECT_EQ("", icons[0].src()); | 451 EXPECT_EQ("", icons[0].src()); |
| 428 EXPECT_FALSE(icons[0].has_hash()); | 452 EXPECT_FALSE(icons[0].has_hash()); |
| 429 EXPECT_TRUE(icons[0].has_image_data()); | 453 EXPECT_TRUE(icons[0].has_image_data()); |
| 430 | 454 |
| 431 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | 455 EXPECT_EQ(icon_url_1, icons[1].src()); |
| 432 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | 456 EXPECT_EQ(icon_url_to_murmur2_hash[icon_url_1], icons[1].hash()); |
| 433 EXPECT_FALSE(icons[1].has_image_data()); | 457 EXPECT_FALSE(icons[1].has_image_data()); |
| 434 | 458 |
| 435 EXPECT_EQ(icon_url_2.spec(), icons[2].src()); | 459 EXPECT_EQ(icon_url_2, icons[2].src()); |
| 436 EXPECT_EQ(icon_murmur2_hash_2, icons[2].hash()); | 460 EXPECT_EQ(icon_url_to_murmur2_hash[icon_url_2], icons[2].hash()); |
| 437 EXPECT_FALSE(icons[2].has_image_data()); | 461 EXPECT_FALSE(icons[2].has_image_data()); |
| 438 } | 462 } |
| 439 | 463 |
| 440 // Tests a WebApk install or update request is built properly when the Chrome | 464 // Tests a WebApk install or update request is built properly when the Chrome |
| 441 // knows the best icon URL of a site after fetching its Web Manifest. | 465 // knows the best icon URL of a site after fetching its Web Manifest. |
| 442 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { | 466 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { |
| 443 GURL icon_url_1 = test_server()->GetURL("/icon.png"); | 467 std::string icon_url_1 = test_server()->GetURL("/icon.png").spec(); |
| 444 GURL best_primary_icon_url = test_server()->GetURL(kBestPrimaryIconUrl); | 468 std::string best_primary_icon_url = |
| 445 std::string icon_murmur2_hash_1 = "1"; | 469 test_server()->GetURL(kBestPrimaryIconUrl).spec(); |
| 446 std::string best_primary_icon_murmur2_hash = "0"; | 470 std::string best_badge_icon_url = |
| 471 test_server()->GetURL(kBestBadgeIconUrl).spec(); |
| 447 std::map<std::string, std::string> icon_url_to_murmur2_hash; | 472 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| 448 icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; | 473 icon_url_to_murmur2_hash[icon_url_1] = "0"; |
| 449 icon_url_to_murmur2_hash[best_primary_icon_url.spec()] = | 474 icon_url_to_murmur2_hash[best_primary_icon_url] = "1"; |
| 450 best_primary_icon_murmur2_hash; | 475 icon_url_to_murmur2_hash[best_badge_icon_url] = "2"; |
| 451 | 476 |
| 452 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); | 477 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
| 453 runner->BuildSync(best_primary_icon_url, icon_url_to_murmur2_hash, | 478 runner->BuildSync(GURL(best_primary_icon_url), GURL(best_badge_icon_url), |
| 454 false /* is_manifest_stale*/); | 479 icon_url_to_murmur2_hash, false /* is_manifest_stale*/); |
| 455 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | 480 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| 456 ASSERT_NE(nullptr, webapk_request); | 481 ASSERT_NE(nullptr, webapk_request); |
| 457 | 482 |
| 483 webapk::WebAppManifest manifest = webapk_request->manifest(); |
| 484 ASSERT_EQ(3, manifest.icons_size()); |
| 485 |
| 486 webapk::Image icons[3]; |
| 487 for (int i = 0; i < 3; ++i) |
| 488 icons[i] = manifest.icons(i); |
| 489 |
| 490 // Check protobuf fields for /icon.png. |
| 491 EXPECT_EQ(icon_url_1, icons[0].src()); |
| 492 EXPECT_EQ(icon_url_to_murmur2_hash[icon_url_1], icons[0].hash()); |
| 493 EXPECT_EQ(0, icons[0].usages_size()); |
| 494 EXPECT_FALSE(icons[0].has_image_data()); |
| 495 |
| 496 // Check protobuf fields for kBestBadgeIconUrl. |
| 497 EXPECT_EQ(best_badge_icon_url, icons[1].src()); |
| 498 EXPECT_EQ(icon_url_to_murmur2_hash[best_badge_icon_url], icons[1].hash()); |
| 499 EXPECT_THAT(icons[1].usages(), |
| 500 testing::ElementsAre(webapk::Image::BADGE_ICON)); |
| 501 EXPECT_TRUE(icons[1].has_image_data()); |
| 502 |
| 503 // Check protobuf fields for kBestPrimaryIconUrl. |
| 504 EXPECT_EQ(best_primary_icon_url, icons[2].src()); |
| 505 EXPECT_EQ(icon_url_to_murmur2_hash[best_primary_icon_url], icons[2].hash()); |
| 506 EXPECT_THAT(icons[2].usages(), |
| 507 testing::ElementsAre(webapk::Image::PRIMARY_ICON)); |
| 508 EXPECT_TRUE(icons[2].has_image_data()); |
| 509 } |
| 510 |
| 511 // Tests a WebApk install or update request is built properly when the Chrome |
| 512 // knows the best icon URL of a site after fetching its Web Manifest, and |
| 513 // primary icon and badge icon share the same URL. |
| 514 TEST_F(WebApkInstallerTest, BuildWebApkProtoPrimaryIconAndBadgeIconSameUrl) { |
| 515 std::string icon_url_1 = test_server()->GetURL("/icon.png").spec(); |
| 516 std::string best_icon_url = test_server()->GetURL(kBestPrimaryIconUrl).spec(); |
| 517 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| 518 icon_url_to_murmur2_hash[icon_url_1] = "1"; |
| 519 icon_url_to_murmur2_hash[best_icon_url] = "0"; |
| 520 |
| 521 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
| 522 runner->BuildSync(GURL(best_icon_url), GURL(best_icon_url), |
| 523 icon_url_to_murmur2_hash, false /* is_manifest_stale*/); |
| 524 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
| 525 ASSERT_NE(nullptr, webapk_request); |
| 526 |
| 458 webapk::WebAppManifest manifest = webapk_request->manifest(); | 527 webapk::WebAppManifest manifest = webapk_request->manifest(); |
| 459 ASSERT_EQ(2, manifest.icons_size()); | 528 ASSERT_EQ(2, manifest.icons_size()); |
| 460 | 529 |
| 461 webapk::Image icons[2]; | 530 webapk::Image icons[2]; |
| 462 for (int i = 0; i < 2; ++i) | 531 for (int i = 0; i < 2; ++i) |
| 463 icons[i] = manifest.icons(i); | 532 icons[i] = manifest.icons(i); |
| 464 | 533 |
| 465 EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src()); | 534 // Check protobuf fields for /icon.png. |
| 466 EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash()); | 535 EXPECT_EQ(icon_url_1, icons[0].src()); |
| 467 EXPECT_TRUE(icons[0].has_image_data()); | 536 EXPECT_EQ(icon_url_to_murmur2_hash[icon_url_1], icons[0].hash()); |
| 537 EXPECT_EQ(0, icons[0].usages_size()); |
| 538 EXPECT_FALSE(icons[0].has_image_data()); |
| 468 | 539 |
| 469 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | 540 // Check protobuf fields for kBestPrimaryIconUrl. |
| 470 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | 541 EXPECT_EQ(best_icon_url, icons[1].src()); |
| 471 EXPECT_FALSE(icons[1].has_image_data()); | 542 EXPECT_EQ(icon_url_to_murmur2_hash[best_icon_url], icons[1].hash()); |
| 543 EXPECT_THAT(icons[1].usages(), |
| 544 testing::ElementsAre(webapk::Image::PRIMARY_ICON, |
| 545 webapk::Image::BADGE_ICON)); |
| 546 EXPECT_TRUE(icons[1].has_image_data()); |
| 472 } | 547 } |
| 473 | 548 |
| 474 TEST_F(WebApkInstallerTest, FailsWhenInstallDisabled) { | 549 TEST_F(WebApkInstallerTest, FailsWhenInstallDisabled) { |
| 475 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 550 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 476 runner->SetCanInstallWebApks(false); | 551 runner->SetCanInstallWebApks(false); |
| 477 runner->RunInstallWebApk(); | 552 runner->RunInstallWebApk(); |
| 478 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | 553 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
| 479 } | 554 } |
| OLD | NEW |