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

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java

Issue 2623993007: 🏠 Extract the ContentSuggestionManager interface from NTP (Closed)
Patch Set: aaaand rebase again Created 3 years, 11 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 package org.chromium.chrome.browser.ntp.cards; 5 package org.chromium.chrome.browser.ntp.cards;
6 6
7 import static org.junit.Assert.assertEquals; 7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse; 8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNotEquals; 9 import static org.junit.Assert.assertNotEquals;
10 import static org.junit.Assert.assertThat; 10 import static org.junit.Assert.assertThat;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 import org.robolectric.shadows.ShadowResources; 46 import org.robolectric.shadows.ShadowResources;
47 47
48 import org.chromium.base.Callback; 48 import org.chromium.base.Callback;
49 import org.chromium.base.ContextUtils; 49 import org.chromium.base.ContextUtils;
50 import org.chromium.base.metrics.RecordHistogram; 50 import org.chromium.base.metrics.RecordHistogram;
51 import org.chromium.base.metrics.RecordUserAction; 51 import org.chromium.base.metrics.RecordUserAction;
52 import org.chromium.base.test.util.Feature; 52 import org.chromium.base.test.util.Feature;
53 import org.chromium.chrome.R; 53 import org.chromium.chrome.R;
54 import org.chromium.chrome.browser.ChromeFeatureList; 54 import org.chromium.chrome.browser.ChromeFeatureList;
55 import org.chromium.chrome.browser.EnableFeatures; 55 import org.chromium.chrome.browser.EnableFeatures;
56 import org.chromium.chrome.browser.ntp.ContextMenuManager;
56 import org.chromium.chrome.browser.ntp.NewTabPage.DestructionObserver; 57 import org.chromium.chrome.browser.ntp.NewTabPage.DestructionObserver;
57 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager;
58 import org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.Categor yInfoBuilder; 58 import org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.Categor yInfoBuilder;
59 import org.chromium.chrome.browser.ntp.cards.SignInPromo.SigninObserver; 59 import org.chromium.chrome.browser.ntp.cards.SignInPromo.SigninObserver;
60 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; 60 import org.chromium.chrome.browser.ntp.snippets.CategoryInt;
61 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; 61 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus;
62 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource; 62 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource;
63 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; 63 import org.chromium.chrome.browser.ntp.snippets.KnownCategories;
64 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; 64 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
65 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; 65 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge;
66 import org.chromium.chrome.browser.preferences.ChromePreferenceManager; 66 import org.chromium.chrome.browser.preferences.ChromePreferenceManager;
67 import org.chromium.chrome.browser.signin.SigninManager; 67 import org.chromium.chrome.browser.signin.SigninManager;
68 import org.chromium.chrome.browser.signin.SigninManager.SignInAllowedObserver; 68 import org.chromium.chrome.browser.signin.SigninManager.SignInAllowedObserver;
69 import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver; 69 import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver;
70 import org.chromium.chrome.browser.suggestions.SuggestionsMetricsReporter; 70 import org.chromium.chrome.browser.suggestions.SuggestionsMetricsReporter;
71 import org.chromium.chrome.browser.suggestions.SuggestionsUiDelegate;
71 import org.chromium.testing.local.LocalRobolectricTestRunner; 72 import org.chromium.testing.local.LocalRobolectricTestRunner;
72 73
73 import java.util.ArrayList; 74 import java.util.ArrayList;
74 import java.util.Collections; 75 import java.util.Collections;
75 import java.util.HashMap; 76 import java.util.HashMap;
76 import java.util.List; 77 import java.util.List;
77 import java.util.Locale; 78 import java.util.Locale;
78 79
79 /** 80 /**
80 * Unit tests for {@link NewTabPageAdapter}. 81 * Unit tests for {@link NewTabPageAdapter}.
81 */ 82 */
82 @RunWith(LocalRobolectricTestRunner.class) 83 @RunWith(LocalRobolectricTestRunner.class)
83 @Config(manifest = Config.NONE) 84 @Config(manifest = Config.NONE)
84 public class NewTabPageAdapterTest { 85 public class NewTabPageAdapterTest {
85 @Rule 86 @Rule
86 public EnableFeatures.Processor mEnableFeatureProcessor = new EnableFeatures .Processor(); 87 public EnableFeatures.Processor mEnableFeatureProcessor = new EnableFeatures .Processor();
87 88
88 private FakeSuggestionsSource mSource; 89 private FakeSuggestionsSource mSource;
89 private NewTabPageAdapter mAdapter; 90 private NewTabPageAdapter mAdapter;
90 @Mock 91 @Mock
91 private SigninManager mMockSigninManager; 92 private SigninManager mMockSigninManager;
92 @Mock 93 @Mock
93 private OfflinePageBridge mOfflinePageBridge; 94 private OfflinePageBridge mOfflinePageBridge;
94 @Mock 95 @Mock
95 private NewTabPageManager mNewTabPageManager; 96 private SuggestionsUiDelegate mUiDelegate;
96 97
97 /** 98 /**
98 * Stores information about a section that should be present in the adapter. 99 * Stores information about a section that should be present in the adapter.
99 */ 100 */
100 private static class SectionDescriptor { 101 private static class SectionDescriptor {
101 public final int mNumSuggestions; 102 public final int mNumSuggestions;
102 public final boolean mStatusCard; 103 public final boolean mStatusCard;
103 public boolean mActionButton; 104 public boolean mActionButton;
104 public boolean mProgressItem; 105 public boolean mProgressItem;
105 public SnippetArticle mFirstItem; 106 public SnippetArticle mFirstItem;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 RecordHistogram.disableForTests(); 204 RecordHistogram.disableForTests();
204 RecordUserAction.disableForTests(); 205 RecordUserAction.disableForTests();
205 206
206 @CategoryInt 207 @CategoryInt
207 final int category = KnownCategories.ARTICLES; 208 final int category = KnownCategories.ARTICLES;
208 mSource = new FakeSuggestionsSource(); 209 mSource = new FakeSuggestionsSource();
209 mSource.setStatusForCategory(category, CategoryStatus.INITIALIZING); 210 mSource.setStatusForCategory(category, CategoryStatus.INITIALIZING);
210 mSource.setInfoForCategory(category, 211 mSource.setInfoForCategory(category,
211 new CategoryInfoBuilder(category).showIfEmpty().build()); 212 new CategoryInfoBuilder(category).showIfEmpty().build());
212 213
213 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(mSource); 214 when(mUiDelegate.getSuggestionsSource()).thenReturn(mSource);
214 when(mNewTabPageManager.getSuggestionsMetricsReporter()) 215 when(mUiDelegate.getMetricsReporter()).thenReturn(mock(SuggestionsMetric sReporter.class));
215 .thenReturn(mock(SuggestionsMetricsReporter.class));
216 when(mNewTabPageManager.isCurrentPage()).thenReturn(true);
217 216
218 reloadNtp(); 217 reloadNtp();
219 } 218 }
220 219
221 @After 220 @After
222 public void tearDown() { 221 public void tearDown() {
223 SigninManager.setInstanceForTesting(null); 222 SigninManager.setInstanceForTesting(null);
224 ChromePreferenceManager.getInstance(RuntimeEnvironment.application) 223 ChromePreferenceManager.getInstance(RuntimeEnvironment.application)
225 .setNewTabPageSigninPromoDismissed(false); 224 .setNewTabPageSigninPromoDismissed(false);
226 } 225 }
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 @CategoryInt 473 @CategoryInt
475 final int category = 42; 474 final int category = 42;
476 475
477 // Part 1: VisibleIfEmpty = true 476 // Part 1: VisibleIfEmpty = true
478 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource(); 477 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource();
479 suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALI ZING); 478 suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALI ZING);
480 suggestionsSource.setInfoForCategory(category, 479 suggestionsSource.setInfoForCategory(category,
481 new CategoryInfoBuilder(category).showIfEmpty().build()); 480 new CategoryInfoBuilder(category).showIfEmpty().build());
482 481
483 // 1.1 - Initial state 482 // 1.1 - Initial state
484 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSo urce); 483 when(mUiDelegate.getSuggestionsSource()).thenReturn(suggestionsSource);
485 reloadNtp(); 484 reloadNtp();
486 assertItemsFor(sectionWithStatusCard().withProgress()); 485 assertItemsFor(sectionWithStatusCard().withProgress());
487 486
488 // 1.2 - With suggestions 487 // 1.2 - With suggestions
489 List<SnippetArticle> articles = 488 List<SnippetArticle> articles =
490 Collections.unmodifiableList(createDummySuggestions(3, category) ); 489 Collections.unmodifiableList(createDummySuggestions(3, category) );
491 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E); 490 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E);
492 suggestionsSource.setSuggestionsForCategory(category, articles); 491 suggestionsSource.setSuggestionsForCategory(category, articles);
493 assertItemsFor(section(3)); 492 assertItemsFor(section(3));
494 493
495 // 1.3 - When all suggestions are dismissed 494 // 1.3 - When all suggestions are dismissed
496 SuggestionsSection section42 = 495 SuggestionsSection section42 =
497 mAdapter.getSectionListForTesting().getSectionForTesting(categor y); 496 mAdapter.getSectionListForTesting().getSectionForTesting(categor y);
498 assertSectionMatches(section(3), section42); 497 assertSectionMatches(section(3), section42);
499 section42.removeSuggestionById(articles.get(0).mIdWithinCategory); 498 section42.removeSuggestionById(articles.get(0).mIdWithinCategory);
500 section42.removeSuggestionById(articles.get(1).mIdWithinCategory); 499 section42.removeSuggestionById(articles.get(1).mIdWithinCategory);
501 section42.removeSuggestionById(articles.get(2).mIdWithinCategory); 500 section42.removeSuggestionById(articles.get(2).mIdWithinCategory);
502 assertItemsFor(sectionWithStatusCard()); 501 assertItemsFor(sectionWithStatusCard());
503 502
504 // Part 2: VisibleIfEmpty = false 503 // Part 2: VisibleIfEmpty = false
505 suggestionsSource = new FakeSuggestionsSource(); 504 suggestionsSource = new FakeSuggestionsSource();
506 suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALI ZING); 505 suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALI ZING);
507 suggestionsSource.setInfoForCategory(category, new CategoryInfoBuilder(c ategory).build()); 506 suggestionsSource.setInfoForCategory(category, new CategoryInfoBuilder(c ategory).build());
508 507
509 // 2.1 - Initial state 508 // 2.1 - Initial state
510 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSo urce); 509 when(mUiDelegate.getSuggestionsSource()).thenReturn(suggestionsSource);
511 reloadNtp(); 510 reloadNtp();
512 assertItemsFor(); 511 assertItemsFor();
513 512
514 // 2.2 - With suggestions 513 // 2.2 - With suggestions
515 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E); 514 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E);
516 suggestionsSource.setSuggestionsForCategory(category, articles); 515 suggestionsSource.setSuggestionsForCategory(category, articles);
517 assertItemsFor(); 516 assertItemsFor();
518 517
519 // 2.3 - When all suggestions are dismissed - N/A, suggestions don't get added. 518 // 2.3 - When all suggestions are dismissed - N/A, suggestions don't get added.
520 } 519 }
521 520
522 /** 521 /**
523 * Tests that the more button is shown for sections that declare it. 522 * Tests that the more button is shown for sections that declare it.
524 */ 523 */
525 @Test 524 @Test
526 @Feature({"Ntp"}) 525 @Feature({"Ntp"})
527 public void testMoreButton() { 526 public void testMoreButton() {
528 @CategoryInt 527 @CategoryInt
529 final int category = 42; 528 final int category = 42;
530 529
531 // Part 1: With "View All" action 530 // Part 1: With "View All" action
532 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource(); 531 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource();
533 suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALI ZING); 532 suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALI ZING);
534 suggestionsSource.setInfoForCategory(category, new CategoryInfoBuilder(c ategory) 533 suggestionsSource.setInfoForCategory(category, new CategoryInfoBuilder(c ategory)
535 .withViewAllActio n() 534 .withViewAllActio n()
536 .showIfEmpty() 535 .showIfEmpty()
537 .build()); 536 .build());
538 537
539 // 1.1 - Initial state. 538 // 1.1 - Initial state.
540 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSo urce); 539 when(mUiDelegate.getSuggestionsSource()).thenReturn(suggestionsSource);
541 reloadNtp(); 540 reloadNtp();
542 assertItemsFor(sectionWithStatusCard().withActionButton().withProgress() ); 541 assertItemsFor(sectionWithStatusCard().withActionButton().withProgress() );
543 542
544 // 1.2 - With suggestions. 543 // 1.2 - With suggestions.
545 List<SnippetArticle> articles = 544 List<SnippetArticle> articles =
546 Collections.unmodifiableList(createDummySuggestions(3, category) ); 545 Collections.unmodifiableList(createDummySuggestions(3, category) );
547 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E); 546 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E);
548 suggestionsSource.setSuggestionsForCategory(category, articles); 547 suggestionsSource.setSuggestionsForCategory(category, articles);
549 assertItemsFor(section(3).withActionButton()); 548 assertItemsFor(section(3).withActionButton());
550 549
551 // 1.3 - When all suggestions are dismissed. 550 // 1.3 - When all suggestions are dismissed.
552 SuggestionsSection section42 = 551 SuggestionsSection section42 =
553 mAdapter.getSectionListForTesting().getSectionForTesting(categor y); 552 mAdapter.getSectionListForTesting().getSectionForTesting(categor y);
554 assertSectionMatches(section(3).withActionButton(), section42); 553 assertSectionMatches(section(3).withActionButton(), section42);
555 section42.removeSuggestionById(articles.get(0).mIdWithinCategory); 554 section42.removeSuggestionById(articles.get(0).mIdWithinCategory);
556 section42.removeSuggestionById(articles.get(1).mIdWithinCategory); 555 section42.removeSuggestionById(articles.get(1).mIdWithinCategory);
557 section42.removeSuggestionById(articles.get(2).mIdWithinCategory); 556 section42.removeSuggestionById(articles.get(2).mIdWithinCategory);
558 assertItemsFor(sectionWithStatusCard().withActionButton()); 557 assertItemsFor(sectionWithStatusCard().withActionButton());
559 558
560 // Part 1: Without "View All" action 559 // Part 1: Without "View All" action
561 suggestionsSource = new FakeSuggestionsSource(); 560 suggestionsSource = new FakeSuggestionsSource();
562 suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALI ZING); 561 suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALI ZING);
563 suggestionsSource.setInfoForCategory(category, 562 suggestionsSource.setInfoForCategory(category,
564 new CategoryInfoBuilder(category).showIfEmpty().build()); 563 new CategoryInfoBuilder(category).showIfEmpty().build());
565 564
566 // 2.1 - Initial state. 565 // 2.1 - Initial state.
567 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSo urce); 566 when(mUiDelegate.getSuggestionsSource()).thenReturn(suggestionsSource);
568 reloadNtp(); 567 reloadNtp();
569 assertItemsFor(sectionWithStatusCard().withProgress()); 568 assertItemsFor(sectionWithStatusCard().withProgress());
570 569
571 // 2.2 - With suggestions. 570 // 2.2 - With suggestions.
572 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E); 571 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E);
573 suggestionsSource.setSuggestionsForCategory(category, articles); 572 suggestionsSource.setSuggestionsForCategory(category, articles);
574 assertItemsFor(section(3)); 573 assertItemsFor(section(3));
575 574
576 // 2.3 - When all suggestions are dismissed. 575 // 2.3 - When all suggestions are dismissed.
577 section42 = mAdapter.getSectionListForTesting().getSectionForTesting(cat egory); 576 section42 = mAdapter.getSectionListForTesting().getSectionForTesting(cat egory);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 assertItemsFor(section(3), section(5).withActionButton(), section(11)); 630 assertItemsFor(section(3), section(5).withActionButton(), section(11));
632 } 631 }
633 632
634 /** 633 /**
635 * Tests that the order of the categories is kept. 634 * Tests that the order of the categories is kept.
636 */ 635 */
637 @Test 636 @Test
638 @Feature({"Ntp"}) 637 @Feature({"Ntp"})
639 public void testCategoryOrder() { 638 public void testCategoryOrder() {
640 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource(); 639 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource();
641 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSo urce); 640 when(mUiDelegate.getSuggestionsSource()).thenReturn(suggestionsSource);
642 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0); 641 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0);
643 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0); 642 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0);
644 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0); 643 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0);
645 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0); 644 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0);
646 reloadNtp(); 645 reloadNtp();
647 646
648 List<TreeNode> children = mAdapter.getSectionListForTesting().getChildre n(); 647 List<TreeNode> children = mAdapter.getSectionListForTesting().getChildre n();
649 assertEquals(4, children.size()); 648 assertEquals(4, children.size());
650 assertEquals(SuggestionsSection.class, children.get(0).getClass()); 649 assertEquals(SuggestionsSection.class, children.get(0).getClass());
651 assertEquals(KnownCategories.ARTICLES, getCategory(children.get(0))); 650 assertEquals(KnownCategories.ARTICLES, getCategory(children.get(0)));
652 assertEquals(SuggestionsSection.class, children.get(1).getClass()); 651 assertEquals(SuggestionsSection.class, children.get(1).getClass());
653 assertEquals(KnownCategories.BOOKMARKS, getCategory(children.get(1))); 652 assertEquals(KnownCategories.BOOKMARKS, getCategory(children.get(1)));
654 assertEquals(SuggestionsSection.class, children.get(2).getClass()); 653 assertEquals(SuggestionsSection.class, children.get(2).getClass());
655 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(children.ge t(2))); 654 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(children.ge t(2)));
656 assertEquals(SuggestionsSection.class, children.get(3).getClass()); 655 assertEquals(SuggestionsSection.class, children.get(3).getClass());
657 assertEquals(KnownCategories.DOWNLOADS, getCategory(children.get(3))); 656 assertEquals(KnownCategories.DOWNLOADS, getCategory(children.get(3)));
658 657
659 // With a different order. 658 // With a different order.
660 suggestionsSource = new FakeSuggestionsSource(); 659 suggestionsSource = new FakeSuggestionsSource();
661 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSo urce); 660 when(mUiDelegate.getSuggestionsSource()).thenReturn(suggestionsSource);
662 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0); 661 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0);
663 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0); 662 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0);
664 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0); 663 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0);
665 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0); 664 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0);
666 reloadNtp(); 665 reloadNtp();
667 666
668 children = mAdapter.getSectionListForTesting().getChildren(); 667 children = mAdapter.getSectionListForTesting().getChildren();
669 assertEquals(4, children.size()); 668 assertEquals(4, children.size());
670 assertEquals(SuggestionsSection.class, children.get(0).getClass()); 669 assertEquals(SuggestionsSection.class, children.get(0).getClass());
671 assertEquals(KnownCategories.ARTICLES, getCategory(children.get(0))); 670 assertEquals(KnownCategories.ARTICLES, getCategory(children.get(0)));
672 assertEquals(SuggestionsSection.class, children.get(1).getClass()); 671 assertEquals(SuggestionsSection.class, children.get(1).getClass());
673 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(children.ge t(1))); 672 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(children.ge t(1)));
674 assertEquals(SuggestionsSection.class, children.get(2).getClass()); 673 assertEquals(SuggestionsSection.class, children.get(2).getClass());
675 assertEquals(KnownCategories.DOWNLOADS, getCategory(children.get(2))); 674 assertEquals(KnownCategories.DOWNLOADS, getCategory(children.get(2)));
676 assertEquals(SuggestionsSection.class, children.get(3).getClass()); 675 assertEquals(SuggestionsSection.class, children.get(3).getClass());
677 assertEquals(KnownCategories.BOOKMARKS, getCategory(children.get(3))); 676 assertEquals(KnownCategories.BOOKMARKS, getCategory(children.get(3)));
678 677
679 // With unknown categories. 678 // With unknown categories.
680 suggestionsSource = new FakeSuggestionsSource(); 679 suggestionsSource = new FakeSuggestionsSource();
681 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSo urce); 680 when(mUiDelegate.getSuggestionsSource()).thenReturn(suggestionsSource);
682 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0); 681 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0);
683 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0); 682 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0);
684 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0); 683 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0);
685 reloadNtp(); 684 reloadNtp();
686 685
687 // The adapter is already initialised, it will not accept new categories anymore. 686 // The adapter is already initialised, it will not accept new categories anymore.
688 registerCategory(suggestionsSource, 42, 1); 687 registerCategory(suggestionsSource, 42, 1);
689 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 1); 688 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 1);
690 689
691 children = mAdapter.getSectionListForTesting().getChildren(); 690 children = mAdapter.getSectionListForTesting().getChildren();
692 assertEquals(3, children.size()); 691 assertEquals(3, children.size());
693 assertEquals(SuggestionsSection.class, children.get(0).getClass()); 692 assertEquals(SuggestionsSection.class, children.get(0).getClass());
694 assertEquals(KnownCategories.ARTICLES, getCategory(children.get(0))); 693 assertEquals(KnownCategories.ARTICLES, getCategory(children.get(0)));
695 assertEquals(SuggestionsSection.class, children.get(1).getClass()); 694 assertEquals(SuggestionsSection.class, children.get(1).getClass());
696 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(children.ge t(1))); 695 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(children.ge t(1)));
697 assertEquals(SuggestionsSection.class, children.get(2).getClass()); 696 assertEquals(SuggestionsSection.class, children.get(2).getClass());
698 assertEquals(KnownCategories.DOWNLOADS, getCategory(children.get(2))); 697 assertEquals(KnownCategories.DOWNLOADS, getCategory(children.get(2)));
699 } 698 }
700 699
701 @Test 700 @Test
702 @Feature({"Ntp"}) 701 @Feature({"Ntp"})
703 public void testChangeNotifications() { 702 public void testChangeNotifications() {
704 FakeSuggestionsSource suggestionsSource = spy(new FakeSuggestionsSource( )); 703 FakeSuggestionsSource suggestionsSource = spy(new FakeSuggestionsSource( ));
705 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 3); 704 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 3);
706 when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSo urce); 705 when(mUiDelegate.getSuggestionsSource()).thenReturn(suggestionsSource);
707 706
708 @SuppressWarnings("unchecked") 707 @SuppressWarnings("unchecked")
709 Callback<String> itemDismissedCallback = mock(Callback.class); 708 Callback<String> itemDismissedCallback = mock(Callback.class);
710 709
711 reloadNtp(); 710 reloadNtp();
712 AdapterDataObserver dataObserver = mock(AdapterDataObserver.class); 711 AdapterDataObserver dataObserver = mock(AdapterDataObserver.class);
713 mAdapter.registerAdapterDataObserver(dataObserver); 712 mAdapter.registerAdapterDataObserver(dataObserver);
714 713
715 // Adapter content: 714 // Adapter content:
716 // Idx | Item 715 // Idx | Item
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
792 } 791 }
793 792
794 @Test 793 @Test
795 @Feature({"Ntp"}) 794 @Feature({"Ntp"})
796 public void testSigninPromo() { 795 public void testSigninPromo() {
797 when(mMockSigninManager.isSignInAllowed()).thenReturn(true); 796 when(mMockSigninManager.isSignInAllowed()).thenReturn(true);
798 when(mMockSigninManager.isSignedInOnNative()).thenReturn(false); 797 when(mMockSigninManager.isSignedInOnNative()).thenReturn(false);
799 ArgumentCaptor<DestructionObserver> observers = 798 ArgumentCaptor<DestructionObserver> observers =
800 ArgumentCaptor.forClass(DestructionObserver.class); 799 ArgumentCaptor.forClass(DestructionObserver.class);
801 800
802 doNothing().when(mNewTabPageManager).addDestructionObserver(observers.ca pture()); 801 doNothing().when(mUiDelegate).addDestructionObserver(observers.capture() );
803 802
804 reloadNtp(); 803 reloadNtp();
805 assertTrue(isSignInPromoVisible()); 804 assertTrue(isSignInPromoVisible());
806 805
807 // Note: As currently implemented, these two variables should point to t he same object, a 806 // Note: As currently implemented, these two variables should point to t he same object, a
808 // SignInPromo.SigninObserver 807 // SignInPromo.SigninObserver
809 SignInStateObserver signInStateObserver = null; 808 SignInStateObserver signInStateObserver = null;
810 SignInAllowedObserver signInAllowedObserver = null; 809 SignInAllowedObserver signInAllowedObserver = null;
811 for (DestructionObserver observer : observers.getAllValues()) { 810 for (DestructionObserver observer : observers.getAllValues()) {
812 if (observer instanceof SignInStateObserver) { 811 if (observer instanceof SignInStateObserver) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 assertFalse(isSignInPromoVisible()); 860 assertFalse(isSignInPromoVisible());
862 } 861 }
863 862
864 @Test 863 @Test
865 @Feature({"Ntp"}) 864 @Feature({"Ntp"})
866 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL) 865 @EnableFeatures(ChromeFeatureList.NTP_SUGGESTIONS_SECTION_DISMISSAL)
867 public void testAllDismissedVisibility() { 866 public void testAllDismissedVisibility() {
868 ArgumentCaptor<DestructionObserver> observers = 867 ArgumentCaptor<DestructionObserver> observers =
869 ArgumentCaptor.forClass(DestructionObserver.class); 868 ArgumentCaptor.forClass(DestructionObserver.class);
870 869
871 verify(mNewTabPageManager, atLeastOnce()).addDestructionObserver(observe rs.capture()); 870 verify(mUiDelegate, atLeastOnce()).addDestructionObserver(observers.capt ure());
872 871
873 SigninObserver signinObserver = null; 872 SigninObserver signinObserver = null;
874 for (DestructionObserver observer : observers.getAllValues()) { 873 for (DestructionObserver observer : observers.getAllValues()) {
875 if (observer instanceof SigninObserver) { 874 if (observer instanceof SigninObserver) {
876 signinObserver = (SigninObserver) observer; 875 signinObserver = (SigninObserver) observer;
877 } 876 }
878 } 877 }
879 878
880 @SuppressWarnings("unchecked") 879 @SuppressWarnings("unchecked")
881 Callback<String> itemDismissedCallback = mock(Callback.class); 880 Callback<String> itemDismissedCallback = mock(Callback.class);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 /** 1038 /**
1040 * To be used with {@link #assertItemsFor(SectionDescriptor...)}, for a sect ion that has no 1039 * To be used with {@link #assertItemsFor(SectionDescriptor...)}, for a sect ion that has no
1041 * suggestions, but a status card to be displayed. 1040 * suggestions, but a status card to be displayed.
1042 * @return A descriptor for the section. 1041 * @return A descriptor for the section.
1043 */ 1042 */
1044 private SectionDescriptor sectionWithStatusCard() { 1043 private SectionDescriptor sectionWithStatusCard() {
1045 return new SectionDescriptor(0); 1044 return new SectionDescriptor(0);
1046 } 1045 }
1047 1046
1048 private void reloadNtp() { 1047 private void reloadNtp() {
1049 mAdapter = new NewTabPageAdapter(mNewTabPageManager, mock(View.class), n ull, 1048 mAdapter = new NewTabPageAdapter(mUiDelegate, mock(View.class), null, mO fflinePageBridge,
1050 mOfflinePageBridge); 1049 mock(ContextMenuManager.class));
1051 } 1050 }
1052 1051
1053 private void assertArticlesEqual(List<SnippetArticle> articles, int start, i nt end) { 1052 private void assertArticlesEqual(List<SnippetArticle> articles, int start, i nt end) {
1054 assertThat(mAdapter.getItemCount(), greaterThanOrEqualTo(end)); 1053 assertThat(mAdapter.getItemCount(), greaterThanOrEqualTo(end));
1055 for (int i = start; i < end; i++) { 1054 for (int i = start; i < end; i++) {
1056 assertEquals(articles.get(i - start), mAdapter.getSuggestionAt(i)); 1055 assertEquals(articles.get(i - start), mAdapter.getSuggestionAt(i));
1057 } 1056 }
1058 } 1057 }
1059 1058
1060 private boolean isSignInPromoVisible() { 1059 private boolean isSignInPromoVisible() {
(...skipping 23 matching lines...) Expand all
1084 viewTypeToString(expectedType)); 1083 viewTypeToString(expectedType));
1085 } 1084 }
1086 addLine(stringBuilder, "explainFailedExpectation -- END --"); 1085 addLine(stringBuilder, "explainFailedExpectation -- END --");
1087 return stringBuilder.toString(); 1086 return stringBuilder.toString();
1088 } 1087 }
1089 1088
1090 private static void addLine(StringBuilder stringBuilder, String template, Ob ject... args) { 1089 private static void addLine(StringBuilder stringBuilder, String template, Ob ject... args) {
1091 stringBuilder.append(String.format(Locale.US, template + "\n", args)); 1090 stringBuilder.append(String.format(Locale.US, template + "\n", args));
1092 } 1091 }
1093 } 1092 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698