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

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

Issue 2278773002: Modify RecentTabHelper to be always-on and observe the loading of the pages. (Closed)
Patch Set: cr feedback Created 4 years, 3 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/recent_tab_helper.h" 5 #include "chrome/browser/android/offline_pages/recent_tab_helper.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/string16.h" 9 #include "base/strings/string16.h"
10 #include "base/test/scoped_feature_list.h"
10 #include "base/test/test_mock_time_task_runner.h" 11 #include "base/test/test_mock_time_task_runner.h"
11 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
12 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 13 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
13 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 14 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "components/offline_pages/offline_page_feature.h"
15 #include "components/offline_pages/offline_page_item.h" 17 #include "components/offline_pages/offline_page_item.h"
16 #include "components/offline_pages/offline_page_model.h" 18 #include "components/offline_pages/offline_page_model.h"
17 #include "components/offline_pages/offline_page_test_archiver.h" 19 #include "components/offline_pages/offline_page_test_archiver.h"
18 #include "content/public/browser/navigation_entry.h" 20 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/navigation_handle.h" 21 #include "content/public/browser/navigation_handle.h"
20 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
21 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
22 24
23 namespace offline_pages { 25 namespace offline_pages {
24 26
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 97
96 private: 98 private:
97 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result); 99 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result);
98 100
99 RecentTabHelper* recent_tab_helper_; // Owned by WebContents. 101 RecentTabHelper* recent_tab_helper_; // Owned by WebContents.
100 OfflinePageModel* model_; // Keyed service 102 OfflinePageModel* model_; // Keyed service
101 size_t model_changed_count_; 103 size_t model_changed_count_;
102 size_t model_removed_count_; 104 size_t model_removed_count_;
103 std::vector<OfflinePageItem> all_pages_; 105 std::vector<OfflinePageItem> all_pages_;
104 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; 106 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
107 base::test::ScopedFeatureList scoped_feature_list_;
105 108
106 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_; 109 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_;
107 110
108 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest); 111 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest);
109 }; 112 };
110 113
111 TestDelegate::TestDelegate( 114 TestDelegate::TestDelegate(
112 OfflinePageTestArchiver::Observer* observer, 115 OfflinePageTestArchiver::Observer* observer,
113 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 116 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
114 int tab_id, 117 int tab_id,
(...skipping 28 matching lines...) Expand all
143 : recent_tab_helper_(nullptr), 146 : recent_tab_helper_(nullptr),
144 model_(nullptr), 147 model_(nullptr),
145 model_changed_count_(0), 148 model_changed_count_(0),
146 model_removed_count_(0), 149 model_removed_count_(0),
147 task_runner_(new base::TestMockTimeTaskRunner), 150 task_runner_(new base::TestMockTimeTaskRunner),
148 weak_ptr_factory_(this) { 151 weak_ptr_factory_(this) {
149 } 152 }
150 153
151 void RecentTabHelperTest::SetUp() { 154 void RecentTabHelperTest::SetUp() {
152 content::RenderViewHostTestHarness::SetUp(); 155 content::RenderViewHostTestHarness::SetUp();
156
157 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature);
153 // Sets up the factory for testing. 158 // Sets up the factory for testing.
154 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( 159 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
155 browser_context(), BuildTestOfflinePageModel); 160 browser_context(), BuildTestOfflinePageModel);
156 RunUntilIdle(); 161 RunUntilIdle();
157 162
158 RecentTabHelper::CreateForWebContents(web_contents()); 163 RecentTabHelper::CreateForWebContents(web_contents());
159 recent_tab_helper_ = 164 recent_tab_helper_ =
160 RecentTabHelper::FromWebContents(web_contents()); 165 RecentTabHelper::FromWebContents(web_contents());
161 166
162 recent_tab_helper_->SetDelegate(base::MakeUnique<TestDelegate>( 167 recent_tab_helper_->SetDelegate(base::MakeUnique<TestDelegate>(
(...skipping 29 matching lines...) Expand all
192 void RecentTabHelperTest::RunUntilIdle() { 197 void RecentTabHelperTest::RunUntilIdle() {
193 base::RunLoop().RunUntilIdle(); 198 base::RunLoop().RunUntilIdle();
194 } 199 }
195 200
196 void RecentTabHelperTest::FastForwardSnapshotController() { 201 void RecentTabHelperTest::FastForwardSnapshotController() {
197 const size_t kLongDelayMs = 100*1000; 202 const size_t kLongDelayMs = 100*1000;
198 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(kLongDelayMs)); 203 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(kLongDelayMs));
199 } 204 }
200 205
201 TEST_F(RecentTabHelperTest, Basic) { 206 TEST_F(RecentTabHelperTest, Basic) {
207 base::test::ScopedFeatureList scoped_feature_list;
208 scoped_feature_list.Init();
202 EXPECT_NE(nullptr, recent_tab_helper()); 209 EXPECT_NE(nullptr, recent_tab_helper());
203 } 210 }
204 211
205 TEST_F(RecentTabHelperTest, SimpleCapture) { 212 TEST_F(RecentTabHelperTest, SimpleCapture) {
206 NavigateAndCommit(kTestPageUrl); 213 NavigateAndCommit(kTestPageUrl);
214 EXPECT_FALSE(recent_tab_helper()->is_page_ready_for_snapshot());
207 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 215 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
208 RunUntilIdle(); 216 RunUntilIdle();
217 EXPECT_TRUE(recent_tab_helper()->is_page_ready_for_snapshot());
209 EXPECT_TRUE(model()->is_loaded()); 218 EXPECT_TRUE(model()->is_loaded());
210 GetAllPages(); 219 GetAllPages();
211 EXPECT_EQ(1U, all_pages().size()); 220 EXPECT_EQ(1U, all_pages().size());
212 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); 221 EXPECT_EQ(kTestPageUrl, all_pages()[0].url);
213 } 222 }
214 223
215 TEST_F(RecentTabHelperTest, NoTabIdNoCapture) { 224 TEST_F(RecentTabHelperTest, NoTabIdNoCapture) {
216 // Create delegate that returns 'false' as TabId retrieval result. 225 // Create delegate that returns 'false' as TabId retrieval result.
217 recent_tab_helper()->SetDelegate(base::MakeUnique<TestDelegate>( 226 recent_tab_helper()->SetDelegate(base::MakeUnique<TestDelegate>(
218 this, task_runner(), kTabId, false)); 227 this, task_runner(), kTabId, false));
219 228
220 NavigateAndCommit(kTestPageUrl); 229 NavigateAndCommit(kTestPageUrl);
221 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 230 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
222 RunUntilIdle(); 231 RunUntilIdle();
223 EXPECT_TRUE(model()->is_loaded()); 232 EXPECT_TRUE(model()->is_loaded());
224 GetAllPages(); 233 GetAllPages();
225 // No page shodul be captured. 234 // No page should be captured.
226 EXPECT_EQ(0U, all_pages().size()); 235 EXPECT_EQ(0U, all_pages().size());
227 } 236 }
228 237
229 // Should end up with 1 page. 238 // Should end up with 1 page.
230 TEST_F(RecentTabHelperTest, TwoCapturesSameUrl) { 239 TEST_F(RecentTabHelperTest, TwoCapturesSameUrl) {
231 NavigateAndCommit(kTestPageUrl); 240 NavigateAndCommit(kTestPageUrl);
232 // Triggers snapshot after a time delay. 241 // Triggers snapshot after a time delay.
233 recent_tab_helper()->DocumentAvailableInMainFrame(); 242 recent_tab_helper()->DocumentAvailableInMainFrame();
234 RunUntilIdle(); 243 RunUntilIdle();
235 EXPECT_TRUE(model()->is_loaded()); 244 EXPECT_TRUE(model()->is_loaded());
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 294
286 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { 295 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) {
287 FailLoad(kTestPageUrl); 296 FailLoad(kTestPageUrl);
288 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 297 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
289 RunUntilIdle(); 298 RunUntilIdle();
290 EXPECT_TRUE(model()->is_loaded()); 299 EXPECT_TRUE(model()->is_loaded());
291 GetAllPages(); 300 GetAllPages();
292 EXPECT_EQ(0U, all_pages().size()); 301 EXPECT_EQ(0U, all_pages().size());
293 } 302 }
294 303
304 TEST_F(RecentTabHelperTest, FeatureNotEnabled) {
305 base::test::ScopedFeatureList scoped_feature_list;
306 scoped_feature_list.Init();
307 NavigateAndCommit(kTestPageUrl);
308 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
309 RunUntilIdle();
310 EXPECT_TRUE(model()->is_loaded());
311 GetAllPages();
312 // No page should be captured.
313 EXPECT_EQ(0U, all_pages().size());
314 }
315
295 } // namespace offline_pages 316 } // namespace offline_pages
OLDNEW
« no previous file with comments | « chrome/browser/android/offline_pages/recent_tab_helper.cc ('k') | chrome/browser/ui/tab_helpers.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698