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" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 // Necessary callbacks for the offline page model. | 58 // Necessary callbacks for the offline page model. |
59 void OnSavePageDone(SavePageResult result, int64_t offlineId); | 59 void OnSavePageDone(SavePageResult result, int64_t offlineId); |
60 void OnClearAllDone(); | 60 void OnClearAllDone(); |
61 void OnExpirePageDone(bool success); | 61 void OnExpirePageDone(bool success); |
62 void OnGetURLDone(const GURL& url); | 62 void OnGetURLDone(const GURL& url); |
63 | 63 |
64 // OfflinePageTestArchiver::Observer implementation: | 64 // OfflinePageTestArchiver::Observer implementation: |
65 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | 65 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
66 | 66 |
67 // Offline page URL for the first page. | |
68 const GURL& offline_url_page_1() const { return offline_url_page_1_; } | |
69 // Offline page URL for the second page. | |
70 const GURL& offline_url_page_2() const { return offline_url_page_2_; } | |
71 // Offline page URL not related to any page. | |
72 const GURL& offline_url_missing() const { return offline_url_missing_; } | |
73 // Offline page URL for expired page. | |
74 const GURL& offline_url_expired() const { return offline_url_expired_; } | |
75 | |
76 TestingProfile* profile() { return &profile_; } | 67 TestingProfile* profile() { return &profile_; } |
77 | 68 |
78 int64_t offline_id() const { return offline_id_; } | 69 int64_t offline_id() const { return offline_id_; } |
79 | 70 |
80 private: | 71 private: |
81 void CreateOfflinePages(); | 72 void CreateOfflinePages(); |
82 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( | 73 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( |
83 const GURL& url, | 74 const GURL& url, |
84 const base::FilePath& file_name); | 75 const base::FilePath& file_name); |
85 | 76 |
86 GURL offline_url_page_1_; | |
87 GURL offline_url_page_2_; | |
88 GURL offline_url_missing_; | |
89 GURL offline_url_expired_; | |
90 | |
91 int64_t offline_id_; | 77 int64_t offline_id_; |
92 GURL url_; | 78 GURL url_; |
93 | 79 |
94 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 80 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
95 base::ThreadTaskRunnerHandle task_runner_handle_; | 81 base::ThreadTaskRunnerHandle task_runner_handle_; |
96 TestingProfile profile_; | 82 TestingProfile profile_; |
97 }; | 83 }; |
98 | 84 |
99 OfflinePageUtilsTest::OfflinePageUtilsTest() | 85 OfflinePageUtilsTest::OfflinePageUtilsTest() |
100 : task_runner_(new base::TestSimpleTaskRunner), | 86 : task_runner_(new base::TestSimpleTaskRunner), |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 // Create page 1. | 138 // Create page 1. |
153 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 139 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
154 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); | 140 kTestPage1Url, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); |
155 offline_pages::ClientId client_id; | 141 offline_pages::ClientId client_id; |
156 client_id.name_space = kBookmarkNamespace; | 142 client_id.name_space = kBookmarkNamespace; |
157 client_id.id = kTestPage1ClientId; | 143 client_id.id = kTestPage1ClientId; |
158 model->SavePage( | 144 model->SavePage( |
159 kTestPage1Url, client_id, 0ul, std::move(archiver), | 145 kTestPage1Url, client_id, 0ul, std::move(archiver), |
160 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 146 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
161 RunUntilIdle(); | 147 RunUntilIdle(); |
162 offline_url_page_1_ = | |
163 model->MaybeGetPageByOfflineId(offline_id())->GetOfflineURL(); | |
164 | 148 |
165 // Create page 2. | 149 // Create page 2. |
166 archiver = BuildArchiver(kTestPage2Url, | 150 archiver = BuildArchiver(kTestPage2Url, |
167 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); | 151 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))); |
168 client_id.id = kTestPage2ClientId; | 152 client_id.id = kTestPage2ClientId; |
169 model->SavePage( | 153 model->SavePage( |
170 kTestPage2Url, client_id, 0ul, std::move(archiver), | 154 kTestPage2Url, client_id, 0ul, std::move(archiver), |
171 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 155 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
172 RunUntilIdle(); | 156 RunUntilIdle(); |
173 offline_url_page_2_ = | |
174 model->MaybeGetPageByOfflineId(offline_id())->GetOfflineURL(); | |
175 | |
176 // Page 3 is not created, as it is missing. | |
177 // Create a file path that is not associated with any offline page. | |
178 offline_url_missing_ = net::FilePathToFileURL( | |
179 profile() | |
180 ->GetPath() | |
181 .Append(chrome::kOfflinePageArchivesDirname) | |
182 .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); | |
183 | 157 |
184 // Create page 4 - expired page. | 158 // Create page 4 - expired page. |
185 archiver = BuildArchiver(kTestPage4Url, | 159 archiver = BuildArchiver(kTestPage4Url, |
186 base::FilePath(FILE_PATH_LITERAL("page4.mhtml"))); | 160 base::FilePath(FILE_PATH_LITERAL("page4.mhtml"))); |
187 client_id.id = kTestPage4ClientId; | 161 client_id.id = kTestPage4ClientId; |
188 model->SavePage( | 162 model->SavePage( |
189 kTestPage4Url, client_id, 0ul, std::move(archiver), | 163 kTestPage4Url, client_id, 0ul, std::move(archiver), |
190 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | 164 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); |
191 RunUntilIdle(); | 165 RunUntilIdle(); |
192 const OfflinePageItem* page_4 = model->MaybeGetPageByOfflineId(offline_id()); | |
193 offline_url_expired_ = page_4->GetOfflineURL(); | |
194 model->ExpirePages( | 166 model->ExpirePages( |
195 std::vector<int64_t>({offline_id()}), base::Time::Now(), | 167 std::vector<int64_t>({offline_id()}), base::Time::Now(), |
196 base::Bind(&OfflinePageUtilsTest::OnExpirePageDone, AsWeakPtr())); | 168 base::Bind(&OfflinePageUtilsTest::OnExpirePageDone, AsWeakPtr())); |
197 RunUntilIdle(); | 169 RunUntilIdle(); |
198 } | 170 } |
199 | 171 |
200 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( | 172 std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( |
201 const GURL& url, | 173 const GURL& url, |
202 const base::FilePath& file_name) { | 174 const base::FilePath& file_name) { |
203 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( | 175 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
204 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 176 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
205 base::string16(), kTestFileSize, base::ThreadTaskRunnerHandle::Get())); | 177 base::string16(), kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
206 archiver->set_filename(file_name); | 178 archiver->set_filename(file_name); |
207 return archiver; | 179 return archiver; |
208 } | 180 } |
209 | 181 |
210 TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) { | |
211 // URL is invalid. | |
212 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("/test.mhtml"))); | |
213 // Scheme is not file. | |
214 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("http://test.com/"))); | |
215 // Does not end with .mhtml. | |
216 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.txt"))); | |
217 // Might still be an offline page. | |
218 EXPECT_TRUE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.mhtml"))); | |
219 } | |
220 | |
221 TEST_F(OfflinePageUtilsTest, MaybeGetOnlineURLForOfflineURL) { | |
222 EXPECT_EQ(kTestPage1Url, OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | |
223 profile(), offline_url_page_1())); | |
224 EXPECT_EQ(kTestPage2Url, OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | |
225 profile(), offline_url_page_2())); | |
226 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | |
227 profile(), offline_url_missing())); | |
228 EXPECT_EQ(kTestPage4Url, OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | |
229 profile(), offline_url_expired())); | |
230 } | |
231 | |
232 TEST_F(OfflinePageUtilsTest, IsOfflinePage) { | |
233 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_1())); | |
234 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_2())); | |
235 EXPECT_FALSE( | |
236 OfflinePageUtils::IsOfflinePage(profile(), offline_url_missing())); | |
237 EXPECT_TRUE( | |
238 OfflinePageUtils::IsOfflinePage(profile(), offline_url_expired())); | |
239 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage1Url)); | |
240 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage2Url)); | |
241 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage4Url)); | |
242 } | |
243 | |
244 } // namespace offline_pages | 182 } // namespace offline_pages |
OLD | NEW |