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

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

Issue 2860573004: [Offline Pages] Add cached offline page utils and show usage in settings. (Closed)
Patch Set: fixed unit tests but CBD tests flaky. Created 3 years, 7 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_utils.h" 5 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file.h" 12 #include "base/files/file.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/strings/string16.h" 17 #include "base/strings/string16.h"
18 #include "base/test/scoped_feature_list.h" 18 #include "base/test/scoped_feature_list.h"
19 #include "base/test/simple_test_clock.h"
19 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/time/time.h" 21 #include "base/time/time.h"
21 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 22 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
22 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" 23 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
23 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" 24 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h"
24 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 25 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
25 #include "chrome/browser/android/offline_pages/test_request_coordinator_builder. h" 26 #include "chrome/browser/android/offline_pages/test_request_coordinator_builder. h"
26 #include "chrome/common/chrome_constants.h" 27 #include "chrome/common/chrome_constants.h"
27 #include "chrome/test/base/testing_profile.h" 28 #include "chrome/test/base/testing_profile.h"
28 #include "components/offline_pages/core/background/network_quality_provider_stub .h" 29 #include "components/offline_pages/core/background/network_quality_provider_stub .h"
29 #include "components/offline_pages/core/background/request_coordinator.h" 30 #include "components/offline_pages/core/background/request_coordinator.h"
30 #include "components/offline_pages/core/client_namespace_constants.h" 31 #include "components/offline_pages/core/client_namespace_constants.h"
31 #include "components/offline_pages/core/offline_page_feature.h" 32 #include "components/offline_pages/core/offline_page_feature.h"
32 #include "components/offline_pages/core/offline_page_model.h" 33 #include "components/offline_pages/core/offline_page_model.h"
34 #include "components/offline_pages/core/offline_page_model_impl.h"
33 #include "components/offline_pages/core/offline_page_test_archiver.h" 35 #include "components/offline_pages/core/offline_page_test_archiver.h"
34 #include "components/offline_pages/core/offline_page_test_store.h" 36 #include "components/offline_pages/core/offline_page_test_store.h"
35 #include "components/offline_pages/core/offline_page_types.h" 37 #include "components/offline_pages/core/offline_page_types.h"
36 #include "content/public/browser/web_contents.h" 38 #include "content/public/browser/web_contents.h"
37 #include "content/public/test/test_browser_thread_bundle.h" 39 #include "content/public/test/test_browser_thread_bundle.h"
38 #include "net/base/filename_util.h" 40 #include "net/base/filename_util.h"
39 #include "testing/gtest/include/gtest/gtest.h" 41 #include "testing/gtest/include/gtest/gtest.h"
40 #include "url/gurl.h" 42 #include "url/gurl.h"
41 43
42 namespace offline_pages { 44 namespace offline_pages {
43 namespace { 45 namespace {
44 46
45 const GURL kTestPage1Url("http://test.org/page1"); 47 const GURL kTestPage1Url("http://test.org/page1");
46 const GURL kTestPage2Url("http://test.org/page2"); 48 const GURL kTestPage2Url("http://test.org/page2");
47 const GURL kTestPage3Url("http://test.org/page3"); 49 const GURL kTestPage3Url("http://test.org/page3");
48 const GURL kTestPage4Url("http://test.org/page4"); 50 const GURL kTestPage4Url("http://test.org/page4");
49 const int64_t kTestFileSize = 876543LL; 51 const int64_t kTestFileSize = 876543LL;
50 const char* kTestPage1ClientId = "1234"; 52 const char* kTestPage1ClientId = "1234";
51 const char* kTestPage2ClientId = "5678"; 53 const char* kTestPage2ClientId = "5678";
52 const char* kTestPage3ClientId = "7890"; 54 const char* kTestPage3ClientId = "7890";
55 const char* kTestPage4ClientId = "42";
53 56
54 void CheckDuplicateDownloadsCallback( 57 void CheckDuplicateDownloadsCallback(
55 OfflinePageUtils::DuplicateCheckResult* out_result, 58 OfflinePageUtils::DuplicateCheckResult* out_result,
56 OfflinePageUtils::DuplicateCheckResult result) { 59 OfflinePageUtils::DuplicateCheckResult result) {
57 DCHECK(out_result); 60 DCHECK(out_result);
58 *out_result = result; 61 *out_result = result;
59 } 62 }
60 63
61 void GetAllRequestsCallback( 64 void GetAllRequestsCallback(
62 std::vector<std::unique_ptr<SavePageRequest>>* out_requests, 65 std::vector<std::unique_ptr<SavePageRequest>>* out_requests,
(...skipping 20 matching lines...) Expand all
83 86
84 // Return number of matches found. 87 // Return number of matches found.
85 int FindRequestByNamespaceAndURL(const std::string& name_space, 88 int FindRequestByNamespaceAndURL(const std::string& name_space,
86 const GURL& url); 89 const GURL& url);
87 90
88 // Necessary callbacks for the offline page model. 91 // Necessary callbacks for the offline page model.
89 void OnSavePageDone(SavePageResult result, int64_t offlineId); 92 void OnSavePageDone(SavePageResult result, int64_t offlineId);
90 void OnClearAllDone(); 93 void OnClearAllDone();
91 void OnExpirePageDone(bool success); 94 void OnExpirePageDone(bool success);
92 void OnGetURLDone(const GURL& url); 95 void OnGetURLDone(const GURL& url);
96 void OnSizeInBytesCalculated(int64_t size);
93 97
94 // OfflinePageTestArchiver::Observer implementation: 98 // OfflinePageTestArchiver::Observer implementation:
95 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; 99 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override;
96 100
97 TestingProfile* profile() { return &profile_; } 101 TestingProfile* profile() { return &profile_; }
98 content::WebContents* web_contents() const { return web_contents_.get(); } 102 content::WebContents* web_contents() const { return web_contents_.get(); }
99 103
100 int64_t offline_id() const { return offline_id_; } 104 int64_t offline_id() const { return offline_id_; }
105 int64_t last_cache_size() { return last_cache_size_; }
106
107 void CreateCachedOfflinePages(base::SimpleTestClock* clock);
101 108
102 private: 109 private:
103 void CreateOfflinePages(); 110 void CreateOfflinePages();
104 void CreateRequests(); 111 void CreateRequests();
105 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( 112 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
106 const GURL& url, 113 const GURL& url,
107 const base::FilePath& file_name); 114 const base::FilePath& file_name);
108 115
109 content::TestBrowserThreadBundle browser_thread_bundle_; 116 content::TestBrowserThreadBundle browser_thread_bundle_;
110 int64_t offline_id_; 117 int64_t offline_id_;
111 GURL url_; 118 GURL url_;
112 TestingProfile profile_; 119 TestingProfile profile_;
113 std::unique_ptr<content::WebContents> web_contents_; 120 std::unique_ptr<content::WebContents> web_contents_;
114 base::test::ScopedFeatureList scoped_feature_list_; 121 base::test::ScopedFeatureList scoped_feature_list_;
122 int64_t last_cache_size_;
115 }; 123 };
116 124
117 OfflinePageUtilsTest::OfflinePageUtilsTest() = default; 125 OfflinePageUtilsTest::OfflinePageUtilsTest() = default;
118 126
119 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} 127 OfflinePageUtilsTest::~OfflinePageUtilsTest() {}
120 128
121 void OfflinePageUtilsTest::SetUp() { 129 void OfflinePageUtilsTest::SetUp() {
122 // Enables offline pages feature. 130 // Enables offline pages feature.
123 // TODO(jianli): Remove this once the feature is completely enabled. 131 // TODO(jianli): Remove this once the feature is completely enabled.
124 scoped_feature_list_.InitAndEnableFeature( 132 scoped_feature_list_.InitAndEnableFeature(
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 } 181 }
174 182
175 void OfflinePageUtilsTest::OnClearAllDone() { 183 void OfflinePageUtilsTest::OnClearAllDone() {
176 // Result ignored here. 184 // Result ignored here.
177 } 185 }
178 186
179 void OfflinePageUtilsTest::OnGetURLDone(const GURL& url) { 187 void OfflinePageUtilsTest::OnGetURLDone(const GURL& url) {
180 url_ = url; 188 url_ = url;
181 } 189 }
182 190
191 void OfflinePageUtilsTest::OnSizeInBytesCalculated(int64_t size) {
192 last_cache_size_ = size;
193 }
194
183 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( 195 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver(
184 const base::FilePath& file_path) {} 196 const base::FilePath& file_path) {}
185 197
186 void OfflinePageUtilsTest::CreateOfflinePages() { 198 void OfflinePageUtilsTest::CreateOfflinePages() {
187 // Create page 1. 199 // Create page 1.
188 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( 200 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(
189 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); 201 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml"))));
190 offline_pages::ClientId client_id; 202 offline_pages::ClientId client_id;
191 client_id.name_space = kDownloadNamespace; 203 client_id.name_space = kDownloadNamespace;
192 client_id.id = kTestPage1ClientId; 204 client_id.id = kTestPage1ClientId;
(...skipping 11 matching lines...) Expand all
204 RequestCoordinatorFactory::GetForBrowserContext(profile()); 216 RequestCoordinatorFactory::GetForBrowserContext(profile());
205 217
206 RequestCoordinator::SavePageLaterParams params; 218 RequestCoordinator::SavePageLaterParams params;
207 params.url = kTestPage3Url; 219 params.url = kTestPage3Url;
208 params.client_id = 220 params.client_id =
209 offline_pages::ClientId(kDownloadNamespace, kTestPage3ClientId); 221 offline_pages::ClientId(kDownloadNamespace, kTestPage3ClientId);
210 request_coordinator->SavePageLater(params); 222 request_coordinator->SavePageLater(params);
211 RunUntilIdle(); 223 RunUntilIdle();
212 } 224 }
213 225
226 void OfflinePageUtilsTest::CreateCachedOfflinePages(
227 base::SimpleTestClock* clock) {
228 // Add 4 temporary pages to the model used for test cases. And setting current
229 // time as the 00:00:00 time anchor.
230 offline_pages::ClientId client_id;
231 client_id.name_space = kBookmarkNamespace;
232
233 clock->SetNow(base::Time::Now());
234 // Time 01:00:00.
235 clock->Advance(base::TimeDelta::FromHours(1));
236 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(
237 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml"))));
238 client_id.id = kTestPage1ClientId;
239 SavePage(kTestPage1Url, client_id, std::move(archiver));
240 // time 02:00:00.
241 clock->Advance(base::TimeDelta::FromHours(1));
242 archiver = BuildArchiver(kTestPage2Url,
243 base::FilePath(FILE_PATH_LITERAL("page2.mhtml")));
244 client_id.id = kTestPage2ClientId;
245 SavePage(kTestPage2Url, client_id, std::move(archiver));
246 // time 03:00:00.
247 clock->Advance(base::TimeDelta::FromHours(1));
248 archiver = BuildArchiver(kTestPage3Url,
249 base::FilePath(FILE_PATH_LITERAL("page3.mhtml")));
250 client_id.id = kTestPage3ClientId;
251 SavePage(kTestPage3Url, client_id, std::move(archiver));
252 // Add a temporary page to test boundary at 10:00:00.
253 clock->Advance(base::TimeDelta::FromHours(7));
254 archiver = BuildArchiver(kTestPage4Url,
255 base::FilePath(FILE_PATH_LITERAL("page4.mhtml")));
256 client_id.id = kTestPage4ClientId;
257 SavePage(kTestPage4Url, client_id, std::move(archiver));
258 // Reset clock->to 03:00:00.
259 clock->SetNow(clock->Now() - base::TimeDelta::FromHours(7));
260 }
261
214 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( 262 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver(
215 const GURL& url, 263 const GURL& url,
216 const base::FilePath& file_name) { 264 const base::FilePath& file_name) {
217 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( 265 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver(
218 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, 266 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED,
219 base::string16(), kTestFileSize, base::ThreadTaskRunnerHandle::Get())); 267 base::string16(), kTestFileSize, base::ThreadTaskRunnerHandle::Get()));
220 archiver->set_filename(file_name); 268 archiver->set_filename(file_name);
221 return archiver; 269 return archiver;
222 } 270 }
223 271
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 EXPECT_TRUE(OfflinePageUtils::EqualsIgnoringFragment( 351 EXPECT_TRUE(OfflinePageUtils::EqualsIgnoringFragment(
304 GURL("http://example.com/"), GURL("http://example.com/#test"))); 352 GURL("http://example.com/"), GURL("http://example.com/#test")));
305 EXPECT_TRUE(OfflinePageUtils::EqualsIgnoringFragment( 353 EXPECT_TRUE(OfflinePageUtils::EqualsIgnoringFragment(
306 GURL("http://example.com/#test"), GURL("http://example.com/"))); 354 GURL("http://example.com/#test"), GURL("http://example.com/")));
307 EXPECT_TRUE(OfflinePageUtils::EqualsIgnoringFragment( 355 EXPECT_TRUE(OfflinePageUtils::EqualsIgnoringFragment(
308 GURL("http://example.com/#test"), GURL("http://example.com/#test2"))); 356 GURL("http://example.com/#test"), GURL("http://example.com/#test2")));
309 EXPECT_FALSE(OfflinePageUtils::EqualsIgnoringFragment( 357 EXPECT_FALSE(OfflinePageUtils::EqualsIgnoringFragment(
310 GURL("http://example.com/"), GURL("http://test.com/#test"))); 358 GURL("http://example.com/"), GURL("http://test.com/#test")));
311 } 359 }
312 360
361 TEST_F(OfflinePageUtilsTest, TestGetCachedOfflinePageSizeBetween) {
362 // Set a test clock before adding cached offline pages.
363 // The clock will be at 03:00:00 after adding pages.
364 OfflinePageModel* model =
365 OfflinePageModelFactory::GetForBrowserContext(profile());
366 base::SimpleTestClock clock;
367 static_cast<OfflinePageModelImpl*>(model)->set_testing_clock(&clock);
carlosk 2017/05/05 18:20:27 Can these 3 initialization steps above that are re
romax 2017/05/05 21:01:30 I thought about it and I prefer not to touch exist
carlosk 2017/05/05 21:30:04 As discussed offline this would not include page c
368 CreateCachedOfflinePages(&clock);
369
370 // Advance the clock so that we don't hit the time check boundary.
371 clock.Advance(base::TimeDelta::FromMinutes(5));
372
373 // Get the size of cached offline pages between 01:05:00 and 03:05:00.
374 bool ret = OfflinePageUtils::GetCachedOfflinePageSizeBetween(
375 profile(),
376 base::Bind(&OfflinePageUtilsTest::OnSizeInBytesCalculated, AsWeakPtr()),
377 clock.Now() - base::TimeDelta::FromHours(2), clock.Now());
378 RunUntilIdle();
379 EXPECT_TRUE(ret);
380 EXPECT_EQ(kTestFileSize * 2, last_cache_size());
381 }
382
383 TEST_F(OfflinePageUtilsTest, TestGetCachedOfflinePageSizeNoPageInModel) {
384 // Set a test clock before adding cached offline pages.
385 // The clock will be at 03:00:00 after adding pages.
carlosk 2017/05/05 18:20:27 This comment doesn't apply for this test and neith
romax 2017/05/05 21:01:30 Done.
386 OfflinePageModel* model =
387 OfflinePageModelFactory::GetForBrowserContext(profile());
388 base::SimpleTestClock clock;
389 static_cast<OfflinePageModelImpl*>(model)->set_testing_clock(&clock);
390
391 // Advance the clock so that we don't hit the time check boundary.
392 clock.Advance(base::TimeDelta::FromMinutes(5));
393
394 // Get the size of cached offline pages between 01:05:00 and 03:05:00.
395 bool ret = OfflinePageUtils::GetCachedOfflinePageSizeBetween(
396 profile(),
397 base::Bind(&OfflinePageUtilsTest::OnSizeInBytesCalculated, AsWeakPtr()),
398 clock.Now() - base::TimeDelta::FromHours(2), clock.Now());
399 RunUntilIdle();
400 EXPECT_TRUE(ret);
401 EXPECT_EQ(0, last_cache_size());
402 }
403
404 TEST_F(OfflinePageUtilsTest, TestGetCachedOfflinePageSizeNoPageInRange) {
405 // Set a test clock before adding cached offline pages.
406 // The clock will be at 03:00:00 after adding pages.
407 OfflinePageModel* model =
408 OfflinePageModelFactory::GetForBrowserContext(profile());
409 base::SimpleTestClock clock;
410 static_cast<OfflinePageModelImpl*>(model)->set_testing_clock(&clock);
411 CreateCachedOfflinePages(&clock);
412
413 // Advance the clock so that we don't hit the time check boundary.
414 clock.Advance(base::TimeDelta::FromMinutes(5));
415
416 // Get the size of cached offline pages between 03:04:00 and 03:05:00.
417 bool ret = OfflinePageUtils::GetCachedOfflinePageSizeBetween(
418 profile(),
419 base::Bind(&OfflinePageUtilsTest::OnSizeInBytesCalculated, AsWeakPtr()),
420 clock.Now() - base::TimeDelta::FromMinutes(1), clock.Now());
421 RunUntilIdle();
422 EXPECT_TRUE(ret);
423 EXPECT_EQ(0, last_cache_size());
424 }
425
426 TEST_F(OfflinePageUtilsTest, TestGetCachedOfflinePageSizeAllPagesInRange) {
427 // Set a test clock before adding cached offline pages.
428 // The clock will be at 03:00:00 after adding pages.
429 OfflinePageModel* model =
430 OfflinePageModelFactory::GetForBrowserContext(profile());
431 base::SimpleTestClock clock;
432 static_cast<OfflinePageModelImpl*>(model)->set_testing_clock(&clock);
433 CreateCachedOfflinePages(&clock);
434
435 // Advance the clock to 23:00:00.
436 clock.Advance(base::TimeDelta::FromHours(20));
437
438 // Get the size of cached offline pages between -01:00:00 and 23:00:00.
439 bool ret = OfflinePageUtils::GetCachedOfflinePageSizeBetween(
440 profile(),
441 base::Bind(&OfflinePageUtilsTest::OnSizeInBytesCalculated, AsWeakPtr()),
442 clock.Now() - base::TimeDelta::FromHours(24), clock.Now());
443 RunUntilIdle();
444 EXPECT_TRUE(ret);
445 EXPECT_EQ(kTestFileSize * 4, last_cache_size());
446 }
447
448 TEST_F(OfflinePageUtilsTest, TestGetCachedOfflinePageSizeAllPagesInvalidRange) {
449 // Set a test clock before adding cached offline pages.
450 // The clock will be at 03:00:00 after adding pages.
451 OfflinePageModel* model =
452 OfflinePageModelFactory::GetForBrowserContext(profile());
453 base::SimpleTestClock clock;
454 static_cast<OfflinePageModelImpl*>(model)->set_testing_clock(&clock);
455 CreateCachedOfflinePages(&clock);
456
457 // Advance the clock to 23:00:00.
458 clock.Advance(base::TimeDelta::FromHours(20));
459
460 // Get the size of cached offline pages between 23:00:00 and -01:00:00, which
461 // is an invalid range, the return value will be false and there will be no
462 // callback.
463 bool ret = OfflinePageUtils::GetCachedOfflinePageSizeBetween(
464 profile(),
465 base::Bind(&OfflinePageUtilsTest::OnSizeInBytesCalculated, AsWeakPtr()),
466 clock.Now(), clock.Now() - base::TimeDelta::FromHours(24));
467 RunUntilIdle();
468 EXPECT_FALSE(ret);
469 }
470
carlosk 2017/05/05 18:20:27 Another test to add would have period limits that
romax 2017/05/05 21:01:30 Done.
313 } // namespace offline_pages 471 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698