| OLD | NEW |
| 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" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 namespace offline_pages { | 34 namespace offline_pages { |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 const GURL kTestPage1Url("http://test.org/page1"); | 37 const GURL kTestPage1Url("http://test.org/page1"); |
| 38 const GURL kTestPage2Url("http://test.org/page2"); | 38 const GURL kTestPage2Url("http://test.org/page2"); |
| 39 const GURL kTestPage3Url("http://test.org/page3"); | 39 const GURL kTestPage3Url("http://test.org/page3"); |
| 40 const GURL kTestPage4Url("http://test.org/page4"); | 40 const GURL kTestPage4Url("http://test.org/page4"); |
| 41 const int64_t kTestFileSize = 876543LL; | 41 const int64_t kTestFileSize = 876543LL; |
| 42 const char* kTestPage1ClientId = "1234"; | 42 const char* kTestPage1ClientId = "1234"; |
| 43 const char* kTestPage2ClientId = "5678"; | 43 const char* kTestPage2ClientId = "5678"; |
| 44 const char* kTestPage4ClientId = "9876"; | |
| 45 | 44 |
| 46 void BoolCallback(bool* actual_result, bool call_result) { | 45 void BoolCallback(bool* actual_result, bool call_result) { |
| 47 DCHECK(actual_result); | 46 DCHECK(actual_result); |
| 48 *actual_result = call_result; | 47 *actual_result = call_result; |
| 49 } | 48 } |
| 50 | 49 |
| 51 } // namespace | 50 } // namespace |
| 52 | 51 |
| 53 class OfflinePageUtilsTest | 52 class OfflinePageUtilsTest |
| 54 : public testing::Test, | 53 : public testing::Test, |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 } | 142 } |
| 144 | 143 |
| 145 void OfflinePageUtilsTest::OnGetURLDone(const GURL& url) { | 144 void OfflinePageUtilsTest::OnGetURLDone(const GURL& url) { |
| 146 url_ = url; | 145 url_ = url; |
| 147 } | 146 } |
| 148 | 147 |
| 149 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( | 148 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( |
| 150 const base::FilePath& file_path) {} | 149 const base::FilePath& file_path) {} |
| 151 | 150 |
| 152 void OfflinePageUtilsTest::CreateOfflinePages() { | 151 void OfflinePageUtilsTest::CreateOfflinePages() { |
| 153 OfflinePageModel* model = | |
| 154 OfflinePageModelFactory::GetForBrowserContext(profile()); | |
| 155 | |
| 156 // Create page 1. | 152 // Create page 1. |
| 157 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 153 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 158 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); | 154 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); |
| 159 offline_pages::ClientId client_id; | 155 offline_pages::ClientId client_id; |
| 160 client_id.name_space = kDownloadNamespace; | 156 client_id.name_space = kDownloadNamespace; |
| 161 client_id.id = kTestPage1ClientId; | 157 client_id.id = kTestPage1ClientId; |
| 162 SavePage(kTestPage1Url, client_id, std::move(archiver)); | 158 SavePage(kTestPage1Url, client_id, std::move(archiver)); |
| 163 | 159 |
| 164 // Create page 2. | 160 // Create page 2. |
| 165 archiver = BuildArchiver(kTestPage2Url, | 161 archiver = BuildArchiver(kTestPage2Url, |
| 166 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); | 162 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); |
| 167 client_id.id = kTestPage2ClientId; | 163 client_id.id = kTestPage2ClientId; |
| 168 SavePage(kTestPage2Url, client_id, std::move(archiver)); | 164 SavePage(kTestPage2Url, client_id, std::move(archiver)); |
| 169 | |
| 170 // Create page 4 - expired page. | |
| 171 archiver = BuildArchiver(kTestPage4Url, | |
| 172 base::FilePath(FILE_PATH_LITERAL("page4.mhtml"))); | |
| 173 client_id.id = kTestPage4ClientId; | |
| 174 SavePage(kTestPage4Url, client_id, std::move(archiver)); | |
| 175 RunUntilIdle(); | |
| 176 model->ExpirePages( | |
| 177 std::vector<int64_t>({offline_id()}), base::Time::Now(), | |
| 178 base::Bind(&OfflinePageUtilsTest::OnExpirePageDone, AsWeakPtr())); | |
| 179 RunUntilIdle(); | |
| 180 } | 165 } |
| 181 | 166 |
| 182 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( | 167 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( |
| 183 const GURL& url, | 168 const GURL& url, |
| 184 const base::FilePath& file_name) { | 169 const base::FilePath& file_name) { |
| 185 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( | 170 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
| 186 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 171 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 187 base::string16(), kTestFileSize, base::ThreadTaskRunnerHandle::Get())); | 172 base::string16(), kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
| 188 archiver->set_filename(file_name); | 173 archiver->set_filename(file_name); |
| 189 return archiver; | 174 return archiver; |
| 190 } | 175 } |
| 191 | 176 |
| 192 TEST_F(OfflinePageUtilsTest, CheckExistenceOfPagesWithURL) { | 177 TEST_F(OfflinePageUtilsTest, CheckExistenceOfPagesWithURL) { |
| 193 bool page_exists = false; | 178 bool page_exists = false; |
| 194 // This page should be available. | 179 // This page should be available. |
| 195 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 180 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 196 profile(), kDownloadNamespace, kTestPage1Url, | 181 profile(), kDownloadNamespace, kTestPage1Url, |
| 197 base::Bind(&BoolCallback, base::Unretained(&page_exists))); | 182 base::Bind(&BoolCallback, base::Unretained(&page_exists))); |
| 198 RunUntilIdle(); | 183 RunUntilIdle(); |
| 199 EXPECT_TRUE(page_exists); | 184 EXPECT_TRUE(page_exists); |
| 200 // This one should be missing | 185 // This one should be missing |
| 201 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 186 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 202 profile(), kDownloadNamespace, kTestPage3Url, | 187 profile(), kDownloadNamespace, kTestPage3Url, |
| 203 base::Bind(&BoolCallback, base::Unretained(&page_exists))); | 188 base::Bind(&BoolCallback, base::Unretained(&page_exists))); |
| 204 RunUntilIdle(); | 189 RunUntilIdle(); |
| 205 EXPECT_FALSE(page_exists); | 190 EXPECT_FALSE(page_exists); |
| 206 } | 191 } |
| 207 | 192 |
| 208 } // namespace offline_pages | 193 } // namespace offline_pages |
| OLD | NEW |