Chromium Code Reviews| 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 17 matching lines...) Expand all Loading... | |
| 28 #include "net/base/filename_util.h" | 28 #include "net/base/filename_util.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 31 | 31 |
| 32 namespace offline_pages { | 32 namespace offline_pages { |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 const GURL kTestPage1Url("http://test.org/page1"); | 35 const GURL kTestPage1Url("http://test.org/page1"); |
| 36 const GURL kTestPage2Url("http://test.org/page2"); | 36 const GURL kTestPage2Url("http://test.org/page2"); |
| 37 const GURL kTestPage3Url("http://test.org/page3"); | 37 const GURL kTestPage3Url("http://test.org/page3"); |
| 38 const GURL kTestPage4Url("http://test.org/page4"); | |
| 38 const int64_t kTestFileSize = 876543LL; | 39 const int64_t kTestFileSize = 876543LL; |
| 39 const char* kTestPage1ClientId = "1234"; | 40 const char* kTestPage1ClientId = "1234"; |
| 40 const char* kTestPage2ClientId = "5678"; | 41 const char* kTestPage2ClientId = "5678"; |
| 42 const char* kTestPage4ClientId = "9876"; | |
| 41 | 43 |
| 42 } // namespace | 44 } // namespace |
| 43 | 45 |
| 44 class OfflinePageUtilsTest | 46 class OfflinePageUtilsTest |
| 45 : public testing::Test, | 47 : public testing::Test, |
| 46 public OfflinePageTestArchiver::Observer, | 48 public OfflinePageTestArchiver::Observer, |
| 47 public base::SupportsWeakPtr<OfflinePageUtilsTest> { | 49 public base::SupportsWeakPtr<OfflinePageUtilsTest> { |
| 48 public: | 50 public: |
| 49 OfflinePageUtilsTest(); | 51 OfflinePageUtilsTest(); |
| 50 ~OfflinePageUtilsTest() override; | 52 ~OfflinePageUtilsTest() override; |
| 51 | 53 |
| 52 void SetUp() override; | 54 void SetUp() override; |
| 53 void RunUntilIdle(); | 55 void RunUntilIdle(); |
| 54 | 56 |
| 55 // Necessary callbacks for the offline page model. | 57 // Necessary callbacks for the offline page model. |
| 56 void OnSavePageDone(SavePageResult result, int64_t offlineId); | 58 void OnSavePageDone(SavePageResult result, int64_t offlineId); |
| 57 void OnClearAllDone(); | 59 void OnClearAllDone(); |
| 60 void OnExpirePageDone(bool success); | |
| 58 | 61 |
| 59 // OfflinePageTestArchiver::Observer implementation: | 62 // OfflinePageTestArchiver::Observer implementation: |
| 60 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | 63 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| 61 | 64 |
| 62 // Offline page URL for the first page. | 65 // Offline page URL for the first page. |
| 63 const GURL& offline_url_page_1() const { return offline_url_page_1_; } | 66 const GURL& offline_url_page_1() const { return offline_url_page_1_; } |
| 64 // Offline page URL for the second page. | 67 // Offline page URL for the second page. |
| 65 const GURL& offline_url_page_2() const { return offline_url_page_2_; } | 68 const GURL& offline_url_page_2() const { return offline_url_page_2_; } |
| 66 // Offline page URL not related to any page. | 69 // Offline page URL not related to any page. |
| 67 const GURL& offline_url_missing() const { return offline_url_missing_; } | 70 const GURL& offline_url_missing() const { return offline_url_missing_; } |
| 71 // Offline page URL for expired page. | |
| 72 const GURL& offline_url_expired() const { return offline_url_expired_; } | |
| 68 | 73 |
| 69 TestingProfile* profile() { return &profile_; } | 74 TestingProfile* profile() { return &profile_; } |
| 70 | 75 |
| 71 int64_t offline_id() const { return offline_id_; } | 76 int64_t offline_id() const { return offline_id_; } |
| 72 | 77 |
| 73 private: | 78 private: |
| 74 void CreateOfflinePages(); | 79 void CreateOfflinePages(); |
| 75 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( | 80 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 76 const GURL& url, | 81 const GURL& url, |
| 77 const base::FilePath& file_name); | 82 const base::FilePath& file_name); |
| 78 | 83 |
| 79 GURL offline_url_page_1_; | 84 GURL offline_url_page_1_; |
| 80 GURL offline_url_page_2_; | 85 GURL offline_url_page_2_; |
| 81 GURL offline_url_missing_; | 86 GURL offline_url_missing_; |
| 87 GURL offline_url_expired_; | |
| 82 | 88 |
| 83 int64_t offline_id_; | 89 int64_t offline_id_; |
| 84 | 90 |
| 85 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 91 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 86 base::ThreadTaskRunnerHandle task_runner_handle_; | 92 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 87 TestingProfile profile_; | 93 TestingProfile profile_; |
| 88 }; | 94 }; |
| 89 | 95 |
| 90 OfflinePageUtilsTest::OfflinePageUtilsTest() | 96 OfflinePageUtilsTest::OfflinePageUtilsTest() |
| 91 : task_runner_(new base::TestSimpleTaskRunner), | 97 : task_runner_(new base::TestSimpleTaskRunner), |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 114 | 120 |
| 115 void OfflinePageUtilsTest::RunUntilIdle() { | 121 void OfflinePageUtilsTest::RunUntilIdle() { |
| 116 task_runner_->RunUntilIdle(); | 122 task_runner_->RunUntilIdle(); |
| 117 } | 123 } |
| 118 | 124 |
| 119 void OfflinePageUtilsTest::OnSavePageDone(SavePageResult result, | 125 void OfflinePageUtilsTest::OnSavePageDone(SavePageResult result, |
| 120 int64_t offline_id) { | 126 int64_t offline_id) { |
| 121 offline_id_ = offline_id; | 127 offline_id_ = offline_id; |
| 122 } | 128 } |
| 123 | 129 |
| 130 void OfflinePageUtilsTest::OnExpirePageDone(bool success) { | |
| 131 // Result ignored here. | |
| 132 } | |
| 133 | |
| 124 void OfflinePageUtilsTest::OnClearAllDone() { | 134 void OfflinePageUtilsTest::OnClearAllDone() { |
| 125 // Result ignored here. | 135 // Result ignored here. |
| 126 } | 136 } |
| 127 | 137 |
| 128 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( | 138 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( |
| 129 const base::FilePath& file_path) {} | 139 const base::FilePath& file_path) {} |
| 130 | 140 |
| 131 void OfflinePageUtilsTest::CreateOfflinePages() { | 141 void OfflinePageUtilsTest::CreateOfflinePages() { |
| 132 OfflinePageModel* model = | 142 OfflinePageModel* model = |
| 133 OfflinePageModelFactory::GetForBrowserContext(profile()); | 143 OfflinePageModelFactory::GetForBrowserContext(profile()); |
| 134 | 144 |
| 135 // Create page 1. | 145 // Create page 1. |
| 136 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 146 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 137 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); | 147 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); |
| 138 offline_pages::ClientId client_id; | 148 offline_pages::ClientId client_id; |
| 139 client_id.name_space = kBookmarkNamespace; | 149 client_id.name_space = kBookmarkNamespace; |
| 140 client_id.id = kTestPage1ClientId; | 150 client_id.id = kTestPage1ClientId; |
| 141 model->SavePage( | 151 model->SavePage( |
| 142 kTestPage1Url, client_id, std::move(archiver), | 152 kTestPage1Url, client_id, std::move(archiver), |
| 143 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 153 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 144 RunUntilIdle(); | 154 RunUntilIdle(); |
| 145 int64_t offline1 = offline_id(); | 155 offline_url_page_1_ = |
| 156 model->MaybeGetPageByOfflineId(offline_id())->GetOfflineURL(); | |
| 146 | 157 |
| 147 client_id.id = kTestPage2ClientId; | |
| 148 // Create page 2. | 158 // Create page 2. |
| 149 archiver = BuildArchiver(kTestPage2Url, | 159 archiver = BuildArchiver(kTestPage2Url, |
| 150 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); | 160 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); |
| 161 client_id.id = kTestPage2ClientId; | |
| 151 model->SavePage( | 162 model->SavePage( |
| 152 kTestPage2Url, client_id, std::move(archiver), | 163 kTestPage2Url, client_id, std::move(archiver), |
| 153 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 164 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 154 RunUntilIdle(); | 165 RunUntilIdle(); |
| 155 int64_t offline2 = offline_id(); | 166 offline_url_page_2_ = |
| 167 model->MaybeGetPageByOfflineId(offline_id())->GetOfflineURL(); | |
| 156 | 168 |
| 157 // Make a copy of local paths of the two pages stored in the model. | 169 // Page 3 is not created, as it is missing. |
| 158 offline_url_page_1_ = | |
| 159 model->MaybeGetPageByOfflineId(offline1)->GetOfflineURL(); | |
| 160 offline_url_page_2_ = | |
| 161 model->MaybeGetPageByOfflineId(offline2)->GetOfflineURL(); | |
| 162 // Create a file path that is not associated with any offline page. | 170 // Create a file path that is not associated with any offline page. |
| 163 offline_url_missing_ = net::FilePathToFileURL( | 171 offline_url_missing_ = net::FilePathToFileURL( |
| 164 profile() | 172 profile() |
| 165 ->GetPath() | 173 ->GetPath() |
| 166 .Append(chrome::kOfflinePageArchviesDirname) | 174 .Append(chrome::kOfflinePageArchviesDirname) |
| 167 .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); | 175 .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); |
| 176 | |
| 177 // Create page 4 - expired page. | |
| 178 archiver = BuildArchiver(kTestPage4Url, | |
| 179 base::FilePath(FILE_PATH_LITERAL("page4.mhtml"))); | |
| 180 client_id.id = kTestPage4ClientId; | |
| 181 model->SavePage( | |
| 182 kTestPage4Url, client_id, std::move(archiver), | |
| 183 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | |
| 184 RunUntilIdle(); | |
| 185 const OfflinePageItem* page_4 = model->MaybeGetPageByOfflineId(offline_id()); | |
| 186 offline_url_expired_ = page_4->GetOfflineURL(); | |
| 187 model->ExpirePages( | |
| 188 std::vector<int64_t>({offline_id()}), base::Time::Now(), | |
| 189 base::Bind(&OfflinePageUtilsTest::OnExpirePageDone, AsWeakPtr())); | |
| 190 RunUntilIdle(); | |
| 168 } | 191 } |
| 169 | 192 |
| 170 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( | 193 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( |
| 171 const GURL& url, | 194 const GURL& url, |
| 172 const base::FilePath& file_name) { | 195 const base::FilePath& file_name) { |
| 173 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( | 196 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
| 174 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 197 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 175 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); | 198 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
| 176 archiver->set_filename(file_name); | 199 archiver->set_filename(file_name); |
| 177 return archiver; | 200 return archiver; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 188 EXPECT_TRUE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.mhtml"))); | 211 EXPECT_TRUE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.mhtml"))); |
| 189 } | 212 } |
| 190 | 213 |
| 191 TEST_F(OfflinePageUtilsTest, MaybeGetOfflineURLForOnlineURL) { | 214 TEST_F(OfflinePageUtilsTest, MaybeGetOfflineURLForOnlineURL) { |
| 192 EXPECT_EQ(offline_url_page_1(), | 215 EXPECT_EQ(offline_url_page_1(), |
| 193 OfflinePageUtils::MaybeGetOfflineURLForOnlineURL(profile(), | 216 OfflinePageUtils::MaybeGetOfflineURLForOnlineURL(profile(), |
| 194 kTestPage1Url)); | 217 kTestPage1Url)); |
| 195 EXPECT_EQ(offline_url_page_2(), | 218 EXPECT_EQ(offline_url_page_2(), |
| 196 OfflinePageUtils::MaybeGetOfflineURLForOnlineURL(profile(), | 219 OfflinePageUtils::MaybeGetOfflineURLForOnlineURL(profile(), |
| 197 kTestPage2Url)); | 220 kTestPage2Url)); |
| 198 EXPECT_EQ(GURL(), OfflinePageUtils::MaybeGetOfflineURLForOnlineURL( | 221 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::MaybeGetOfflineURLForOnlineURL( |
| 199 profile(), GURL(kTestPage3Url))); | 222 profile(), kTestPage3Url)); |
| 223 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::MaybeGetOfflineURLForOnlineURL( | |
| 224 profile(), GURL(kTestPage3Url))); | |
|
fgorski
2016/06/07 22:46:15
I am not sure I understand this line.
Except for c
romax
2016/06/08 01:31:25
yeah.. copy paste.. (╯‵□′)╯︵┻━┻
| |
| 200 } | 225 } |
| 201 | 226 |
| 202 TEST_F(OfflinePageUtilsTest, MaybeGetOnlineURLForOfflineURL) { | 227 TEST_F(OfflinePageUtilsTest, MaybeGetOnlineURLForOfflineURL) { |
| 203 EXPECT_EQ(kTestPage1Url, OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | 228 EXPECT_EQ(kTestPage1Url, OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( |
| 204 profile(), offline_url_page_1())); | 229 profile(), offline_url_page_1())); |
| 205 EXPECT_EQ(kTestPage2Url, OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | 230 EXPECT_EQ(kTestPage2Url, OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( |
| 206 profile(), offline_url_page_2())); | 231 profile(), offline_url_page_2())); |
| 207 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | 232 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( |
| 208 profile(), offline_url_missing())); | 233 profile(), offline_url_missing())); |
| 234 EXPECT_EQ(kTestPage4Url, OfflinePageUtils::GetOnlineURLForOfflineURL( | |
| 235 profile(), offline_url_expired())); | |
| 209 } | 236 } |
| 210 | 237 |
| 211 TEST_F(OfflinePageUtilsTest, IsOfflinePage) { | 238 TEST_F(OfflinePageUtilsTest, IsOfflinePage) { |
| 212 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_1())); | 239 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_1())); |
| 213 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_2())); | 240 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_2())); |
| 214 EXPECT_FALSE( | 241 EXPECT_FALSE( |
| 215 OfflinePageUtils::IsOfflinePage(profile(), offline_url_missing())); | 242 OfflinePageUtils::IsOfflinePage(profile(), offline_url_missing())); |
| 243 EXPECT_TRUE( | |
| 244 OfflinePageUtils::IsOfflinePage(profile(), offline_url_expired())); | |
| 216 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage1Url)); | 245 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage1Url)); |
| 217 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage2Url)); | 246 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage2Url)); |
| 247 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage4Url)); | |
| 218 } | 248 } |
| 219 | 249 |
| 220 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { | 250 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { |
| 221 EXPECT_TRUE( | 251 EXPECT_TRUE( |
| 222 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url)); | 252 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url)); |
| 223 EXPECT_TRUE( | 253 EXPECT_TRUE( |
| 224 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url)); | 254 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url)); |
| 225 EXPECT_FALSE( | 255 EXPECT_FALSE( |
| 226 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url)); | 256 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url)); |
| 257 EXPECT_FALSE( | |
| 258 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage4Url)); | |
| 227 } | 259 } |
| 228 | 260 |
| 229 } // namespace offline_pages | 261 } // namespace offline_pages |
| OLD | NEW |