Chromium Code Reviews| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 | 188 |
| 184 scoped_refptr<content::SiteInstance> site_instance = | 189 scoped_refptr<content::SiteInstance> site_instance = |
| 185 content::SiteInstance::Create(browser_context()); | 190 content::SiteInstance::Create(browser_context()); |
| 186 site_instance->GetProcess()->Init(); | 191 site_instance->GetProcess()->Init(); |
| 187 MockWebContents* mock_web_contents = new MockWebContents(browser_context()); | 192 MockWebContents* mock_web_contents = new MockWebContents(browser_context()); |
| 188 mock_web_contents->Init(content::WebContents::CreateParams( | 193 mock_web_contents->Init(content::WebContents::CreateParams( |
| 189 browser_context(), std::move(site_instance))); | 194 browser_context(), std::move(site_instance))); |
| 190 SetContents(mock_web_contents); | 195 SetContents(mock_web_contents); |
| 191 } | 196 } |
| 192 | 197 |
| 198 void TearDown() override { | |
| 199 embedded_worker_test_helper_.reset(); | |
| 200 ChromeRenderViewHostTestHarness::TearDown(); | |
| 201 } | |
| 202 | |
| 193 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( | 203 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( |
| 194 bool check_webapk_compatible, | 204 bool check_webapk_compatible, |
| 195 AddToHomescreenDataFetcher::Observer* observer) { | 205 AddToHomescreenDataFetcher::Observer* observer) { |
| 196 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, | 206 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, |
| 197 check_webapk_compatible, observer); | 207 check_webapk_compatible, observer); |
| 198 } | 208 } |
| 199 | 209 |
| 200 // Set the manifest to be returned as a result of WebContents::GetManifest(). | 210 // Set the manifest to be returned as a result of WebContents::GetManifest(). |
| 201 void SetManifest(const GURL& manifest_url, | 211 void SetManifest(const GURL& manifest_url, |
| 202 const content::Manifest& manifest, | 212 const content::Manifest& manifest, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 } | 279 } |
| 270 | 280 |
| 271 // The value of |check_webapk_compatible| used when building the | 281 // The value of |check_webapk_compatible| used when building the |
| 272 // AddToHomescreenDataFetcher. | 282 // AddToHomescreenDataFetcher. |
| 273 bool check_webapk_compatibility() { return GetParam(); } | 283 bool check_webapk_compatibility() { return GetParam(); } |
| 274 | 284 |
| 275 private: | 285 private: |
| 276 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); | 286 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); |
| 277 }; | 287 }; |
| 278 | 288 |
| 289 // Test that when the manifest provides Manifest::short_name but not | |
| 290 // Manifest::name that Manifest::short_name is used as the name instead of | |
| 291 // WebApplicationInfo::title. | |
| 292 TEST_P(AddToHomescreenDataFetcherTestCommon, | |
| 293 ManifestShortNameClobbersWebApplicationName) { | |
| 294 WebApplicationInfo web_application_info; | |
| 295 web_application_info.title = base::UTF8ToUTF16("Meta Title"); | |
| 296 | |
| 297 content::Manifest manifest(BuildDefaultManifest()); | |
| 298 manifest.name = base::NullableString16(); | |
| 299 | |
| 300 RegisterServiceWorker(GURL(kDefaultStartUrl)); | |
| 301 SetManifest(GURL(kDefaultManifestUrl), manifest, 0); | |
| 302 | |
| 303 ObserverWaiter waiter; | |
| 304 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | |
| 305 fetcher->OnDidGetWebApplicationInfo(web_application_info); | |
| 306 waiter.WaitForDataAvailable(); | |
| 307 | |
| 308 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, | |
| 309 kDefaultManifestShortName)); | |
| 310 | |
| 311 fetcher->set_weak_observer(nullptr); | |
| 312 } | |
| 313 | |
| 314 // Test that when the manifest does not provide either Manifest::short_name nor | |
| 315 // Manifest::name that: | |
| 316 // - The page is not WebAPK compatible. | |
| 317 // - WebApplicationInfo::title is used as the "name". | |
| 318 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { | |
| 319 const char* kWebApplicationInfoTitle = "Meta Title"; | |
| 320 WebApplicationInfo web_application_info; | |
| 321 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); | |
| 322 | |
| 323 content::Manifest manifest(BuildDefaultManifest()); | |
| 324 manifest.name = base::NullableString16(); | |
| 325 manifest.short_name = base::NullableString16(); | |
| 326 | |
| 327 RegisterServiceWorker(GURL(kDefaultStartUrl)); | |
|
gone
2016/12/20 18:44:14
Lines 327 and 333 are (more or less) repeated thre
| |
| 328 SetManifest(GURL(kDefaultManifestUrl), manifest, 0); | |
| 329 | |
| 330 ObserverWaiter waiter; | |
| 331 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | |
| 332 fetcher->OnDidGetWebApplicationInfo(web_application_info); | |
| 333 waiter.WaitForDataAvailable(); | |
| 334 | |
| 335 EXPECT_FALSE(waiter.is_webapk_compatible()); | |
| 336 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, | |
| 337 kWebApplicationInfoTitle)); | |
| 338 | |
| 339 fetcher->set_weak_observer(nullptr); | |
| 340 } | |
| 341 | |
| 279 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called | 342 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called |
| 280 // when a service worker is registered and the manifest fetch times out. | 343 // when a service worker is registered and the manifest fetch times out. |
| 281 TEST_P(AddToHomescreenDataFetcherTestCommon, DISABLED_ManifestFetchTimesOut) { | 344 TEST_P(AddToHomescreenDataFetcherTestCommon, DISABLED_ManifestFetchTimesOut) { |
| 282 RegisterServiceWorker(GURL(kDefaultStartUrl)); | 345 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 283 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest(), 10000); | 346 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest(), 10000); |
| 284 | 347 |
| 285 ObserverWaiter waiter; | 348 ObserverWaiter waiter; |
| 286 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | 349 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 287 fetcher->OnDidGetWebApplicationInfo(WebApplicationInfo()); | 350 fetcher->OnDidGetWebApplicationInfo(WebApplicationInfo()); |
| 288 waiter.WaitForDataAvailable(); | 351 waiter.WaitForDataAvailable(); |
| 289 | 352 |
| 290 if (check_webapk_compatibility()) { | 353 if (check_webapk_compatibility()) { |
| 291 EXPECT_TRUE(waiter.determined_webapk_compatibility()); | 354 EXPECT_TRUE(waiter.determined_webapk_compatibility()); |
| 292 EXPECT_FALSE(waiter.is_webapk_compatible()); | 355 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 293 } else { | 356 } else { |
| 294 EXPECT_FALSE(waiter.determined_webapk_compatibility()); | 357 EXPECT_FALSE(waiter.determined_webapk_compatibility()); |
| 295 } | 358 } |
| 296 // This callback enables the text field in the add-to-homescreen dialog. | 359 // This callback enables the text field in the add-to-homescreen dialog. |
| 297 EXPECT_TRUE(waiter.title_available()); | 360 EXPECT_TRUE(waiter.title_available()); |
| 298 | 361 |
| 299 fetcher->set_weak_observer(nullptr); | 362 fetcher->set_weak_observer(nullptr); |
| 300 } | 363 } |
| 301 | 364 |
| 302 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, | 365 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, |
| 303 AddToHomescreenDataFetcherTestCommon, | 366 AddToHomescreenDataFetcherTestCommon, |
| 304 ::testing::Values(false, true)); | 367 ::testing::Values(false, true)); |
| OLD | NEW |