Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: chrome/browser/android/webapps/add_to_homescreen_data_fetcher_unittest.cc

Issue 2960103002: Improve add to homescreen data fetcher unit tests. (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/installable/installable_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/files/file_path.h"
12 #include "base/macros.h" 10 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 12 #include "base/run_loop.h"
16 #include "base/strings/nullable_string16.h" 13 #include "base/strings/nullable_string16.h"
17 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/installable/installable_manager.h" 16 #include "chrome/browser/installable/installable_manager.h"
21 #include "chrome/common/web_application_info.h" 17 #include "chrome/common/web_application_info.h"
22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
23 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
24 #include "content/browser/service_worker/embedded_worker_test_helper.h"
25 #include "content/browser/service_worker/service_worker_context_core.h"
26 #include "content/common/service_worker/service_worker_status_code.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/site_instance.h"
29 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
30 #include "content/public/common/manifest.h" 21 #include "content/public/common/manifest.h"
31 #include "content/test/test_web_contents.h"
32 #include "net/http/http_status_code.h"
33 #include "third_party/WebKit/public/platform/WebDisplayMode.h" 22 #include "third_party/WebKit/public/platform/WebDisplayMode.h"
34 #include "ui/gfx/image/image_unittest_util.h" 23 #include "ui/gfx/image/image_unittest_util.h"
35 #include "url/gurl.h" 24 #include "url/gurl.h"
36 25
37 namespace { 26 namespace {
38 27
39 const char* kWebApplicationInfoTitle = "Meta Title"; 28 const char* kWebApplicationInfoTitle = "Meta Title";
40 const char* kDefaultManifestUrl = "https://www.example.com/manifest.json"; 29 const char* kDefaultManifestUrl = "https://www.example.com/manifest.json";
30 const char* kDefaultIconUrl = "https://www.example.com/icon.png";
41 const char* kDefaultManifestName = "Default Name"; 31 const char* kDefaultManifestName = "Default Name";
42 const char* kDefaultManifestShortName = "Default Short Name"; 32 const char* kDefaultManifestShortName = "Default Short Name";
43 const char* kDefaultStartUrl = "https://www.example.com/index.html"; 33 const char* kDefaultStartUrl = "https://www.example.com/index.html";
44 const blink::WebDisplayMode kDefaultManifestDisplayMode = 34 const blink::WebDisplayMode kDefaultManifestDisplayMode =
45 blink::kWebDisplayModeStandalone; 35 blink::kWebDisplayModeStandalone;
46 36 const int kIconSizePx = 144;
47 // WebContents subclass which mocks out image and manifest fetching.
48 class MockWebContents : public content::TestWebContents {
49 public:
50 explicit MockWebContents(content::BrowserContext* browser_context)
51 : content::TestWebContents(browser_context),
52 should_image_time_out_(false),
53 should_manifest_time_out_(false) {}
54
55 ~MockWebContents() override {}
56
57 void SetManifest(const GURL& manifest_url,
58 const content::Manifest& manifest) {
59 manifest_url_ = manifest_url;
60 manifest_ = manifest;
61 }
62
63 int DownloadImage(const GURL& url,
64 bool is_favicon,
65 uint32_t max_bitmap_size,
66 bool bypass_cache,
67 const ImageDownloadCallback& callback) override {
68 if (should_image_time_out_)
69 return 0;
70
71 const int kIconSizePx = 144;
72 SkBitmap icon = gfx::test::CreateBitmap(kIconSizePx, kIconSizePx);
73 std::vector<SkBitmap> icons(1u, icon);
74 std::vector<gfx::Size> pixel_sizes(1u, gfx::Size(kIconSizePx, kIconSizePx));
75 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI)
76 ->PostTask(FROM_HERE, base::Bind(callback, 0, net::HTTP_OK, url, icons,
77 pixel_sizes));
78 return 0;
79 }
80
81 void GetManifest(const GetManifestCallback& callback) override {
82 if (should_manifest_time_out_)
83 return;
84
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;
95 }
96
97 private:
98 GURL manifest_url_;
99 content::Manifest manifest_;
100 bool should_image_time_out_;
101 bool should_manifest_time_out_;
102
103 DISALLOW_COPY_AND_ASSIGN(MockWebContents);
104 };
105 37
106 // Tracks which of the AddToHomescreenDataFetcher::Observer callbacks have been 38 // Tracks which of the AddToHomescreenDataFetcher::Observer callbacks have been
107 // called. 39 // called.
108 class ObserverWaiter : public AddToHomescreenDataFetcher::Observer { 40 class ObserverWaiter : public AddToHomescreenDataFetcher::Observer {
109 public: 41 public:
110 ObserverWaiter() 42 ObserverWaiter()
111 : is_webapk_compatible_(false), 43 : is_webapk_compatible_(false),
112 determined_webapk_compatibility_(false), 44 determined_webapk_compatibility_(false),
113 title_available_(false), 45 title_available_(false),
114 data_available_(false) {} 46 data_available_(false) {}
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 base::Closure quit_closure_; 100 base::Closure quit_closure_;
169 101
170 DISALLOW_COPY_AND_ASSIGN(ObserverWaiter); 102 DISALLOW_COPY_AND_ASSIGN(ObserverWaiter);
171 }; 103 };
172 104
173 // Builds non-null base::NullableString16 from a UTF8 string. 105 // Builds non-null base::NullableString16 from a UTF8 string.
174 base::NullableString16 NullableStringFromUTF8(const std::string& value) { 106 base::NullableString16 NullableStringFromUTF8(const std::string& value) {
175 return base::NullableString16(base::UTF8ToUTF16(value), false); 107 return base::NullableString16(base::UTF8ToUTF16(value), false);
176 } 108 }
177 109
178 content::Manifest BuildEmptyManifest() { 110 content::Manifest BuildNoIconManifest() {
179 return content::Manifest();
180 }
181
182 // Builds WebAPK compatible content::Manifest.
183 content::Manifest BuildDefaultManifest() {
184 content::Manifest manifest; 111 content::Manifest manifest;
185 manifest.name = NullableStringFromUTF8(kDefaultManifestName); 112 manifest.name = NullableStringFromUTF8(kDefaultManifestName);
186 manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName); 113 manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName);
187 manifest.start_url = GURL(kDefaultStartUrl); 114 manifest.start_url = GURL(kDefaultStartUrl);
188 manifest.display = kDefaultManifestDisplayMode; 115 manifest.display = kDefaultManifestDisplayMode;
189 116
117 return manifest;
118 }
119
120 // Builds WebAPK compatible content::Manifest.
121 content::Manifest BuildDefaultManifest() {
122 content::Manifest manifest = BuildNoIconManifest();
123
190 content::Manifest::Icon primary_icon; 124 content::Manifest::Icon primary_icon;
191 primary_icon.type = base::ASCIIToUTF16("image/png"); 125 primary_icon.type = base::ASCIIToUTF16("image/png");
192 primary_icon.sizes.push_back(gfx::Size(144, 144)); 126 primary_icon.sizes.push_back(gfx::Size(144, 144));
193 primary_icon.purpose.push_back(content::Manifest::Icon::IconPurpose::ANY); 127 primary_icon.purpose.push_back(content::Manifest::Icon::IconPurpose::ANY);
194 primary_icon.src = GURL("https://www.google.com/image.png"); 128 primary_icon.src = GURL("https://www.google.com/image.png");
195 manifest.icons.push_back(primary_icon); 129 manifest.icons.push_back(primary_icon);
196 130
197 return manifest; 131 return manifest;
198 } 132 }
199 133
200 } // anonymous namespace 134 } // anonymous namespace
201 135
136 class TestInstallableManager : public InstallableManager {
137 public:
138 explicit TestInstallableManager(content::WebContents* web_contents)
139 : InstallableManager(web_contents) {}
140
141 void GetData(const InstallableParams& params,
142 const InstallableCallback& callback) override {
143 if (should_manifest_time_out_ ||
144 (params.check_installable && should_installable_time_out_)) {
145 return;
146 }
147
148 InstallableStatusCode code = code_;
149 if (params.check_installable) {
150 if (!IsManifestValidForWebApp(manifest_))
151 code = valid_manifest_->error;
152 else if (!is_installable_)
153 code = NO_MATCHING_SERVICE_WORKER;
154 }
155
156 callback.Run({code, manifest_url_, manifest_, primary_icon_url_,
157 &primary_icon_, badge_icon_url_, &badge_icon_,
158 params.check_installable ? is_installable_ : false});
pkotwicz 2017/06/28 18:17:02 For the sake of clarity, you should only return a
159 }
160
161 void SetStatus(InstallableStatusCode code, bool is_installable) {
162 code_ = code;
163 is_installable_ = is_installable;
164 }
165
166 void SetManifest(const GURL& url, const content::Manifest& manifest) {
167 manifest_url_ = url;
168 manifest_ = manifest;
169 }
170
171 void SetPrimaryIcon(const GURL& url, const SkBitmap& icon) {
172 primary_icon_url_ = url;
173 primary_icon_ = icon;
174 }
175
176 void SetBadgeIcon(const GURL& url, const SkBitmap& icon) {
177 badge_icon_url_ = url;
178 badge_icon_ = icon;
179 }
180
181 void SetShouldManifestTimeOut(bool should_time_out) {
182 should_manifest_time_out_ = should_time_out;
183 }
184
185 void SetShouldInstallableTimeOut(bool should_time_out) {
186 should_installable_time_out_ = should_time_out;
187 }
188
189 private:
190 InstallableStatusCode code_;
191 content::Manifest manifest_;
192 GURL manifest_url_;
193 GURL primary_icon_url_;
194 GURL badge_icon_url_;
195 SkBitmap primary_icon_;
196 SkBitmap badge_icon_;
197
198 bool is_installable_ = false;
199
200 bool should_manifest_time_out_ = false;
201 bool should_installable_time_out_ = false;
202 };
203
202 // Tests AddToHomescreenDataFetcher. These tests should be browser tests but 204 // Tests AddToHomescreenDataFetcher. These tests should be browser tests but
203 // Android does not support browser tests yet (crbug.com/611756). 205 // Android does not support browser tests yet (crbug.com/611756).
204 class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness { 206 class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness {
205 public: 207 public:
206 AddToHomescreenDataFetcherTest() {} 208 AddToHomescreenDataFetcherTest() {}
207 ~AddToHomescreenDataFetcherTest() override {} 209 ~AddToHomescreenDataFetcherTest() override {}
208 210
209 void SetUp() override { 211 void SetUp() override {
210 ChromeRenderViewHostTestHarness::SetUp(); 212 ChromeRenderViewHostTestHarness::SetUp();
211 213
212 ASSERT_TRUE(profile()->CreateHistoryService(false, true)); 214 ASSERT_TRUE(profile()->CreateHistoryService(false, true));
213 profile()->CreateFaviconService(); 215 profile()->CreateFaviconService();
214 216
215 embedded_worker_test_helper_.reset( 217 // Manually inject the TestInstallableManager as a "InstallableManager"
216 new content::EmbeddedWorkerTestHelper(base::FilePath())); 218 // WebContentsUserData. We can't directly call ::CreateForWebContents due to
217 219 // typing issues since TestInstallableManager doesn't directly inherit from
218 scoped_refptr<content::SiteInstance> site_instance = 220 // WebContentsUserData.
219 content::SiteInstance::Create(browser_context()); 221 web_contents()->SetUserData(
220 site_instance->GetProcess()->Init(); 222 TestInstallableManager::UserDataKey(),
221 MockWebContents* mock_web_contents = new MockWebContents(browser_context()); 223 base::WrapUnique(new TestInstallableManager(web_contents())));
222 mock_web_contents->Init(content::WebContents::CreateParams( 224 installable_manager_ = static_cast<TestInstallableManager*>(
223 browser_context(), std::move(site_instance))); 225 web_contents()->GetUserData(TestInstallableManager::UserDataKey()));
224 InstallableManager::CreateForWebContents(mock_web_contents);
225 SetContents(mock_web_contents);
226 NavigateAndCommit(GURL(kDefaultStartUrl));
227 }
228
229 void TearDown() override {
230 embedded_worker_test_helper_.reset();
231 ChromeRenderViewHostTestHarness::TearDown();
232 } 226 }
233 227
234 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( 228 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher(
235 bool check_webapk_compatible, 229 bool check_webapk_compatible,
236 AddToHomescreenDataFetcher::Observer* observer) { 230 AddToHomescreenDataFetcher::Observer* observer) {
237 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1, 500, 231 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1, 500,
238 check_webapk_compatible, observer); 232 check_webapk_compatible, observer);
239 } 233 }
240 234
241 // Set the manifest to be returned as a result of WebContents::GetManifest(). 235 void SetManifest(const content::Manifest& manifest) {
242 void SetManifest(const GURL& manifest_url, 236 installable_manager_->SetManifest(GURL(kDefaultManifestUrl), manifest);
243 const content::Manifest& manifest) { 237 installable_manager_->SetStatus(NO_ERROR_DETECTED, false);
244 MockWebContents* mock_web_contents =
245 static_cast<MockWebContents*>(web_contents());
246 mock_web_contents->SetManifest(manifest_url, manifest);
247 } 238 }
248 239
249 void SetShouldImageTimeOut(bool should_time_out) { 240 void SetManifestAndIcons(const content::Manifest& manifest) {
250 MockWebContents* mock_web_contents = 241 SetManifest(manifest);
251 static_cast<MockWebContents*>(web_contents()); 242 installable_manager_->SetPrimaryIcon(
252 mock_web_contents->SetShouldImageTimeOut(should_time_out); 243 GURL(kDefaultIconUrl),
244 gfx::test::CreateBitmap(kIconSizePx, kIconSizePx));
245 installable_manager_->SetStatus(NO_ERROR_DETECTED, false);
246 }
247
248 void SetInstallable(const content::Manifest& manifest) {
249 SetManifestAndIcons(manifest);
250 installable_manager_->SetStatus(NO_ERROR_DETECTED, true);
251 }
252
253 void SetWebApkInstallable(const content::Manifest& manifest) {
254 SetInstallable(manifest);
255 installable_manager_->SetBadgeIcon(
256 GURL(kDefaultIconUrl),
257 gfx::test::CreateBitmap(kIconSizePx, kIconSizePx));
253 } 258 }
254 259
255 void SetShouldManifestTimeOut(bool should_time_out) { 260 void SetShouldManifestTimeOut(bool should_time_out) {
256 MockWebContents* mock_web_contents = 261 installable_manager_->SetShouldManifestTimeOut(should_time_out);
257 static_cast<MockWebContents*>(web_contents());
258 mock_web_contents->SetShouldManifestTimeOut(should_time_out);
259 } 262 }
260 263
261 // Registers service worker at |url|. Blocks till the service worker is 264 void SetShouldInstallableTimeOut(bool should_time_out) {
262 // registered. 265 installable_manager_->SetShouldInstallableTimeOut(should_time_out);
263 void RegisterServiceWorker(const GURL& url) {
264 base::RunLoop run_loop;
265 embedded_worker_test_helper_->context()->RegisterServiceWorker(
266 url, GURL(url.spec() + "/service_worker.js"), nullptr,
267 base::Bind(&AddToHomescreenDataFetcherTest::OnServiceWorkerRegistered,
268 base::Unretained(this), run_loop.QuitClosure()));
269 } 266 }
270 267
271 private: 268 private:
272 // Callback for RegisterServiceWorker() for when service worker registration 269 TestInstallableManager* installable_manager_;
273 // has completed.
274 void OnServiceWorkerRegistered(const base::Closure& callback,
275 content::ServiceWorkerStatusCode status,
276 const std::string& status_message,
277 int64_t registration_id) {
278 ASSERT_EQ(content::SERVICE_WORKER_OK, status)
279 << content::ServiceWorkerStatusToString(status);
280 callback.Run();
281 }
282
283 std::unique_ptr<content::EmbeddedWorkerTestHelper>
284 embedded_worker_test_helper_;
285 270
286 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest); 271 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest);
287 }; 272 };
288 273
289 // Class for tests which should be run with AddToHomescreenDataFetcher built 274 // Class for tests which should be run with AddToHomescreenDataFetcher built
290 // with both true and false values of |check_webapk_compatible|. 275 // with both true and false values of |check_webapk_compatible|.
291 class AddToHomescreenDataFetcherTestCommon 276 class AddToHomescreenDataFetcherTestCommon
292 : public AddToHomescreenDataFetcherTest, 277 : public AddToHomescreenDataFetcherTest,
293 public testing::WithParamInterface<bool> { 278 public testing::WithParamInterface<bool> {
294 public: 279 public:
(...skipping 14 matching lines...) Expand all
309 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); 294 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon);
310 }; 295 };
311 296
312 // Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is 297 // Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is
313 // called when the web manifest returned is empty. The add-to-homescreen dialog 298 // 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. 299 // makes the dialog's text field editable once OnUserTitleAvailable() is called.
315 TEST_P(AddToHomescreenDataFetcherTestCommon, EmptyManifest) { 300 TEST_P(AddToHomescreenDataFetcherTestCommon, EmptyManifest) {
316 WebApplicationInfo web_application_info; 301 WebApplicationInfo web_application_info;
317 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 302 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
318 303
319 SetManifest(GURL(kDefaultManifestUrl), BuildEmptyManifest());
320
321 ObserverWaiter waiter; 304 ObserverWaiter waiter;
322 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 305 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
323 fetcher->OnDidGetWebApplicationInfo(web_application_info); 306 fetcher->OnDidGetWebApplicationInfo(web_application_info);
324 waiter.WaitForDataAvailable(); 307 waiter.WaitForDataAvailable();
325 308
326 EXPECT_EQ(check_webapk_compatibility(), 309 EXPECT_EQ(check_webapk_compatibility(),
327 waiter.determined_webapk_compatibility()); 310 waiter.determined_webapk_compatibility());
328 EXPECT_FALSE(waiter.is_webapk_compatible()); 311 EXPECT_FALSE(waiter.is_webapk_compatible());
329 EXPECT_TRUE(waiter.title_available()); 312 EXPECT_TRUE(waiter.title_available());
330 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); 313 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
331 314
332 fetcher->set_weak_observer(nullptr); 315 fetcher->set_weak_observer(nullptr);
333 } 316 }
334 317
318 // Test a manifest with no icons.
319 TEST_P(AddToHomescreenDataFetcherTestCommon, NoIconManifest) {
320 WebApplicationInfo web_application_info;
321 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
322
pkotwicz 2017/06/28 18:17:02 Given that there is only a single caller to BuildN
pkotwicz 2017/06/29 19:22:05 Ping on this comment
dominickn 2017/06/30 02:42:30 Somehow missed this. Done.
323 SetManifest(BuildNoIconManifest());
324
325 ObserverWaiter waiter;
326 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
327 fetcher->OnDidGetWebApplicationInfo(web_application_info);
328 waiter.WaitForDataAvailable();
329
330 EXPECT_TRUE(waiter.title_available());
331 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
332 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title,
333 kDefaultManifestShortName));
334 EXPECT_TRUE(fetcher->primary_icon().drawsNothing());
335 EXPECT_TRUE(fetcher->shortcut_info().best_primary_icon_url.is_empty());
336 EXPECT_EQ(check_webapk_compatibility(),
337 waiter.determined_webapk_compatibility());
338 EXPECT_FALSE(waiter.is_webapk_compatible());
339 EXPECT_TRUE(fetcher->badge_icon().drawsNothing());
340 EXPECT_TRUE(fetcher->shortcut_info().best_badge_icon_url.is_empty());
341 fetcher->set_weak_observer(nullptr);
342 }
343
344 // Test an installable manifest with and without a service worker.
345 TEST_P(AddToHomescreenDataFetcherTestCommon, NonInstallableManifest) {
346 WebApplicationInfo web_application_info;
347 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
348
349 content::Manifest manifest(BuildDefaultManifest());
350
351 {
352 SetManifestAndIcons(manifest);
353
354 ObserverWaiter waiter;
355 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
356 fetcher->OnDidGetWebApplicationInfo(web_application_info);
357 waiter.WaitForDataAvailable();
358
359 EXPECT_TRUE(waiter.title_available());
360 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
361 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title,
362 kDefaultManifestShortName));
363 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
364 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
365 GURL(kDefaultIconUrl));
366 EXPECT_EQ(check_webapk_compatibility(),
367 waiter.determined_webapk_compatibility());
368 EXPECT_FALSE(waiter.is_webapk_compatible());
369 EXPECT_TRUE(fetcher->badge_icon().drawsNothing());
370 EXPECT_TRUE(fetcher->shortcut_info().best_badge_icon_url.is_empty());
371 fetcher->set_weak_observer(nullptr);
372 }
373
374 {
375 SetInstallable(manifest);
376
377 ObserverWaiter waiter;
378 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
379 fetcher->OnDidGetWebApplicationInfo(web_application_info);
380 waiter.WaitForDataAvailable();
381
382 EXPECT_TRUE(waiter.title_available());
383 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
384 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title,
385 kDefaultManifestShortName));
386 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
387 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
388 GURL(kDefaultIconUrl));
389 EXPECT_EQ(check_webapk_compatibility(),
390 waiter.determined_webapk_compatibility());
391 EXPECT_EQ(check_webapk_compatibility(), waiter.is_webapk_compatible());
392 EXPECT_TRUE(fetcher->badge_icon().drawsNothing());
393 EXPECT_TRUE(fetcher->shortcut_info().best_badge_icon_url.is_empty());
394 fetcher->set_weak_observer(nullptr);
395 }
396 }
397
398 // Test a WebAPK compatible manifest, including badge icon, on an installable
399 // site.
400 TEST_P(AddToHomescreenDataFetcherTestCommon, WebApkCompatibleManifest) {
401 WebApplicationInfo web_application_info;
402 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
403
404 content::Manifest manifest(BuildDefaultManifest());
405 SetWebApkInstallable(manifest);
406
407 ObserverWaiter waiter;
408 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
409 fetcher->OnDidGetWebApplicationInfo(web_application_info);
410 waiter.WaitForDataAvailable();
411
412 EXPECT_TRUE(waiter.title_available());
413 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
414 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title,
415 kDefaultManifestShortName));
416 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
417 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
418 GURL(kDefaultIconUrl));
419 EXPECT_EQ(check_webapk_compatibility(),
420 waiter.determined_webapk_compatibility());
421 if (check_webapk_compatibility()) {
422 EXPECT_TRUE(waiter.is_webapk_compatible());
423 EXPECT_FALSE(fetcher->badge_icon().drawsNothing());
424 EXPECT_EQ(fetcher->shortcut_info().best_badge_icon_url,
425 GURL(kDefaultIconUrl));
426 }
427 fetcher->set_weak_observer(nullptr);
428 }
429
335 // Test that when the manifest provides Manifest::short_name but not 430 // Test that when the manifest provides Manifest::short_name but not
336 // Manifest::name that Manifest::short_name is used as the name instead of 431 // Manifest::name that Manifest::short_name is used as the name instead of
337 // WebApplicationInfo::title. 432 // WebApplicationInfo::title.
338 TEST_P(AddToHomescreenDataFetcherTestCommon, 433 TEST_P(AddToHomescreenDataFetcherTestCommon,
339 ManifestShortNameClobbersWebApplicationName) { 434 ManifestShortNameClobbersWebApplicationName) {
340 WebApplicationInfo web_application_info; 435 WebApplicationInfo web_application_info;
341 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 436 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
342 437
343 content::Manifest manifest(BuildDefaultManifest()); 438 content::Manifest manifest(BuildDefaultManifest());
344 manifest.name = base::NullableString16(); 439 manifest.name = base::NullableString16();
345 440
346 RegisterServiceWorker(GURL(kDefaultStartUrl)); 441 {
347 SetManifest(GURL(kDefaultManifestUrl), manifest); 442 SetManifestAndIcons(manifest);
348 443 ObserverWaiter waiter;
349 ObserverWaiter waiter; 444 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
350 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 445 fetcher->OnDidGetWebApplicationInfo(web_application_info);
351 fetcher->OnDidGetWebApplicationInfo(web_application_info); 446 waiter.WaitForDataAvailable();
352 waiter.WaitForDataAvailable(); 447
353 448 EXPECT_EQ(check_webapk_compatibility(),
354 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); 449 waiter.determined_webapk_compatibility());
355 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, 450 EXPECT_TRUE(waiter.title_available());
356 kDefaultManifestShortName)); 451 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
357 452 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name,
358 fetcher->set_weak_observer(nullptr); 453 kDefaultManifestShortName));
454 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
455 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
456 GURL(kDefaultIconUrl));
457 fetcher->set_weak_observer(nullptr);
458 }
459
460 {
461 SetInstallable(manifest);
462 ObserverWaiter waiter;
463 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
464 fetcher->OnDidGetWebApplicationInfo(web_application_info);
465 waiter.WaitForDataAvailable();
466
467 EXPECT_EQ(check_webapk_compatibility(),
468 waiter.determined_webapk_compatibility());
469 EXPECT_TRUE(waiter.title_available());
470 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
471 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name,
472 kDefaultManifestShortName));
473 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
474 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
475 GURL(kDefaultIconUrl));
476
477 fetcher->set_weak_observer(nullptr);
478 }
359 } 479 }
360 480
361 // Test that when the manifest does not provide either Manifest::short_name nor 481 // Test that when the manifest does not provide either Manifest::short_name nor
362 // Manifest::name that: 482 // Manifest::name that:
363 // - The page is not WebAPK compatible. 483 // - The page is not WebAPK compatible.
364 // - WebApplicationInfo::title is used as the "name". 484 // - WebApplicationInfo::title is used as the "name".
485 // - We still use the icons from the manifest.
365 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { 486 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) {
366 WebApplicationInfo web_application_info; 487 WebApplicationInfo web_application_info;
367 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 488 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
368 489
369 content::Manifest manifest(BuildDefaultManifest()); 490 content::Manifest manifest(BuildDefaultManifest());
370 manifest.name = base::NullableString16(); 491 manifest.name = base::NullableString16();
371 manifest.short_name = base::NullableString16(); 492 manifest.short_name = base::NullableString16();
372 493
373 RegisterServiceWorker(GURL(kDefaultStartUrl)); 494 {
374 SetManifest(GURL(kDefaultManifestUrl), manifest); 495 SetManifestAndIcons(manifest);
375 496 ObserverWaiter waiter;
376 ObserverWaiter waiter; 497 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
377 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 498 fetcher->OnDidGetWebApplicationInfo(web_application_info);
378 fetcher->OnDidGetWebApplicationInfo(web_application_info); 499 waiter.WaitForDataAvailable();
379 waiter.WaitForDataAvailable(); 500
380 501 EXPECT_EQ(check_webapk_compatibility(),
381 EXPECT_EQ(check_webapk_compatibility(), 502 waiter.determined_webapk_compatibility());
382 waiter.determined_webapk_compatibility()); 503 EXPECT_TRUE(waiter.title_available());
383 EXPECT_FALSE(waiter.is_webapk_compatible()); 504 EXPECT_FALSE(waiter.is_webapk_compatible());
384 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); 505 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
385 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, 506 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name,
386 kWebApplicationInfoTitle)); 507 kWebApplicationInfoTitle));
387 508
388 fetcher->set_weak_observer(nullptr); 509 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
510 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
511 GURL(kDefaultIconUrl));
512 fetcher->set_weak_observer(nullptr);
513 }
514
515 {
516 SetInstallable(manifest);
517 ObserverWaiter waiter;
518 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
519 fetcher->OnDidGetWebApplicationInfo(web_application_info);
520 waiter.WaitForDataAvailable();
521
522 EXPECT_EQ(check_webapk_compatibility(),
523 waiter.determined_webapk_compatibility());
524 EXPECT_TRUE(waiter.title_available());
525 EXPECT_FALSE(waiter.is_webapk_compatible());
526 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
527 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name,
528 kWebApplicationInfoTitle));
529
530 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
531 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
532 GURL(kDefaultIconUrl));
533 fetcher->set_weak_observer(nullptr);
534 }
389 } 535 }
390 536
391 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called 537 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called
392 // when the manifest fetch times out. 538 // when the first call to InstallableManager::GetData times out.
393 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) { 539 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) {
394 WebApplicationInfo web_application_info; 540 WebApplicationInfo web_application_info;
395 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 541 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
396
397 RegisterServiceWorker(GURL(kDefaultStartUrl));
398 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest());
399 SetShouldManifestTimeOut(true); 542 SetShouldManifestTimeOut(true);
400 SetShouldImageTimeOut(false); 543
401 544 {
402 ObserverWaiter waiter; 545 // Check with a site that doesn't have a service worker.
403 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 546 SetManifestAndIcons(BuildDefaultManifest());
404 fetcher->OnDidGetWebApplicationInfo(web_application_info); 547 ObserverWaiter waiter;
405 waiter.WaitForDataAvailable(); 548 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
406 549 fetcher->OnDidGetWebApplicationInfo(web_application_info);
407 EXPECT_EQ(check_webapk_compatibility(), 550 waiter.WaitForDataAvailable();
408 waiter.determined_webapk_compatibility()); 551
409 EXPECT_FALSE(waiter.is_webapk_compatible()); 552 EXPECT_EQ(check_webapk_compatibility(),
410 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); 553 waiter.determined_webapk_compatibility());
411 EXPECT_TRUE(waiter.title_available()); 554 EXPECT_TRUE(waiter.title_available());
412 555 EXPECT_FALSE(waiter.is_webapk_compatible());
413 fetcher->set_weak_observer(nullptr); 556 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
557
558 EXPECT_TRUE(fetcher->primary_icon().drawsNothing());
559 EXPECT_TRUE(fetcher->shortcut_info().best_primary_icon_url.is_empty());
560 fetcher->set_weak_observer(nullptr);
561 }
562
563 {
564 // Check with a site that passes the PWA check.
565 SetInstallable(BuildDefaultManifest());
566 ObserverWaiter waiter;
567 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
568 fetcher->OnDidGetWebApplicationInfo(web_application_info);
569 waiter.WaitForDataAvailable();
570
571 EXPECT_EQ(check_webapk_compatibility(),
572 waiter.determined_webapk_compatibility());
573 EXPECT_TRUE(waiter.title_available());
574 EXPECT_FALSE(waiter.is_webapk_compatible());
575 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
576
577 EXPECT_TRUE(fetcher->primary_icon().drawsNothing());
578 EXPECT_TRUE(fetcher->shortcut_info().best_primary_icon_url.is_empty());
579 fetcher->set_weak_observer(nullptr);
580 }
414 } 581 }
415 582
416 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called 583 // 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. 584 // when the service worker check times out.
443 TEST_P(AddToHomescreenDataFetcherTestCommon, ServiceWorkerCheckTimesOut) { 585 TEST_P(AddToHomescreenDataFetcherTestCommon, ServiceWorkerCheckTimesOut) {
444 WebApplicationInfo web_application_info; 586 WebApplicationInfo web_application_info;
445 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 587 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
446 588
447 // Not registering a service worker means we'll wait and time out for the 589 SetInstallable(BuildDefaultManifest());
448 // worker. 590 SetShouldInstallableTimeOut(true);
449 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest());
450 SetShouldManifestTimeOut(false);
451 SetShouldImageTimeOut(false);
452 591
453 ObserverWaiter waiter; 592 ObserverWaiter waiter;
454 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 593 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
455 fetcher->OnDidGetWebApplicationInfo(web_application_info); 594 fetcher->OnDidGetWebApplicationInfo(web_application_info);
456 waiter.WaitForDataAvailable(); 595 waiter.WaitForDataAvailable();
457 596
458 EXPECT_EQ(check_webapk_compatibility(), 597 EXPECT_EQ(check_webapk_compatibility(),
459 waiter.determined_webapk_compatibility()); 598 waiter.determined_webapk_compatibility());
599 EXPECT_TRUE(waiter.title_available());
460 EXPECT_FALSE(waiter.is_webapk_compatible()); 600 EXPECT_FALSE(waiter.is_webapk_compatible());
461 EXPECT_TRUE(waiter.title_available());
462 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); 601 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
463 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title, 602 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title,
464 kDefaultManifestShortName)); 603 kDefaultManifestShortName));
465 604
605 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
606 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
607 GURL(kDefaultIconUrl));
608
466 fetcher->set_weak_observer(nullptr); 609 fetcher->set_weak_observer(nullptr);
467 } 610 }
468 611
469 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, 612 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility,
470 AddToHomescreenDataFetcherTestCommon, 613 AddToHomescreenDataFetcherTestCommon,
471 ::testing::Values(false, true)); 614 ::testing::Values(false, true));
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/installable/installable_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698