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/feature_list.h" | 12 #include "base/feature_list.h" |
| 13 #include "base/files/file.h" | 13 #include "base/files/file.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/run_loop.h" | |
| 16 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 17 #include "base/test/test_simple_task_runner.h" | |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 20 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " | 20 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " |
| 21 #include "chrome/common/chrome_constants.h" | 21 #include "chrome/common/chrome_constants.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/offline_pages/client_namespace_constants.h" | 23 #include "components/offline_pages/client_namespace_constants.h" |
| 24 #include "components/offline_pages/offline_page_feature.h" | 24 #include "components/offline_pages/offline_page_feature.h" |
| 25 #include "components/offline_pages/offline_page_model.h" | 25 #include "components/offline_pages/offline_page_model.h" |
| 26 #include "components/offline_pages/offline_page_test_archiver.h" | 26 #include "components/offline_pages/offline_page_test_archiver.h" |
| 27 #include "components/offline_pages/offline_page_test_store.h" | 27 #include "components/offline_pages/offline_page_test_store.h" |
| 28 #include "components/offline_pages/offline_page_types.h" | 28 #include "components/offline_pages/offline_page_types.h" |
| 29 #include "content/public/test/test_browser_thread_bundle.h" | |
| 29 #include "net/base/filename_util.h" | 30 #include "net/base/filename_util.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 32 | 33 |
| 33 namespace offline_pages { | 34 namespace offline_pages { |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 const GURL kTestPage1Url("http://test.org/page1"); | 37 const GURL kTestPage1Url("http://test.org/page1"); |
| 37 const GURL kTestPage2Url("http://test.org/page2"); | 38 const GURL kTestPage2Url("http://test.org/page2"); |
| 38 const GURL kTestPage3Url("http://test.org/page3"); | 39 const GURL kTestPage3Url("http://test.org/page3"); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 51 | 52 |
| 52 class OfflinePageUtilsTest | 53 class OfflinePageUtilsTest |
| 53 : public testing::Test, | 54 : public testing::Test, |
| 54 public OfflinePageTestArchiver::Observer, | 55 public OfflinePageTestArchiver::Observer, |
| 55 public base::SupportsWeakPtr<OfflinePageUtilsTest> { | 56 public base::SupportsWeakPtr<OfflinePageUtilsTest> { |
| 56 public: | 57 public: |
| 57 OfflinePageUtilsTest(); | 58 OfflinePageUtilsTest(); |
| 58 ~OfflinePageUtilsTest() override; | 59 ~OfflinePageUtilsTest() override; |
| 59 | 60 |
| 60 void SetUp() override; | 61 void SetUp() override; |
| 61 void RunUntilIdle(); | |
| 62 | 62 |
| 63 // Necessary callbacks for the offline page model. | 63 // Necessary callbacks for the offline page model. |
| 64 void OnSavePageDone(SavePageResult result, int64_t offlineId); | 64 void OnSavePageDone(SavePageResult result, int64_t offlineId); |
| 65 void OnClearAllDone(); | 65 void OnClearAllDone(); |
| 66 void OnExpirePageDone(bool success); | 66 void OnExpirePageDone(bool success); |
| 67 void OnGetURLDone(const GURL& url); | 67 void OnGetURLDone(const GURL& url); |
| 68 | 68 |
| 69 // OfflinePageTestArchiver::Observer implementation: | 69 // OfflinePageTestArchiver::Observer implementation: |
| 70 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | 70 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| 71 | 71 |
| 72 TestingProfile* profile() { return &profile_; } | 72 TestingProfile* profile() { return &profile_; } |
| 73 | 73 |
| 74 int64_t offline_id() const { return offline_id_; } | 74 int64_t offline_id() const { return offline_id_; } |
| 75 | 75 |
| 76 private: | 76 private: |
| 77 void CreateOfflinePages(); | 77 void CreateOfflinePages(); |
| 78 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( | 78 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 79 const GURL& url, | 79 const GURL& url, |
| 80 const base::FilePath& file_name); | 80 const base::FilePath& file_name); |
| 81 | 81 |
| 82 content::TestBrowserThreadBundle browser_thread_bundle_; | |
| 82 int64_t offline_id_; | 83 int64_t offline_id_; |
| 83 GURL url_; | 84 GURL url_; |
| 84 | |
| 85 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 86 base::ThreadTaskRunnerHandle task_runner_handle_; | |
| 87 TestingProfile profile_; | 85 TestingProfile profile_; |
| 88 }; | 86 }; |
| 89 | 87 |
| 90 OfflinePageUtilsTest::OfflinePageUtilsTest() | 88 OfflinePageUtilsTest::OfflinePageUtilsTest() = default; |
| 91 : task_runner_(new base::TestSimpleTaskRunner), | |
| 92 task_runner_handle_(task_runner_) {} | |
| 93 | 89 |
| 94 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} | 90 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} |
| 95 | 91 |
| 96 void OfflinePageUtilsTest::SetUp() { | 92 void OfflinePageUtilsTest::SetUp() { |
| 97 // Enables offline pages feature. | 93 // Enables offline pages feature. |
| 98 // TODO(jianli): Remove this once the feature is completely enabled. | 94 // TODO(jianli): Remove this once the feature is completely enabled. |
| 99 base::FeatureList::ClearInstanceForTesting(); | 95 base::FeatureList::ClearInstanceForTesting(); |
| 100 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 96 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 101 feature_list->InitializeFromCommandLine( | 97 feature_list->InitializeFromCommandLine( |
| 102 offline_pages::kOfflineBookmarksFeature.name, ""); | 98 offline_pages::kOfflineBookmarksFeature.name, ""); |
| 103 base::FeatureList::SetInstance(std::move(feature_list)); | 99 base::FeatureList::SetInstance(std::move(feature_list)); |
| 104 | 100 |
| 105 // Set up the factory for testing. | 101 // Set up the factory for testing. |
| 106 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | 102 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 107 &profile_, BuildTestOfflinePageModel); | 103 &profile_, BuildTestOfflinePageModel); |
| 108 RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
| 109 | 105 |
| 110 // Make sure the store contains the right offline pages before the load | 106 // Make sure the store contains the right offline pages before the load |
| 111 // happens. | 107 // happens. |
| 112 CreateOfflinePages(); | 108 CreateOfflinePages(); |
| 113 } | 109 } |
| 114 | 110 |
| 115 void OfflinePageUtilsTest::RunUntilIdle() { | |
| 116 task_runner_->RunUntilIdle(); | |
|
fgorski
2016/10/12 19:52:19
can you implement this one in terms of base::RunLo
fdoray
2016/10/12 19:57:06
Done.
| |
| 117 } | |
| 118 | |
| 119 void OfflinePageUtilsTest::OnSavePageDone(SavePageResult result, | 111 void OfflinePageUtilsTest::OnSavePageDone(SavePageResult result, |
| 120 int64_t offline_id) { | 112 int64_t offline_id) { |
| 121 offline_id_ = offline_id; | 113 offline_id_ = offline_id; |
| 122 } | 114 } |
| 123 | 115 |
| 124 void OfflinePageUtilsTest::OnExpirePageDone(bool success) { | 116 void OfflinePageUtilsTest::OnExpirePageDone(bool success) { |
| 125 // Result ignored here. | 117 // Result ignored here. |
| 126 } | 118 } |
| 127 | 119 |
| 128 void OfflinePageUtilsTest::OnClearAllDone() { | 120 void OfflinePageUtilsTest::OnClearAllDone() { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 142 | 134 |
| 143 // Create page 1. | 135 // Create page 1. |
| 144 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 136 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 145 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); | 137 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); |
| 146 offline_pages::ClientId client_id; | 138 offline_pages::ClientId client_id; |
| 147 client_id.name_space = kDownloadNamespace; | 139 client_id.name_space = kDownloadNamespace; |
| 148 client_id.id = kTestPage1ClientId; | 140 client_id.id = kTestPage1ClientId; |
| 149 model->SavePage( | 141 model->SavePage( |
| 150 kTestPage1Url, client_id, 0l, std::move(archiver), | 142 kTestPage1Url, client_id, 0l, std::move(archiver), |
| 151 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 143 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 152 RunUntilIdle(); | 144 base::RunLoop().RunUntilIdle(); |
| 153 | 145 |
| 154 // Create page 2. | 146 // Create page 2. |
| 155 archiver = BuildArchiver(kTestPage2Url, | 147 archiver = BuildArchiver(kTestPage2Url, |
| 156 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); | 148 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); |
| 157 client_id.id = kTestPage2ClientId; | 149 client_id.id = kTestPage2ClientId; |
| 158 model->SavePage( | 150 model->SavePage( |
| 159 kTestPage2Url, client_id, 0l, std::move(archiver), | 151 kTestPage2Url, client_id, 0l, std::move(archiver), |
| 160 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 152 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 161 RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
| 162 | 154 |
| 163 // Create page 4 - expired page. | 155 // Create page 4 - expired page. |
| 164 archiver = BuildArchiver(kTestPage4Url, | 156 archiver = BuildArchiver(kTestPage4Url, |
| 165 base::FilePath(FILE_PATH_LITERAL("page4.mhtml"))); | 157 base::FilePath(FILE_PATH_LITERAL("page4.mhtml"))); |
| 166 client_id.id = kTestPage4ClientId; | 158 client_id.id = kTestPage4ClientId; |
| 167 model->SavePage( | 159 model->SavePage( |
| 168 kTestPage4Url, client_id, 0l, std::move(archiver), | 160 kTestPage4Url, client_id, 0l, std::move(archiver), |
| 169 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 161 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
| 170 RunUntilIdle(); | 162 base::RunLoop().RunUntilIdle(); |
| 171 model->ExpirePages( | 163 model->ExpirePages( |
| 172 std::vector<int64_t>({offline_id()}), base::Time::Now(), | 164 std::vector<int64_t>({offline_id()}), base::Time::Now(), |
| 173 base::Bind(&OfflinePageUtilsTest::OnExpirePageDone, AsWeakPtr())); | 165 base::Bind(&OfflinePageUtilsTest::OnExpirePageDone, AsWeakPtr())); |
| 174 RunUntilIdle(); | 166 base::RunLoop().RunUntilIdle(); |
| 175 } | 167 } |
| 176 | 168 |
| 177 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( | 169 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( |
| 178 const GURL& url, | 170 const GURL& url, |
| 179 const base::FilePath& file_name) { | 171 const base::FilePath& file_name) { |
| 180 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( | 172 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
| 181 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 173 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 182 base::string16(), kTestFileSize, base::ThreadTaskRunnerHandle::Get())); | 174 base::string16(), kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
| 183 archiver->set_filename(file_name); | 175 archiver->set_filename(file_name); |
| 184 return archiver; | 176 return archiver; |
| 185 } | 177 } |
| 186 | 178 |
| 187 TEST_F(OfflinePageUtilsTest, CheckExistenceOfPagesWithURL) { | 179 TEST_F(OfflinePageUtilsTest, CheckExistenceOfPagesWithURL) { |
| 188 bool page_exists = false; | 180 bool page_exists = false; |
| 189 // This page should be available. | 181 // This page should be available. |
| 190 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 182 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 191 profile(), kDownloadNamespace, kTestPage1Url, | 183 profile(), kDownloadNamespace, kTestPage1Url, |
| 192 base::Bind(&BoolCallback, base::Unretained(&page_exists))); | 184 base::Bind(&BoolCallback, base::Unretained(&page_exists))); |
| 193 RunUntilIdle(); | 185 base::RunLoop().RunUntilIdle(); |
| 194 EXPECT_TRUE(page_exists); | 186 EXPECT_TRUE(page_exists); |
| 195 // This one should be missing | 187 // This one should be missing |
| 196 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 188 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 197 profile(), kDownloadNamespace, kTestPage3Url, | 189 profile(), kDownloadNamespace, kTestPage3Url, |
| 198 base::Bind(&BoolCallback, base::Unretained(&page_exists))); | 190 base::Bind(&BoolCallback, base::Unretained(&page_exists))); |
| 199 RunUntilIdle(); | 191 base::RunLoop().RunUntilIdle(); |
| 200 EXPECT_FALSE(page_exists); | 192 EXPECT_FALSE(page_exists); |
| 201 } | 193 } |
| 202 | 194 |
| 203 } // namespace offline_pages | 195 } // namespace offline_pages |
| OLD | NEW |