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" |
| 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/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 17 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 18 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " | 18 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " |
| 19 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "components/offline_pages/offline_page_model.h" | 21 #include "components/offline_pages/offline_page_model.h" |
| 22 #include "components/offline_pages/offline_page_switches.h" | 22 #include "components/offline_pages/offline_page_switches.h" |
| 23 #include "components/offline_pages/offline_page_test_archiver.h" | 23 #include "components/offline_pages/offline_page_test_archiver.h" |
| 24 #include "components/offline_pages/offline_page_test_store.h" | 24 #include "components/offline_pages/offline_page_test_store.h" |
| 25 #include "components/offline_pages/proto/offline_pages.pb.h" | |
| 25 #include "net/base/filename_util.h" | 26 #include "net/base/filename_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 28 | 29 |
| 29 namespace offline_pages { | 30 namespace offline_pages { |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const GURL kTestPage1Url("http://test.org/page1"); | 33 const GURL kTestPage1Url("http://test.org/page1"); |
| 33 const GURL kTestPage2Url("http://test.org/page2"); | 34 const GURL kTestPage2Url("http://test.org/page2"); |
| 34 const GURL kTestPage3Url("http://test.org/page3"); | 35 const GURL kTestPage3Url("http://test.org/page3"); |
| 35 const int64_t kTestPage1BookmarkId = 1234; | 36 const int64_t kTestPage1OfflineId = 1234; |
| 36 const int64_t kTestPage2BookmarkId = 5678; | 37 const int64_t kTestPage2OfflineId = 5678; |
| 37 const int64_t kTestFileSize = 876543LL; | 38 const int64_t kTestFileSize = 876543LL; |
| 39 const char* kTestClientNamespace = "test"; | |
| 40 const char* kTestPage1ClientId = "1234"; | |
| 41 const char* kTestPage2ClientId = "5678"; | |
| 38 | 42 |
| 39 } // namespace | 43 } // namespace |
| 40 | 44 |
| 41 class OfflinePageUtilsTest | 45 class OfflinePageUtilsTest |
| 42 : public testing::Test, | 46 : public testing::Test, |
| 43 public OfflinePageTestArchiver::Observer, | 47 public OfflinePageTestArchiver::Observer, |
| 44 public base::SupportsWeakPtr<OfflinePageUtilsTest> { | 48 public base::SupportsWeakPtr<OfflinePageUtilsTest> { |
| 45 public: | 49 public: |
| 46 OfflinePageUtilsTest(); | 50 OfflinePageUtilsTest(); |
| 47 ~OfflinePageUtilsTest() override; | 51 ~OfflinePageUtilsTest() override; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( | 121 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( |
| 118 const base::FilePath& file_path) {} | 122 const base::FilePath& file_path) {} |
| 119 | 123 |
| 120 void OfflinePageUtilsTest::CreateOfflinePages() { | 124 void OfflinePageUtilsTest::CreateOfflinePages() { |
| 121 OfflinePageModel* model = | 125 OfflinePageModel* model = |
| 122 OfflinePageModelFactory::GetForBrowserContext(profile()); | 126 OfflinePageModelFactory::GetForBrowserContext(profile()); |
| 123 | 127 |
| 124 // Create page 1. | 128 // Create page 1. |
| 125 scoped_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 129 scoped_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 126 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); | 130 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); |
| 131 offline_pages::ClientId cid; | |
|
Dmitry Titov
2016/02/25 03:31:52
client_id would be a bit better
bburns
2016/02/26 00:14:35
Done.
| |
| 132 cid.name_space = kTestClientNamespace; | |
| 133 cid.id = kTestPage1ClientId; | |
| 127 model->SavePage( | 134 model->SavePage( |
| 128 kTestPage1Url, kTestPage1BookmarkId, std::move(archiver), | 135 kTestPage1Url, kTestPage1OfflineId, cid, std::move(archiver), |
| 129 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 136 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 130 RunUntilIdle(); | 137 RunUntilIdle(); |
| 131 | 138 |
| 139 cid.id = kTestPage2ClientId; | |
| 132 // Create page 2. | 140 // Create page 2. |
| 133 archiver = BuildArchiver(kTestPage2Url, | 141 archiver = BuildArchiver(kTestPage2Url, |
| 134 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); | 142 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); |
| 135 model->SavePage( | 143 model->SavePage( |
| 136 kTestPage2Url, kTestPage2BookmarkId, std::move(archiver), | 144 kTestPage2Url, kTestPage2OfflineId, cid, std::move(archiver), |
| 137 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 145 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 138 RunUntilIdle(); | 146 RunUntilIdle(); |
| 139 | 147 |
| 140 // Make a copy of local paths of the two pages stored in the model. | 148 // Make a copy of local paths of the two pages stored in the model. |
| 141 offline_url_page_1_ = | 149 offline_url_page_1_ = |
| 142 model->GetPageByBookmarkId(kTestPage1BookmarkId)->GetOfflineURL(); | 150 model->GetPageByOfflineId(kTestPage1OfflineId)->GetOfflineURL(); |
| 143 offline_url_page_2_ = | 151 offline_url_page_2_ = |
| 144 model->GetPageByBookmarkId(kTestPage2BookmarkId)->GetOfflineURL(); | 152 model->GetPageByOfflineId(kTestPage2OfflineId)->GetOfflineURL(); |
| 145 // Create a file path that is not associated with any offline page. | 153 // Create a file path that is not associated with any offline page. |
| 146 offline_url_missing_ = net::FilePathToFileURL( | 154 offline_url_missing_ = net::FilePathToFileURL( |
| 147 profile() | 155 profile() |
| 148 ->GetPath() | 156 ->GetPath() |
| 149 .Append(chrome::kOfflinePageArchviesDirname) | 157 .Append(chrome::kOfflinePageArchviesDirname) |
| 150 .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); | 158 .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); |
| 151 } | 159 } |
| 152 | 160 |
| 153 scoped_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( | 161 scoped_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( |
| 154 const GURL& url, | 162 const GURL& url, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 TEST_F(OfflinePageUtilsTest, GetOnlineURLForOfflineURL) { | 202 TEST_F(OfflinePageUtilsTest, GetOnlineURLForOfflineURL) { |
| 195 EXPECT_EQ(kTestPage1Url, OfflinePageUtils::GetOnlineURLForOfflineURL( | 203 EXPECT_EQ(kTestPage1Url, OfflinePageUtils::GetOnlineURLForOfflineURL( |
| 196 profile(), offline_url_page_1())); | 204 profile(), offline_url_page_1())); |
| 197 EXPECT_EQ(kTestPage2Url, OfflinePageUtils::GetOnlineURLForOfflineURL( | 205 EXPECT_EQ(kTestPage2Url, OfflinePageUtils::GetOnlineURLForOfflineURL( |
| 198 profile(), offline_url_page_2())); | 206 profile(), offline_url_page_2())); |
| 199 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::GetOfflineURLForOnlineURL( | 207 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::GetOfflineURLForOnlineURL( |
| 200 profile(), offline_url_missing())); | 208 profile(), offline_url_missing())); |
| 201 } | 209 } |
| 202 | 210 |
| 203 TEST_F(OfflinePageUtilsTest, GetBookmarkIdForOfflineURL) { | 211 TEST_F(OfflinePageUtilsTest, GetBookmarkIdForOfflineURL) { |
| 204 EXPECT_EQ(kTestPage1BookmarkId, OfflinePageUtils::GetBookmarkIdForOfflineURL( | 212 EXPECT_EQ(kTestPage1OfflineId, OfflinePageUtils::GetBookmarkIdForOfflineURL( |
| 205 profile(), offline_url_page_1())); | 213 profile(), offline_url_page_1())); |
| 206 EXPECT_EQ(kTestPage2BookmarkId, OfflinePageUtils::GetBookmarkIdForOfflineURL( | 214 EXPECT_EQ(kTestPage2OfflineId, OfflinePageUtils::GetBookmarkIdForOfflineURL( |
| 207 profile(), offline_url_page_2())); | 215 profile(), offline_url_page_2())); |
| 208 EXPECT_EQ(-1, OfflinePageUtils::GetBookmarkIdForOfflineURL( | 216 EXPECT_EQ(-1, OfflinePageUtils::GetBookmarkIdForOfflineURL( |
| 209 profile(), offline_url_missing())); | 217 profile(), offline_url_missing())); |
| 210 } | 218 } |
| 211 | 219 |
| 212 TEST_F(OfflinePageUtilsTest, IsOfflinePage) { | 220 TEST_F(OfflinePageUtilsTest, IsOfflinePage) { |
| 213 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_1())); | 221 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_1())); |
| 214 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_2())); | 222 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_2())); |
| 215 EXPECT_FALSE( | 223 EXPECT_FALSE( |
| 216 OfflinePageUtils::IsOfflinePage(profile(), offline_url_missing())); | 224 OfflinePageUtils::IsOfflinePage(profile(), offline_url_missing())); |
| 217 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage1Url)); | 225 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage1Url)); |
| 218 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage2Url)); | 226 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage2Url)); |
| 219 } | 227 } |
| 220 | 228 |
| 221 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { | 229 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { |
| 222 EXPECT_TRUE( | 230 EXPECT_TRUE( |
| 223 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url)); | 231 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url)); |
| 224 EXPECT_TRUE( | 232 EXPECT_TRUE( |
| 225 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url)); | 233 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url)); |
| 226 EXPECT_FALSE( | 234 EXPECT_FALSE( |
| 227 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url)); | 235 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url)); |
| 228 } | 236 } |
| 229 | 237 |
| 230 } // namespace offline_pages | 238 } // namespace offline_pages |
| OLD | NEW |