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_archiver.h" | |
21 #include "components/offline_pages/offline_page_test_store.h" | |
22 #include "net/base/filename_util.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 #include "url/gurl.h" | |
25 | |
26 namespace offline_pages { | |
27 namespace { | |
28 | |
29 const GURL kTestPage1Url("http://test.org/page1"); | |
30 const GURL kTestPage2Url("http://test.org/page2"); | |
31 const GURL kTestPage3Url("http://test.org/page3"); | |
32 int64 kTestPage1BookmarkId = 1234; | |
jianli
2015/12/16 23:08:08
nit: why not also adding const for this and one be
fgorski
2015/12/17 19:00:14
Done.
| |
33 int64 kTestPage2BookmarkId = 5678; | |
34 const int64 kTestFileSize = 876543LL; | |
35 | |
36 } // namespace | |
37 | |
38 class OfflinePageUtilsTest | |
39 : public testing::Test, | |
40 public OfflinePageTestArchiver::Observer, | |
41 public base::SupportsWeakPtr<OfflinePageUtilsTest> { | |
42 public: | |
43 OfflinePageUtilsTest(); | |
44 ~OfflinePageUtilsTest() override; | |
45 | |
46 void SetUp() override; | |
47 void RunUntilIdle(); | |
48 | |
49 // Necessary callbacks for the offline page model. | |
50 void OnSavePageDone(OfflinePageModel::SavePageResult result); | |
51 void OnClearAllDone(); | |
52 | |
53 // OfflinePageTestArchiver::Observer implementation: | |
54 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | |
55 | |
56 const GURL& offline_url_1() const { return offline_url_1_; } | |
jianli
2015/12/16 23:08:08
Please comment on these URLs, like their behaviors
fgorski
2015/12/17 19:00:14
Done.
| |
57 const GURL& offline_url_2() const { return offline_url_2_; } | |
58 const GURL& offline_url_3() const { return offline_url_3_; } | |
59 TestingProfile* profile() { return &profile_; } | |
60 | |
61 private: | |
62 void CreateOfflinePages(); | |
63 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( | |
64 const GURL& url, | |
65 const base::FilePath& file_name); | |
66 | |
67 GURL offline_url_1_; | |
68 GURL offline_url_2_; | |
69 GURL offline_url_3_; | |
70 | |
71 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
72 base::ThreadTaskRunnerHandle task_runner_handle_; | |
73 TestingProfile profile_; | |
74 }; | |
75 | |
76 OfflinePageUtilsTest::OfflinePageUtilsTest() | |
77 : task_runner_(new base::TestSimpleTaskRunner), | |
78 task_runner_handle_(task_runner_) {} | |
79 | |
80 OfflinePageUtilsTest::~OfflinePageUtilsTest() {} | |
81 | |
82 void OfflinePageUtilsTest::SetUp() { | |
83 // Enable offline pages feature. | |
84 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
85 switches::kEnableOfflinePages); | |
86 | |
87 // Set up the factory for testing. | |
88 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | |
89 &profile_, BuildTestOfflinePageModel); | |
90 RunUntilIdle(); | |
91 | |
92 // Make sure the store contains the right offline pages before the load | |
93 // happens. | |
94 CreateOfflinePages(); | |
95 } | |
96 | |
97 void OfflinePageUtilsTest::RunUntilIdle() { | |
98 task_runner_->RunUntilIdle(); | |
99 } | |
100 | |
101 void OfflinePageUtilsTest::OnSavePageDone( | |
102 OfflinePageModel::SavePageResult result) { | |
103 // Result ignored here. | |
104 } | |
105 | |
106 void OfflinePageUtilsTest::OnClearAllDone() { | |
107 // Result ignored here. | |
108 } | |
109 | |
110 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver( | |
111 const base::FilePath& file_path) { | |
112 LOG(INFO) << file_path.value(); | |
113 if (offline_url_1_.is_empty()) { | |
114 offline_url_1_ = net::FilePathToFileURL(file_path); | |
jianli
2015/12/16 23:08:07
I think this trick is not better than trying to ge
fgorski
2015/12/17 19:00:14
Done, but I simply decided to ask the model, which
| |
115 return; | |
116 } | |
117 | |
118 ASSERT_TRUE(offline_url_2_.is_empty()); | |
119 offline_url_2_ = net::FilePathToFileURL(file_path); | |
120 } | |
121 | |
122 void OfflinePageUtilsTest::CreateOfflinePages() { | |
123 // Create page 1. | |
124 scoped_ptr<OfflinePageTestArchiver> archiver( | |
125 BuildArchiver(kTestPage1Url, | |
126 base::FilePath(FILE_PATH_LITERAL("page1.mhtml"))) | |
127 .Pass()); | |
128 OfflinePageModelFactory::GetForBrowserContext(profile())->SavePage( | |
129 kTestPage1Url, kTestPage1BookmarkId, archiver.Pass(), | |
130 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | |
131 RunUntilIdle(); | |
132 | |
133 // Create page 2. | |
134 archiver = BuildArchiver(kTestPage2Url, | |
135 base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))) | |
136 .Pass(); | |
137 OfflinePageModelFactory::GetForBrowserContext(profile())->SavePage( | |
138 kTestPage2Url, kTestPage2BookmarkId, archiver.Pass(), | |
139 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr())); | |
140 RunUntilIdle(); | |
141 | |
142 // Create path to 3rd file. | |
jianli
2015/12/16 23:08:07
nit: Create a file that is not associated with any
fgorski
2015/12/17 19:00:14
Done.
| |
143 offline_url_3_ = net::FilePathToFileURL( | |
144 profile() | |
145 ->GetPath() | |
146 .Append(chrome::kOfflinePageArchviesDirname) | |
147 .Append(FILE_PATH_LITERAL("missing_file.mhtml"))); | |
148 } | |
149 | |
150 scoped_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver( | |
151 const GURL& url, | |
152 const base::FilePath& file_name) { | |
153 scoped_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( | |
154 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | |
155 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); | |
156 archiver->set_file_name(file_name); | |
157 return archiver.Pass(); | |
158 } | |
159 | |
160 // Simple test for offline page model having any pages loaded. | |
161 TEST_F(OfflinePageUtilsTest, HasOfflinePages) { | |
162 EXPECT_TRUE(OfflinePageUtils::HasOfflinePages(profile())); | |
163 | |
164 OfflinePageModelFactory::GetForBrowserContext(profile())->ClearAll( | |
165 base::Bind(&OfflinePageUtilsTest::OnClearAllDone, AsWeakPtr())); | |
166 RunUntilIdle(); | |
167 | |
168 EXPECT_FALSE(OfflinePageUtils::HasOfflinePages(profile())); | |
169 } | |
170 | |
171 TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) { | |
172 // URL is invalid. | |
173 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("/test.mhtml"))); | |
174 // Scheme is not file. | |
175 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("http://test.com/"))); | |
176 // Does not end with .mhtml. | |
177 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.txt"))); | |
178 // Might still be an offline page. | |
179 EXPECT_TRUE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.mhtml"))); | |
180 } | |
181 | |
182 TEST_F(OfflinePageUtilsTest, GetOfflineURLByOnlineURL) { | |
183 EXPECT_EQ(offline_url_1(), OfflinePageUtils::GetOfflineURLByOnlineURL( | |
184 profile(), kTestPage1Url)); | |
185 EXPECT_EQ(offline_url_2(), OfflinePageUtils::GetOfflineURLByOnlineURL( | |
186 profile(), kTestPage2Url)); | |
187 EXPECT_EQ(GURL(), OfflinePageUtils::GetOfflineURLByOnlineURL( | |
188 profile(), GURL(kTestPage3Url))); | |
189 } | |
190 | |
191 TEST_F(OfflinePageUtilsTest, GetOnlineURLByOfflineURL) { | |
192 EXPECT_EQ(kTestPage1Url, OfflinePageUtils::GetOnlineURLByOfflineURL( | |
193 profile(), offline_url_1())); | |
194 EXPECT_EQ(kTestPage2Url, OfflinePageUtils::GetOnlineURLByOfflineURL( | |
195 profile(), offline_url_2())); | |
196 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::GetOfflineURLByOnlineURL( | |
197 profile(), offline_url_3())); | |
198 } | |
199 | |
200 TEST_F(OfflinePageUtilsTest, GetBookmarkIdByOfflineURL) { | |
201 EXPECT_EQ(kTestPage1BookmarkId, OfflinePageUtils::GetBookmarkIdByOfflineURL( | |
202 profile(), offline_url_1())); | |
203 EXPECT_EQ(kTestPage2BookmarkId, OfflinePageUtils::GetBookmarkIdByOfflineURL( | |
204 profile(), offline_url_2())); | |
205 EXPECT_EQ(-1, OfflinePageUtils::GetBookmarkIdByOfflineURL(profile(), | |
206 offline_url_3())); | |
207 } | |
208 | |
209 TEST_F(OfflinePageUtilsTest, IsOfflinePage) { | |
210 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_1())); | |
211 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_2())); | |
212 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_3())); | |
213 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage1Url)); | |
214 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage2Url)); | |
215 } | |
216 | |
217 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) { | |
218 EXPECT_TRUE( | |
219 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url)); | |
220 EXPECT_TRUE( | |
221 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url)); | |
222 EXPECT_FALSE( | |
223 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url)); | |
224 } | |
225 | |
226 } // namespace offline_pages | |
OLD | NEW |