Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_utils_unittest.cc

Issue 1521193002: [Offline pages] Refactor URL conversions from TabAndroid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing final CR feedback Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 const int64 kTestPage1BookmarkId = 1234;
33 const 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 // Offline page URL for the first page.
57 const GURL& offline_url_page_1() const { return offline_url_page_1_; }
58 // Offline page URL for the second page.
59 const GURL& offline_url_page_2() const { return offline_url_page_2_; }
60 // Offline page URL not related to any page.
61 const GURL& offline_url_missing() const { return offline_url_missing_; }
62
63 TestingProfile* profile() { return &profile_; }
64
65 private:
66 void CreateOfflinePages();
67 scoped_ptr<OfflinePageTestArchiver> BuildArchiver(
68 const GURL& url,
69 const base::FilePath& file_name);
70
71 GURL offline_url_page_1_;
72 GURL offline_url_page_2_;
73 GURL offline_url_missing_;
74
75 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
76 base::ThreadTaskRunnerHandle task_runner_handle_;
77 TestingProfile profile_;
78 };
79
80 OfflinePageUtilsTest::OfflinePageUtilsTest()
81 : task_runner_(new base::TestSimpleTaskRunner),
82 task_runner_handle_(task_runner_) {}
83
84 OfflinePageUtilsTest::~OfflinePageUtilsTest() {}
85
86 void OfflinePageUtilsTest::SetUp() {
87 // Enable offline pages feature.
88 base::CommandLine::ForCurrentProcess()->AppendSwitch(
89 switches::kEnableOfflinePages);
90
91 // Set up the factory for testing.
92 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
93 &profile_, BuildTestOfflinePageModel);
94 RunUntilIdle();
95
96 // Make sure the store contains the right offline pages before the load
97 // happens.
98 CreateOfflinePages();
99 }
100
101 void OfflinePageUtilsTest::RunUntilIdle() {
102 task_runner_->RunUntilIdle();
103 }
104
105 void OfflinePageUtilsTest::OnSavePageDone(
106 OfflinePageModel::SavePageResult result) {
107 // Result ignored here.
108 }
109
110 void OfflinePageUtilsTest::OnClearAllDone() {
111 // Result ignored here.
112 }
113
114 void OfflinePageUtilsTest::SetLastPathCreatedByArchiver(
115 const base::FilePath& file_path) {}
116
117 void OfflinePageUtilsTest::CreateOfflinePages() {
118 OfflinePageModel* model =
119 OfflinePageModelFactory::GetForBrowserContext(profile());
120
121 // Create page 1.
122 scoped_ptr<OfflinePageTestArchiver> archiver(
123 BuildArchiver(kTestPage1Url,
124 base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))
125 .Pass());
126 model->SavePage(
127 kTestPage1Url, kTestPage1BookmarkId, archiver.Pass(),
128 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr()));
129 RunUntilIdle();
130
131 // Create page 2.
132 archiver = BuildArchiver(kTestPage2Url,
133 base::FilePath(FILE_PATH_LITERAL("page2.mhtml")))
134 .Pass();
135 model->SavePage(
136 kTestPage2Url, kTestPage2BookmarkId, archiver.Pass(),
137 base::Bind(&OfflinePageUtilsTest::OnSavePageDone, AsWeakPtr()));
138 RunUntilIdle();
139
140 // Make a copy of local paths of the two pages stored in the model.
141 offline_url_page_1_ =
142 model->GetPageByBookmarkId(kTestPage1BookmarkId)->GetOfflineURL();
143 offline_url_page_2_ =
144 model->GetPageByBookmarkId(kTestPage2BookmarkId)->GetOfflineURL();
145 // Create a file path that is not associated with any offline page.
146 offline_url_missing_ = net::FilePathToFileURL(
147 profile()
148 ->GetPath()
149 .Append(chrome::kOfflinePageArchviesDirname)
150 .Append(FILE_PATH_LITERAL("missing_file.mhtml")));
151 }
152
153 scoped_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver(
154 const GURL& url,
155 const base::FilePath& file_name) {
156 scoped_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver(
157 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED,
158 kTestFileSize, base::ThreadTaskRunnerHandle::Get()));
159 archiver->set_filename(file_name);
160 return archiver.Pass();
161 }
162
163 // Simple test for offline page model having any pages loaded.
164 TEST_F(OfflinePageUtilsTest, HasOfflinePages) {
165 EXPECT_TRUE(OfflinePageUtils::HasOfflinePages(profile()));
166
167 OfflinePageModelFactory::GetForBrowserContext(profile())->ClearAll(
168 base::Bind(&OfflinePageUtilsTest::OnClearAllDone, AsWeakPtr()));
169 RunUntilIdle();
170
171 EXPECT_FALSE(OfflinePageUtils::HasOfflinePages(profile()));
172 }
173
174 TEST_F(OfflinePageUtilsTest, MightBeOfflineURL) {
175 // URL is invalid.
176 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("/test.mhtml")));
177 // Scheme is not file.
178 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("http://test.com/")));
179 // Does not end with .mhtml.
180 EXPECT_FALSE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.txt")));
181 // Might still be an offline page.
182 EXPECT_TRUE(OfflinePageUtils::MightBeOfflineURL(GURL("file:///test.mhtml")));
183 }
184
185 TEST_F(OfflinePageUtilsTest, GetOfflineURLForOnlineURL) {
186 EXPECT_EQ(offline_url_page_1(), OfflinePageUtils::GetOfflineURLForOnlineURL(
187 profile(), kTestPage1Url));
188 EXPECT_EQ(offline_url_page_2(), OfflinePageUtils::GetOfflineURLForOnlineURL(
189 profile(), kTestPage2Url));
190 EXPECT_EQ(GURL(), OfflinePageUtils::GetOfflineURLForOnlineURL(
191 profile(), GURL(kTestPage3Url)));
192 }
193
194 TEST_F(OfflinePageUtilsTest, GetOnlineURLForOfflineURL) {
195 EXPECT_EQ(kTestPage1Url, OfflinePageUtils::GetOnlineURLForOfflineURL(
196 profile(), offline_url_page_1()));
197 EXPECT_EQ(kTestPage2Url, OfflinePageUtils::GetOnlineURLForOfflineURL(
198 profile(), offline_url_page_2()));
199 EXPECT_EQ(GURL::EmptyGURL(), OfflinePageUtils::GetOfflineURLForOnlineURL(
200 profile(), offline_url_missing()));
201 }
202
203 TEST_F(OfflinePageUtilsTest, GetBookmarkIdForOfflineURL) {
204 EXPECT_EQ(kTestPage1BookmarkId, OfflinePageUtils::GetBookmarkIdForOfflineURL(
205 profile(), offline_url_page_1()));
206 EXPECT_EQ(kTestPage2BookmarkId, OfflinePageUtils::GetBookmarkIdForOfflineURL(
207 profile(), offline_url_page_2()));
208 EXPECT_EQ(-1, OfflinePageUtils::GetBookmarkIdForOfflineURL(
209 profile(), offline_url_missing()));
210 }
211
212 TEST_F(OfflinePageUtilsTest, IsOfflinePage) {
213 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_1()));
214 EXPECT_TRUE(OfflinePageUtils::IsOfflinePage(profile(), offline_url_page_2()));
215 EXPECT_FALSE(
216 OfflinePageUtils::IsOfflinePage(profile(), offline_url_missing()));
217 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage1Url));
218 EXPECT_FALSE(OfflinePageUtils::IsOfflinePage(profile(), kTestPage2Url));
219 }
220
221 TEST_F(OfflinePageUtilsTest, HasOfflinePageForOnlineURL) {
222 EXPECT_TRUE(
223 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage1Url));
224 EXPECT_TRUE(
225 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage2Url));
226 EXPECT_FALSE(
227 OfflinePageUtils::HasOfflinePageForOnlineURL(profile(), kTestPage3Url));
228 }
229
230 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698