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 { | |
|
jianli
2015/12/15 01:45:24
nit: empty line
fgorski
2015/12/15 16:33:02
Done.
| |
| 29 const char kTestPage1Url[] = "http://test.org/page1"; | |
| 30 const char kTestPage2Url[] = "http://test.org/page2"; | |
| 31 const char kTestPage3Url[] = "http://test.org/page3"; | |
| 32 int64 kTestPage1BookmarkId = 1234; | |
| 33 int64 kTestPage2BookmarkId = 5678; | |
| 34 const base::FilePath::CharType kTestPage1FilePath[] = | |
| 35 FILE_PATH_LITERAL("test_page1.mhtml"); | |
| 36 const base::FilePath::CharType kTestPage2FilePath[] = | |
| 37 FILE_PATH_LITERAL("test_page2.mhtml"); | |
| 38 // const base::FilePath::CharType kTestPage3FilePath[] = | |
| 39 // FILE_PATH_LITERAL("/offline_pages/test_org_page3.mhtml"); | |
| 40 int64 kTestPage1FileSize = 654321; | |
| 41 int64 kTestPage2FileSize = 765432; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 class OfflinePageUtilsBasicTest : public testing::Test { | |
| 46 public: | |
| 47 OfflinePageUtilsBasicTest(); | |
| 48 ~OfflinePageUtilsBasicTest() override; | |
| 49 | |
| 50 void SetUp() override; | |
| 51 | |
| 52 void FactorySetup(); | |
| 53 void DisableOfflinePagesFeature(); | |
| 54 void EnableOfflinePagesFeature(); | |
| 55 void PumpLoop(); | |
| 56 | |
| 57 TestingProfile* profile() { return &profile_; } | |
| 58 | |
| 59 private: | |
| 60 base::FilePath CreateArchiveFile(const base::FilePath& file_name); | |
| 61 | |
| 62 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 63 base::ThreadTaskRunnerHandle task_runner_handle_; | |
| 64 | |
| 65 TestingProfile profile_; | |
| 66 }; | |
| 67 | |
| 68 class OfflinePageUtilsTest : public OfflinePageUtilsBasicTest { | |
|
jianli
2015/12/15 01:45:24
This class should be grouped with all its tests.
fgorski
2015/12/15 16:33:02
Done.
| |
| 69 public: | |
| 70 ~OfflinePageUtilsTest() override; | |
| 71 | |
| 72 void SetUp() override; | |
| 73 | |
| 74 void PreloadStore(); | |
| 75 | |
| 76 OfflinePageTestStore* store() { return store_; } | |
| 77 OfflinePageModel* model() { return model_; } | |
| 78 | |
| 79 const base::FilePath& test_file_path_1() { return test_file_path_1_; } | |
| 80 const base::FilePath& test_file_path_2() { return test_file_path_2_; } | |
| 81 const base::FilePath& test_file_path_3() { return test_file_path_3_; } | |
| 82 | |
| 83 private: | |
| 84 base::FilePath CreateArchiveFile(const base::FilePath& file_name); | |
| 85 | |
| 86 OfflinePageModel* model_; | |
| 87 OfflinePageTestStore* store_; | |
| 88 | |
| 89 base::FilePath test_file_path_1_; | |
| 90 base::FilePath test_file_path_2_; | |
| 91 base::FilePath test_file_path_3_; | |
| 92 }; | |
| 93 | |
| 94 OfflinePageUtilsBasicTest::OfflinePageUtilsBasicTest() | |
| 95 : task_runner_(new base::TestSimpleTaskRunner), | |
| 96 task_runner_handle_(task_runner_) {} | |
| 97 | |
| 98 OfflinePageUtilsBasicTest::~OfflinePageUtilsBasicTest() {} | |
| 99 | |
| 100 void OfflinePageUtilsBasicTest::SetUp() { | |
| 101 FactorySetup(); | |
| 102 PumpLoop(); | |
| 103 } | |
| 104 | |
| 105 void OfflinePageUtilsBasicTest::FactorySetup() { | |
| 106 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 107 &profile_, BuildTestOfflinePageModel); | |
| 108 } | |
| 109 | |
| 110 void OfflinePageUtilsBasicTest::DisableOfflinePagesFeature() { | |
| 111 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 112 switches::kDisableOfflinePages); | |
| 113 } | |
| 114 | |
| 115 void OfflinePageUtilsBasicTest::EnableOfflinePagesFeature() { | |
| 116 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 117 switches::kEnableOfflinePages); | |
| 118 } | |
| 119 | |
| 120 void OfflinePageUtilsBasicTest::PumpLoop() { | |
| 121 task_runner_->RunUntilIdle(); | |
| 122 } | |
| 123 | |
| 124 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} | |
| 125 | |
| 126 void OfflinePageUtilsTest::SetUp() { | |
| 127 EnableOfflinePagesFeature(); | |
| 128 | |
| 129 base::FilePath archives_dir = | |
| 130 profile()->GetPath().Append(chrome::kOfflinePageArchviesDirname); | |
| 131 base::CreateDirectory(archives_dir); | |
| 132 test_file_path_1_ = CreateArchiveFile(base::FilePath(kTestPage1FilePath)); | |
| 133 test_file_path_2_ = CreateArchiveFile(base::FilePath(kTestPage2FilePath)); | |
| 134 test_file_path_3_ = | |
| 135 archives_dir.Append(FILE_PATH_LITERAL("missing_file.mhtml")); | |
| 136 | |
| 137 FactorySetup(); | |
| 138 model_ = OfflinePageModelFactory::GetForBrowserContext(profile()); | |
| 139 store_ = static_cast<OfflinePageTestStore*>(model()->GetStoreForTesting()); | |
| 140 // Make sure the store contains the right offline pages before the load | |
| 141 // happens. | |
| 142 PreloadStore(); | |
|
jianli
2015/12/15 01:45:24
We're really not interested in testing loading log
fgorski
2015/12/15 16:33:02
Moved the offline page creation related code to a
| |
| 143 // Load from store happens here. | |
| 144 PumpLoop(); | |
| 145 } | |
| 146 | |
| 147 void OfflinePageUtilsTest::PreloadStore() { | |
| 148 std::vector<OfflinePageItem> offline_pages; | |
| 149 offline_pages.push_back( | |
| 150 OfflinePageItem(GURL(kTestPage1Url), kTestPage1BookmarkId, | |
| 151 test_file_path_1(), kTestPage1FileSize)); | |
| 152 offline_pages.push_back( | |
| 153 OfflinePageItem(GURL(kTestPage2Url), kTestPage2BookmarkId, | |
| 154 test_file_path_2(), kTestPage2FileSize)); | |
| 155 store()->SetStoreState(offline_pages); | |
| 156 } | |
| 157 | |
| 158 base::FilePath OfflinePageUtilsTest::CreateArchiveFile( | |
| 159 const base::FilePath& file_name) { | |
| 160 base::FilePath archives_dir = | |
| 161 profile()->GetPath().Append(chrome::kOfflinePageArchviesDirname); | |
| 162 base::FilePath file_path(archives_dir.Append(file_name)); | |
| 163 base::File file(file_path, base::File::FLAG_OPEN_ALWAYS); | |
| 164 return file_path; | |
| 165 } | |
| 166 | |
| 167 // Simple test for checking if offline page has pages when the feature is | |
| 168 // disabled. | |
| 169 TEST_F(OfflinePageUtilsBasicTest, HasOfflinePagesDisabled) { | |
| 170 DisableOfflinePagesFeature(); | |
|
jianli
2015/12/15 01:45:24
Adding command line switch should normally be done
fgorski
2015/12/15 16:33:02
Notice that I explicitly extracted the methods and
| |
| 171 EXPECT_FALSE(HasOfflinePages(profile())); | |
| 172 } | |
| 173 | |
| 174 // Simple test for offline page model having none pages loaded. | |
| 175 TEST_F(OfflinePageUtilsBasicTest, HasOfflinePagesEmpty) { | |
| 176 EnableOfflinePagesFeature(); | |
| 177 EXPECT_FALSE(HasOfflinePages(profile())); | |
|
jianli
2015/12/15 01:45:24
What's the difference between HasOfflinePagesDisab
fgorski
2015/12/15 16:33:02
Store is empty in the latter case and the feature
jianli
2015/12/15 23:53:34
Both tests only check if the store is empty. Thus
| |
| 178 } | |
| 179 | |
| 180 // Simple test for offline page model having any pages loaded. | |
| 181 TEST_F(OfflinePageUtilsTest, HasOfflinePages) { | |
| 182 EXPECT_TRUE(HasOfflinePages(profile())); | |
| 183 } | |
| 184 | |
| 185 TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) { | |
| 186 // URL is invalid. | |
| 187 EXPECT_FALSE(MightBeOfflineURL(GURL("/test.mhtml"))); | |
| 188 // Scheme is not file. | |
| 189 EXPECT_FALSE(MightBeOfflineURL(GURL("http://test.com/"))); | |
| 190 // Does not end with .mhtml. | |
| 191 EXPECT_FALSE(MightBeOfflineURL(GURL("file:///test.txt"))); | |
| 192 // Might still be an offline page. | |
| 193 EXPECT_TRUE(MightBeOfflineURL(GURL("file:///test.mhtml"))); | |
| 194 } | |
| 195 | |
| 196 TEST_F(OfflinePageUtilsTest, GetOfflineURLByOnlineURL) { | |
| 197 EXPECT_EQ(net::FilePathToFileURL(test_file_path_1()), | |
| 198 GetOfflineURLByOnlineURL(profile(), GURL(kTestPage1Url))); | |
| 199 EXPECT_EQ(net::FilePathToFileURL(test_file_path_2()), | |
| 200 GetOfflineURLByOnlineURL(profile(), GURL(kTestPage2Url))); | |
| 201 EXPECT_EQ(GURL(), GetOfflineURLByOnlineURL(profile(), GURL(kTestPage3Url))); | |
| 202 } | |
| 203 | |
| 204 TEST_F(OfflinePageUtilsTest, GetOnlineURLByOfflineURL) { | |
| 205 EXPECT_EQ(GURL(kTestPage1Url), | |
| 206 GetOnlineURLByOfflineURL( | |
| 207 profile(), net::FilePathToFileURL(test_file_path_1()))); | |
| 208 EXPECT_EQ(GURL(kTestPage2Url), | |
| 209 GetOnlineURLByOfflineURL( | |
| 210 profile(), net::FilePathToFileURL(test_file_path_2()))); | |
| 211 EXPECT_EQ(GURL(), GetOfflineURLByOnlineURL( | |
| 212 profile(), net::FilePathToFileURL(test_file_path_3()))); | |
| 213 } | |
| 214 | |
| 215 TEST_F(OfflinePageUtilsTest, GetBookmarkIdByOfflineURL) { | |
| 216 EXPECT_EQ(kTestPage1BookmarkId, | |
| 217 GetBookmarkIdByOfflineURL( | |
| 218 profile(), net::FilePathToFileURL(test_file_path_1()))); | |
| 219 EXPECT_EQ(kTestPage2BookmarkId, | |
| 220 GetBookmarkIdByOfflineURL( | |
| 221 profile(), net::FilePathToFileURL(test_file_path_2()))); | |
| 222 EXPECT_EQ(-1, GetBookmarkIdByOfflineURL( | |
| 223 profile(), net::FilePathToFileURL(test_file_path_3()))); | |
| 224 } | |
| 225 | |
| 226 TEST_F(OfflinePageUtilsTest, IsOfflinePage) { | |
| 227 EXPECT_TRUE( | |
| 228 IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_1()))); | |
| 229 EXPECT_TRUE( | |
| 230 IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_2()))); | |
| 231 EXPECT_FALSE( | |
| 232 IsOfflinePage(profile(), net::FilePathToFileURL(test_file_path_3()))); | |
| 233 EXPECT_FALSE(IsOfflinePage(profile(), GURL(kTestPage1Url))); | |
| 234 EXPECT_FALSE(IsOfflinePage(profile(), GURL(kTestPage2Url))); | |
| 235 } | |
| 236 | |
| 237 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { | |
| 238 EXPECT_TRUE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage1Url))); | |
| 239 EXPECT_TRUE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage2Url))); | |
| 240 EXPECT_FALSE(HasOfflinePageForOnlineURL(profile(), GURL(kTestPage3Url))); | |
| 241 } | |
| 242 | |
| 243 } // namespace android | |
| 244 } // namespace offline_pages | |
| OLD | NEW |