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

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

Issue 2968693003: [Android Webapps] Make AddToHomescreenDataFetcher easier to test (Closed)
Patch Set: Merge branch 'master' into homescreen_fetcher_weak_ptr2 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
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" 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/ptr_util.h"
13 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 16 #include "base/run_loop.h"
16 #include "base/strings/nullable_string16.h" 17 #include "base/strings/nullable_string16.h"
17 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
19 #include "base/time/time.h" 20 #include "base/time/time.h"
20 #include "chrome/browser/installable/installable_manager.h" 21 #include "chrome/browser/installable/installable_manager.h"
21 #include "chrome/common/web_application_info.h" 22 #include "chrome/common/web_application_info.h"
22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 determined_webapk_compatibility_ = true; 130 determined_webapk_compatibility_ = true;
130 is_webapk_compatible_ = is_webapk_compatible; 131 is_webapk_compatible_ = is_webapk_compatible;
131 } 132 }
132 133
133 void OnUserTitleAvailable(const base::string16& title) override { 134 void OnUserTitleAvailable(const base::string16& title) override {
134 EXPECT_FALSE(data_available_); 135 EXPECT_FALSE(data_available_);
135 title_available_ = true; 136 title_available_ = true;
136 title_ = title; 137 title_ = title;
137 } 138 }
138 139
139 SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon,
140 const GURL& url,
141 bool* is_generated) override {
142 *is_generated = false;
143 return icon;
144 }
145
146 void OnDataAvailable(const ShortcutInfo& info, 140 void OnDataAvailable(const ShortcutInfo& info,
147 const SkBitmap& primary_icon, 141 const SkBitmap& primary_icon,
148 const SkBitmap& badge_icon) override { 142 const SkBitmap& badge_icon) override {
149 EXPECT_TRUE(title_available_); 143 EXPECT_TRUE(title_available_);
150 data_available_ = true; 144 data_available_ = true;
151 if (!quit_closure_.is_null()) 145 if (!quit_closure_.is_null())
152 quit_closure_.Run(); 146 quit_closure_.Run();
153 } 147 }
154 148
155 base::string16 title() const { return title_; } 149 base::string16 title() const { return title_; }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 InstallableManager::CreateForWebContents(mock_web_contents); 218 InstallableManager::CreateForWebContents(mock_web_contents);
225 SetContents(mock_web_contents); 219 SetContents(mock_web_contents);
226 NavigateAndCommit(GURL(kDefaultStartUrl)); 220 NavigateAndCommit(GURL(kDefaultStartUrl));
227 } 221 }
228 222
229 void TearDown() override { 223 void TearDown() override {
230 embedded_worker_test_helper_.reset(); 224 embedded_worker_test_helper_.reset();
231 ChromeRenderViewHostTestHarness::TearDown(); 225 ChromeRenderViewHostTestHarness::TearDown();
232 } 226 }
233 227
234 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( 228 std::unique_ptr<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 base::MakeUnique<AddToHomescreenDataFetcher>(
238 check_webapk_compatible, observer); 232 web_contents(), 1, 1, 1, 1, 1, 500, check_webapk_compatible, observer);
239 } 233 }
240 234
241 // Set the manifest to be returned as a result of WebContents::GetManifest(). 235 // Set the manifest to be returned as a result of WebContents::GetManifest().
242 void SetManifest(const GURL& manifest_url, 236 void SetManifest(const GURL& manifest_url,
243 const content::Manifest& manifest) { 237 const content::Manifest& manifest) {
244 MockWebContents* mock_web_contents = 238 MockWebContents* mock_web_contents =
245 static_cast<MockWebContents*>(web_contents()); 239 static_cast<MockWebContents*>(web_contents());
246 mock_web_contents->SetManifest(manifest_url, manifest); 240 mock_web_contents->SetManifest(manifest_url, manifest);
247 } 241 }
248 242
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 282
289 // Class for tests which should be run with AddToHomescreenDataFetcher built 283 // Class for tests which should be run with AddToHomescreenDataFetcher built
290 // with both true and false values of |check_webapk_compatible|. 284 // with both true and false values of |check_webapk_compatible|.
291 class AddToHomescreenDataFetcherTestCommon 285 class AddToHomescreenDataFetcherTestCommon
292 : public AddToHomescreenDataFetcherTest, 286 : public AddToHomescreenDataFetcherTest,
293 public testing::WithParamInterface<bool> { 287 public testing::WithParamInterface<bool> {
294 public: 288 public:
295 AddToHomescreenDataFetcherTestCommon() {} 289 AddToHomescreenDataFetcherTestCommon() {}
296 ~AddToHomescreenDataFetcherTestCommon() override {} 290 ~AddToHomescreenDataFetcherTestCommon() override {}
297 291
298 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( 292 std::unique_ptr<AddToHomescreenDataFetcher> BuildFetcher(
299 AddToHomescreenDataFetcher::Observer* observer) { 293 AddToHomescreenDataFetcher::Observer* observer) {
300 return AddToHomescreenDataFetcherTest::BuildFetcher( 294 return AddToHomescreenDataFetcherTest::BuildFetcher(
301 check_webapk_compatibility(), observer); 295 check_webapk_compatibility(), observer);
302 } 296 }
303 297
304 // The value of |check_webapk_compatible| used when building the 298 // The value of |check_webapk_compatible| used when building the
305 // AddToHomescreenDataFetcher. 299 // AddToHomescreenDataFetcher.
306 bool check_webapk_compatibility() { return GetParam(); } 300 bool check_webapk_compatibility() { return GetParam(); }
307 301
308 private: 302 private:
309 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); 303 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon);
310 }; 304 };
311 305
312 // Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is 306 // Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is
313 // called when the web manifest returned is empty. The add-to-homescreen dialog 307 // 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. 308 // makes the dialog's text field editable once OnUserTitleAvailable() is called.
315 TEST_P(AddToHomescreenDataFetcherTestCommon, EmptyManifest) { 309 TEST_P(AddToHomescreenDataFetcherTestCommon, EmptyManifest) {
316 WebApplicationInfo web_application_info; 310 WebApplicationInfo web_application_info;
317 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 311 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
318 312
319 SetManifest(GURL(kDefaultManifestUrl), BuildEmptyManifest()); 313 SetManifest(GURL(kDefaultManifestUrl), BuildEmptyManifest());
320 314
321 ObserverWaiter waiter; 315 ObserverWaiter waiter;
322 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 316 std::unique_ptr<AddToHomescreenDataFetcher> fetcher = BuildFetcher(&waiter);
323 fetcher->OnDidGetWebApplicationInfo(web_application_info); 317 fetcher->OnDidGetWebApplicationInfo(web_application_info);
324 waiter.WaitForDataAvailable(); 318 waiter.WaitForDataAvailable();
325 319
326 EXPECT_EQ(check_webapk_compatibility(), 320 EXPECT_EQ(check_webapk_compatibility(),
327 waiter.determined_webapk_compatibility()); 321 waiter.determined_webapk_compatibility());
328 EXPECT_FALSE(waiter.is_webapk_compatible()); 322 EXPECT_FALSE(waiter.is_webapk_compatible());
329 EXPECT_TRUE(waiter.title_available()); 323 EXPECT_TRUE(waiter.title_available());
330 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); 324 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
331
332 fetcher->set_weak_observer(nullptr);
333 } 325 }
334 326
335 // Test that when the manifest provides Manifest::short_name but not 327 // 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 328 // Manifest::name that Manifest::short_name is used as the name instead of
337 // WebApplicationInfo::title. 329 // WebApplicationInfo::title.
338 TEST_P(AddToHomescreenDataFetcherTestCommon, 330 TEST_P(AddToHomescreenDataFetcherTestCommon,
339 ManifestShortNameClobbersWebApplicationName) { 331 ManifestShortNameClobbersWebApplicationName) {
340 WebApplicationInfo web_application_info; 332 WebApplicationInfo web_application_info;
341 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 333 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
342 334
343 content::Manifest manifest(BuildDefaultManifest()); 335 content::Manifest manifest(BuildDefaultManifest());
344 manifest.name = base::NullableString16(); 336 manifest.name = base::NullableString16();
345 337
346 RegisterServiceWorker(GURL(kDefaultStartUrl)); 338 RegisterServiceWorker(GURL(kDefaultStartUrl));
347 SetManifest(GURL(kDefaultManifestUrl), manifest); 339 SetManifest(GURL(kDefaultManifestUrl), manifest);
348 340
349 ObserverWaiter waiter; 341 ObserverWaiter waiter;
350 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 342 std::unique_ptr<AddToHomescreenDataFetcher> fetcher = BuildFetcher(&waiter);
351 fetcher->OnDidGetWebApplicationInfo(web_application_info); 343 fetcher->OnDidGetWebApplicationInfo(web_application_info);
352 waiter.WaitForDataAvailable(); 344 waiter.WaitForDataAvailable();
353 345
354 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); 346 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
355 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, 347 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name,
356 kDefaultManifestShortName)); 348 kDefaultManifestShortName));
357
358 fetcher->set_weak_observer(nullptr);
359 } 349 }
360 350
361 // Test that when the manifest does not provide either Manifest::short_name nor 351 // Test that when the manifest does not provide either Manifest::short_name nor
362 // Manifest::name that: 352 // Manifest::name that:
363 // - The page is not WebAPK compatible. 353 // - The page is not WebAPK compatible.
364 // - WebApplicationInfo::title is used as the "name". 354 // - WebApplicationInfo::title is used as the "name".
365 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { 355 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) {
366 WebApplicationInfo web_application_info; 356 WebApplicationInfo web_application_info;
367 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 357 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
368 358
369 content::Manifest manifest(BuildDefaultManifest()); 359 content::Manifest manifest(BuildDefaultManifest());
370 manifest.name = base::NullableString16(); 360 manifest.name = base::NullableString16();
371 manifest.short_name = base::NullableString16(); 361 manifest.short_name = base::NullableString16();
372 362
373 RegisterServiceWorker(GURL(kDefaultStartUrl)); 363 RegisterServiceWorker(GURL(kDefaultStartUrl));
374 SetManifest(GURL(kDefaultManifestUrl), manifest); 364 SetManifest(GURL(kDefaultManifestUrl), manifest);
375 365
376 ObserverWaiter waiter; 366 ObserverWaiter waiter;
377 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 367 std::unique_ptr<AddToHomescreenDataFetcher> fetcher = BuildFetcher(&waiter);
378 fetcher->OnDidGetWebApplicationInfo(web_application_info); 368 fetcher->OnDidGetWebApplicationInfo(web_application_info);
379 waiter.WaitForDataAvailable(); 369 waiter.WaitForDataAvailable();
380 370
381 EXPECT_EQ(check_webapk_compatibility(), 371 EXPECT_EQ(check_webapk_compatibility(),
382 waiter.determined_webapk_compatibility()); 372 waiter.determined_webapk_compatibility());
383 EXPECT_FALSE(waiter.is_webapk_compatible()); 373 EXPECT_FALSE(waiter.is_webapk_compatible());
384 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); 374 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
385 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, 375 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name,
386 kWebApplicationInfoTitle)); 376 kWebApplicationInfoTitle));
387
388 fetcher->set_weak_observer(nullptr);
389 } 377 }
390 378
391 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called 379 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called
392 // when the manifest fetch times out. 380 // when the manifest fetch times out.
393 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) { 381 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) {
394 WebApplicationInfo web_application_info; 382 WebApplicationInfo web_application_info;
395 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 383 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
396 384
397 RegisterServiceWorker(GURL(kDefaultStartUrl)); 385 RegisterServiceWorker(GURL(kDefaultStartUrl));
398 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); 386 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest());
399 SetShouldManifestTimeOut(true); 387 SetShouldManifestTimeOut(true);
400 SetShouldImageTimeOut(false); 388 SetShouldImageTimeOut(false);
401 389
402 ObserverWaiter waiter; 390 ObserverWaiter waiter;
403 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 391 std::unique_ptr<AddToHomescreenDataFetcher> fetcher = BuildFetcher(&waiter);
404 fetcher->OnDidGetWebApplicationInfo(web_application_info); 392 fetcher->OnDidGetWebApplicationInfo(web_application_info);
405 waiter.WaitForDataAvailable(); 393 waiter.WaitForDataAvailable();
406 394
407 EXPECT_EQ(check_webapk_compatibility(), 395 EXPECT_EQ(check_webapk_compatibility(),
408 waiter.determined_webapk_compatibility()); 396 waiter.determined_webapk_compatibility());
409 EXPECT_FALSE(waiter.is_webapk_compatible()); 397 EXPECT_FALSE(waiter.is_webapk_compatible());
410 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); 398 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
411 EXPECT_TRUE(waiter.title_available()); 399 EXPECT_TRUE(waiter.title_available());
412
413 fetcher->set_weak_observer(nullptr);
414 } 400 }
415 401
416 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called 402 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called
417 // when the image fetch times out. 403 // when the image fetch times out.
418 TEST_P(AddToHomescreenDataFetcherTestCommon, ImageFetchTimesOut) { 404 TEST_P(AddToHomescreenDataFetcherTestCommon, ImageFetchTimesOut) {
419 WebApplicationInfo web_application_info; 405 WebApplicationInfo web_application_info;
420 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 406 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
421 407
422 RegisterServiceWorker(GURL(kDefaultStartUrl)); 408 RegisterServiceWorker(GURL(kDefaultStartUrl));
423 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); 409 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest());
424 SetShouldManifestTimeOut(false); 410 SetShouldManifestTimeOut(false);
425 SetShouldImageTimeOut(true); 411 SetShouldImageTimeOut(true);
426 412
427 ObserverWaiter waiter; 413 ObserverWaiter waiter;
428 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 414 std::unique_ptr<AddToHomescreenDataFetcher> fetcher = BuildFetcher(&waiter);
429 fetcher->OnDidGetWebApplicationInfo(web_application_info); 415 fetcher->OnDidGetWebApplicationInfo(web_application_info);
430 waiter.WaitForDataAvailable(); 416 waiter.WaitForDataAvailable();
431 417
432 EXPECT_EQ(check_webapk_compatibility(), 418 EXPECT_EQ(check_webapk_compatibility(),
433 waiter.determined_webapk_compatibility()); 419 waiter.determined_webapk_compatibility());
434 EXPECT_FALSE(waiter.is_webapk_compatible()); 420 EXPECT_FALSE(waiter.is_webapk_compatible());
435 EXPECT_TRUE(waiter.title_available()); 421 EXPECT_TRUE(waiter.title_available());
436 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); 422 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
437
438 fetcher->set_weak_observer(nullptr);
439 } 423 }
440 424
441 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called 425 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called
442 // when the service worker check times out. 426 // when the service worker check times out.
443 TEST_P(AddToHomescreenDataFetcherTestCommon, ServiceWorkerCheckTimesOut) { 427 TEST_P(AddToHomescreenDataFetcherTestCommon, ServiceWorkerCheckTimesOut) {
444 WebApplicationInfo web_application_info; 428 WebApplicationInfo web_application_info;
445 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 429 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
446 430
447 // Not registering a service worker means we'll wait and time out for the 431 // Not registering a service worker means we'll wait and time out for the
448 // worker. 432 // worker.
449 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest()); 433 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest());
450 SetShouldManifestTimeOut(false); 434 SetShouldManifestTimeOut(false);
451 SetShouldImageTimeOut(false); 435 SetShouldImageTimeOut(false);
452 436
453 ObserverWaiter waiter; 437 ObserverWaiter waiter;
454 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 438 std::unique_ptr<AddToHomescreenDataFetcher> fetcher = BuildFetcher(&waiter);
455 fetcher->OnDidGetWebApplicationInfo(web_application_info); 439 fetcher->OnDidGetWebApplicationInfo(web_application_info);
456 waiter.WaitForDataAvailable(); 440 waiter.WaitForDataAvailable();
457 441
458 EXPECT_EQ(check_webapk_compatibility(), 442 EXPECT_EQ(check_webapk_compatibility(),
459 waiter.determined_webapk_compatibility()); 443 waiter.determined_webapk_compatibility());
460 EXPECT_FALSE(waiter.is_webapk_compatible()); 444 EXPECT_FALSE(waiter.is_webapk_compatible());
461 EXPECT_TRUE(waiter.title_available()); 445 EXPECT_TRUE(waiter.title_available());
462 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); 446 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
463 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title, 447 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title,
464 kDefaultManifestShortName)); 448 kDefaultManifestShortName));
465
466 fetcher->set_weak_observer(nullptr);
467 } 449 }
468 450
469 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, 451 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility,
470 AddToHomescreenDataFetcherTestCommon, 452 AddToHomescreenDataFetcherTestCommon,
471 ::testing::Values(false, true)); 453 ::testing::Values(false, true));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698