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), |
(...skipping 11 matching lines...) Expand all Loading... |
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 { |
121 determined_webapk_compatibility_ = true; | 128 determined_webapk_compatibility_ = true; |
122 is_webapk_compatible_ = is_webapk_compatible; | 129 is_webapk_compatible_ = is_webapk_compatible; |
123 } | 130 } |
124 | 131 |
125 void OnUserTitleAvailable(const base::string16& title) override { | 132 void OnUserTitleAvailable(const base::string16& title) override { |
| 133 EXPECT_FALSE(data_available_); |
126 title_available_ = true; | 134 title_available_ = true; |
| 135 title_ = title; |
127 } | 136 } |
128 | 137 |
129 SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon, | 138 SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon, |
130 const GURL& url, | 139 const GURL& url, |
131 bool* is_generated) override { | 140 bool* is_generated) override { |
132 *is_generated = false; | 141 *is_generated = false; |
133 return icon; | 142 return icon; |
134 } | 143 } |
135 | 144 |
136 void OnDataAvailable(const ShortcutInfo& info, | 145 void OnDataAvailable(const ShortcutInfo& info, |
137 const SkBitmap& primary_icon, | 146 const SkBitmap& primary_icon, |
138 const SkBitmap& badge_icon) override { | 147 const SkBitmap& badge_icon) override { |
| 148 EXPECT_TRUE(title_available_); |
139 data_available_ = true; | 149 data_available_ = true; |
140 if (!quit_closure_.is_null()) | 150 if (!quit_closure_.is_null()) |
141 quit_closure_.Run(); | 151 quit_closure_.Run(); |
142 } | 152 } |
143 | 153 |
| 154 base::string16 title() const { return title_; } |
144 bool is_webapk_compatible() const { return is_webapk_compatible_; } | 155 bool is_webapk_compatible() const { return is_webapk_compatible_; } |
145 bool determined_webapk_compatibility() const { | 156 bool determined_webapk_compatibility() const { |
146 return determined_webapk_compatibility_; | 157 return determined_webapk_compatibility_; |
147 } | 158 } |
148 bool title_available() const { return title_available_; } | 159 bool title_available() const { return title_available_; } |
149 | 160 |
150 private: | 161 private: |
| 162 base::string16 title_; |
151 bool is_webapk_compatible_; | 163 bool is_webapk_compatible_; |
152 bool determined_webapk_compatibility_; | 164 bool determined_webapk_compatibility_; |
153 bool title_available_; | 165 bool title_available_; |
154 bool data_available_; | 166 bool data_available_; |
155 base::Closure quit_closure_; | 167 base::Closure quit_closure_; |
156 | 168 |
157 DISALLOW_COPY_AND_ASSIGN(ObserverWaiter); | 169 DISALLOW_COPY_AND_ASSIGN(ObserverWaiter); |
158 }; | 170 }; |
159 | 171 |
160 // Builds non-null base::NullableString16 from a UTF8 string. | 172 // Builds non-null base::NullableString16 from a UTF8 string. |
161 base::NullableString16 NullableStringFromUTF8(const std::string& value) { | 173 base::NullableString16 NullableStringFromUTF8(const std::string& value) { |
162 return base::NullableString16(base::UTF8ToUTF16(value), false); | 174 return base::NullableString16(base::UTF8ToUTF16(value), false); |
163 } | 175 } |
164 | 176 |
| 177 content::Manifest BuildEmptyManifest() { |
| 178 return content::Manifest(); |
| 179 } |
| 180 |
165 // Builds WebAPK compatible content::Manifest. | 181 // Builds WebAPK compatible content::Manifest. |
166 content::Manifest BuildDefaultManifest() { | 182 content::Manifest BuildDefaultManifest() { |
167 content::Manifest manifest; | 183 content::Manifest manifest; |
168 manifest.name = NullableStringFromUTF8(kDefaultManifestName); | 184 manifest.name = NullableStringFromUTF8(kDefaultManifestName); |
169 manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName); | 185 manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName); |
170 manifest.start_url = GURL(kDefaultStartUrl); | 186 manifest.start_url = GURL(kDefaultStartUrl); |
171 manifest.display = kDefaultManifestDisplayMode; | 187 manifest.display = kDefaultManifestDisplayMode; |
| 188 |
| 189 content::Manifest::Icon primary_icon; |
| 190 primary_icon.type = base::ASCIIToUTF16("image/png"); |
| 191 primary_icon.sizes.push_back(gfx::Size(144, 144)); |
| 192 primary_icon.purpose.push_back(content::Manifest::Icon::IconPurpose::ANY); |
| 193 primary_icon.src = GURL("https://www.google.com/image.png"); |
| 194 manifest.icons.push_back(primary_icon); |
| 195 |
172 return manifest; | 196 return manifest; |
173 } | 197 } |
174 | 198 |
175 } // anonymous namespace | 199 } // anonymous namespace |
176 | 200 |
177 // Tests AddToHomescreenDataFetcher. These tests should be browser tests but | 201 // Tests AddToHomescreenDataFetcher. These tests should be browser tests but |
178 // Android does not support browser tests yet (crbug.com/611756). | 202 // Android does not support browser tests yet (crbug.com/611756). |
179 class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness { | 203 class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness { |
180 public: | 204 public: |
181 AddToHomescreenDataFetcherTest() {} | 205 AddToHomescreenDataFetcherTest() {} |
182 ~AddToHomescreenDataFetcherTest() override {} | 206 ~AddToHomescreenDataFetcherTest() override {} |
183 | 207 |
184 void SetUp() override { | 208 void SetUp() override { |
185 ChromeRenderViewHostTestHarness::SetUp(); | 209 ChromeRenderViewHostTestHarness::SetUp(); |
186 | 210 |
187 ASSERT_TRUE(profile()->CreateHistoryService(false, true)); | 211 ASSERT_TRUE(profile()->CreateHistoryService(false, true)); |
188 profile()->CreateFaviconService(); | 212 profile()->CreateFaviconService(); |
189 | 213 |
190 embedded_worker_test_helper_.reset( | 214 embedded_worker_test_helper_.reset( |
191 new content::EmbeddedWorkerTestHelper(base::FilePath())); | 215 new content::EmbeddedWorkerTestHelper(base::FilePath())); |
192 | 216 |
193 scoped_refptr<content::SiteInstance> site_instance = | 217 scoped_refptr<content::SiteInstance> site_instance = |
194 content::SiteInstance::Create(browser_context()); | 218 content::SiteInstance::Create(browser_context()); |
195 site_instance->GetProcess()->Init(); | 219 site_instance->GetProcess()->Init(); |
196 MockWebContents* mock_web_contents = new MockWebContents(browser_context()); | 220 MockWebContents* mock_web_contents = new MockWebContents(browser_context()); |
197 mock_web_contents->Init(content::WebContents::CreateParams( | 221 mock_web_contents->Init(content::WebContents::CreateParams( |
198 browser_context(), std::move(site_instance))); | 222 browser_context(), std::move(site_instance))); |
| 223 InstallableManager::CreateForWebContents(mock_web_contents); |
199 SetContents(mock_web_contents); | 224 SetContents(mock_web_contents); |
| 225 NavigateAndCommit(GURL(kDefaultStartUrl)); |
200 } | 226 } |
201 | 227 |
202 void TearDown() override { | 228 void TearDown() override { |
203 embedded_worker_test_helper_.reset(); | 229 embedded_worker_test_helper_.reset(); |
204 ChromeRenderViewHostTestHarness::TearDown(); | 230 ChromeRenderViewHostTestHarness::TearDown(); |
205 } | 231 } |
206 | 232 |
207 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( | 233 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( |
208 bool check_webapk_compatible, | 234 bool check_webapk_compatible, |
209 AddToHomescreenDataFetcher::Observer* observer) { | 235 AddToHomescreenDataFetcher::Observer* observer) { |
210 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1, | 236 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1, 500, |
211 check_webapk_compatible, observer); | 237 check_webapk_compatible, observer); |
212 } | 238 } |
213 | 239 |
214 // Set the manifest to be returned as a result of WebContents::GetManifest(). | 240 // Set the manifest to be returned as a result of WebContents::GetManifest(). |
215 void SetManifest(const GURL& manifest_url, | 241 void SetManifest(const GURL& manifest_url, |
216 const content::Manifest& manifest, | 242 const content::Manifest& manifest) { |
217 int fetch_delay_ms) { | |
218 MockWebContents* mock_web_contents = | 243 MockWebContents* mock_web_contents = |
219 static_cast<MockWebContents*>(web_contents()); | 244 static_cast<MockWebContents*>(web_contents()); |
220 mock_web_contents->SetManifest(manifest_url, manifest, fetch_delay_ms); | 245 mock_web_contents->SetManifest(manifest_url, manifest); |
| 246 } |
| 247 |
| 248 void SetShouldImageTimeOut(bool should_time_out) { |
| 249 MockWebContents* mock_web_contents = |
| 250 static_cast<MockWebContents*>(web_contents()); |
| 251 mock_web_contents->SetShouldImageTimeOut(should_time_out); |
| 252 } |
| 253 |
| 254 void SetShouldManifestTimeOut(bool should_time_out) { |
| 255 MockWebContents* mock_web_contents = |
| 256 static_cast<MockWebContents*>(web_contents()); |
| 257 mock_web_contents->SetShouldManifestTimeOut(should_time_out); |
221 } | 258 } |
222 | 259 |
223 // Registers service worker at |url|. Blocks till the service worker is | 260 // Registers service worker at |url|. Blocks till the service worker is |
224 // registered. | 261 // registered. |
225 void RegisterServiceWorker(const GURL& url) { | 262 void RegisterServiceWorker(const GURL& url) { |
226 base::RunLoop run_loop; | 263 base::RunLoop run_loop; |
227 embedded_worker_test_helper_->context()->RegisterServiceWorker( | 264 embedded_worker_test_helper_->context()->RegisterServiceWorker( |
228 url, GURL(url.spec() + "/service_worker.js"), nullptr, | 265 url, GURL(url.spec() + "/service_worker.js"), nullptr, |
229 base::Bind(&AddToHomescreenDataFetcherTest::OnServiceWorkerRegistered, | 266 base::Bind(&AddToHomescreenDataFetcherTest::OnServiceWorkerRegistered, |
230 base::Unretained(this), run_loop.QuitClosure())); | 267 base::Unretained(this), run_loop.QuitClosure())); |
(...skipping 10 matching lines...) Expand all Loading... |
241 << content::ServiceWorkerStatusToString(status); | 278 << content::ServiceWorkerStatusToString(status); |
242 callback.Run(); | 279 callback.Run(); |
243 } | 280 } |
244 | 281 |
245 std::unique_ptr<content::EmbeddedWorkerTestHelper> | 282 std::unique_ptr<content::EmbeddedWorkerTestHelper> |
246 embedded_worker_test_helper_; | 283 embedded_worker_test_helper_; |
247 | 284 |
248 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest); | 285 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest); |
249 }; | 286 }; |
250 | 287 |
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 | 288 // Class for tests which should be run with AddToHomescreenDataFetcher built |
271 // with both true and false values of |check_webapk_compatible|. | 289 // with both true and false values of |check_webapk_compatible|. |
272 class AddToHomescreenDataFetcherTestCommon | 290 class AddToHomescreenDataFetcherTestCommon |
273 : public AddToHomescreenDataFetcherTest, | 291 : public AddToHomescreenDataFetcherTest, |
274 public testing::WithParamInterface<bool> { | 292 public testing::WithParamInterface<bool> { |
275 public: | 293 public: |
276 AddToHomescreenDataFetcherTestCommon() {} | 294 AddToHomescreenDataFetcherTestCommon() {} |
277 ~AddToHomescreenDataFetcherTestCommon() override {} | 295 ~AddToHomescreenDataFetcherTestCommon() override {} |
278 | 296 |
279 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( | 297 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( |
280 AddToHomescreenDataFetcher::Observer* observer) { | 298 AddToHomescreenDataFetcher::Observer* observer) { |
281 return AddToHomescreenDataFetcherTest::BuildFetcher( | 299 return AddToHomescreenDataFetcherTest::BuildFetcher( |
282 check_webapk_compatibility(), observer); | 300 check_webapk_compatibility(), observer); |
283 } | 301 } |
284 | 302 |
285 // The value of |check_webapk_compatible| used when building the | 303 // The value of |check_webapk_compatible| used when building the |
286 // AddToHomescreenDataFetcher. | 304 // AddToHomescreenDataFetcher. |
287 bool check_webapk_compatibility() { return GetParam(); } | 305 bool check_webapk_compatibility() { return GetParam(); } |
288 | 306 |
289 private: | 307 private: |
290 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); | 308 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); |
291 }; | 309 }; |
292 | 310 |
| 311 // Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is |
| 312 // called when the web manifest returned is empty. The add-to-homescreen dialog |
| 313 // makes the dialog's text field editable once OnUserTitleAvailable() is called. |
| 314 TEST_P(AddToHomescreenDataFetcherTestCommon, EmptyManifest) { |
| 315 WebApplicationInfo web_application_info; |
| 316 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 317 |
| 318 SetManifest(GURL(kDefaultManifestUrl), BuildEmptyManifest()); |
| 319 |
| 320 ObserverWaiter waiter; |
| 321 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 322 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 323 waiter.WaitForDataAvailable(); |
| 324 |
| 325 EXPECT_EQ(check_webapk_compatibility(), |
| 326 waiter.determined_webapk_compatibility()); |
| 327 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 328 EXPECT_TRUE(waiter.title_available()); |
| 329 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); |
| 330 |
| 331 fetcher->set_weak_observer(nullptr); |
| 332 } |
| 333 |
293 // Test that when the manifest provides Manifest::short_name but not | 334 // 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 | 335 // Manifest::name that Manifest::short_name is used as the name instead of |
295 // WebApplicationInfo::title. | 336 // WebApplicationInfo::title. |
296 TEST_P(AddToHomescreenDataFetcherTestCommon, | 337 TEST_P(AddToHomescreenDataFetcherTestCommon, |
297 ManifestShortNameClobbersWebApplicationName) { | 338 ManifestShortNameClobbersWebApplicationName) { |
298 WebApplicationInfo web_application_info; | 339 WebApplicationInfo web_application_info; |
299 web_application_info.title = base::UTF8ToUTF16("Meta Title"); | 340 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
300 | 341 |
301 content::Manifest manifest(BuildDefaultManifest()); | 342 content::Manifest manifest(BuildDefaultManifest()); |
302 manifest.name = base::NullableString16(); | 343 manifest.name = base::NullableString16(); |
303 | 344 |
304 RegisterServiceWorker(GURL(kDefaultStartUrl)); | 345 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
305 SetManifest(GURL(kDefaultManifestUrl), manifest, 0); | 346 SetManifest(GURL(kDefaultManifestUrl), manifest); |
306 | 347 |
307 ObserverWaiter waiter; | 348 ObserverWaiter waiter; |
308 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | 349 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
309 fetcher->OnDidGetWebApplicationInfo(web_application_info); | 350 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
310 waiter.WaitForDataAvailable(); | 351 waiter.WaitForDataAvailable(); |
311 | 352 |
| 353 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); |
312 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, | 354 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, |
313 kDefaultManifestShortName)); | 355 kDefaultManifestShortName)); |
314 | 356 |
315 fetcher->set_weak_observer(nullptr); | 357 fetcher->set_weak_observer(nullptr); |
316 } | 358 } |
317 | 359 |
318 // Test that when the manifest does not provide either Manifest::short_name nor | 360 // Test that when the manifest does not provide either Manifest::short_name nor |
319 // Manifest::name that: | 361 // Manifest::name that: |
320 // - The page is not WebAPK compatible. | 362 // - The page is not WebAPK compatible. |
321 // - WebApplicationInfo::title is used as the "name". | 363 // - WebApplicationInfo::title is used as the "name". |
322 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { | 364 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { |
323 const char* kWebApplicationInfoTitle = "Meta Title"; | 365 WebApplicationInfo web_application_info; |
324 WebApplicationInfo web_application_info; | 366 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
325 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); | |
326 | 367 |
327 content::Manifest manifest(BuildDefaultManifest()); | 368 content::Manifest manifest(BuildDefaultManifest()); |
328 manifest.name = base::NullableString16(); | 369 manifest.name = base::NullableString16(); |
329 manifest.short_name = base::NullableString16(); | 370 manifest.short_name = base::NullableString16(); |
330 | 371 |
331 RegisterServiceWorker(GURL(kDefaultStartUrl)); | 372 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
332 SetManifest(GURL(kDefaultManifestUrl), manifest, 0); | 373 SetManifest(GURL(kDefaultManifestUrl), manifest); |
333 | 374 |
334 ObserverWaiter waiter; | 375 ObserverWaiter waiter; |
335 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | 376 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
336 fetcher->OnDidGetWebApplicationInfo(web_application_info); | 377 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
337 waiter.WaitForDataAvailable(); | 378 waiter.WaitForDataAvailable(); |
338 | 379 |
339 EXPECT_FALSE(waiter.is_webapk_compatible()); | 380 EXPECT_EQ(check_webapk_compatibility(), |
340 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, | 381 waiter.determined_webapk_compatibility()); |
341 kWebApplicationInfoTitle)); | 382 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 383 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); |
| 384 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, |
| 385 kWebApplicationInfoTitle)); |
342 | 386 |
343 fetcher->set_weak_observer(nullptr); | 387 fetcher->set_weak_observer(nullptr); |
344 } | 388 } |
345 | 389 |
346 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called | 390 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called |
347 // when a service worker is registered and the manifest fetch times out. | 391 // when the manifest fetch times out. |
348 TEST_P(AddToHomescreenDataFetcherTestCommon, DISABLED_ManifestFetchTimesOut) { | 392 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) { |
349 RegisterServiceWorker(GURL(kDefaultStartUrl)); | 393 WebApplicationInfo web_application_info; |
350 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest(), 10000); | 394 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
351 | 395 |
352 ObserverWaiter waiter; | 396 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
353 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); | 397 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); |
354 fetcher->OnDidGetWebApplicationInfo(WebApplicationInfo()); | 398 SetShouldManifestTimeOut(true); |
355 waiter.WaitForDataAvailable(); | 399 SetShouldImageTimeOut(false); |
356 | 400 |
357 if (check_webapk_compatibility()) { | 401 ObserverWaiter waiter; |
358 EXPECT_TRUE(waiter.determined_webapk_compatibility()); | 402 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
359 EXPECT_FALSE(waiter.is_webapk_compatible()); | 403 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
360 } else { | 404 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 | 405 |
366 fetcher->set_weak_observer(nullptr); | 406 EXPECT_EQ(check_webapk_compatibility(), |
| 407 waiter.determined_webapk_compatibility()); |
| 408 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 409 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); |
| 410 EXPECT_TRUE(waiter.title_available()); |
| 411 |
| 412 fetcher->set_weak_observer(nullptr); |
| 413 } |
| 414 |
| 415 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called |
| 416 // when the image fetch times out. |
| 417 TEST_P(AddToHomescreenDataFetcherTestCommon, ImageFetchTimesOut) { |
| 418 WebApplicationInfo web_application_info; |
| 419 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 420 |
| 421 RegisterServiceWorker(GURL(kDefaultStartUrl)); |
| 422 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); |
| 423 SetShouldManifestTimeOut(false); |
| 424 SetShouldImageTimeOut(true); |
| 425 |
| 426 ObserverWaiter waiter; |
| 427 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 428 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 429 waiter.WaitForDataAvailable(); |
| 430 |
| 431 EXPECT_EQ(check_webapk_compatibility(), |
| 432 waiter.determined_webapk_compatibility()); |
| 433 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 434 EXPECT_TRUE(waiter.title_available()); |
| 435 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); |
| 436 |
| 437 fetcher->set_weak_observer(nullptr); |
| 438 } |
| 439 |
| 440 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called |
| 441 // when the service worker check times out. |
| 442 TEST_P(AddToHomescreenDataFetcherTestCommon, ServiceWorkerCheckTimesOut) { |
| 443 WebApplicationInfo web_application_info; |
| 444 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); |
| 445 |
| 446 // Not registering a service worker means we'll wait and time out for the |
| 447 // worker. |
| 448 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); |
| 449 SetShouldManifestTimeOut(false); |
| 450 SetShouldImageTimeOut(false); |
| 451 |
| 452 ObserverWaiter waiter; |
| 453 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); |
| 454 fetcher->OnDidGetWebApplicationInfo(web_application_info); |
| 455 waiter.WaitForDataAvailable(); |
| 456 |
| 457 EXPECT_EQ(check_webapk_compatibility(), |
| 458 waiter.determined_webapk_compatibility()); |
| 459 EXPECT_FALSE(waiter.is_webapk_compatible()); |
| 460 EXPECT_TRUE(waiter.title_available()); |
| 461 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); |
| 462 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title, |
| 463 kDefaultManifestShortName)); |
| 464 |
| 465 fetcher->set_weak_observer(nullptr); |
367 } | 466 } |
368 | 467 |
369 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, | 468 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, |
370 AddToHomescreenDataFetcherTestCommon, | 469 AddToHomescreenDataFetcherTestCommon, |
371 ::testing::Values(false, true)); | 470 ::testing::Values(false, true)); |
OLD | NEW |