| 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/webapps/add_to_homescreen_data_fetcher.h" | 5 #include "chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/nullable_string16.h" | 16 #include "base/strings/nullable_string16.h" |
| 17 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 19 #include "chrome/common/web_application_info.h" | 20 #include "chrome/common/web_application_info.h" |
| 20 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 21 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 21 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 22 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 23 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 23 #include "content/browser/service_worker/service_worker_context_core.h" | 24 #include "content/browser/service_worker/service_worker_context_core.h" |
| 24 #include "content/common/service_worker/service_worker_status_code.h" | 25 #include "content/common/service_worker/service_worker_status_code.h" |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/site_instance.h" | 27 #include "content/public/browser/site_instance.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 content::Manifest manifest_; | 90 content::Manifest manifest_; |
| 90 int manifest_fetch_delay_ms_; | 91 int manifest_fetch_delay_ms_; |
| 91 | 92 |
| 92 DISALLOW_COPY_AND_ASSIGN(MockWebContents); | 93 DISALLOW_COPY_AND_ASSIGN(MockWebContents); |
| 93 }; | 94 }; |
| 94 | 95 |
| 95 // Tracks which of the AddToHomescreenDataFetcher::Observer callbacks have been | 96 // Tracks which of the AddToHomescreenDataFetcher::Observer callbacks have been |
| 96 // called. | 97 // called. |
| 97 class ObserverWaiter : public AddToHomescreenDataFetcher::Observer { | 98 class ObserverWaiter : public AddToHomescreenDataFetcher::Observer { |
| 98 public: | 99 public: |
| 99 ObserverWaiter() {} | 100 ObserverWaiter() |
| 101 : is_webapk_compatible_(false), |
| 102 determined_webapk_compatibility_(false), |
| 103 title_available_(false), |
| 104 data_available_(false) {} |
| 100 ~ObserverWaiter() override {} | 105 ~ObserverWaiter() override {} |
| 101 | 106 |
| 102 // Waits till the OnDataAvailable() callback is called. | 107 // Waits till the OnDataAvailable() callback is called. |
| 103 void WaitForDataAvailable() { | 108 void WaitForDataAvailable() { |
| 104 if (data_available_) | 109 if (data_available_) |
| 105 return; | 110 return; |
| 106 | 111 |
| 107 base::RunLoop run_loop; | 112 base::RunLoop run_loop; |
| 108 quit_closure_ = run_loop.QuitClosure(); | 113 quit_closure_ = run_loop.QuitClosure(); |
| 109 run_loop.Run(); | 114 run_loop.Run(); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 } | 273 } |
| 269 | 274 |
| 270 // The value of |check_webapk_compatible| used when building the | 275 // The value of |check_webapk_compatible| used when building the |
| 271 // AddToHomescreenDataFetcher. | 276 // AddToHomescreenDataFetcher. |
| 272 bool check_webapk_compatibility() { return GetParam(); } | 277 bool check_webapk_compatibility() { return GetParam(); } |
| 273 | 278 |
| 274 private: | 279 private: |
| 275 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); | 280 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); |
| 276 }; | 281 }; |
| 277 | 282 |
| 283 // Test that when the manifest provides Manifest::short_name but not |
| 284 // Manifest::name that Manifest::short_name is used as the name instead of |
| 285 // WebApplicationInfo::title. |
| 286 TEST_P(AddToHomescreenDataFetcherTestCommon, |
| 287 ManifestShortNameClobbersWebApplicationName) { |
| 288 WebApplicationInfo web_application_info; |
| 289 web_application_info.title = base::UTF8ToUTF16("Meta Title"); |
| 290 |
| 291 content::Manifest manifest(BuildDefaultManifest()); |
| 292 manifest.name = base::NullableString16(); |
| 293 |
| 294 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 295 SetManifest(GURL(kDefaultManifestUrl), manifest, 0); |
| 296 |
| 297 ObserverWaiter waiter; |
| 298 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 299 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 300 waiter.WaitForDataAvailable(); |
| 301 |
| 302 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, |
| 303 kDefaultManifestShortName)); |
| 304 |
| 305 fetcher->set_weak_observer(nullptr); |
| 306 } |
| 307 |
| 308 // Test that when the manifest does not provide either Manifest::short_name nor |
| 309 // Manifest::name that: |
| 310 // - The page is not WebAPK compatible. |
| 311 // - WebApplicationInfo::title is used as the "name". |
| 312 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { |
| 313 const char* kWebApplicationInfoTitle = "Meta Title"; |
| 314 WebApplicationInfo web_application_info; |
| 315 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 316 |
| 317 content::Manifest manifest(BuildDefaultManifest()); |
| 318 manifest.name = base::NullableString16(); |
| 319 manifest.short_name = base::NullableString16(); |
| 320 |
| 321 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 322 SetManifest(GURL(kDefaultManifestUrl), manifest, 0); |
| 323 |
| 324 ObserverWaiter waiter; |
| 325 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 326 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 327 waiter.WaitForDataAvailable(); |
| 328 |
| 329 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 330 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, |
| 331 kWebApplicationInfoTitle)); |
| 332 |
| 333 fetcher->set_weak_observer(nullptr); |
| 334 } |
| 335 |
| 278 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called | 336 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called |
| 279 // when a service worker is registered and the manifest fetch times out. | 337 // when a service worker is registered and the manifest fetch times out. |
| 280 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) { | 338 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) { |
| 281 RegisterServiceWorker(GURL(kDefaultStartUrl)); | 339 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 282 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest(), 10000); | 340 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest(), 10000); |
| 283 | 341 |
| 284 ObserverWaiter waiter; | 342 ObserverWaiter waiter; |
| 285 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | 343 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 286 fetcher->OnDidGetWebApplicationInfo(WebApplicationInfo()); | 344 fetcher->OnDidGetWebApplicationInfo(WebApplicationInfo()); |
| 287 waiter.WaitForDataAvailable(); | 345 waiter.WaitForDataAvailable(); |
| 288 | 346 |
| 289 if (check_webapk_compatibility()) { | 347 if (check_webapk_compatibility()) { |
| 290 EXPECT_TRUE(waiter.determined_webapk_compatibility()); | 348 EXPECT_TRUE(waiter.determined_webapk_compatibility()); |
| 291 EXPECT_FALSE(waiter.is_webapk_compatible()); | 349 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 292 } else { | 350 } else { |
| 293 EXPECT_FALSE(waiter.determined_webapk_compatibility()); | 351 EXPECT_FALSE(waiter.determined_webapk_compatibility()); |
| 294 } | 352 } |
| 295 // This callback enables the text field in the add-to-homescreen dialog. | 353 // This callback enables the text field in the add-to-homescreen dialog. |
| 296 EXPECT_TRUE(waiter.title_available()); | 354 EXPECT_TRUE(waiter.title_available()); |
| 297 | 355 |
| 298 fetcher->set_weak_observer(nullptr); | 356 fetcher->set_weak_observer(nullptr); |
| 299 } | 357 } |
| 300 | 358 |
| 301 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, | 359 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, |
| 302 AddToHomescreenDataFetcherTestCommon, | 360 AddToHomescreenDataFetcherTestCommon, |
| 303 ::testing::Values(false, true)); | 361 ::testing::Values(false, true)); |
| OLD | NEW |