Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/files/file.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/test/test_simple_task_runner.h" | |
| 13 #include "base/thread_task_runner_handle.h" | |
| 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
| 15 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " | |
| 16 #include "chrome/common/chrome_constants.h" | |
| 17 #include "chrome/test/base/testing_profile.h" | |
| 18 #include "components/offline_pages/offline_page_model.h" | |
| 19 #include "components/offline_pages/offline_page_switches.h" | |
| 20 #include "components/offline_pages/offline_page_test_store.h" | |
| 21 #include "net/base/filename_util.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 namespace offline_pages { | |
| 26 namespace android { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 const char kTestPage1Url[] = "http://test.org/page1"; | |
| 31 const char kTestPage2Url[] = "http://test.org/page2"; | |
| 32 const char kTestPage3Url[] = "http://test.org/page3"; | |
| 33 int64 kTestPage1BookmarkId = 1234; | |
| 34 int64 kTestPage2BookmarkId = 5678; | |
| 35 const base::FilePath::CharType kTestPage1FilePath[] = | |
| 36 FILE_PATH_LITERAL("test_page1.mhtml"); | |
| 37 const base::FilePath::CharType kTestPage2FilePath[] = | |
| 38 FILE_PATH_LITERAL("test_page2.mhtml"); | |
| 39 // const base::FilePath::CharType kTestPage3FilePath[] = | |
|
dewittj
2015/12/15 18:30:31
nit: remove commented out code
fgorski
2015/12/16 21:43:16
Done.
| |
| 40 // FILE_PATH_LITERAL("/offline_pages/test_org_page3.mhtml"); | |
| 41 int64 kTestPage1FileSize = 654321; | |
| 42 int64 kTestPage2FileSize = 765432; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 class OfflinePageUtilsBasicTest : public testing::Test { | |
| 47 public: | |
| 48 OfflinePageUtilsBasicTest(); | |
| 49 ~OfflinePageUtilsBasicTest() override; | |
| 50 | |
| 51 void SetUp() override; | |
| 52 | |
| 53 void FactorySetup(); | |
| 54 void DisableOfflinePagesFeature(); | |
| 55 void EnableOfflinePagesFeature(); | |
| 56 void PumpLoop(); | |
| 57 | |
| 58 TestingProfile* profile() { return &profile_; } | |
| 59 | |
| 60 private: | |
| 61 base::FilePath CreateArchiveFile(const base::FilePath& file_name); | |
| 62 | |
| 63 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 64 base::ThreadTaskRunnerHandle task_runner_handle_; | |
| 65 | |
| 66 TestingProfile profile_; | |
| 67 }; | |
| 68 | |
| 69 OfflinePageUtilsBasicTest::OfflinePageUtilsBasicTest() | |
| 70 : task_runner_(new base::TestSimpleTaskRunner), | |
| 71 task_runner_handle_(task_runner_) {} | |
| 72 | |
| 73 OfflinePageUtilsBasicTest::~OfflinePageUtilsBasicTest() {} | |
| 74 | |
| 75 void OfflinePageUtilsBasicTest::SetUp() { | |
| 76 FactorySetup(); | |
| 77 PumpLoop(); | |
| 78 } | |
| 79 | |
| 80 void OfflinePageUtilsBasicTest::FactorySetup() { | |
| 81 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 82 &profile_, BuildTestOfflinePageModel); | |
| 83 } | |
| 84 | |
| 85 void OfflinePageUtilsBasicTest::DisableOfflinePagesFeature() { | |
| 86 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 87 switches::kDisableOfflinePages); | |
| 88 } | |
| 89 | |
| 90 void OfflinePageUtilsBasicTest::EnableOfflinePagesFeature() { | |
| 91 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 92 switches::kEnableOfflinePages); | |
| 93 } | |
| 94 | |
| 95 void OfflinePageUtilsBasicTest::PumpLoop() { | |
| 96 task_runner_->RunUntilIdle(); | |
| 97 } | |
| 98 | |
| 99 // Simple test for checking if offline page has pages when the feature is | |
| 100 // disabled. | |
| 101 TEST_F(OfflinePageUtilsBasicTest, HasOfflinePagesWhenFeatureDisabled) { | |
|
jianli
2015/12/15 23:53:34
HasOfflinePagesWhenFeatureDisabled is confusing. T
fgorski
2015/12/16 21:43:16
Done.
| |
| 102 DisableOfflinePagesFeature(); | |
| 103 EXPECT_FALSE(HasOfflinePages(profile())); | |
| 104 } | |
| 105 | |
| 106 // Simple test for offline page model having none pages loaded. | |
| 107 TEST_F(OfflinePageUtilsBasicTest, HasOfflinePagesWhenStoreIsEmpty) { | |
|
jianli
2015/12/15 23:53:34
ditto for test method name
fgorski
2015/12/16 21:43:16
Done.
| |
| 108 EnableOfflinePagesFeature(); | |
| 109 EXPECT_FALSE(HasOfflinePages(profile())); | |
| 110 } | |
| 111 | |
| 112 class OfflinePageUtilsTest : public OfflinePageUtilsBasicTest { | |
| 113 public: | |
| 114 ~OfflinePageUtilsTest() override; | |
| 115 | |
| 116 void SetUp() override; | |
| 117 | |
| 118 OfflinePageTestStore* store() { return store_; } | |
|
jianli
2015/12/15 23:53:34
nit: add const modifier
fgorski
2015/12/16 21:43:16
Done.
| |
| 119 OfflinePageModel* model() { return model_; } | |
|
jianli
2015/12/15 23:53:34
ditto
fgorski
2015/12/16 21:43:16
Done.
| |
| 120 | |
| 121 const base::FilePath& test_file_path_1() { return test_file_path_1_; } | |
|
jianli
2015/12/15 23:53:34
ditto for this and two below
fgorski
2015/12/16 21:43:16
Done.
| |
| 122 const base::FilePath& test_file_path_2() { return test_file_path_2_; } | |
| 123 const base::FilePath& test_file_path_3() { return test_file_path_3_; } | |
| 124 | |
| 125 private: | |
| 126 void CreateOfflinePages(); | |
| 127 base::FilePath CreateArchiveFile(const base::FilePath& file_name); | |
| 128 | |
| 129 OfflinePageModel* model_; | |
| 130 OfflinePageTestStore* store_; | |
| 131 | |
| 132 base::FilePath test_file_path_1_; | |
| 133 base::FilePath test_file_path_2_; | |
| 134 base::FilePath test_file_path_3_; | |
| 135 }; | |
| 136 | |
| 137 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} | |
| 138 | |
| 139 void OfflinePageUtilsTest::SetUp() { | |
| 140 EnableOfflinePagesFeature(); | |
| 141 | |
| 142 FactorySetup(); | |
| 143 model_ = OfflinePageModelFactory::GetForBrowserContext(profile()); | |
| 144 store_ = static_cast<OfflinePageTestStore*>(model()->GetStoreForTesting()); | |
|
dewittj
2015/12/15 18:30:31
Any way to get this without doing a static_cast?
fgorski
2015/12/16 21:43:16
Done.
| |
| 145 // Make sure the store contains the right offline pages before the load | |
| 146 // happens. | |
| 147 CreateOfflinePages(); | |
| 148 // Load from store happens here. | |
| 149 PumpLoop(); | |
| 150 } | |
| 151 | |
| 152 void OfflinePageUtilsTest::CreateOfflinePages() { | |
| 153 // Create offline copies. | |
| 154 base::FilePath archives_dir = | |
| 155 profile()->GetPath().Append(chrome::kOfflinePageArchviesDirname); | |
| 156 base::CreateDirectory(archives_dir); | |
| 157 test_file_path_1_ = CreateArchiveFile(base::FilePath(kTestPage1FilePath)); | |
| 158 test_file_path_2_ = CreateArchiveFile(base::FilePath(kTestPage2FilePath)); | |
| 159 test_file_path_3_ = | |
| 160 archives_dir.Append(FILE_PATH_LITERAL("missing_file.mhtml")); | |
| 161 | |
| 162 // Update store metadata. | |
| 163 store()->AddOrUpdateOfflinePage( | |
| 164 OfflinePageItem(GURL(kTestPage1Url), kTestPage1BookmarkId, | |
| 165 test_file_path_1(), kTestPage1FileSize), | |
| 166 base::Callback<void(bool)>()); | |
| 167 store()->AddOrUpdateOfflinePage( | |
| 168 OfflinePageItem(GURL(kTestPage2Url), kTestPage2BookmarkId, | |
| 169 test_file_path_2(), kTestPage2FileSize), | |
| 170 base::Callback<void(bool)>()); | |
| 171 } | |
| 172 | |
| 173 base::FilePath OfflinePageUtilsTest::CreateArchiveFile( | |
| 174 const base::FilePath& file_name) { | |
| 175 base::FilePath archives_dir = | |
| 176 profile()->GetPath().Append(chrome::kOfflinePageArchviesDirname); | |
| 177 base::FilePath file_path(archives_dir.Append(file_name)); | |
| 178 base::File file(file_path, base::File::FLAG_OPEN_ALWAYS); | |
| 179 return file_path; | |
| 180 } | |
| 181 | |
| 182 // Simple test for offline page model having any pages loaded. | |
| 183 TEST_F(OfflinePageUtilsTest, HasOfflinePages) { | |
| 184 EXPECT_TRUE(HasOfflinePages(profile())); | |
| 185 } | |
| 186 | |
| 187 TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) { | |
|
dewittj
2015/12/15 18:30:31
Could this be in the basic tests?
fgorski
2015/12/16 21:43:16
Done.
| |
| 188 // URL is invalid. | |
| 189 EXPECT_FALSE(MightBeOfflineURL(GURL("/test.mhtml"))); | |
| 190 // Scheme is not file. | |
| 191 EXPECT_FALSE(MightBeOfflineURL(GURL("http://test.com/"))); | |
| 192 // Does not end with .mhtml. | |
| 193 EXPECT_FALSE(MightBeOfflineURL(GURL("file:///test.txt"))); | |
| 194 // Might still be an offline page. | |
| 195 EXPECT_TRUE(MightBeOfflineURL(GURL("file:///test.mhtml"))); | |
| 196 } | |
| 197 | |
| 198 TEST_F(OfflinePageUtilsTest, GetOfflineURLByOnlineURL) { | |
| 199 EXPECT_EQ(net::FilePathToFileURL(test_file_path_1()), | |
| 200 GetOfflineURLByOnlineURL(profile(), GURL(kTestPage1Url))); | |
| 201 EXPECT_EQ(net::FilePathToFileURL(test_file_path_2()), | |
| 202 GetOfflineURLByOnlineURL(profile(), GURL(kTestPage2Url))); | |
| 203 EXPECT_EQ(GURL(), GetOfflineURLByOnlineURL(profile(), GURL(kTestPage3Url))); | |
| 204 } | |
| 205 | |
| 206 TEST_F(OfflinePageUtilsTest, GetOnlineURLByOfflineURL) { | |
| 207 EXPECT_EQ(GURL(kTestPage1Url), | |
| 208 GetOnlineURLByOfflineURL( | |
| 209 profile(), net::FilePathToFileURL(test_file_path_1()))); | |
| 210 EXPECT_EQ(GURL(kTestPage2Url), | |
| 211 GetOnlineURLByOfflineURL( | |
| 212 profile(), net::FilePathToFileURL(test_file_path_2()))); | |
| 213 EXPECT_EQ(GURL(), GetOfflineURLByOnlineURL( | |
| 214 profile(), net::FilePathToFileURL(test_file_path_3()))); | |
| 215 } | |
| 216 | |
| 217 TEST_F(OfflinePageUtilsTest, GetBookmarkIdByOfflineURL) { | |
| 218 EXPECT_EQ(kTestPage1BookmarkId, | |
| 219 GetBookmarkIdByOfflineURL( | |
| 220 profile(), net::FilePathToFileURL(test_file_path_1()))); | |
| 221 EXPECT_EQ(kTestPage2BookmarkId, | |
| 222 GetBookmarkIdByOfflineURL( | |
| 223 profile(), net::FilePathToFileURL(test_file_path_2()))); | |
| 224 EXPECT_EQ(-1, GetBookmarkIdByOfflineURL( | |
| 225 profile(), net::FilePathToFileURL(test_file_path_3()))); | |
| 226 } | |
| 227 | |
| 228 TEST_F(OfflinePageUtilsTest, IsOfflinePage) { | |
| 229 EXPECT_TRUE( | |
| 230 IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_1()))); | |
| 231 EXPECT_TRUE( | |
| 232 IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_2()))); | |
| 233 EXPECT_FALSE( | |
| 234 IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_3()))); | |
| 235 EXPECT_FALSE(IsOfflinePage(profile(), GURL(kTestPage1Url))); | |
| 236 EXPECT_FALSE(IsOfflinePage(profile(), GURL(kTestPage2Url))); | |
| 237 } | |
| 238 | |
| 239 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { | |
| 240 EXPECT_TRUE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage1Url))); | |
| 241 EXPECT_TRUE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage2Url))); | |
| 242 EXPECT_FALSE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage3Url))); | |
| 243 } | |
| 244 | |
| 245 } // namespace android | |
| 246 } // namespace offline_pages | |
| OLD | NEW |