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

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_suggestions_observer_unittest.cc

Issue 2811813002: [Offline Pages] Set up the initial prefetching service. (Closed)
Patch Set: Remove offline page model. Created 3 years, 8 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
(Empty)
1 // Copyright 2017 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/android/offline_pages/offline_page_suggestions_observer .h"
6
7 #include "base/run_loop.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "base/threading/thread_task_runner_handle.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "components/offline_pages/core/stub_offline_page_model.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "url/gurl.h"
15
16 namespace offline_pages {
17
18 namespace {
19
20 class TestingPrefetchService : public PrefetchService {
21 public:
22 TestingPrefetchService() = default;
23
24 void OnNewURLsToPrefetch(std::vector<GURL> suggested_urls) override {
25 latest_url_suggestions = suggested_urls;
26 new_suggestions_count++;
27 }
28 void RemoveAllUnprocessedURLsToPrefetch() override {
29 latest_url_suggestions.clear();
30 remove_all_suggestions_count++;
31 }
32
33 std::vector<GURL> latest_url_suggestions;
34 int new_suggestions_count = 0;
35 int remove_all_suggestions_count = 0;
36 };
37
38 class TestDelegate : public SuggestionsObserver::Delegate {
39 public:
40 TestDelegate() = default;
41 ~TestDelegate() override = default;
42
43 std::vector<GURL> GetSuggestionURLs(
44 const ntp_snippets::Category& category) override {
45 get_suggestion_urls_count++;
46 return suggestion_urls;
47 }
48
49 PrefetchService* GetPrefetchService(
50 content::BrowserContext* context) override {
51 return &prefetch_service;
52 }
53
54 TestingPrefetchService prefetch_service;
55
56 // Public for test manipulation.
57 std::vector<GURL> suggestion_urls;
58
59 // Signals that delegate was called.
60 int get_suggestion_urls_count = 0;
61 };
62
63 } // namespace
64
65 class OfflinePageSuggestionsObserverTest : public testing::Test {
66 public:
67 OfflinePageSuggestionsObserverTest() = default;
68
69 void SetUp() override {
70 observer_ = base::MakeUnique<SuggestionsObserver>(&profile_, MakeDelegate(),
71 category);
72 }
73
74 virtual std::unique_ptr<SuggestionsObserver::Delegate> MakeDelegate() {
75 auto delegate_ptr = base::MakeUnique<TestDelegate>();
76 test_delegate_ = delegate_ptr.get();
77 return std::move(delegate_ptr);
78 }
79
80 SuggestionsObserver* observer() { return observer_.get(); }
81
82 TestDelegate* test_delegate() { return test_delegate_; }
83 TestingPrefetchService* test_prefetch_service() {
84 return &(test_delegate()->prefetch_service);
85 }
86
87 protected:
88 ntp_snippets::Category category = ntp_snippets::Category::FromKnownCategory(
89 ntp_snippets::KnownCategories::ARTICLES);
90 content::TestBrowserThreadBundle thread_bundle_;
91 TestingProfile profile_;
92
93 private:
94 std::unique_ptr<SuggestionsObserver> observer_;
95 TestDelegate* test_delegate_;
96 };
97
98 TEST_F(OfflinePageSuggestionsObserverTest, CallsDelegateOnNewSuggestions) {
99 // We should not do anything if the category is not loaded.
100 observer()->OnNewSuggestions(category);
101 EXPECT_EQ(0, test_delegate()->get_suggestion_urls_count);
102 EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count);
103
104 // Once the category becomes available, new suggestions should cause us to ask
105 // the delegate for suggestion URLs.
106 observer()->OnCategoryStatusChanged(category,
107 ntp_snippets::CategoryStatus::AVAILABLE);
108 observer()->OnNewSuggestions(category);
109 EXPECT_EQ(1, test_delegate()->get_suggestion_urls_count);
110
111 // We expect that no pages were forwarded to the prefetch service since no
112 // pages were prepopulated.
113 EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count);
114 }
115
116 TEST_F(OfflinePageSuggestionsObserverTest,
117 ForwardsSuggestionsToPrefetchService) {
118 const GURL test_url_1("https://www.example.com/1");
119 test_delegate()->suggestion_urls.push_back(test_url_1);
120
121 observer()->OnCategoryStatusChanged(category,
122 ntp_snippets::CategoryStatus::AVAILABLE);
123 observer()->OnNewSuggestions(category);
124 EXPECT_EQ(1, test_prefetch_service()->new_suggestions_count);
125 EXPECT_EQ(1U, test_prefetch_service()->latest_url_suggestions.size());
126 EXPECT_EQ(test_url_1, test_prefetch_service()->latest_url_suggestions[0]);
127 }
128
129 TEST_F(OfflinePageSuggestionsObserverTest, RemovesAllOnBadStatus) {
130 const GURL test_url_1("https://www.example.com/1");
131 const GURL test_url_2("https://www.example.com/2");
132 test_delegate()->suggestion_urls.push_back(test_url_1);
133 test_delegate()->suggestion_urls.push_back(test_url_2);
134
135 observer()->OnCategoryStatusChanged(category,
136 ntp_snippets::CategoryStatus::AVAILABLE);
137 observer()->OnNewSuggestions(category);
138 ASSERT_EQ(2U, test_prefetch_service()->latest_url_suggestions.size());
139
140 observer()->OnCategoryStatusChanged(
141 category, ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED);
142 ASSERT_EQ(1, test_prefetch_service()->remove_all_suggestions_count);
143 observer()->OnCategoryStatusChanged(
144 category,
145 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED);
146 ASSERT_EQ(2, test_prefetch_service()->remove_all_suggestions_count);
147 }
148
149 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698