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

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

Issue 2100043004: [Offline pages] Filter out pages cached by different tabs on tab helper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updating comment to address CR Created 4 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/offline_pages/offline_page_tab_helper.h" 5 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/feature_list.h" 10 #include "base/feature_list.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/memory/ptr_util.h"
12 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "base/test/histogram_tester.h" 15 #include "base/test/histogram_tester.h"
15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
16 #include "chrome/browser/android/offline_pages/offline_page_utils.h" 17 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
17 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 18 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
19 #include "components/offline_pages/client_namespace_constants.h" 20 #include "components/offline_pages/client_namespace_constants.h"
20 #include "components/offline_pages/offline_page_feature.h" 21 #include "components/offline_pages/offline_page_feature.h"
21 #include "components/offline_pages/offline_page_item.h" 22 #include "components/offline_pages/offline_page_item.h"
(...skipping 10 matching lines...) Expand all
32 33
33 namespace { 34 namespace {
34 35
35 const GURL kTestPageUrl("http://test.org/page1"); 36 const GURL kTestPageUrl("http://test.org/page1");
36 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); 37 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234");
37 const int64_t kTestFileSize = 876543LL; 38 const int64_t kTestFileSize = 876543LL;
38 const char kBadNetworkHistogram[] = "OfflinePages.ShowOfflinePageOnBadNetwork"; 39 const char kBadNetworkHistogram[] = "OfflinePages.ShowOfflinePageOnBadNetwork";
39 const char kRedirectToOfflineHistogram[] = 40 const char kRedirectToOfflineHistogram[] =
40 "OfflinePages.RedirectToOfflineCount"; 41 "OfflinePages.RedirectToOfflineCount";
41 const char kRedirectToOnlineHistogram[] = "OfflinePages.RedirectToOnlineCount"; 42 const char kRedirectToOnlineHistogram[] = "OfflinePages.RedirectToOnlineCount";
43 const char kTabId[] = "42";
42 44
43 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { 45 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
44 public: 46 public:
45 TestNetworkChangeNotifier() : online_(true) {} 47 TestNetworkChangeNotifier() : online_(true) {}
46 48
47 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() 49 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType()
48 const override { 50 const override {
49 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN 51 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN
50 : net::NetworkChangeNotifier::CONNECTION_NONE; 52 : net::NetworkChangeNotifier::CONNECTION_NONE;
51 } 53 }
52 54
53 void set_online(bool online) { online_ = online; } 55 void set_online(bool online) { online_ = online; }
54 56
55 private: 57 private:
56 bool online_; 58 bool online_;
57 59
58 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); 60 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
59 }; 61 };
60 62
63 class TestDelegate : public OfflinePageTabHelper::Delegate {
64 public:
65 TestDelegate(bool has_tab_android, std::string tab_id)
66 : has_tab_android_(has_tab_android), tab_id_(tab_id) {}
67 ~TestDelegate() override {}
68
69 // offline_pages::OfflinePageTabHelper::Delegate implementation:
70 bool GetTabId(content::WebContents* web_contents,
71 std::string* tab_id) const override {
72 if (has_tab_android_)
73 *tab_id = tab_id_;
74 return has_tab_android_;
75 }
76
77 private:
78 bool has_tab_android_;
79 std::string tab_id_;
80 };
81
61 } // namespace 82 } // namespace
62 83
63 class OfflinePageTabHelperTest : 84 class OfflinePageTabHelperTest :
64 public ChromeRenderViewHostTestHarness, 85 public ChromeRenderViewHostTestHarness,
65 public OfflinePageTestArchiver::Observer, 86 public OfflinePageTestArchiver::Observer,
66 public base::SupportsWeakPtr<OfflinePageTabHelperTest> { 87 public base::SupportsWeakPtr<OfflinePageTabHelperTest> {
67 public: 88 public:
68 OfflinePageTabHelperTest() 89 OfflinePageTabHelperTest()
69 : network_change_notifier_(new TestNetworkChangeNotifier()) {} 90 : network_change_notifier_(new TestNetworkChangeNotifier()) {}
70 ~OfflinePageTabHelperTest() override {} 91 ~OfflinePageTabHelperTest() override {}
71 92
72 void SetUp() override; 93 void SetUp() override;
73 void TearDown() override; 94 void TearDown() override;
74 95
75 void RunUntilIdle(); 96 void RunUntilIdle();
76 void SimulateHasNetworkConnectivity(bool has_connectivity); 97 void SimulateHasNetworkConnectivity(bool has_connectivity);
77 void StartLoad(const GURL& url); 98 void StartLoad(const GURL& url);
78 void FailLoad(const GURL& url); 99 void FailLoad(const GURL& url);
100 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
101 const GURL& url,
102 const base::FilePath& file_name);
103 void OnSavePageDone(SavePageResult result, int64_t offline_id);
79 104
80 OfflinePageTabHelper* offline_page_tab_helper() const { 105 OfflinePageTabHelper* offline_page_tab_helper() const {
81 return offline_page_tab_helper_; 106 return offline_page_tab_helper_;
82 } 107 }
83 108
84 const GURL& online_url() const { return offline_page_item_->url; } 109 const GURL& online_url() const { return offline_page_item_->url; }
85 GURL offline_url() const { return offline_page_item_->GetOfflineURL(); } 110 GURL offline_url() const { return offline_page_item_->GetOfflineURL(); }
111 int64_t offline_id() const { return offline_page_item_->offline_id; }
86 112
87 const base::HistogramTester& histograms() const { return histogram_tester_; } 113 const base::HistogramTester& histograms() const { return histogram_tester_; }
88 114
89 private: 115 private:
90 // OfflinePageTestArchiver::Observer implementation: 116 // OfflinePageTestArchiver::Observer implementation:
91 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; 117 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override;
92 118
93 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
94 const GURL& url,
95 const base::FilePath& file_name);
96 void OnSavePageDone(SavePageResult result, int64_t offline_id);
97 void OnGetPageByOfflineIdDone(const OfflinePageItem* result); 119 void OnGetPageByOfflineIdDone(const OfflinePageItem* result);
98 120
99 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; 121 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
100 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. 122 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
101 123
102 std::unique_ptr<OfflinePageItem> offline_page_item_; 124 std::unique_ptr<OfflinePageItem> offline_page_item_;
103 125
104 base::HistogramTester histogram_tester_; 126 base::HistogramTester histogram_tester_;
105 127
106 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); 128 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest);
107 }; 129 };
108 130
109 void OfflinePageTabHelperTest::SetUp() { 131 void OfflinePageTabHelperTest::SetUp() {
110 // Enables offline pages feature. 132 // Enables offline pages feature.
111 // TODO(jianli): Remove this once the feature is completely enabled. 133 // TODO(jianli): Remove this once the feature is completely enabled.
112 base::FeatureList::ClearInstanceForTesting(); 134 base::FeatureList::ClearInstanceForTesting();
113 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); 135 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
114 feature_list->InitializeFromCommandLine( 136 feature_list->InitializeFromCommandLine(
115 offline_pages::kOfflineBookmarksFeature.name, ""); 137 offline_pages::kOfflineBookmarksFeature.name, "");
116 base::FeatureList::SetInstance(std::move(feature_list)); 138 base::FeatureList::SetInstance(std::move(feature_list));
117 139
118 // Creates a test web contents. 140 // Creates a test web contents.
119 content::RenderViewHostTestHarness::SetUp(); 141 content::RenderViewHostTestHarness::SetUp();
120 OfflinePageTabHelper::CreateForWebContents(web_contents()); 142 OfflinePageTabHelper::CreateForWebContents(web_contents());
121 offline_page_tab_helper_ = 143 offline_page_tab_helper_ =
122 OfflinePageTabHelper::FromWebContents(web_contents()); 144 OfflinePageTabHelper::FromWebContents(web_contents());
145 offline_page_tab_helper_->SetDelegateForTesting(
146 base::MakeUnique<TestDelegate>(true, kTabId));
123 147
124 // Sets up the factory for testing. 148 // Sets up the factory for testing.
125 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( 149 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
126 browser_context(), BuildTestOfflinePageModel); 150 browser_context(), BuildTestOfflinePageModel);
127 RunUntilIdle(); 151 RunUntilIdle();
128 152
129 // Saves an offline page. 153 // Saves an offline page.
130 OfflinePageModel* model = 154 OfflinePageModel* model =
131 OfflinePageModelFactory::GetForBrowserContext(browser_context()); 155 OfflinePageModelFactory::GetForBrowserContext(browser_context());
132 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( 156 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(
(...skipping 27 matching lines...) Expand all
160 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); 184 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url);
161 // Set up the error code for the failed navigation. 185 // Set up the error code for the failed navigation.
162 content::RenderFrameHostTester::For(main_rfh())-> 186 content::RenderFrameHostTester::For(main_rfh())->
163 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED); 187 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED);
164 content::RenderFrameHostTester::For(main_rfh())-> 188 content::RenderFrameHostTester::For(main_rfh())->
165 SimulateNavigationErrorPageCommit(); 189 SimulateNavigationErrorPageCommit();
166 // Gives a chance to run delayed task to do redirection. 190 // Gives a chance to run delayed task to do redirection.
167 RunUntilIdle(); 191 RunUntilIdle();
168 } 192 }
169 193
170 void OfflinePageTabHelperTest::SetLastPathCreatedByArchiver(
171 const base::FilePath& file_path) {
172 }
173
174 std::unique_ptr<OfflinePageTestArchiver> 194 std::unique_ptr<OfflinePageTestArchiver>
175 OfflinePageTabHelperTest::BuildArchiver(const GURL& url, 195 OfflinePageTabHelperTest::BuildArchiver(const GURL& url,
176 const base::FilePath& file_name) { 196 const base::FilePath& file_name) {
177 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( 197 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver(
178 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, 198 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED,
179 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); 199 kTestFileSize, base::ThreadTaskRunnerHandle::Get()));
180 archiver->set_filename(file_name); 200 archiver->set_filename(file_name);
181 return archiver; 201 return archiver;
182 } 202 }
183 203
184 void OfflinePageTabHelperTest::OnSavePageDone(SavePageResult result, 204 void OfflinePageTabHelperTest::OnSavePageDone(SavePageResult result,
185 int64_t offline_id) { 205 int64_t offline_id) {
186 OfflinePageModel* model = 206 OfflinePageModel* model =
187 OfflinePageModelFactory::GetForBrowserContext(browser_context()); 207 OfflinePageModelFactory::GetForBrowserContext(browser_context());
188 model->GetPageByOfflineId(offline_id, 208 model->GetPageByOfflineId(offline_id,
189 base::Bind(&OfflinePageTabHelperTest::OnGetPageByOfflineIdDone, 209 base::Bind(&OfflinePageTabHelperTest::OnGetPageByOfflineIdDone,
190 AsWeakPtr())); 210 AsWeakPtr()));
191 } 211 }
192 212
213 void OfflinePageTabHelperTest::SetLastPathCreatedByArchiver(
214 const base::FilePath& file_path) {}
215
193 void OfflinePageTabHelperTest::OnGetPageByOfflineIdDone( 216 void OfflinePageTabHelperTest::OnGetPageByOfflineIdDone(
194 const OfflinePageItem* result) { 217 const OfflinePageItem* result) {
195 DCHECK(result); 218 DCHECK(result);
196 offline_page_item_.reset(new OfflinePageItem(*result)); 219 offline_page_item_.reset(new OfflinePageItem(*result));
197 } 220 }
198 221
199 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) { 222 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) {
200 SimulateHasNetworkConnectivity(true); 223 SimulateHasNetworkConnectivity(true);
201 224
202 StartLoad(offline_url()); 225 StartLoad(offline_url());
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 RunUntilIdle(); 298 RunUntilIdle();
276 299
277 // Redirection should be cancelled so we should still navigate to 300 // Redirection should be cancelled so we should still navigate to
278 // |unsaved_url|. 301 // |unsaved_url|.
279 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL()); 302 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL());
280 303
281 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); 304 histograms().ExpectTotalCount(kBadNetworkHistogram, 0);
282 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0); 305 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0);
283 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); 306 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0);
284 } 307 }
308
309 // This test saves 3 pages (one in setup and 2 in test). The most appropriate
310 // test is related to |kTabId|, as it is saved in the latest moment and can be
311 // used in the current tab.
312 TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) {
313 // Saves an offline page.
314 OfflinePageModel* model =
315 OfflinePageModelFactory::GetForBrowserContext(browser_context());
316 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(
317 kTestPageUrl, base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))));
318
319 // We expect this copy to be used later.
320 ClientId client_id(kLastNNamespace, kTabId);
321 model->SavePage(
322 kTestPageUrl, client_id, std::move(archiver),
323 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr()));
324 RunUntilIdle();
325 const int64_t expected_offline_id = offline_id();
326 const GURL expected_offline_url = offline_url();
327
328 archiver = BuildArchiver(kTestPageUrl,
329 base::FilePath(FILE_PATH_LITERAL("page3.html")));
330 client_id.id = "39";
331 model->SavePage(
332 kTestPageUrl, client_id, std::move(archiver),
333 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr()));
334 RunUntilIdle();
335
336 SimulateHasNetworkConnectivity(false);
337 StartLoad(kTestPageUrl);
338 // Gives a chance to run delayed task to do redirection.
339 RunUntilIdle();
340
341 const OfflinePageItem* item =
342 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents());
343 EXPECT_EQ(expected_offline_id, item->offline_id);
344 EXPECT_EQ(expected_offline_url, item->GetOfflineURL());
345 EXPECT_EQ(kLastNNamespace, item->client_id.name_space);
346 EXPECT_EQ(kTabId, item->client_id.id);
347 }
285 } // namespace offline_pages 348 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698