| 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"); |
| 36 const int64_t kTestFileSize = 876543LL; |
| 37 const char* kTestPage1ClientId = "1234"; |
| 38 const char* kTestPage2ClientId = "5678"; |
| 35 const int64_t kTestPage1BookmarkId = 1234; | 39 const int64_t kTestPage1BookmarkId = 1234; |
| 36 const int64_t kTestPage2BookmarkId = 5678; | 40 const int64_t kTestPage2BookmarkId = 5678; |
| 37 const int64_t kTestFileSize = 876543LL; | |
| 38 | 41 |
| 39 } // namespace | 42 } // namespace |
| 40 | 43 |
| 41 class OfflinePageUtilsTest | 44 class OfflinePageUtilsTest |
| 42 : public testing::Test, | 45 : public testing::Test, |
| 43 public OfflinePageTestArchiver::Observer, | 46 public OfflinePageTestArchiver::Observer, |
| 44 public base::SupportsWeakPtr<OfflinePageUtilsTest> { | 47 public base::SupportsWeakPtr<OfflinePageUtilsTest> { |
| 45 public: | 48 public: |
| 46 OfflinePageUtilsTest(); | 49 OfflinePageUtilsTest(); |
| 47 ~OfflinePageUtilsTest() override; | 50 ~OfflinePageUtilsTest() override; |
| 48 | 51 |
| 49 void SetUp() override; | 52 void SetUp() override; |
| 50 void RunUntilIdle(); | 53 void RunUntilIdle(); |
| 51 | 54 |
| 52 // Necessary callbacks for the offline page model. | 55 // Necessary callbacks for the offline page model. |
| 53 void OnSavePageDone(OfflinePageModel::SavePageResult result); | 56 void OnSavePageDone(OfflinePageModel::SavePageResult result, |
| 57 int64_t offlineId); |
| 54 void OnClearAllDone(); | 58 void OnClearAllDone(); |
| 55 | 59 |
| 56 // OfflinePageTestArchiver::Observer implementation: | 60 // OfflinePageTestArchiver::Observer implementation: |
| 57 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | 61 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| 58 | 62 |
| 59 // Offline page URL for the first page. | 63 // Offline page URL for the first page. |
| 60 const GURL& offline_url_page_1() const { return offline_url_page_1_; } | 64 const GURL& offline_url_page_1() const { return offline_url_page_1_; } |
| 61 // Offline page URL for the second page. | 65 // Offline page URL for the second page. |
| 62 const GURL& offline_url_page_2() const { return offline_url_page_2_; } | 66 const GURL& offline_url_page_2() const { return offline_url_page_2_; } |
| 63 // Offline page URL not related to any page. | 67 // Offline page URL not related to any page. |
| 64 const GURL& offline_url_missing() const { return offline_url_missing_; } | 68 const GURL& offline_url_missing() const { return offline_url_missing_; } |
| 65 | 69 |
| 66 TestingProfile* profile() { return &profile_; } | 70 TestingProfile* profile() { return &profile_; } |
| 67 | 71 |
| 72 int64_t offline_id() const { return offline_id_; } |
| 73 |
| 68 private: | 74 private: |
| 69 void CreateOfflinePages(); | 75 void CreateOfflinePages(); |
| 70 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( | 76 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 71 const GURL& url, | 77 const GURL& url, |
| 72 const base::FilePath& file_name); | 78 const base::FilePath& file_name); |
| 73 | 79 |
| 74 GURL offline_url_page_1_; | 80 GURL offline_url_page_1_; |
| 75 GURL offline_url_page_2_; | 81 GURL offline_url_page_2_; |
| 76 GURL offline_url_missing_; | 82 GURL offline_url_missing_; |
| 77 | 83 |
| 84 int64_t offline_id_; |
| 85 |
| 78 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 86 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 79 base::ThreadTaskRunnerHandle task_runner_handle_; | 87 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 80 TestingProfile profile_; | 88 TestingProfile profile_; |
| 81 }; | 89 }; |
| 82 | 90 |
| 83 OfflinePageUtilsTest::OfflinePageUtilsTest() | 91 OfflinePageUtilsTest::OfflinePageUtilsTest() |
| 84 : task_runner_(new base::TestSimpleTaskRunner), | 92 : task_runner_(new base::TestSimpleTaskRunner), |
| 85 task_runner_handle_(task_runner_) {} | 93 task_runner_handle_(task_runner_) {} |
| 86 | 94 |
| 87 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} | 95 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} |
| (...skipping 11 matching lines...) Expand all Loading... |
| 99 // Make sure the store contains the right offline pages before the load | 107 // Make sure the store contains the right offline pages before the load |
| 100 // happens. | 108 // happens. |
| 101 CreateOfflinePages(); | 109 CreateOfflinePages(); |
| 102 } | 110 } |
| 103 | 111 |
| 104 void OfflinePageUtilsTest::RunUntilIdle() { | 112 void OfflinePageUtilsTest::RunUntilIdle() { |
| 105 task_runner_->RunUntilIdle(); | 113 task_runner_->RunUntilIdle(); |
| 106 } | 114 } |
| 107 | 115 |
| 108 void OfflinePageUtilsTest::OnSavePageDone( | 116 void OfflinePageUtilsTest::OnSavePageDone( |
| 109 OfflinePageModel::SavePageResult result) { | 117 OfflinePageModel::SavePageResult result, |
| 110 // Result ignored here. | 118 int64_t offline_id) { |
| 119 offline_id_ = offline_id; |
| 111 } | 120 } |
| 112 | 121 |
| 113 void OfflinePageUtilsTest::OnClearAllDone() { | 122 void OfflinePageUtilsTest::OnClearAllDone() { |
| 114 // Result ignored here. | 123 // Result ignored here. |
| 115 } | 124 } |
| 116 | 125 |
| 117 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( | 126 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( |
| 118 const base::FilePath& file_path) {} | 127 const base::FilePath& file_path) {} |
| 119 | 128 |
| 120 void OfflinePageUtilsTest::CreateOfflinePages() { | 129 void OfflinePageUtilsTest::CreateOfflinePages() { |
| 121 OfflinePageModel* model = | 130 OfflinePageModel* model = |
| 122 OfflinePageModelFactory::GetForBrowserContext(profile()); | 131 OfflinePageModelFactory::GetForBrowserContext(profile()); |
| 123 | 132 |
| 124 // Create page 1. | 133 // Create page 1. |
| 125 scoped_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 134 scoped_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 126 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); | 135 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); |
| 136 offline_pages::ClientId client_id; |
| 137 client_id.name_space = BOOKMARK_NAMESPACE; |
| 138 client_id.id = kTestPage1ClientId; |
| 127 model->SavePage( | 139 model->SavePage( |
| 128 kTestPage1Url, kTestPage1BookmarkId, std::move(archiver), | 140 kTestPage1Url, client_id, std::move(archiver), |
| 129 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 141 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 130 RunUntilIdle(); | 142 RunUntilIdle(); |
| 143 int64_t offline1 = offline_id(); |
| 131 | 144 |
| 145 client_id.id = kTestPage2ClientId; |
| 132 // Create page 2. | 146 // Create page 2. |
| 133 archiver = BuildArchiver(kTestPage2Url, | 147 archiver = BuildArchiver(kTestPage2Url, |
| 134 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); | 148 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); |
| 135 model->SavePage( | 149 model->SavePage( |
| 136 kTestPage2Url, kTestPage2BookmarkId, std::move(archiver), | 150 kTestPage2Url, client_id, std::move(archiver), |
| 137 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 151 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 138 RunUntilIdle(); | 152 RunUntilIdle(); |
| 153 int64_t offline2 = offline_id(); |
| 139 | 154 |
| 140 // Make a copy of local paths of the two pages stored in the model. | 155 // Make a copy of local paths of the two pages stored in the model. |
| 141 offline_url_page_1_ = | 156 offline_url_page_1_ = model->GetPageByOfflineId(offline1)->GetOfflineURL(); |
| 142 model->GetPageByBookmarkId(kTestPage1BookmarkId)->GetOfflineURL(); | 157 offline_url_page_2_ = model->GetPageByOfflineId(offline2)->GetOfflineURL(); |
| 143 offline_url_page_2_ = | |
| 144 model->GetPageByBookmarkId(kTestPage2BookmarkId)->GetOfflineURL(); | |
| 145 // Create a file path that is not associated with any offline page. | 158 // Create a file path that is not associated with any offline page. |
| 146 offline_url_missing_ = net::FilePathToFileURL( | 159 offline_url_missing_ = net::FilePathToFileURL( |
| 147 profile() | 160 profile() |
| 148 ->GetPath() | 161 ->GetPath() |
| 149 .Append(chrome::kOfflinePageArchviesDirname) | 162 .Append(chrome::kOfflinePageArchviesDirname) |
| 150 .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); | 163 .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); |
| 151 } | 164 } |
| 152 | 165 |
| 153 scoped_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( | 166 scoped_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( |
| 154 const GURL& url, | 167 const GURL& url, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { | 234 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { |
| 222 EXPECT_TRUE( | 235 EXPECT_TRUE( |
| 223 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url)); | 236 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url)); |
| 224 EXPECT_TRUE( | 237 EXPECT_TRUE( |
| 225 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url)); | 238 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url)); |
| 226 EXPECT_FALSE( | 239 EXPECT_FALSE( |
| 227 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url)); | 240 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url)); |
| 228 } | 241 } |
| 229 | 242 |
| 230 } // namespace offline_pages | 243 } // namespace offline_pages |
| OLD | NEW |