| OLD | NEW |
| 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/scoped_feature_list.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; } | 73 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; } |
| 74 | 74 |
| 75 OfflinePageModel* model() const { return model_; } | 75 OfflinePageModel* model() const { return model_; } |
| 76 | 76 |
| 77 const std::vector<OfflinePageItem>& all_pages() { return all_pages_; } | 77 const std::vector<OfflinePageItem>& all_pages() { return all_pages_; } |
| 78 | 78 |
| 79 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() { | 79 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() { |
| 80 return task_runner_; | 80 return task_runner_; |
| 81 } | 81 } |
| 82 | 82 |
| 83 size_t model_changed_count() { return model_changed_count_; } | 83 size_t page_added_count() { return page_added_count_; } |
| 84 size_t model_removed_count() { return model_removed_count_; } | 84 size_t model_removed_count() { return model_removed_count_; } |
| 85 | 85 |
| 86 // OfflinePageModel::Observer | 86 // OfflinePageModel::Observer |
| 87 void OfflinePageModelLoaded(OfflinePageModel* model) override { } | 87 void OfflinePageModelLoaded(OfflinePageModel* model) override { } |
| 88 void OfflinePageModelChanged(OfflinePageModel* model) override { | 88 void OfflinePageAdded(OfflinePageModel* model, |
| 89 model_changed_count_++; | 89 SavePageResult result, |
| 90 const OfflinePageItem* added) override { |
| 91 page_added_count_++; |
| 90 } | 92 } |
| 91 void OfflinePageDeleted(int64_t, const offline_pages::ClientId&) override { | 93 void OfflinePageDeleted(int64_t, const offline_pages::ClientId&) override { |
| 92 model_removed_count_++; | 94 model_removed_count_++; |
| 93 } | 95 } |
| 94 | 96 |
| 95 // OfflinePageTestArchiver::Observer | 97 // OfflinePageTestArchiver::Observer |
| 96 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override {} | 98 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override {} |
| 97 | 99 |
| 98 private: | 100 private: |
| 99 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result); | 101 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result); |
| 100 | 102 |
| 101 RecentTabHelper* recent_tab_helper_; // Owned by WebContents. | 103 RecentTabHelper* recent_tab_helper_; // Owned by WebContents. |
| 102 OfflinePageModel* model_; // Keyed service | 104 OfflinePageModel* model_; // Keyed service |
| 103 size_t model_changed_count_; | 105 size_t page_added_count_; |
| 104 size_t model_removed_count_; | 106 size_t model_removed_count_; |
| 105 std::vector<OfflinePageItem> all_pages_; | 107 std::vector<OfflinePageItem> all_pages_; |
| 106 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 108 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 107 base::test::ScopedFeatureList scoped_feature_list_; | 109 base::test::ScopedFeatureList scoped_feature_list_; |
| 108 | 110 |
| 109 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_; | 111 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_; |
| 110 | 112 |
| 111 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest); | 113 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest); |
| 112 }; | 114 }; |
| 113 | 115 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 138 } | 140 } |
| 139 // There is no expectations that tab_id is always present. | 141 // There is no expectations that tab_id is always present. |
| 140 bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) { | 142 bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) { |
| 141 *tab_id = tab_id_; | 143 *tab_id = tab_id_; |
| 142 return tab_id_result_; | 144 return tab_id_result_; |
| 143 } | 145 } |
| 144 | 146 |
| 145 RecentTabHelperTest::RecentTabHelperTest() | 147 RecentTabHelperTest::RecentTabHelperTest() |
| 146 : recent_tab_helper_(nullptr), | 148 : recent_tab_helper_(nullptr), |
| 147 model_(nullptr), | 149 model_(nullptr), |
| 148 model_changed_count_(0), | 150 page_added_count_(0), |
| 149 model_removed_count_(0), | 151 model_removed_count_(0), |
| 150 task_runner_(new base::TestMockTimeTaskRunner), | 152 task_runner_(new base::TestMockTimeTaskRunner), |
| 151 weak_ptr_factory_(this) { | 153 weak_ptr_factory_(this) {} |
| 152 } | |
| 153 | 154 |
| 154 void RecentTabHelperTest::SetUp() { | 155 void RecentTabHelperTest::SetUp() { |
| 155 content::RenderViewHostTestHarness::SetUp(); | 156 content::RenderViewHostTestHarness::SetUp(); |
| 156 | 157 |
| 157 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); | 158 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); |
| 158 // Sets up the factory for testing. | 159 // Sets up the factory for testing. |
| 159 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | 160 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 160 browser_context(), BuildTestOfflinePageModel); | 161 browser_context(), BuildTestOfflinePageModel); |
| 161 RunUntilIdle(); | 162 RunUntilIdle(); |
| 162 | 163 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 EXPECT_EQ(0U, all_pages().size()); | 236 EXPECT_EQ(0U, all_pages().size()); |
| 236 } | 237 } |
| 237 | 238 |
| 238 // Should end up with 1 page. | 239 // Should end up with 1 page. |
| 239 TEST_F(RecentTabHelperTest, TwoCapturesSameUrl) { | 240 TEST_F(RecentTabHelperTest, TwoCapturesSameUrl) { |
| 240 NavigateAndCommit(kTestPageUrl); | 241 NavigateAndCommit(kTestPageUrl); |
| 241 // Triggers snapshot after a time delay. | 242 // Triggers snapshot after a time delay. |
| 242 recent_tab_helper()->DocumentAvailableInMainFrame(); | 243 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 243 RunUntilIdle(); | 244 RunUntilIdle(); |
| 244 EXPECT_TRUE(model()->is_loaded()); | 245 EXPECT_TRUE(model()->is_loaded()); |
| 245 EXPECT_EQ(0U, model_changed_count()); | 246 EXPECT_EQ(0U, page_added_count()); |
| 246 // Move the snapshot controller's time forward so it gets past timeouts. | 247 // Move the snapshot controller's time forward so it gets past timeouts. |
| 247 FastForwardSnapshotController(); | 248 FastForwardSnapshotController(); |
| 248 RunUntilIdle(); | 249 RunUntilIdle(); |
| 249 EXPECT_EQ(1U, model_changed_count()); | 250 EXPECT_EQ(1U, page_added_count()); |
| 250 EXPECT_EQ(0U, model_removed_count()); | 251 EXPECT_EQ(0U, model_removed_count()); |
| 251 GetAllPages(); | 252 GetAllPages(); |
| 252 EXPECT_EQ(1U, all_pages().size()); | 253 EXPECT_EQ(1U, all_pages().size()); |
| 253 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 254 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 254 | 255 |
| 255 // Triggers snapshot after a time delay. | 256 // Triggers snapshot after a time delay. |
| 256 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 257 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 257 // Move the snapshot controller's time forward so it gets past timeouts. | 258 // Move the snapshot controller's time forward so it gets past timeouts. |
| 258 FastForwardSnapshotController(); | 259 FastForwardSnapshotController(); |
| 259 RunUntilIdle(); | 260 RunUntilIdle(); |
| 260 EXPECT_EQ(2U, model_changed_count()); | 261 EXPECT_EQ(2U, page_added_count()); |
| 261 EXPECT_EQ(1U, model_removed_count()); | 262 EXPECT_EQ(1U, model_removed_count()); |
| 262 // the same page should be simply overridden. | 263 // the same page should be simply overridden. |
| 263 GetAllPages(); | 264 GetAllPages(); |
| 264 EXPECT_EQ(1U, all_pages().size()); | 265 EXPECT_EQ(1U, all_pages().size()); |
| 265 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 266 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 266 } | 267 } |
| 267 | 268 |
| 268 // Should end up with 1 page. | 269 // Should end up with 1 page. |
| 269 TEST_F(RecentTabHelperTest, TwoCapturesDifferentUrls) { | 270 TEST_F(RecentTabHelperTest, TwoCapturesDifferentUrls) { |
| 270 NavigateAndCommit(kTestPageUrl); | 271 NavigateAndCommit(kTestPageUrl); |
| 271 // Triggers snapshot after a time delay. | 272 // Triggers snapshot after a time delay. |
| 272 recent_tab_helper()->DocumentAvailableInMainFrame(); | 273 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 273 RunUntilIdle(); | 274 RunUntilIdle(); |
| 274 EXPECT_TRUE(model()->is_loaded()); | 275 EXPECT_TRUE(model()->is_loaded()); |
| 275 EXPECT_EQ(0U, model_changed_count()); | 276 EXPECT_EQ(0U, page_added_count()); |
| 276 // Move the snapshot controller's time forward so it gets past timeouts. | 277 // Move the snapshot controller's time forward so it gets past timeouts. |
| 277 FastForwardSnapshotController(); | 278 FastForwardSnapshotController(); |
| 278 RunUntilIdle(); | 279 RunUntilIdle(); |
| 279 EXPECT_EQ(1U, model_changed_count()); | 280 EXPECT_EQ(1U, page_added_count()); |
| 280 EXPECT_EQ(0U, model_removed_count()); | 281 EXPECT_EQ(0U, model_removed_count()); |
| 281 GetAllPages(); | 282 GetAllPages(); |
| 282 EXPECT_EQ(1U, all_pages().size()); | 283 EXPECT_EQ(1U, all_pages().size()); |
| 283 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 284 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 284 | 285 |
| 285 NavigateAndCommit(kTestPageUrlOther); | 286 NavigateAndCommit(kTestPageUrlOther); |
| 286 // Triggers snapshot after a time delay. | 287 // Triggers snapshot after a time delay. |
| 287 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 288 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 288 // Move the snapshot controller's time forward so it gets past timeouts. | 289 // Move the snapshot controller's time forward so it gets past timeouts. |
| 289 FastForwardSnapshotController(); | 290 FastForwardSnapshotController(); |
| 290 RunUntilIdle(); | 291 RunUntilIdle(); |
| 291 EXPECT_EQ(2U, model_changed_count()); | 292 EXPECT_EQ(2U, page_added_count()); |
| 292 EXPECT_EQ(1U, model_removed_count()); | 293 EXPECT_EQ(1U, model_removed_count()); |
| 293 // the same page should be simply overridden. | 294 // the same page should be simply overridden. |
| 294 GetAllPages(); | 295 GetAllPages(); |
| 295 EXPECT_EQ(1U, all_pages().size()); | 296 EXPECT_EQ(1U, all_pages().size()); |
| 296 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url); | 297 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url); |
| 297 } | 298 } |
| 298 | 299 |
| 299 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { | 300 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { |
| 300 FailLoad(kTestPageUrl); | 301 FailLoad(kTestPageUrl); |
| 301 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 302 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 329 GetAllPages(); | 330 GetAllPages(); |
| 330 EXPECT_EQ(1U, all_pages().size()); | 331 EXPECT_EQ(1U, all_pages().size()); |
| 331 const OfflinePageItem& page = all_pages()[0]; | 332 const OfflinePageItem& page = all_pages()[0]; |
| 332 EXPECT_EQ(kTestPageUrl, page.url); | 333 EXPECT_EQ(kTestPageUrl, page.url); |
| 333 EXPECT_EQ("download", page.client_id.name_space); | 334 EXPECT_EQ("download", page.client_id.name_space); |
| 334 EXPECT_EQ("id1", page.client_id.id); | 335 EXPECT_EQ("id1", page.client_id.id); |
| 335 EXPECT_EQ(153l, page.offline_id); | 336 EXPECT_EQ(153l, page.offline_id); |
| 336 } | 337 } |
| 337 | 338 |
| 338 } // namespace offline_pages | 339 } // namespace offline_pages |
| OLD | NEW |