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

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

Issue 2657013002: Introduce ThreadTaskRunnerHandle::OverrideForTesting and TestMockTimeTaskRunner::ScopedContext. (Closed)
Patch Set: fix RecentTabHelperTest crash? Created 3 years, 9 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 <memory>
8
7 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
8 #include "base/run_loop.h"
9 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
10 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
11 #include "base/test/scoped_feature_list.h" 12 #include "base/test/scoped_feature_list.h"
12 #include "base/test/test_mock_time_task_runner.h" 13 #include "base/test/scoped_mock_time_message_loop_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
15 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" 16 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h"
16 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 17 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
17 #include "chrome/browser/android/offline_pages/test_request_coordinator_builder. h" 18 #include "chrome/browser/android/offline_pages/test_request_coordinator_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/core/client_namespace_constants.h" 20 #include "components/offline_pages/core/client_namespace_constants.h"
20 #include "components/offline_pages/core/offline_page_feature.h" 21 #include "components/offline_pages/core/offline_page_feature.h"
21 #include "components/offline_pages/core/offline_page_item.h" 22 #include "components/offline_pages/core/offline_page_item.h"
22 #include "components/offline_pages/core/offline_page_model.h" 23 #include "components/offline_pages/core/offline_page_model.h"
(...skipping 13 matching lines...) Expand all
36 const GURL kTestPageUrlOther("http://crazy.site/foo_other.html"); 37 const GURL kTestPageUrlOther("http://crazy.site/foo_other.html");
37 const int kTabId = 153; 38 const int kTabId = 153;
38 } // namespace 39 } // namespace
39 40
40 class TestDelegate: public RecentTabHelper::Delegate { 41 class TestDelegate: public RecentTabHelper::Delegate {
41 public: 42 public:
42 const size_t kArchiveSizeToReport = 1234; 43 const size_t kArchiveSizeToReport = 1234;
43 44
44 explicit TestDelegate( 45 explicit TestDelegate(
45 OfflinePageTestArchiver::Observer* observer, 46 OfflinePageTestArchiver::Observer* observer,
46 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
47 int tab_id, 47 int tab_id,
48 bool tab_id_result); 48 bool tab_id_result);
49 ~TestDelegate() override {} 49 ~TestDelegate() override {}
50 50
51 std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( 51 std::unique_ptr<OfflinePageArchiver> CreatePageArchiver(
52 content::WebContents* web_contents) override; 52 content::WebContents* web_contents) override;
53 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override;
54 // There is no expectations that tab_id is always present. 53 // There is no expectations that tab_id is always present.
55 bool GetTabId(content::WebContents* web_contents, int* tab_id) override; 54 bool GetTabId(content::WebContents* web_contents, int* tab_id) override;
56 bool IsLowEndDevice() override { return is_low_end_device_; } 55 bool IsLowEndDevice() override { return is_low_end_device_; }
57 56
58 void set_archive_result( 57 void set_archive_result(
59 offline_pages::OfflinePageArchiver::ArchiverResult result) { 58 offline_pages::OfflinePageArchiver::ArchiverResult result) {
60 archive_result_ = result; 59 archive_result_ = result;
61 } 60 }
62 61
63 void set_archive_size(int64_t size) { archive_size_ = size; } 62 void set_archive_size(int64_t size) { archive_size_ = size; }
64 63
65 void SetAsLowEndDevice() { is_low_end_device_ = true; } 64 void SetAsLowEndDevice() { is_low_end_device_ = true; }
66 65
67 private: 66 private:
68 OfflinePageTestArchiver::Observer* observer_; // observer owns this. 67 OfflinePageTestArchiver::Observer* observer_; // observer owns this.
69 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
70 int tab_id_; 68 int tab_id_;
71 bool tab_id_result_; 69 bool tab_id_result_;
72 70
73 // These values can be updated so that new OfflinePageTestArchiver instances 71 // These values can be updated so that new OfflinePageTestArchiver instances
74 // will return different results. 72 // will return different results.
75 offline_pages::OfflinePageArchiver::ArchiverResult archive_result_ = 73 offline_pages::OfflinePageArchiver::ArchiverResult archive_result_ =
76 offline_pages::OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED; 74 offline_pages::OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED;
77 int64_t archive_size_ = kArchiveSizeToReport; 75 int64_t archive_size_ = kArchiveSizeToReport;
78 bool is_low_end_device_ = false; 76 bool is_low_end_device_ = false;
79 }; 77 };
80 78
81 class RecentTabHelperTest 79 class RecentTabHelperTest
82 : public ChromeRenderViewHostTestHarness, 80 : public ChromeRenderViewHostTestHarness,
83 public OfflinePageModel::Observer, 81 public OfflinePageModel::Observer,
84 public OfflinePageTestArchiver::Observer { 82 public OfflinePageTestArchiver::Observer {
85 public: 83 public:
86 RecentTabHelperTest(); 84 RecentTabHelperTest();
87 ~RecentTabHelperTest() override {} 85 ~RecentTabHelperTest() override {}
88 86
89 void SetUp() override; 87 void SetUp() override;
88 void TearDown() override;
90 const std::vector<OfflinePageItem>& GetAllPages(); 89 const std::vector<OfflinePageItem>& GetAllPages();
91 90
92 void FailLoad(const GURL& url); 91 void FailLoad(const GURL& url);
93 92
94 // Runs default thread. 93 // Runs main thread.
95 void RunUntilIdle(); 94 void RunUntilIdle();
96 // Moves forward the snapshot controller's task runner. 95 // Advances main thread time to trigger the snapshot controller's timeouts.
97 void FastForwardSnapshotController(); 96 void FastForwardSnapshotController();
98 97
99 // Navigates to the URL and commit as if it has been typed in the address bar. 98 // Navigates to the URL and commit as if it has been typed in the address bar.
100 // Note: we need this to simulate navigations to the same URL that more like a 99 // Note: we need this to simulate navigations to the same URL that more like a
101 // reload and not same page. NavigateAndCommit simulates a click on a link 100 // reload and not same page. NavigateAndCommit simulates a click on a link
102 // and when reusing the same URL that will be considered a same page 101 // and when reusing the same URL that will be considered a same page
103 // navigation. 102 // navigation.
104 void NavigateAndCommitTyped(const GURL& url); 103 void NavigateAndCommitTyped(const GURL& url);
105 104
106 ClientId NewDownloadClientId(); 105 ClientId NewDownloadClientId();
107 106
108 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; } 107 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; }
109 108
110 OfflinePageModel* model() const { return model_; } 109 OfflinePageModel* model() const { return model_; }
111 110
112 TestDelegate* default_test_delegate() { return default_test_delegate_; } 111 TestDelegate* default_test_delegate() { return default_test_delegate_; }
113 112
114 // Returns a OfflinePageItem pointer from |all_pages| that matches the 113 // Returns a OfflinePageItem pointer from |all_pages| that matches the
115 // provided |offline_id|. If a match is not found returns nullptr. 114 // provided |offline_id|. If a match is not found returns nullptr.
116 const OfflinePageItem* FindPageForOfflineId(int64_t offline_id) { 115 const OfflinePageItem* FindPageForOfflineId(int64_t offline_id) {
117 for (const OfflinePageItem& page : GetAllPages()) { 116 for (const OfflinePageItem& page : GetAllPages()) {
118 if (page.offline_id == offline_id) 117 if (page.offline_id == offline_id)
119 return &page; 118 return &page;
120 } 119 }
121 return nullptr; 120 return nullptr;
122 } 121 }
123 122
124 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() {
125 return task_runner_;
126 }
127
128 size_t page_added_count() { return page_added_count_; } 123 size_t page_added_count() { return page_added_count_; }
129 size_t model_removed_count() { return model_removed_count_; } 124 size_t model_removed_count() { return model_removed_count_; }
130 125
131 // OfflinePageModel::Observer 126 // OfflinePageModel::Observer
132 void OfflinePageModelLoaded(OfflinePageModel* model) override { 127 void OfflinePageModelLoaded(OfflinePageModel* model) override {
133 all_pages_needs_updating_ = true; 128 all_pages_needs_updating_ = true;
134 } 129 }
135 void OfflinePageAdded(OfflinePageModel* model, 130 void OfflinePageAdded(OfflinePageModel* model,
136 const OfflinePageItem& added_page) override { 131 const OfflinePageItem& added_page) override {
137 page_added_count_++; 132 page_added_count_++;
(...skipping 10 matching lines...) Expand all
148 private: 143 private:
149 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result); 144 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result);
150 145
151 RecentTabHelper* recent_tab_helper_; // Owned by WebContents. 146 RecentTabHelper* recent_tab_helper_; // Owned by WebContents.
152 OfflinePageModel* model_; // Keyed service. 147 OfflinePageModel* model_; // Keyed service.
153 TestDelegate* default_test_delegate_; // Created at SetUp. 148 TestDelegate* default_test_delegate_; // Created at SetUp.
154 size_t page_added_count_; 149 size_t page_added_count_;
155 size_t model_removed_count_; 150 size_t model_removed_count_;
156 std::vector<OfflinePageItem> all_pages_; 151 std::vector<OfflinePageItem> all_pages_;
157 bool all_pages_needs_updating_; 152 bool all_pages_needs_updating_;
158 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
159 base::test::ScopedFeatureList scoped_feature_list_; 153 base::test::ScopedFeatureList scoped_feature_list_;
160 154
155 // Mocks the RenderViewHostTestHarness' main thread runner. Needs to be delay
156 // initialized in SetUp() -- can't be a simple member -- since
157 // RenderViewHostTestHarness only initializes its main thread environment in
158 // its SetUp() :(.
159 std::unique_ptr<base::ScopedMockTimeMessageLoopTaskRunner>
160 mocked_main_runner_;
161
161 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_; 162 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_;
162 163
163 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest); 164 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest);
164 }; 165 };
165 166
166 TestDelegate::TestDelegate( 167 TestDelegate::TestDelegate(
167 OfflinePageTestArchiver::Observer* observer, 168 OfflinePageTestArchiver::Observer* observer,
168 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
169 int tab_id, 169 int tab_id,
170 bool tab_id_result) 170 bool tab_id_result)
171 : observer_(observer), 171 : observer_(observer),
172 task_runner_(task_runner),
173 tab_id_(tab_id), 172 tab_id_(tab_id),
174 tab_id_result_(tab_id_result) { 173 tab_id_result_(tab_id_result) {
175 } 174 }
176 175
177 std::unique_ptr<OfflinePageArchiver> TestDelegate::CreatePageArchiver( 176 std::unique_ptr<OfflinePageArchiver> TestDelegate::CreatePageArchiver(
178 content::WebContents* web_contents) { 177 content::WebContents* web_contents) {
179 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( 178 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver(
180 observer_, web_contents->GetLastCommittedURL(), archive_result_, 179 observer_, web_contents->GetLastCommittedURL(), archive_result_,
181 base::string16(), kArchiveSizeToReport, 180 base::string16(), kArchiveSizeToReport,
182 base::ThreadTaskRunnerHandle::Get())); 181 base::ThreadTaskRunnerHandle::Get()));
183 return std::move(archiver); 182 return std::move(archiver);
184 } 183 }
185 184
186 scoped_refptr<base::SingleThreadTaskRunner> TestDelegate::GetTaskRunner() { 185 // There is no expectations that tab_id is always present.
187 return task_runner_;
188 }
189 // There is no expectations that tab_id is always present.
190 bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) { 186 bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) {
191 *tab_id = tab_id_; 187 *tab_id = tab_id_;
192 return tab_id_result_; 188 return tab_id_result_;
193 } 189 }
194 190
195 RecentTabHelperTest::RecentTabHelperTest() 191 RecentTabHelperTest::RecentTabHelperTest()
196 : recent_tab_helper_(nullptr), 192 : recent_tab_helper_(nullptr),
197 model_(nullptr), 193 model_(nullptr),
198 default_test_delegate_(nullptr), 194 default_test_delegate_(nullptr),
199 page_added_count_(0), 195 page_added_count_(0),
200 model_removed_count_(0), 196 model_removed_count_(0),
201 all_pages_needs_updating_(true), 197 all_pages_needs_updating_(true),
202 task_runner_(new base::TestMockTimeTaskRunner),
203 weak_ptr_factory_(this) {} 198 weak_ptr_factory_(this) {}
204 199
205 void RecentTabHelperTest::SetUp() { 200 void RecentTabHelperTest::SetUp() {
206 ChromeRenderViewHostTestHarness::SetUp(); 201 ChromeRenderViewHostTestHarness::SetUp();
207 202
203 mocked_main_runner_ =
204 base::MakeUnique<base::ScopedMockTimeMessageLoopTaskRunner>();
205
208 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); 206 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature);
209 // Sets up the factories for testing. 207 // Sets up the factories for testing.
210 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( 208 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
211 browser_context(), BuildTestOfflinePageModel); 209 browser_context(), BuildTestOfflinePageModel);
212 RunUntilIdle(); 210 RunUntilIdle();
213 RequestCoordinatorFactory::GetInstance()->SetTestingFactoryAndUse( 211 RequestCoordinatorFactory::GetInstance()->SetTestingFactoryAndUse(
214 browser_context(), BuildTestRequestCoordinator); 212 browser_context(), BuildTestRequestCoordinator);
215 RunUntilIdle(); 213 RunUntilIdle();
216 214
217 RecentTabHelper::CreateForWebContents(web_contents()); 215 RecentTabHelper::CreateForWebContents(web_contents());
218 recent_tab_helper_ = RecentTabHelper::FromWebContents(web_contents()); 216 recent_tab_helper_ = RecentTabHelper::FromWebContents(web_contents());
219 217
220 std::unique_ptr<TestDelegate> test_delegate( 218 std::unique_ptr<TestDelegate> test_delegate(
221 new TestDelegate(this, task_runner(), kTabId, true)); 219 new TestDelegate(this, kTabId, true));
222 default_test_delegate_ = test_delegate.get(); 220 default_test_delegate_ = test_delegate.get();
223 recent_tab_helper_->SetDelegate(std::move(test_delegate)); 221 recent_tab_helper_->SetDelegate(std::move(test_delegate));
224 222
225 model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context()); 223 model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context());
226 model_->AddObserver(this); 224 model_->AddObserver(this);
227 } 225 }
228 226
227 void RecentTabHelperTest::TearDown() {
228 mocked_main_runner_.reset();
229 ChromeRenderViewHostTestHarness::TearDown();
230 }
231
229 void RecentTabHelperTest::FailLoad(const GURL& url) { 232 void RecentTabHelperTest::FailLoad(const GURL& url) {
230 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, 233 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED,
231 std::string()); 234 std::string());
232 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); 235 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url);
233 content::RenderFrameHostTester::For(main_rfh())-> 236 content::RenderFrameHostTester::For(main_rfh())->
234 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED); 237 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED);
235 content::RenderFrameHostTester::For(main_rfh())-> 238 content::RenderFrameHostTester::For(main_rfh())->
236 SimulateNavigationErrorPageCommit(); 239 SimulateNavigationErrorPageCommit();
237 } 240 }
238 241
239 const std::vector<OfflinePageItem>& RecentTabHelperTest::GetAllPages() { 242 const std::vector<OfflinePageItem>& RecentTabHelperTest::GetAllPages() {
240 if (all_pages_needs_updating_) { 243 if (all_pages_needs_updating_) {
241 model()->GetAllPages(base::Bind(&RecentTabHelperTest::OnGetAllPagesDone, 244 model()->GetAllPages(base::Bind(&RecentTabHelperTest::OnGetAllPagesDone,
242 weak_ptr_factory_.GetWeakPtr())); 245 weak_ptr_factory_.GetWeakPtr()));
243 RunUntilIdle(); 246 RunUntilIdle();
244 all_pages_needs_updating_ = false; 247 all_pages_needs_updating_ = false;
245 } 248 }
246 return all_pages_; 249 return all_pages_;
247 } 250 }
248 251
249 void RecentTabHelperTest::OnGetAllPagesDone( 252 void RecentTabHelperTest::OnGetAllPagesDone(
250 const std::vector<OfflinePageItem>& result) { 253 const std::vector<OfflinePageItem>& result) {
251 all_pages_ = result; 254 all_pages_ = result;
252 } 255 }
253 256
254 void RecentTabHelperTest::RunUntilIdle() { 257 void RecentTabHelperTest::RunUntilIdle() {
255 base::RunLoop().RunUntilIdle(); 258 (*mocked_main_runner_)->RunUntilIdle();
256 } 259 }
257 260
258 void RecentTabHelperTest::FastForwardSnapshotController() { 261 void RecentTabHelperTest::FastForwardSnapshotController() {
259 const size_t kLongDelayMs = 100*1000; 262 constexpr base::TimeDelta kLongDelay = base::TimeDelta::FromSeconds(100);
260 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(kLongDelayMs)); 263 (*mocked_main_runner_)->FastForwardBy(kLongDelay);
261 } 264 }
262 265
263 void RecentTabHelperTest::NavigateAndCommitTyped(const GURL& url) { 266 void RecentTabHelperTest::NavigateAndCommitTyped(const GURL& url) {
264 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, 267 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED,
265 std::string()); 268 std::string());
266 content::WebContentsTester* web_contents_tester = 269 content::WebContentsTester* web_contents_tester =
267 content::WebContentsTester::For(web_contents()); 270 content::WebContentsTester::For(web_contents());
268 web_contents_tester->CommitPendingNavigation(); 271 web_contents_tester->CommitPendingNavigation();
269 } 272 }
270 273
(...skipping 11 matching lines...) Expand all
282 } 285 }
283 286
284 // Fully loads a page then simulates the tab being hidden. Verifies that a 287 // Fully loads a page then simulates the tab being hidden. Verifies that a
285 // snapshot is created only when the latter happens. 288 // snapshot is created only when the latter happens.
286 TEST_F(RecentTabHelperTest, LastNCaptureAfterLoad) { 289 TEST_F(RecentTabHelperTest, LastNCaptureAfterLoad) {
287 // Navigate and finish loading. Nothing should be saved. 290 // Navigate and finish loading. Nothing should be saved.
288 NavigateAndCommit(kTestPageUrl); 291 NavigateAndCommit(kTestPageUrl);
289 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 292 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
290 // Move the snapshot controller's time forward so it gets past timeouts. 293 // Move the snapshot controller's time forward so it gets past timeouts.
291 FastForwardSnapshotController(); 294 FastForwardSnapshotController();
292 RunUntilIdle();
293 EXPECT_TRUE(model()->is_loaded()); 295 EXPECT_TRUE(model()->is_loaded());
294 EXPECT_EQ(0U, page_added_count()); 296 EXPECT_EQ(0U, page_added_count());
295 ASSERT_EQ(0U, GetAllPages().size()); 297 ASSERT_EQ(0U, GetAllPages().size());
296 298
297 // Tab is hidden with a fully loaded page. A snapshot save should happen. 299 // Tab is hidden with a fully loaded page. A snapshot save should happen.
298 recent_tab_helper()->WasHidden(); 300 recent_tab_helper()->WasHidden();
299 RunUntilIdle(); 301 RunUntilIdle();
300 EXPECT_TRUE(model()->is_loaded()); 302 EXPECT_TRUE(model()->is_loaded());
301 EXPECT_EQ(1U, page_added_count()); 303 EXPECT_EQ(1U, page_added_count());
302 ASSERT_EQ(1U, GetAllPages().size()); 304 ASSERT_EQ(1U, GetAllPages().size());
303 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); 305 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url);
304 EXPECT_EQ(kLastNNamespace, GetAllPages()[0].client_id.name_space); 306 EXPECT_EQ(kLastNNamespace, GetAllPages()[0].client_id.name_space);
305 } 307 }
306 308
307 // Simulates the tab being hidden too early in the page loading so that a 309 // Simulates the tab being hidden too early in the page loading so that a
308 // snapshot should not be created. 310 // snapshot should not be created.
309 TEST_F(RecentTabHelperTest, NoLastNCaptureIfTabHiddenTooEarlyInPageLoad) { 311 TEST_F(RecentTabHelperTest, NoLastNCaptureIfTabHiddenTooEarlyInPageLoad) {
310 // Commit the navigation and hide the tab. Nothing should be saved. 312 // Commit the navigation and hide the tab. Nothing should be saved.
311 NavigateAndCommit(kTestPageUrl); 313 NavigateAndCommit(kTestPageUrl);
312 recent_tab_helper()->WasHidden(); 314 recent_tab_helper()->WasHidden();
313 RunUntilIdle(); 315 RunUntilIdle();
314 EXPECT_TRUE(model()->is_loaded()); 316 EXPECT_TRUE(model()->is_loaded());
315 EXPECT_EQ(0U, page_added_count()); 317 EXPECT_EQ(0U, page_added_count());
316 ASSERT_EQ(0U, GetAllPages().size()); 318 ASSERT_EQ(0U, GetAllPages().size());
317 319
318 // Then allow the page to fully load. Nothing should be saved. 320 // Then allow the page to fully load. Nothing should be saved.
319 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 321 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
320 // Move the snapshot controller's time forward so it gets past timeouts. 322 // Move the snapshot controller's time forward so it gets past timeouts.
321 FastForwardSnapshotController(); 323 FastForwardSnapshotController();
322 RunUntilIdle();
323 EXPECT_EQ(0U, page_added_count()); 324 EXPECT_EQ(0U, page_added_count());
324 ASSERT_EQ(0U, GetAllPages().size()); 325 ASSERT_EQ(0U, GetAllPages().size());
325 } 326 }
326 327
327 // Checks that WebContents with no tab IDs have snapshot requests properly 328 // Checks that WebContents with no tab IDs have snapshot requests properly
328 // ignored from both last_n and downloads. 329 // ignored from both last_n and downloads.
329 TEST_F(RecentTabHelperTest, NoTabIdNoCapture) { 330 TEST_F(RecentTabHelperTest, NoTabIdNoCapture) {
330 // Create delegate that returns 'false' as TabId retrieval result. 331 // Create delegate that returns 'false' as TabId retrieval result.
331 recent_tab_helper()->SetDelegate(base::MakeUnique<TestDelegate>( 332 recent_tab_helper()->SetDelegate(
332 this, task_runner(), kTabId, false)); 333 base::MakeUnique<TestDelegate>(this, kTabId, false));
333 334
334 NavigateAndCommit(kTestPageUrl); 335 NavigateAndCommit(kTestPageUrl);
335 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 336 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
336 FastForwardSnapshotController(); 337 FastForwardSnapshotController();
337 recent_tab_helper()->WasHidden(); 338 recent_tab_helper()->WasHidden();
338 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(), 339 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(),
339 123L); 340 123L);
340 RunUntilIdle(); 341 RunUntilIdle();
341 EXPECT_TRUE(model()->is_loaded()); 342 EXPECT_TRUE(model()->is_loaded());
342 // No page should be captured. 343 // No page should be captured.
(...skipping 25 matching lines...) Expand all
368 ASSERT_EQ(1U, GetAllPages().size()); 369 ASSERT_EQ(1U, GetAllPages().size());
369 } 370 }
370 371
371 // Triggers two last_n snapshot captures during a single page load. Should end 372 // Triggers two last_n snapshot captures during a single page load. Should end
372 // up with one snapshot, the 1st being replaced by the 2nd. 373 // up with one snapshot, the 1st being replaced by the 2nd.
373 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { 374 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) {
374 NavigateAndCommit(kTestPageUrl); 375 NavigateAndCommit(kTestPageUrl);
375 // Set page loading state to the 1st snapshot-able stage. No capture so far. 376 // Set page loading state to the 1st snapshot-able stage. No capture so far.
376 recent_tab_helper()->DocumentAvailableInMainFrame(); 377 recent_tab_helper()->DocumentAvailableInMainFrame();
377 FastForwardSnapshotController(); 378 FastForwardSnapshotController();
378 RunUntilIdle();
379 EXPECT_TRUE(model()->is_loaded()); 379 EXPECT_TRUE(model()->is_loaded());
380 EXPECT_EQ(0U, page_added_count()); 380 EXPECT_EQ(0U, page_added_count());
381 381
382 // Tab is hidden and a snapshot should be saved. 382 // Tab is hidden and a snapshot should be saved.
383 recent_tab_helper()->WasHidden(); 383 recent_tab_helper()->WasHidden();
384 RunUntilIdle(); 384 RunUntilIdle();
385 EXPECT_EQ(1U, page_added_count()); 385 EXPECT_EQ(1U, page_added_count());
386 EXPECT_EQ(0U, model_removed_count()); 386 EXPECT_EQ(0U, model_removed_count());
387 ASSERT_EQ(1U, GetAllPages().size()); 387 ASSERT_EQ(1U, GetAllPages().size());
388 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); 388 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url);
389 int64_t first_offline_id = GetAllPages()[0].offline_id; 389 int64_t first_offline_id = GetAllPages()[0].offline_id;
390 390
391 // Set page loading state to the 2nd and last snapshot-able stage. No new 391 // Set page loading state to the 2nd and last snapshot-able stage. No new
392 // capture should happen. 392 // capture should happen.
393 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 393 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
394 FastForwardSnapshotController(); 394 FastForwardSnapshotController();
395 RunUntilIdle();
396 EXPECT_EQ(1U, page_added_count()); 395 EXPECT_EQ(1U, page_added_count());
397 EXPECT_EQ(0U, model_removed_count()); 396 EXPECT_EQ(0U, model_removed_count());
398 ASSERT_EQ(1U, GetAllPages().size()); 397 ASSERT_EQ(1U, GetAllPages().size());
399 398
400 // Tab is hidden again. At this point a higher quality snapshot is expected so 399 // Tab is hidden again. At this point a higher quality snapshot is expected so
401 // a new one should be captured and replace the previous one. 400 // a new one should be captured and replace the previous one.
402 recent_tab_helper()->WasHidden(); 401 recent_tab_helper()->WasHidden();
403 RunUntilIdle(); 402 RunUntilIdle();
404 EXPECT_EQ(2U, page_added_count()); 403 EXPECT_EQ(2U, page_added_count());
405 EXPECT_EQ(1U, model_removed_count()); 404 EXPECT_EQ(1U, model_removed_count());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 EXPECT_EQ(0U, model_removed_count()); 457 EXPECT_EQ(0U, model_removed_count());
459 ASSERT_EQ(1U, GetAllPages().size()); 458 ASSERT_EQ(1U, GetAllPages().size());
460 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); 459 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url);
461 int64_t first_offline_id = GetAllPages()[0].offline_id; 460 int64_t first_offline_id = GetAllPages()[0].offline_id;
462 461
463 // Navigate with the same URL until the page is minimally loaded then hide the 462 // Navigate with the same URL until the page is minimally loaded then hide the
464 // tab. The previous snapshot should be removed and a new one taken. 463 // tab. The previous snapshot should be removed and a new one taken.
465 NavigateAndCommitTyped(kTestPageUrl); 464 NavigateAndCommitTyped(kTestPageUrl);
466 recent_tab_helper()->DocumentAvailableInMainFrame(); 465 recent_tab_helper()->DocumentAvailableInMainFrame();
467 FastForwardSnapshotController(); 466 FastForwardSnapshotController();
468 RunUntilIdle();
469 EXPECT_EQ(1U, page_added_count()); 467 EXPECT_EQ(1U, page_added_count());
470 EXPECT_EQ(0U, model_removed_count()); 468 EXPECT_EQ(0U, model_removed_count());
471 ASSERT_EQ(1U, GetAllPages().size()); 469 ASSERT_EQ(1U, GetAllPages().size());
472 470
473 recent_tab_helper()->WasHidden(); 471 recent_tab_helper()->WasHidden();
474 RunUntilIdle(); 472 RunUntilIdle();
475 EXPECT_EQ(2U, page_added_count()); 473 EXPECT_EQ(2U, page_added_count());
476 EXPECT_EQ(1U, model_removed_count()); 474 EXPECT_EQ(1U, model_removed_count());
477 ASSERT_EQ(1U, GetAllPages().size()); 475 ASSERT_EQ(1U, GetAllPages().size());
478 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); 476 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 EXPECT_EQ(1U, page_added_count()); 522 EXPECT_EQ(1U, page_added_count());
525 EXPECT_EQ(0U, model_removed_count()); 523 EXPECT_EQ(0U, model_removed_count());
526 ASSERT_EQ(1U, GetAllPages().size()); 524 ASSERT_EQ(1U, GetAllPages().size());
527 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); 525 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url);
528 526
529 // Fully load the second URL then hide the tab and check for a single snapshot 527 // Fully load the second URL then hide the tab and check for a single snapshot
530 // of the new page. 528 // of the new page.
531 NavigateAndCommitTyped(kTestPageUrlOther); 529 NavigateAndCommitTyped(kTestPageUrlOther);
532 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 530 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
533 FastForwardSnapshotController(); 531 FastForwardSnapshotController();
534 RunUntilIdle();
535 EXPECT_EQ(1U, page_added_count()); 532 EXPECT_EQ(1U, page_added_count());
536 EXPECT_EQ(0U, model_removed_count()); 533 EXPECT_EQ(0U, model_removed_count());
537 ASSERT_EQ(1U, GetAllPages().size()); 534 ASSERT_EQ(1U, GetAllPages().size());
538 535
539 recent_tab_helper()->WasHidden(); 536 recent_tab_helper()->WasHidden();
540 RunUntilIdle(); 537 RunUntilIdle();
541 EXPECT_EQ(2U, page_added_count()); 538 EXPECT_EQ(2U, page_added_count());
542 EXPECT_EQ(1U, model_removed_count()); 539 EXPECT_EQ(1U, model_removed_count());
543 ASSERT_EQ(1U, GetAllPages().size()); 540 ASSERT_EQ(1U, GetAllPages().size());
544 EXPECT_EQ(kTestPageUrlOther, GetAllPages()[0].url); 541 EXPECT_EQ(kTestPageUrlOther, GetAllPages()[0].url);
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 632
636 // Simulates a download request to offline the current page made early during 633 // Simulates a download request to offline the current page made early during
637 // loading. Should execute two captures but only the final one is kept. 634 // loading. Should execute two captures but only the final one is kept.
638 TEST_F(RecentTabHelperTest, DownloadRequestEarlyInLoad) { 635 TEST_F(RecentTabHelperTest, DownloadRequestEarlyInLoad) {
639 // Commit the navigation and request the snapshot from downloads. No captures 636 // Commit the navigation and request the snapshot from downloads. No captures
640 // so far. 637 // so far.
641 NavigateAndCommit(kTestPageUrl); 638 NavigateAndCommit(kTestPageUrl);
642 const ClientId client_id = NewDownloadClientId(); 639 const ClientId client_id = NewDownloadClientId();
643 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L); 640 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L);
644 FastForwardSnapshotController(); 641 FastForwardSnapshotController();
645 RunUntilIdle();
646 EXPECT_TRUE(model()->is_loaded()); 642 EXPECT_TRUE(model()->is_loaded());
647 ASSERT_EQ(0U, GetAllPages().size()); 643 ASSERT_EQ(0U, GetAllPages().size());
648 644
649 // Minimally load the page. First capture should occur. 645 // Minimally load the page. First capture should occur.
650 recent_tab_helper()->DocumentAvailableInMainFrame(); 646 recent_tab_helper()->DocumentAvailableInMainFrame();
651 FastForwardSnapshotController(); 647 FastForwardSnapshotController();
652 RunUntilIdle();
653 ASSERT_EQ(1U, GetAllPages().size()); 648 ASSERT_EQ(1U, GetAllPages().size());
654 const OfflinePageItem& early_page = GetAllPages()[0]; 649 const OfflinePageItem& early_page = GetAllPages()[0];
655 EXPECT_EQ(kTestPageUrl, early_page.url); 650 EXPECT_EQ(kTestPageUrl, early_page.url);
656 EXPECT_EQ(client_id, early_page.client_id); 651 EXPECT_EQ(client_id, early_page.client_id);
657 EXPECT_EQ(153L, early_page.offline_id); 652 EXPECT_EQ(153L, early_page.offline_id);
658 653
659 // Fully load the page. A second capture should replace the first one. 654 // Fully load the page. A second capture should replace the first one.
660 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 655 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
661 FastForwardSnapshotController(); 656 FastForwardSnapshotController();
662 RunUntilIdle();
663 EXPECT_EQ(2U, page_added_count()); 657 EXPECT_EQ(2U, page_added_count());
664 EXPECT_EQ(1U, model_removed_count()); 658 EXPECT_EQ(1U, model_removed_count());
665 ASSERT_EQ(1U, GetAllPages().size()); 659 ASSERT_EQ(1U, GetAllPages().size());
666 const OfflinePageItem& later_page = GetAllPages()[0]; 660 const OfflinePageItem& later_page = GetAllPages()[0];
667 EXPECT_EQ(kTestPageUrl, later_page.url); 661 EXPECT_EQ(kTestPageUrl, later_page.url);
668 EXPECT_EQ(client_id, later_page.client_id); 662 EXPECT_EQ(client_id, later_page.client_id);
669 EXPECT_EQ(153L, later_page.offline_id); 663 EXPECT_EQ(153L, later_page.offline_id);
670 } 664 }
671 665
672 // Simulates a download request to offline the current page made when the page 666 // Simulates a download request to offline the current page made when the page
673 // is minimally loaded. Should execute two captures but only the final one is 667 // is minimally loaded. Should execute two captures but only the final one is
674 // kept. 668 // kept.
675 TEST_F(RecentTabHelperTest, DownloadRequestLaterInLoad) { 669 TEST_F(RecentTabHelperTest, DownloadRequestLaterInLoad) {
676 NavigateAndCommit(kTestPageUrl); 670 NavigateAndCommit(kTestPageUrl);
677 recent_tab_helper()->DocumentAvailableInMainFrame(); 671 recent_tab_helper()->DocumentAvailableInMainFrame();
678 FastForwardSnapshotController(); 672 FastForwardSnapshotController();
679 RunUntilIdle();
680 EXPECT_TRUE(model()->is_loaded()); 673 EXPECT_TRUE(model()->is_loaded());
681 ASSERT_EQ(0U, GetAllPages().size()); 674 ASSERT_EQ(0U, GetAllPages().size());
682 675
683 const ClientId client_id = NewDownloadClientId(); 676 const ClientId client_id = NewDownloadClientId();
684 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L); 677 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L);
685 RunUntilIdle(); 678 RunUntilIdle();
686 ASSERT_EQ(1U, GetAllPages().size()); 679 ASSERT_EQ(1U, GetAllPages().size());
687 const OfflinePageItem& page = GetAllPages()[0]; 680 const OfflinePageItem& page = GetAllPages()[0];
688 EXPECT_EQ(kTestPageUrl, page.url); 681 EXPECT_EQ(kTestPageUrl, page.url);
689 EXPECT_EQ(client_id, page.client_id); 682 EXPECT_EQ(client_id, page.client_id);
690 EXPECT_EQ(153L, page.offline_id); 683 EXPECT_EQ(153L, page.offline_id);
691 684
692 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 685 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
693 FastForwardSnapshotController(); 686 FastForwardSnapshotController();
694 RunUntilIdle();
695 EXPECT_EQ(2U, page_added_count()); 687 EXPECT_EQ(2U, page_added_count());
696 EXPECT_EQ(1U, model_removed_count()); 688 EXPECT_EQ(1U, model_removed_count());
697 ASSERT_EQ(1U, GetAllPages().size()); 689 ASSERT_EQ(1U, GetAllPages().size());
698 } 690 }
699 691
700 // Simulates a download request to offline the current page made after loading 692 // Simulates a download request to offline the current page made after loading
701 // is completed. Should end up with one offline pages. 693 // is completed. Should end up with one offline pages.
702 TEST_F(RecentTabHelperTest, DownloadRequestAfterFullyLoad) { 694 TEST_F(RecentTabHelperTest, DownloadRequestAfterFullyLoad) {
703 NavigateAndCommit(kTestPageUrl); 695 NavigateAndCommit(kTestPageUrl);
704 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 696 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
705 FastForwardSnapshotController(); 697 FastForwardSnapshotController();
706 RunUntilIdle();
707 EXPECT_TRUE(model()->is_loaded()); 698 EXPECT_TRUE(model()->is_loaded());
708 ASSERT_EQ(0U, GetAllPages().size()); 699 ASSERT_EQ(0U, GetAllPages().size());
709 700
710 const ClientId client_id = NewDownloadClientId(); 701 const ClientId client_id = NewDownloadClientId();
711 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L); 702 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L);
712 RunUntilIdle(); 703 RunUntilIdle();
713 ASSERT_EQ(1U, GetAllPages().size()); 704 ASSERT_EQ(1U, GetAllPages().size());
714 const OfflinePageItem& page = GetAllPages()[0]; 705 const OfflinePageItem& page = GetAllPages()[0];
715 EXPECT_EQ(kTestPageUrl, page.url); 706 EXPECT_EQ(kTestPageUrl, page.url);
716 EXPECT_EQ(client_id, page.client_id); 707 EXPECT_EQ(client_id, page.client_id);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 NavigateAndCommit(kTestPageUrl); 777 NavigateAndCommit(kTestPageUrl);
787 const ClientId client_id_1 = NewDownloadClientId(); 778 const ClientId client_id_1 = NewDownloadClientId();
788 const int64_t offline_id_1 = 153L; 779 const int64_t offline_id_1 = 153L;
789 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id_1, offline_id_1); 780 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id_1, offline_id_1);
790 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(), 781 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(),
791 351L); 782 351L);
792 783
793 // Finish loading the page. Only the first request should be executed. 784 // Finish loading the page. Only the first request should be executed.
794 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 785 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
795 FastForwardSnapshotController(); 786 FastForwardSnapshotController();
796 RunUntilIdle();
797 EXPECT_EQ(1U, page_added_count()); 787 EXPECT_EQ(1U, page_added_count());
798 EXPECT_EQ(0U, model_removed_count()); 788 EXPECT_EQ(0U, model_removed_count());
799 ASSERT_EQ(1U, GetAllPages().size()); 789 ASSERT_EQ(1U, GetAllPages().size());
800 const OfflinePageItem& fist_page = GetAllPages()[0]; 790 const OfflinePageItem& fist_page = GetAllPages()[0];
801 EXPECT_EQ(client_id_1, fist_page.client_id); 791 EXPECT_EQ(client_id_1, fist_page.client_id);
802 EXPECT_EQ(offline_id_1, fist_page.offline_id); 792 EXPECT_EQ(offline_id_1, fist_page.offline_id);
803 793
804 // Make two additional download snapshot requests. Again only the first should 794 // Make two additional download snapshot requests. Again only the first should
805 // generate a snapshot. 795 // generate a snapshot.
806 const ClientId client_id_3 = NewDownloadClientId(); 796 const ClientId client_id_3 = NewDownloadClientId();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 871 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
882 FastForwardSnapshotController(); 872 FastForwardSnapshotController();
883 recent_tab_helper()->WasHidden(); 873 recent_tab_helper()->WasHidden();
884 RunUntilIdle(); 874 RunUntilIdle();
885 EXPECT_EQ(2U, page_added_count()); 875 EXPECT_EQ(2U, page_added_count());
886 EXPECT_EQ(1U, model_removed_count()); 876 EXPECT_EQ(1U, model_removed_count());
887 ASSERT_EQ(1U, GetAllPages().size()); 877 ASSERT_EQ(1U, GetAllPages().size());
888 } 878 }
889 879
890 } // namespace offline_pages 880 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698