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 package org.chromium.chrome.browser.ntp.cards; | 5 package org.chromium.chrome.browser.ntp.cards; |
6 | 6 |
7 import static org.hamcrest.Matchers.is; | 7 import static org.hamcrest.Matchers.is; |
8 import static org.hamcrest.collection.IsIterableContainingInOrder.contains; | 8 import static org.hamcrest.collection.IsIterableContainingInOrder.contains; |
9 import static org.junit.Assert.assertEquals; | 9 import static org.junit.Assert.assertEquals; |
10 import static org.junit.Assert.assertFalse; | 10 import static org.junit.Assert.assertFalse; |
(...skipping 25 matching lines...) Expand all Loading... | |
36 import org.junit.runner.RunWith; | 36 import org.junit.runner.RunWith; |
37 import org.mockito.Mock; | 37 import org.mockito.Mock; |
38 import org.mockito.MockitoAnnotations; | 38 import org.mockito.MockitoAnnotations; |
39 import org.robolectric.annotation.Config; | 39 import org.robolectric.annotation.Config; |
40 | 40 |
41 import org.chromium.base.Callback; | 41 import org.chromium.base.Callback; |
42 import org.chromium.base.metrics.RecordUserAction; | 42 import org.chromium.base.metrics.RecordUserAction; |
43 import org.chromium.base.test.util.Feature; | 43 import org.chromium.base.test.util.Feature; |
44 import org.chromium.chrome.browser.DisableHistogramsRule; | 44 import org.chromium.chrome.browser.DisableHistogramsRule; |
45 import org.chromium.chrome.browser.ntp.cards.NewTabPageViewHolder.UpdateLayoutPa ramsCallback; | 45 import org.chromium.chrome.browser.ntp.cards.NewTabPageViewHolder.UpdateLayoutPa ramsCallback; |
46 import org.chromium.chrome.browser.ntp.cards.SuggestionsSection.SuggestionsList; | |
46 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; | 47 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; |
47 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; | 48 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
48 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; | 49 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; |
49 import org.chromium.chrome.browser.offlinepages.OfflinePageItem; | 50 import org.chromium.chrome.browser.offlinepages.OfflinePageItem; |
50 import org.chromium.chrome.browser.suggestions.ContentSuggestionsAdditionalActio n; | 51 import org.chromium.chrome.browser.suggestions.ContentSuggestionsAdditionalActio n; |
51 import org.chromium.chrome.browser.suggestions.SuggestionsEventReporter; | 52 import org.chromium.chrome.browser.suggestions.SuggestionsEventReporter; |
52 import org.chromium.chrome.browser.suggestions.SuggestionsNavigationDelegate; | 53 import org.chromium.chrome.browser.suggestions.SuggestionsNavigationDelegate; |
53 import org.chromium.chrome.browser.suggestions.SuggestionsRanker; | 54 import org.chromium.chrome.browser.suggestions.SuggestionsRanker; |
54 import org.chromium.chrome.browser.suggestions.SuggestionsUiDelegate; | 55 import org.chromium.chrome.browser.suggestions.SuggestionsUiDelegate; |
55 import org.chromium.chrome.test.util.browser.suggestions.ContentSuggestionsTestU tils.CategoryInfoBuilder; | 56 import org.chromium.chrome.test.util.browser.suggestions.ContentSuggestionsTestU tils.CategoryInfoBuilder; |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
478 // Indices in section are off-by-one (index 0 is the header). | 479 // Indices in section are off-by-one (index 0 is the header). |
479 bindViewHolders(section, 1, 2); | 480 bindViewHolders(section, 1, 2); |
480 | 481 |
481 List<SnippetArticle> newSnippets = | 482 List<SnippetArticle> newSnippets = |
482 createDummySuggestions(3, TEST_CATEGORY_ID, "new"); | 483 createDummySuggestions(3, TEST_CATEGORY_ID, "new"); |
483 // Copy the list when passing to the section - it may alter it but we la ter need it. | 484 // Copy the list when passing to the section - it may alter it but we la ter need it. |
484 section.updateSuggestions(createSourceFor(new ArrayList<>(newSnippets))) ; | 485 section.updateSuggestions(createSourceFor(new ArrayList<>(newSnippets))) ; |
485 verify(mParent).onItemRangeRemoved(section, 2, 3); | 486 verify(mParent).onItemRangeRemoved(section, 2, 3); |
486 verify(mParent).onItemRangeInserted(section, 2, 2); | 487 verify(mParent).onItemRangeInserted(section, 2, 2); |
487 assertEquals(3, section.getSuggestionsCount()); | 488 assertEquals(3, section.getSuggestionsCount()); |
488 assertEquals(snippets.get(0), section.getSuggestionAt(1)); | 489 SuggestionsList suggestionList = section.getSuggestionListForTesting(); |
489 assertNotEquals(snippets.get(1), section.getSuggestionAt(2)); | 490 assertEquals(snippets.get(0), suggestionList.getSuggestionAt(0)); |
490 assertEquals(newSnippets.get(0), section.getSuggestionAt(2)); | 491 assertNotEquals(snippets.get(1), suggestionList.getSuggestionAt(1)); |
491 assertNotEquals(snippets.get(2), section.getSuggestionAt(3)); | 492 assertEquals(newSnippets.get(0), suggestionList.getSuggestionAt(1)); |
492 assertEquals(newSnippets.get(1), section.getSuggestionAt(3)); | 493 assertNotEquals(snippets.get(2), suggestionList.getSuggestionAt(2)); |
494 assertEquals(newSnippets.get(1), suggestionList.getSuggestionAt(2)); | |
493 | 495 |
494 assertTrue(section.isDataStale()); | 496 assertTrue(section.isDataStale()); |
495 } | 497 } |
496 | 498 |
497 /** | 499 /** |
498 * Tests that the UI does not update the first two items of the section if t hey have been | 500 * Tests that the UI does not update the first two items of the section if t hey have been |
499 * viewed. | 501 * viewed. |
500 */ | 502 */ |
501 @Test | 503 @Test |
502 @Feature({"Ntp"}) | 504 @Feature({"Ntp"}) |
503 public void testUpdateSectionDoesNotReplaceFirstTwoSuggestionWhenSeen() { | 505 public void testUpdateSectionDoesNotReplaceFirstTwoSuggestionWhenSeen() { |
504 List<SnippetArticle> snippets = createDummySuggestions(4, TEST_CATEGORY_ ID, "old"); | 506 List<SnippetArticle> snippets = createDummySuggestions(4, TEST_CATEGORY_ ID, "old"); |
505 // Copy the list when passing to the section - it may alter it but we la ter need it. | 507 // Copy the list when passing to the section - it may alter it but we la ter need it. |
506 SuggestionsSection section = | 508 SuggestionsSection section = |
507 createSectionWithSuggestions(new ArrayList<>(snippets)); | 509 createSectionWithSuggestions(new ArrayList<>(snippets)); |
508 assertEquals(4, section.getSuggestionsCount()); | 510 assertEquals(4, section.getSuggestionsCount()); |
509 | 511 |
510 // Bind the first two suggestions - indicate that they are being viewed. | 512 // Bind the first two suggestions - indicate that they are being viewed. |
511 // Indices in section are off-by-one (index 0 is the header). | 513 // Indices in section are off-by-one (index 0 is the header). |
512 bindViewHolders(section, 1, 3); | 514 bindViewHolders(section, 1, 3); |
513 | 515 |
514 List<SnippetArticle> newSnippets = | 516 List<SnippetArticle> newSnippets = |
515 createDummySuggestions(3, TEST_CATEGORY_ID, "new"); | 517 createDummySuggestions(3, TEST_CATEGORY_ID, "new"); |
516 // Copy the list when passing to the section - it may alter it but we la ter need it. | 518 // Copy the list when passing to the section - it may alter it but we la ter need it. |
517 section.updateSuggestions(createSourceFor(new ArrayList<>(newSnippets))) ; | 519 section.updateSuggestions(createSourceFor(new ArrayList<>(newSnippets))) ; |
518 verify(mParent).onItemRangeRemoved(section, 3, 2); | 520 verify(mParent).onItemRangeRemoved(section, 3, 2); |
519 verify(mParent).onItemRangeInserted(section, 3, 1); | 521 verify(mParent).onItemRangeInserted(section, 3, 1); |
520 assertEquals(3, section.getSuggestionsCount()); | 522 assertEquals(3, section.getSuggestionsCount()); |
521 assertEquals(snippets.get(0), section.getSuggestionAt(1)); | 523 SuggestionsList suggestionList = section.getSuggestionListForTesting(); |
dgn
2017/05/24 11:30:31
isn't it possible to use visitor to check that?
Bernhard Bauer
2017/05/25 12:27:54
Sure, done.
| |
522 assertEquals(snippets.get(1), section.getSuggestionAt(2)); | 524 assertEquals(snippets.get(0), suggestionList.getSuggestionAt(0)); |
523 assertNotEquals(snippets.get(2), section.getSuggestionAt(3)); | 525 assertEquals(snippets.get(1), suggestionList.getSuggestionAt(1)); |
524 assertEquals(newSnippets.get(0), section.getSuggestionAt(3)); | 526 assertNotEquals(snippets.get(2), suggestionList.getSuggestionAt(2)); |
527 assertEquals(newSnippets.get(0), suggestionList.getSuggestionAt(2)); | |
525 | 528 |
526 assertTrue(section.isDataStale()); | 529 assertTrue(section.isDataStale()); |
527 } | 530 } |
528 | 531 |
529 /** | 532 /** |
530 * Tests that the UI does not update any items of the section if the new lis t is shorter than | 533 * Tests that the UI does not update any items of the section if the new lis t is shorter than |
531 * what has been viewed. | 534 * what has been viewed. |
532 */ | 535 */ |
533 @Test | 536 @Test |
534 @Feature({"Ntp"}) | 537 @Feature({"Ntp"}) |
535 public void testUpdateSectionDoesNothingWhenNewListIsShorterThanItemsSeen() { | 538 public void testUpdateSectionDoesNothingWhenNewListIsShorterThanItemsSeen() { |
536 List<SnippetArticle> snippets = createDummySuggestions(4, TEST_CATEGORY_ ID, "old"); | 539 List<SnippetArticle> snippets = createDummySuggestions(4, TEST_CATEGORY_ ID, "old"); |
537 // Copy the list when passing to the section - it may alter it but we la ter need it. | 540 // Copy the list when passing to the section - it may alter it but we la ter need it. |
538 SuggestionsSection section = | 541 SuggestionsSection section = |
539 createSectionWithSuggestions(new ArrayList<>(snippets)); | 542 createSectionWithSuggestions(new ArrayList<>(snippets)); |
540 assertEquals(4, section.getSuggestionsCount()); | 543 assertEquals(4, section.getSuggestionsCount()); |
541 | 544 |
542 // Bind the first two suggestions - indicate that they are being viewed. | 545 // Bind the first two suggestions - indicate that they are being viewed. |
543 // Indices in section are off-by-one (index 0 is the header). | 546 // Indices in section are off-by-one (index 0 is the header). |
544 bindViewHolders(section, 1, 3); | 547 bindViewHolders(section, 1, 3); |
545 | 548 |
546 section.updateSuggestions(createSourceFor(createDummySuggestions(1, TEST _CATEGORY_ID))); | 549 section.updateSuggestions(createSourceFor(createDummySuggestions(1, TEST _CATEGORY_ID))); |
547 // Even though the new list has just one suggestion, we need to keep the two seen ones | 550 // Even though the new list has just one suggestion, we need to keep the two seen ones |
548 // around. | 551 // around. |
549 verify(mParent).onItemRangeRemoved(section, 3, 2); | 552 verify(mParent).onItemRangeRemoved(section, 3, 2); |
550 verify(mParent, never()).onItemRangeInserted(any(TreeNode.class), anyInt (), anyInt()); | 553 verify(mParent, never()).onItemRangeInserted(any(TreeNode.class), anyInt (), anyInt()); |
551 assertEquals(2, section.getSuggestionsCount()); | 554 assertEquals(2, section.getSuggestionsCount()); |
552 assertEquals(snippets.get(0), section.getSuggestionAt(1)); | 555 SuggestionsList suggestionList = section.getSuggestionListForTesting(); |
553 assertEquals(snippets.get(1), section.getSuggestionAt(2)); | 556 assertEquals(snippets.get(0), suggestionList.getSuggestionAt(0)); |
557 assertEquals(snippets.get(1), suggestionList.getSuggestionAt(1)); | |
554 | 558 |
555 assertTrue(section.isDataStale()); | 559 assertTrue(section.isDataStale()); |
556 } | 560 } |
557 | 561 |
558 /** | 562 /** |
559 * Tests that the UI does not update any items of the section if the current list is shorter | 563 * Tests that the UI does not update any items of the section if the current list is shorter |
560 * than what has been viewed. | 564 * than what has been viewed. |
561 */ | 565 */ |
562 @Test | 566 @Test |
563 @Feature({"Ntp"}) | 567 @Feature({"Ntp"}) |
564 public void testUpdateSectionDoesNothingWhenCurrentListIsShorterThanItemsSee n() { | 568 public void testUpdateSectionDoesNothingWhenCurrentListIsShorterThanItemsSee n() { |
565 List<SnippetArticle> snippets = createDummySuggestions(3, TEST_CATEGORY_ ID, "old"); | 569 List<SnippetArticle> snippets = createDummySuggestions(3, TEST_CATEGORY_ ID, "old"); |
566 // Copy the list when passing to the section - it may alter it but we la ter need it. | 570 // Copy the list when passing to the section - it may alter it but we la ter need it. |
567 SuggestionsSection section = | 571 SuggestionsSection section = |
568 createSectionWithSuggestions(new ArrayList<>(snippets)); | 572 createSectionWithSuggestions(new ArrayList<>(snippets)); |
569 assertEquals(3, section.getSuggestionsCount()); | 573 assertEquals(3, section.getSuggestionsCount()); |
570 | 574 |
571 // Bind the first two suggestions - indicate that they are being viewed. | 575 // Bind the first two suggestions - indicate that they are being viewed. |
572 // Indices in section are off-by-one (index 0 is the header). | 576 // Indices in section are off-by-one (index 0 is the header). |
573 bindViewHolders(section, 1, 3); | 577 bindViewHolders(section, 1, 3); |
574 | 578 |
575 // Remove last two items. | 579 // Remove last two items. |
576 section.removeSuggestionById(section.getSuggestionAt(3).mIdWithinCategor y); | 580 SuggestionsList suggestionList = section.getSuggestionListForTesting(); |
577 section.removeSuggestionById(section.getSuggestionAt(2).mIdWithinCategor y); | 581 section.removeSuggestionById(suggestionList.getSuggestionAt(2).mIdWithin Category); |
582 section.removeSuggestionById(suggestionList.getSuggestionAt(1).mIdWithin Category); | |
578 reset(mParent); | 583 reset(mParent); |
579 | 584 |
580 assertEquals(1, section.getSuggestionsCount()); | 585 assertEquals(1, section.getSuggestionsCount()); |
581 | 586 |
582 section.updateSuggestions(createSourceFor(createDummySuggestions(4, TEST _CATEGORY_ID))); | 587 section.updateSuggestions(createSourceFor(createDummySuggestions(4, TEST _CATEGORY_ID))); |
583 // We do not touch the current list if all has been seen. | 588 // We do not touch the current list if all has been seen. |
584 verify(mParent, never()).onItemRangeRemoved(any(TreeNode.class), anyInt( ), anyInt()); | 589 verify(mParent, never()).onItemRangeRemoved(any(TreeNode.class), anyInt( ), anyInt()); |
585 verify(mParent, never()).onItemRangeInserted(any(TreeNode.class), anyInt (), anyInt()); | 590 verify(mParent, never()).onItemRangeInserted(any(TreeNode.class), anyInt (), anyInt()); |
586 assertEquals(1, section.getSuggestionsCount()); | 591 assertEquals(1, section.getSuggestionsCount()); |
587 assertEquals(snippets.get(0), section.getSuggestionAt(1)); | 592 assertEquals(snippets.get(0), suggestionList.getSuggestionAt(0)); |
588 | 593 |
589 assertTrue(section.isDataStale()); | 594 assertTrue(section.isDataStale()); |
590 } | 595 } |
591 | 596 |
592 /** | 597 /** |
593 * Tests that the UI does not update when the section has been viewed. | 598 * Tests that the UI does not update when the section has been viewed. |
594 */ | 599 */ |
595 @Test | 600 @Test |
596 @Feature({"Ntp"}) | 601 @Feature({"Ntp"}) |
597 public void testUpdateSectionDoesNothingWhenAllSeen() { | 602 public void testUpdateSectionDoesNothingWhenAllSeen() { |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
786 verify(section.getCategoryInfo(), | 791 verify(section.getCategoryInfo(), |
787 (action == ContentSuggestionsAdditionalAction.VIEW_ALL ? times(1 ) : never())) | 792 (action == ContentSuggestionsAdditionalAction.VIEW_ALL ? times(1 ) : never())) |
788 .performViewAllAction(navDelegate); | 793 .performViewAllAction(navDelegate); |
789 verify(suggestionsSource, | 794 verify(suggestionsSource, |
790 (action == ContentSuggestionsAdditionalAction.FETCH ? times(1) : never())) | 795 (action == ContentSuggestionsAdditionalAction.FETCH ? times(1) : never())) |
791 .fetchSuggestions(anyInt(), any(String[].class)); | 796 .fetchSuggestions(anyInt(), any(String[].class)); |
792 } | 797 } |
793 | 798 |
794 private static void verifySnippets(SuggestionsSection section, List<SnippetA rticle> snippets) { | 799 private static void verifySnippets(SuggestionsSection section, List<SnippetA rticle> snippets) { |
795 assertEquals(snippets.size(), section.getSuggestionsCount()); | 800 assertEquals(snippets.size(), section.getSuggestionsCount()); |
796 // Indices in section are off-by-one (index 0 is the header). | 801 SuggestionsList suggestionsList = section.getSuggestionListForTesting(); |
797 int index = 1; | 802 |
803 int index = 0; | |
798 for (SnippetArticle snippet : snippets) { | 804 for (SnippetArticle snippet : snippets) { |
799 assertEquals(snippet, section.getSuggestionAt(index++)); | 805 assertEquals(snippet, suggestionsList.getSuggestionAt(index++)); |
800 } | 806 } |
801 } | 807 } |
802 } | 808 } |
OLD | NEW |