| 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/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "chrome/browser/installable/installable_manager.h" |
| 20 #include "chrome/common/web_application_info.h" | 21 #include "chrome/common/web_application_info.h" |
| 21 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 23 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 24 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 24 #include "content/browser/service_worker/service_worker_context_core.h" | 25 #include "content/browser/service_worker/service_worker_context_core.h" |
| 25 #include "content/common/service_worker/service_worker_status_code.h" | 26 #include "content/common/service_worker/service_worker_status_code.h" |
| 26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/site_instance.h" | 28 #include "content/public/browser/site_instance.h" |
| 28 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/common/manifest.h" | 30 #include "content/public/common/manifest.h" |
| 30 #include "content/test/test_web_contents.h" | 31 #include "content/test/test_web_contents.h" |
| 31 #include "net/http/http_status_code.h" | 32 #include "net/http/http_status_code.h" |
| 32 #include "third_party/WebKit/public/platform/WebDisplayMode.h" | 33 #include "third_party/WebKit/public/platform/WebDisplayMode.h" |
| 33 #include "ui/gfx/image/image_unittest_util.h" | 34 #include "ui/gfx/image/image_unittest_util.h" |
| 34 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 35 | 36 |
| 36 // TODO(zpeng): Effectively test scenarios where both timeout callback and | |
| 37 // success callback are invoked. See crbug.com/697228. | |
| 38 | |
| 39 namespace { | 37 namespace { |
| 40 | 38 |
| 39 const char* kWebApplicationInfoTitle = "Meta Title"; |
| 41 const char* kDefaultManifestUrl = "https://www.example.com/manifest.json"; | 40 const char* kDefaultManifestUrl = "https://www.example.com/manifest.json"; |
| 42 const char* kDefaultManifestName = "Default Name"; | 41 const char* kDefaultManifestName = "Default Name"; |
| 43 const char* kDefaultManifestShortName = "Default Short Name"; | 42 const char* kDefaultManifestShortName = "Default Short Name"; |
| 44 const char* kDefaultStartUrl = "https://www.example.com/index.html"; | 43 const char* kDefaultStartUrl = "https://www.example.com/index.html"; |
| 45 const blink::WebDisplayMode kDefaultManifestDisplayMode = | 44 const blink::WebDisplayMode kDefaultManifestDisplayMode = |
| 46 blink::kWebDisplayModeStandalone; | 45 blink::kWebDisplayModeStandalone; |
| 47 | 46 |
| 48 // WebContents subclass which mocks out image and manifest fetching. | 47 // WebContents subclass which mocks out image and manifest fetching. |
| 49 class MockWebContents : public content::TestWebContents { | 48 class MockWebContents : public content::TestWebContents { |
| 50 public: | 49 public: |
| 51 explicit MockWebContents(content::BrowserContext* browser_context) | 50 explicit MockWebContents(content::BrowserContext* browser_context) |
| 52 : content::TestWebContents(browser_context) {} | 51 : content::TestWebContents(browser_context), |
| 52 should_image_time_out_(false), |
| 53 should_manifest_time_out_(false) {} |
| 53 | 54 |
| 54 ~MockWebContents() override { | 55 ~MockWebContents() override {} |
| 55 } | |
| 56 | 56 |
| 57 // Sets the manifest to be returned by GetManifest(). | |
| 58 // |fetch_delay_ms| is the time in milliseconds that the simulated fetch of | |
| 59 // the web manifest should take. | |
| 60 void SetManifest(const GURL& manifest_url, | 57 void SetManifest(const GURL& manifest_url, |
| 61 const content::Manifest& manifest, | 58 const content::Manifest& manifest) { |
| 62 int fetch_delay_ms) { | |
| 63 manifest_url_ = manifest_url; | 59 manifest_url_ = manifest_url; |
| 64 manifest_ = manifest; | 60 manifest_ = manifest; |
| 65 manifest_fetch_delay_ms_ = fetch_delay_ms; | |
| 66 } | 61 } |
| 67 | 62 |
| 68 int DownloadImage(const GURL& url, | 63 int DownloadImage(const GURL& url, |
| 69 bool is_favicon, | 64 bool is_favicon, |
| 70 uint32_t max_bitmap_size, | 65 uint32_t max_bitmap_size, |
| 71 bool bypass_cache, | 66 bool bypass_cache, |
| 72 const ImageDownloadCallback& callback) override { | 67 const ImageDownloadCallback& callback) override { |
| 68 if (should_image_time_out_) |
| 69 return 0; |
| 70 |
| 73 const int kIconSizePx = 144; | 71 const int kIconSizePx = 144; |
| 74 SkBitmap icon = gfx::test::CreateBitmap(kIconSizePx, kIconSizePx); | 72 SkBitmap icon = gfx::test::CreateBitmap(kIconSizePx, kIconSizePx); |
| 75 std::vector<SkBitmap> icons(1u, icon); | 73 std::vector<SkBitmap> icons(1u, icon); |
| 76 std::vector<gfx::Size> pixel_sizes(1u, gfx::Size(kIconSizePx, kIconSizePx)); | 74 std::vector<gfx::Size> pixel_sizes(1u, gfx::Size(kIconSizePx, kIconSizePx)); |
| 77 content::BrowserThread::PostTask( | 75 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI) |
| 78 content::BrowserThread::UI, | 76 ->PostTask(FROM_HERE, base::Bind(callback, 0, net::HTTP_OK, url, icons, |
| 79 FROM_HERE, | 77 pixel_sizes)); |
| 80 base::Bind(callback, 0, net::HTTP_OK, url, icons, pixel_sizes)); | |
| 81 return 0; | 78 return 0; |
| 82 } | 79 } |
| 83 | 80 |
| 84 void GetManifest(const GetManifestCallback& callback) override { | 81 void GetManifest(const GetManifestCallback& callback) override { |
| 85 content::BrowserThread::PostDelayedTask( | 82 if (should_manifest_time_out_) |
| 86 content::BrowserThread::UI, FROM_HERE, | 83 return; |
| 87 base::Bind(callback, manifest_url_, manifest_), | 84 |
| 88 base::TimeDelta::FromMilliseconds(manifest_fetch_delay_ms_)); | 85 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI) |
| 86 ->PostTask(FROM_HERE, base::Bind(callback, manifest_url_, manifest_)); |
| 87 } |
| 88 |
| 89 void SetShouldImageTimeOut(bool should_time_out) { |
| 90 should_image_time_out_ = should_time_out; |
| 91 } |
| 92 |
| 93 void SetShouldManifestTimeOut(bool should_time_out) { |
| 94 should_manifest_time_out_ = should_time_out; |
| 89 } | 95 } |
| 90 | 96 |
| 91 private: | 97 private: |
| 92 GURL manifest_url_; | 98 GURL manifest_url_; |
| 93 content::Manifest manifest_; | 99 content::Manifest manifest_; |
| 94 int manifest_fetch_delay_ms_; | 100 bool should_image_time_out_; |
| 101 bool should_manifest_time_out_; |
| 95 | 102 |
| 96 DISALLOW_COPY_AND_ASSIGN(MockWebContents); | 103 DISALLOW_COPY_AND_ASSIGN(MockWebContents); |
| 97 }; | 104 }; |
| 98 | 105 |
| 99 // Tracks which of the AddToHomescreenDataFetcher::Observer callbacks have been | 106 // Tracks which of the AddToHomescreenDataFetcher::Observer callbacks have been |
| 100 // called. | 107 // called. |
| 101 class ObserverWaiter : public AddToHomescreenDataFetcher::Observer { | 108 class ObserverWaiter : public AddToHomescreenDataFetcher::Observer { |
| 102 public: | 109 public: |
| 103 ObserverWaiter() | 110 ObserverWaiter() |
| 104 : is_webapk_compatible_(false), | 111 : is_webapk_compatible_(false), |
| 105 determined_webapk_compatibility_(false), | 112 determined_webapk_compatibility_(false), |
| 106 title_available_(false), | 113 title_available_(false), |
| 107 data_available_(false) {} | 114 data_available_(false) {} |
| 108 ~ObserverWaiter() override {} | 115 ~ObserverWaiter() override {} |
| 109 | 116 |
| 110 // Waits till the OnDataAvailable() callback is called. | 117 // Waits till the OnDataAvailable() callback is called. |
| 111 void WaitForDataAvailable() { | 118 void WaitForDataAvailable() { |
| 112 if (data_available_) | 119 if (data_available_) |
| 113 return; | 120 return; |
| 114 | 121 |
| 115 base::RunLoop run_loop; | 122 base::RunLoop run_loop; |
| 116 quit_closure_ = run_loop.QuitClosure(); | 123 quit_closure_ = run_loop.QuitClosure(); |
| 117 run_loop.Run(); | 124 run_loop.Run(); |
| 118 } | 125 } |
| 119 | 126 |
| 120 void OnDidDetermineWebApkCompatibility(bool is_webapk_compatible) override { | 127 void OnDidDetermineWebApkCompatibility(bool is_webapk_compatible) override { |
| 128 EXPECT_FALSE(title_available_); |
| 121 determined_webapk_compatibility_ = true; | 129 determined_webapk_compatibility_ = true; |
| 122 is_webapk_compatible_ = is_webapk_compatible; | 130 is_webapk_compatible_ = is_webapk_compatible; |
| 123 } | 131 } |
| 124 | 132 |
| 125 void OnUserTitleAvailable(const base::string16& title) override { | 133 void OnUserTitleAvailable(const base::string16& title) override { |
| 134 EXPECT_FALSE(data_available_); |
| 126 title_available_ = true; | 135 title_available_ = true; |
| 136 title_ = title; |
| 127 } | 137 } |
| 128 | 138 |
| 129 SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon, | 139 SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon, |
| 130 const GURL& url, | 140 const GURL& url, |
| 131 bool* is_generated) override { | 141 bool* is_generated) override { |
| 132 *is_generated = false; | 142 *is_generated = false; |
| 133 return icon; | 143 return icon; |
| 134 } | 144 } |
| 135 | 145 |
| 136 void OnDataAvailable(const ShortcutInfo& info, | 146 void OnDataAvailable(const ShortcutInfo& info, |
| 137 const SkBitmap& primary_icon, | 147 const SkBitmap& primary_icon, |
| 138 const SkBitmap& badge_icon) override { | 148 const SkBitmap& badge_icon) override { |
| 149 EXPECT_TRUE(title_available_); |
| 139 data_available_ = true; | 150 data_available_ = true; |
| 140 if (!quit_closure_.is_null()) | 151 if (!quit_closure_.is_null()) |
| 141 quit_closure_.Run(); | 152 quit_closure_.Run(); |
| 142 } | 153 } |
| 143 | 154 |
| 155 base::string16 title() const { return title_; } |
| 144 bool is_webapk_compatible() const { return is_webapk_compatible_; } | 156 bool is_webapk_compatible() const { return is_webapk_compatible_; } |
| 145 bool determined_webapk_compatibility() const { | 157 bool determined_webapk_compatibility() const { |
| 146 return determined_webapk_compatibility_; | 158 return determined_webapk_compatibility_; |
| 147 } | 159 } |
| 148 bool title_available() const { return title_available_; } | 160 bool title_available() const { return title_available_; } |
| 149 | 161 |
| 150 private: | 162 private: |
| 163 base::string16 title_; |
| 151 bool is_webapk_compatible_; | 164 bool is_webapk_compatible_; |
| 152 bool determined_webapk_compatibility_; | 165 bool determined_webapk_compatibility_; |
| 153 bool title_available_; | 166 bool title_available_; |
| 154 bool data_available_; | 167 bool data_available_; |
| 155 base::Closure quit_closure_; | 168 base::Closure quit_closure_; |
| 156 | 169 |
| 157 DISALLOW_COPY_AND_ASSIGN(ObserverWaiter); | 170 DISALLOW_COPY_AND_ASSIGN(ObserverWaiter); |
| 158 }; | 171 }; |
| 159 | 172 |
| 160 // Builds non-null base::NullableString16 from a UTF8 string. | 173 // Builds non-null base::NullableString16 from a UTF8 string. |
| 161 base::NullableString16 NullableStringFromUTF8(const std::string& value) { | 174 base::NullableString16 NullableStringFromUTF8(const std::string& value) { |
| 162 return base::NullableString16(base::UTF8ToUTF16(value), false); | 175 return base::NullableString16(base::UTF8ToUTF16(value), false); |
| 163 } | 176 } |
| 164 | 177 |
| 178 content::Manifest BuildEmptyManifest() { |
| 179 return content::Manifest(); |
| 180 } |
| 181 |
| 165 // Builds WebAPK compatible content::Manifest. | 182 // Builds WebAPK compatible content::Manifest. |
| 166 content::Manifest BuildDefaultManifest() { | 183 content::Manifest BuildDefaultManifest() { |
| 167 content::Manifest manifest; | 184 content::Manifest manifest; |
| 168 manifest.name = NullableStringFromUTF8(kDefaultManifestName); | 185 manifest.name = NullableStringFromUTF8(kDefaultManifestName); |
| 169 manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName); | 186 manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName); |
| 170 manifest.start_url = GURL(kDefaultStartUrl); | 187 manifest.start_url = GURL(kDefaultStartUrl); |
| 171 manifest.display = kDefaultManifestDisplayMode; | 188 manifest.display = kDefaultManifestDisplayMode; |
| 189 |
| 190 content::Manifest::Icon primary_icon; |
| 191 primary_icon.type = base::ASCIIToUTF16("image/png"); |
| 192 primary_icon.sizes.push_back(gfx::Size(144, 144)); |
| 193 primary_icon.purpose.push_back(content::Manifest::Icon::IconPurpose::ANY); |
| 194 primary_icon.src = GURL("https://www.google.com/image.png"); |
| 195 manifest.icons.push_back(primary_icon); |
| 196 |
| 172 return manifest; | 197 return manifest; |
| 173 } | 198 } |
| 174 | 199 |
| 175 } // anonymous namespace | 200 } // anonymous namespace |
| 176 | 201 |
| 177 // Tests AddToHomescreenDataFetcher. These tests should be browser tests but | 202 // Tests AddToHomescreenDataFetcher. These tests should be browser tests but |
| 178 // Android does not support browser tests yet (crbug.com/611756). | 203 // Android does not support browser tests yet (crbug.com/611756). |
| 179 class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness { | 204 class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness { |
| 180 public: | 205 public: |
| 181 AddToHomescreenDataFetcherTest() {} | 206 AddToHomescreenDataFetcherTest() {} |
| 182 ~AddToHomescreenDataFetcherTest() override {} | 207 ~AddToHomescreenDataFetcherTest() override {} |
| 183 | 208 |
| 184 void SetUp() override { | 209 void SetUp() override { |
| 185 ChromeRenderViewHostTestHarness::SetUp(); | 210 ChromeRenderViewHostTestHarness::SetUp(); |
| 186 | 211 |
| 187 ASSERT_TRUE(profile()->CreateHistoryService(false, true)); | 212 ASSERT_TRUE(profile()->CreateHistoryService(false, true)); |
| 188 profile()->CreateFaviconService(); | 213 profile()->CreateFaviconService(); |
| 189 | 214 |
| 190 embedded_worker_test_helper_.reset( | 215 embedded_worker_test_helper_.reset( |
| 191 new content::EmbeddedWorkerTestHelper(base::FilePath())); | 216 new content::EmbeddedWorkerTestHelper(base::FilePath())); |
| 192 | 217 |
| 193 scoped_refptr<content::SiteInstance> site_instance = | 218 scoped_refptr<content::SiteInstance> site_instance = |
| 194 content::SiteInstance::Create(browser_context()); | 219 content::SiteInstance::Create(browser_context()); |
| 195 site_instance->GetProcess()->Init(); | 220 site_instance->GetProcess()->Init(); |
| 196 MockWebContents* mock_web_contents = new MockWebContents(browser_context()); | 221 MockWebContents* mock_web_contents = new MockWebContents(browser_context()); |
| 197 mock_web_contents->Init(content::WebContents::CreateParams( | 222 mock_web_contents->Init(content::WebContents::CreateParams( |
| 198 browser_context(), std::move(site_instance))); | 223 browser_context(), std::move(site_instance))); |
| 224 InstallableManager::CreateForWebContents(mock_web_contents); |
| 199 SetContents(mock_web_contents); | 225 SetContents(mock_web_contents); |
| 226 NavigateAndCommit(GURL(kDefaultStartUrl)); |
| 200 } | 227 } |
| 201 | 228 |
| 202 void TearDown() override { | 229 void TearDown() override { |
| 203 embedded_worker_test_helper_.reset(); | 230 embedded_worker_test_helper_.reset(); |
| 204 ChromeRenderViewHostTestHarness::TearDown(); | 231 ChromeRenderViewHostTestHarness::TearDown(); |
| 205 } | 232 } |
| 206 | 233 |
| 207 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( | 234 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( |
| 208 bool check_webapk_compatible, | 235 bool check_webapk_compatible, |
| 209 AddToHomescreenDataFetcher::Observer* observer) { | 236 AddToHomescreenDataFetcher::Observer* observer) { |
| 210 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1, | 237 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1, 500, |
| 211 check_webapk_compatible, observer); | 238 check_webapk_compatible, observer); |
| 212 } | 239 } |
| 213 | 240 |
| 214 // Set the manifest to be returned as a result of WebContents::GetManifest(). | 241 // Set the manifest to be returned as a result of WebContents::GetManifest(). |
| 215 void SetManifest(const GURL& manifest_url, | 242 void SetManifest(const GURL& manifest_url, |
| 216 const content::Manifest& manifest, | 243 const content::Manifest& manifest) { |
| 217 int fetch_delay_ms) { | |
| 218 MockWebContents* mock_web_contents = | 244 MockWebContents* mock_web_contents = |
| 219 static_cast<MockWebContents*>(web_contents()); | 245 static_cast<MockWebContents*>(web_contents()); |
| 220 mock_web_contents->SetManifest(manifest_url, manifest, fetch_delay_ms); | 246 mock_web_contents->SetManifest(manifest_url, manifest); |
| 247 } |
| 248 |
| 249 void SetShouldImageTimeOut(bool should_time_out) { |
| 250 MockWebContents* mock_web_contents = |
| 251 static_cast<MockWebContents*>(web_contents()); |
| 252 mock_web_contents->SetShouldImageTimeOut(should_time_out); |
| 253 } |
| 254 |
| 255 void SetShouldManifestTimeOut(bool should_time_out) { |
| 256 MockWebContents* mock_web_contents = |
| 257 static_cast<MockWebContents*>(web_contents()); |
| 258 mock_web_contents->SetShouldManifestTimeOut(should_time_out); |
| 221 } | 259 } |
| 222 | 260 |
| 223 // Registers service worker at |url|. Blocks till the service worker is | 261 // Registers service worker at |url|. Blocks till the service worker is |
| 224 // registered. | 262 // registered. |
| 225 void RegisterServiceWorker(const GURL& url) { | 263 void RegisterServiceWorker(const GURL& url) { |
| 226 base::RunLoop run_loop; | 264 base::RunLoop run_loop; |
| 227 embedded_worker_test_helper_->context()->RegisterServiceWorker( | 265 embedded_worker_test_helper_->context()->RegisterServiceWorker( |
| 228 url, GURL(url.spec() + "/service_worker.js"), nullptr, | 266 url, GURL(url.spec() + "/service_worker.js"), nullptr, |
| 229 base::Bind(&AddToHomescreenDataFetcherTest::OnServiceWorkerRegistered, | 267 base::Bind(&AddToHomescreenDataFetcherTest::OnServiceWorkerRegistered, |
| 230 base::Unretained(this), run_loop.QuitClosure())); | 268 base::Unretained(this), run_loop.QuitClosure())); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 241 << content::ServiceWorkerStatusToString(status); | 279 << content::ServiceWorkerStatusToString(status); |
| 242 callback.Run(); | 280 callback.Run(); |
| 243 } | 281 } |
| 244 | 282 |
| 245 std::unique_ptr<content::EmbeddedWorkerTestHelper> | 283 std::unique_ptr<content::EmbeddedWorkerTestHelper> |
| 246 embedded_worker_test_helper_; | 284 embedded_worker_test_helper_; |
| 247 | 285 |
| 248 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest); | 286 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest); |
| 249 }; | 287 }; |
| 250 | 288 |
| 251 // Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is | |
| 252 // called when the web manifest fetch times out. The add-to-homescreen dialog | |
| 253 // makes the dialog's text field editable once OnUserTitleAvailable() is called. | |
| 254 TEST_F(AddToHomescreenDataFetcherTest, | |
| 255 DISABLED_ManifestFetchTimesOutNoServiceWorker) { | |
| 256 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest(), 10000); | |
| 257 | |
| 258 ObserverWaiter waiter; | |
| 259 scoped_refptr<AddToHomescreenDataFetcher> fetcher( | |
| 260 BuildFetcher(false, &waiter)); | |
| 261 fetcher->OnDidGetWebApplicationInfo(WebApplicationInfo()); | |
| 262 waiter.WaitForDataAvailable(); | |
| 263 | |
| 264 EXPECT_FALSE(waiter.determined_webapk_compatibility()); | |
| 265 EXPECT_TRUE(waiter.title_available()); | |
| 266 | |
| 267 fetcher->set_weak_observer(nullptr); | |
| 268 } | |
| 269 | |
| 270 // Class for tests which should be run with AddToHomescreenDataFetcher built | 289 // Class for tests which should be run with AddToHomescreenDataFetcher built |
| 271 // with both true and false values of |check_webapk_compatible|. | 290 // with both true and false values of |check_webapk_compatible|. |
| 272 class AddToHomescreenDataFetcherTestCommon | 291 class AddToHomescreenDataFetcherTestCommon |
| 273 : public AddToHomescreenDataFetcherTest, | 292 : public AddToHomescreenDataFetcherTest, |
| 274 public testing::WithParamInterface<bool> { | 293 public testing::WithParamInterface<bool> { |
| 275 public: | 294 public: |
| 276 AddToHomescreenDataFetcherTestCommon() {} | 295 AddToHomescreenDataFetcherTestCommon() {} |
| 277 ~AddToHomescreenDataFetcherTestCommon() override {} | 296 ~AddToHomescreenDataFetcherTestCommon() override {} |
| 278 | 297 |
| 279 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( | 298 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( |
| 280 AddToHomescreenDataFetcher::Observer* observer) { | 299 AddToHomescreenDataFetcher::Observer* observer) { |
| 281 return AddToHomescreenDataFetcherTest::BuildFetcher( | 300 return AddToHomescreenDataFetcherTest::BuildFetcher( |
| 282 check_webapk_compatibility(), observer); | 301 check_webapk_compatibility(), observer); |
| 283 } | 302 } |
| 284 | 303 |
| 285 // The value of |check_webapk_compatible| used when building the | 304 // The value of |check_webapk_compatible| used when building the |
| 286 // AddToHomescreenDataFetcher. | 305 // AddToHomescreenDataFetcher. |
| 287 bool check_webapk_compatibility() { return GetParam(); } | 306 bool check_webapk_compatibility() { return GetParam(); } |
| 288 | 307 |
| 289 private: | 308 private: |
| 290 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); | 309 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); |
| 291 }; | 310 }; |
| 292 | 311 |
| 312 // Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is |
| 313 // called when the web manifest returned is empty. The add-to-homescreen dialog |
| 314 // makes the dialog's text field editable once OnUserTitleAvailable() is called. |
| 315 TEST_P(AddToHomescreenDataFetcherTestCommon, EmptyManifest) { |
| 316 WebApplicationInfo web_application_info; |
| 317 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 318 |
| 319 SetManifest(GURL(kDefaultManifestUrl), BuildEmptyManifest()); |
| 320 |
| 321 ObserverWaiter waiter; |
| 322 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 323 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 324 waiter.WaitForDataAvailable(); |
| 325 |
| 326 EXPECT_EQ(check_webapk_compatibility(), |
| 327 waiter.determined_webapk_compatibility()); |
| 328 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 329 EXPECT_TRUE(waiter.title_available()); |
| 330 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); |
| 331 |
| 332 fetcher->set_weak_observer(nullptr); |
| 333 } |
| 334 |
| 293 // Test that when the manifest provides Manifest::short_name but not | 335 // Test that when the manifest provides Manifest::short_name but not |
| 294 // Manifest::name that Manifest::short_name is used as the name instead of | 336 // Manifest::name that Manifest::short_name is used as the name instead of |
| 295 // WebApplicationInfo::title. | 337 // WebApplicationInfo::title. |
| 296 TEST_P(AddToHomescreenDataFetcherTestCommon, | 338 TEST_P(AddToHomescreenDataFetcherTestCommon, |
| 297 ManifestShortNameClobbersWebApplicationName) { | 339 ManifestShortNameClobbersWebApplicationName) { |
| 298 WebApplicationInfo web_application_info; | 340 WebApplicationInfo web_application_info; |
| 299 web_application_info.title = base::UTF8ToUTF16("Meta Title"); | 341 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 300 | 342 |
| 301 content::Manifest manifest(BuildDefaultManifest()); | 343 content::Manifest manifest(BuildDefaultManifest()); |
| 302 manifest.name = base::NullableString16(); | 344 manifest.name = base::NullableString16(); |
| 303 | 345 |
| 304 RegisterServiceWorker(GURL(kDefaultStartUrl)); | 346 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 305 SetManifest(GURL(kDefaultManifestUrl), manifest, 0); | 347 SetManifest(GURL(kDefaultManifestUrl), manifest); |
| 306 | 348 |
| 307 ObserverWaiter waiter; | 349 ObserverWaiter waiter; |
| 308 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | 350 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 309 fetcher->OnDidGetWebApplicationInfo(web_application_info); | 351 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 310 waiter.WaitForDataAvailable(); | 352 waiter.WaitForDataAvailable(); |
| 311 | 353 |
| 354 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); |
| 312 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, | 355 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, |
| 313 kDefaultManifestShortName)); | 356 kDefaultManifestShortName)); |
| 314 | 357 |
| 315 fetcher->set_weak_observer(nullptr); | 358 fetcher->set_weak_observer(nullptr); |
| 316 } | 359 } |
| 317 | 360 |
| 318 // Test that when the manifest does not provide either Manifest::short_name nor | 361 // Test that when the manifest does not provide either Manifest::short_name nor |
| 319 // Manifest::name that: | 362 // Manifest::name that: |
| 320 // - The page is not WebAPK compatible. | 363 // - The page is not WebAPK compatible. |
| 321 // - WebApplicationInfo::title is used as the "name". | 364 // - WebApplicationInfo::title is used as the "name". |
| 322 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { | 365 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { |
| 323 const char* kWebApplicationInfoTitle = "Meta Title"; | 366 WebApplicationInfo web_application_info; |
| 324 WebApplicationInfo web_application_info; | 367 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 325 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); | |
| 326 | 368 |
| 327 content::Manifest manifest(BuildDefaultManifest()); | 369 content::Manifest manifest(BuildDefaultManifest()); |
| 328 manifest.name = base::NullableString16(); | 370 manifest.name = base::NullableString16(); |
| 329 manifest.short_name = base::NullableString16(); | 371 manifest.short_name = base::NullableString16(); |
| 330 | 372 |
| 331 RegisterServiceWorker(GURL(kDefaultStartUrl)); | 373 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 332 SetManifest(GURL(kDefaultManifestUrl), manifest, 0); | 374 SetManifest(GURL(kDefaultManifestUrl), manifest); |
| 333 | 375 |
| 334 ObserverWaiter waiter; | 376 ObserverWaiter waiter; |
| 335 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | 377 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 336 fetcher->OnDidGetWebApplicationInfo(web_application_info); | 378 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 337 waiter.WaitForDataAvailable(); | 379 waiter.WaitForDataAvailable(); |
| 338 | 380 |
| 339 EXPECT_FALSE(waiter.is_webapk_compatible()); | 381 EXPECT_EQ(check_webapk_compatibility(), |
| 340 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, | 382 waiter.determined_webapk_compatibility()); |
| 341 kWebApplicationInfoTitle)); | 383 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 384 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); |
| 385 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, |
| 386 kWebApplicationInfoTitle)); |
| 342 | 387 |
| 343 fetcher->set_weak_observer(nullptr); | 388 fetcher->set_weak_observer(nullptr); |
| 344 } | 389 } |
| 345 | 390 |
| 346 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called | 391 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called |
| 347 // when a service worker is registered and the manifest fetch times out. | 392 // when the manifest fetch times out. |
| 348 TEST_P(AddToHomescreenDataFetcherTestCommon, DISABLED_ManifestFetchTimesOut) { | 393 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) { |
| 349 RegisterServiceWorker(GURL(kDefaultStartUrl)); | 394 WebApplicationInfo web_application_info; |
| 350 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest(), 10000); | 395 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 351 | 396 |
| 352 ObserverWaiter waiter; | 397 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 353 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | 398 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); |
| 354 fetcher->OnDidGetWebApplicationInfo(WebApplicationInfo()); | 399 SetShouldManifestTimeOut(true); |
| 355 waiter.WaitForDataAvailable(); | 400 SetShouldImageTimeOut(false); |
| 356 | 401 |
| 357 if (check_webapk_compatibility()) { | 402 ObserverWaiter waiter; |
| 358 EXPECT_TRUE(waiter.determined_webapk_compatibility()); | 403 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 359 EXPECT_FALSE(waiter.is_webapk_compatible()); | 404 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 360 } else { | 405 waiter.WaitForDataAvailable(); |
| 361 EXPECT_FALSE(waiter.determined_webapk_compatibility()); | |
| 362 } | |
| 363 // This callback enables the text field in the add-to-homescreen dialog. | |
| 364 EXPECT_TRUE(waiter.title_available()); | |
| 365 | 406 |
| 366 fetcher->set_weak_observer(nullptr); | 407 EXPECT_EQ(check_webapk_compatibility(), |
| 408 waiter.determined_webapk_compatibility()); |
| 409 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 410 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); |
| 411 EXPECT_TRUE(waiter.title_available()); |
| 412 |
| 413 fetcher->set_weak_observer(nullptr); |
| 414 } |
| 415 |
| 416 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called |
| 417 // when the image fetch times out. |
| 418 TEST_P(AddToHomescreenDataFetcherTestCommon, ImageFetchTimesOut) { |
| 419 WebApplicationInfo web_application_info; |
| 420 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 421 |
| 422 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 423 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); |
| 424 SetShouldManifestTimeOut(false); |
| 425 SetShouldImageTimeOut(true); |
| 426 |
| 427 ObserverWaiter waiter; |
| 428 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 429 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 430 waiter.WaitForDataAvailable(); |
| 431 |
| 432 EXPECT_EQ(check_webapk_compatibility(), |
| 433 waiter.determined_webapk_compatibility()); |
| 434 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 435 EXPECT_TRUE(waiter.title_available()); |
| 436 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); |
| 437 |
| 438 fetcher->set_weak_observer(nullptr); |
| 439 } |
| 440 |
| 441 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called |
| 442 // when the service worker check times out. |
| 443 TEST_P(AddToHomescreenDataFetcherTestCommon, ServiceWorkerCheckTimesOut) { |
| 444 WebApplicationInfo web_application_info; |
| 445 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 446 |
| 447 // Not registering a service worker means we'll wait and time out for the |
| 448 // worker. |
| 449 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); |
| 450 SetShouldManifestTimeOut(false); |
| 451 SetShouldImageTimeOut(false); |
| 452 |
| 453 ObserverWaiter waiter; |
| 454 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 455 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 456 waiter.WaitForDataAvailable(); |
| 457 |
| 458 EXPECT_EQ(check_webapk_compatibility(), |
| 459 waiter.determined_webapk_compatibility()); |
| 460 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 461 EXPECT_TRUE(waiter.title_available()); |
| 462 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); |
| 463 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title, |
| 464 kDefaultManifestShortName)); |
| 465 |
| 466 fetcher->set_weak_observer(nullptr); |
| 367 } | 467 } |
| 368 | 468 |
| 369 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, | 469 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, |
| 370 AddToHomescreenDataFetcherTestCommon, | 470 AddToHomescreenDataFetcherTestCommon, |
| 371 ::testing::Values(false, true)); | 471 ::testing::Values(false, true)); |
| OLD | NEW |