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