OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ntp_snippets/bookmark_last_visit_updater.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "components/bookmarks/browser/bookmark_model.h" |
| 13 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 14 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "content/public/test/test_web_contents_factory.h" |
| 18 #include "content/public/test/web_contents_tester.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 using content::WebContents; |
| 23 using content::WebContentsTester; |
| 24 using content::TestBrowserThreadBundle; |
| 25 using content::TestWebContentsFactory; |
| 26 using testing::IsEmpty; |
| 27 using testing::Not; |
| 28 |
| 29 TEST(BookmarkLastVisitUpdaterTest, DoesNotCrashForNoBookmarkModel) { |
| 30 TestBrowserThreadBundle thread_bundle; |
| 31 TestingProfile profile; |
| 32 |
| 33 // Create a tab with the BookmarkLastVisitUpdater (with no BookmarkModel*). |
| 34 TestWebContentsFactory factory; |
| 35 WebContents* tab = factory.CreateWebContents(&profile); |
| 36 BookmarkLastVisitUpdater::MaybeCreateForWebContentsWithBookmarkModel( |
| 37 tab, /*bookmark_model=*/nullptr); |
| 38 |
| 39 // Visit a URL. |
| 40 WebContentsTester* tester = WebContentsTester::For(tab); |
| 41 tester->StartNavigation(GURL("http://foo.org/")); |
| 42 |
| 43 // The only expectation is that it does not crash. |
| 44 } |
| 45 |
| 46 TEST(BookmarkLastVisitUpdaterTest, IsTrackingVisits) { |
| 47 TestBrowserThreadBundle thread_bundle; |
| 48 TestingProfile profile; |
| 49 profile.CreateBookmarkModel(true); |
| 50 bookmarks::BookmarkModel* bookmark_model = |
| 51 BookmarkModelFactory::GetForBrowserContext(&profile); |
| 52 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model); |
| 53 |
| 54 // Create a bookmark. |
| 55 GURL url = GURL("http://foo.org/"); |
| 56 bookmark_model->AddURL(bookmark_model->bookmark_bar_node(), 0, |
| 57 base::string16(), url); |
| 58 |
| 59 // Create a tab with the BookmarkLastVisitUpdater. |
| 60 TestWebContentsFactory factory; |
| 61 WebContents* tab = factory.CreateWebContents(&profile); |
| 62 BookmarkLastVisitUpdater::MaybeCreateForWebContentsWithBookmarkModel( |
| 63 tab, bookmark_model); |
| 64 |
| 65 // Visit the bookmarked URL. |
| 66 WebContentsTester* tester = WebContentsTester::For(tab); |
| 67 tester->StartNavigation(url); |
| 68 |
| 69 EXPECT_THAT(ntp_snippets::GetRecentlyVisitedBookmarks( |
| 70 bookmark_model, 2, base::Time::UnixEpoch(), |
| 71 /*consider_visits_from_desktop=*/true), |
| 72 Not(IsEmpty())); |
| 73 } |
| 74 |
| 75 TEST(BookmarkLastVisitUpdaterTest, IsNotTrackingIncognitoVisits) { |
| 76 TestBrowserThreadBundle thread_bundle; |
| 77 TestingProfile profile; |
| 78 profile.CreateBookmarkModel(true); |
| 79 bookmarks::BookmarkModel* bookmark_model = |
| 80 BookmarkModelFactory::GetForBrowserContext(&profile); |
| 81 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model); |
| 82 |
| 83 // Create a couple of bookmarks. |
| 84 GURL url = GURL("http://foo.org/"); |
| 85 bookmark_model->AddURL(bookmark_model->bookmark_bar_node(), 0, |
| 86 base::string16(), url); |
| 87 |
| 88 // Create an incognito tab with the BookmarkLastVisitUpdater. |
| 89 Profile* incognito = profile.GetOffTheRecordProfile(); |
| 90 TestWebContentsFactory factory; |
| 91 WebContents* tab = factory.CreateWebContents(incognito); |
| 92 BookmarkLastVisitUpdater::MaybeCreateForWebContentsWithBookmarkModel( |
| 93 tab, bookmark_model); |
| 94 |
| 95 // Visit the bookmarked URL. |
| 96 WebContentsTester* tester = WebContentsTester::For(tab); |
| 97 tester->StartNavigation(url); |
| 98 |
| 99 // The incognito visit should _not_ appear in recent bookmarks. |
| 100 EXPECT_THAT(ntp_snippets::GetRecentlyVisitedBookmarks( |
| 101 bookmark_model, 2, base::Time::UnixEpoch(), |
| 102 /*consider_visits_from_desktop=*/true), |
| 103 IsEmpty()); |
| 104 } |
OLD | NEW |