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

Side by Side Diff: chrome/browser/importer/edge_importer_browsertest_win.cc

Issue 1465853002: Implement support for importing favorites from Edge on Windows 10. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added missing include files 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 <vector>
6
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/registry.h"
15 #include "base/win/windows_version.h"
16 #include "chrome/browser/importer/external_process_importer_host.h"
17 #include "chrome/browser/importer/importer_progress_observer.h"
18 #include "chrome/browser/importer/importer_unittest_utils.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/importer/edge_importer_utils_win.h"
22 #include "chrome/common/importer/imported_bookmark_entry.h"
23 #include "chrome/common/importer/importer_bridge.h"
24 #include "chrome/common/importer/importer_data_types.h"
25 #include "chrome/common/importer/importer_test_registry_overrider_win.h"
26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chrome/test/base/testing_profile.h"
28 #include "components/compression/compression_utils.h"
29 #include "components/favicon_base/favicon_usage_data.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31
32 namespace {
33
34 struct FaviconGroup {
35 const base::char16* favicon_url;
36 const base::char16* site_url;
37 };
38
39 class TestObserver : public ProfileWriter,
40 public importer::ImporterProgressObserver {
41 public:
42 explicit TestObserver(
43 const std::vector<BookmarkInfo>& expected_bookmark_entries,
44 const std::vector<FaviconGroup>& expected_favicon_groups)
45 : ProfileWriter(nullptr),
46 bookmark_count_(0),
47 expected_bookmark_entries_(expected_bookmark_entries),
48 expected_favicon_groups_(expected_favicon_groups),
49 favicon_count_(0) {}
50
51 // importer::ImporterProgressObserver:
52 void ImportStarted() override {}
53 void ImportItemStarted(importer::ImportItem item) override {}
54 void ImportItemEnded(importer::ImportItem item) override {}
55 void ImportEnded() override {
56 base::MessageLoop::current()->QuitWhenIdle();
57 EXPECT_EQ(expected_bookmark_entries_.size(), bookmark_count_);
58 EXPECT_EQ(expected_favicon_groups_.size(), favicon_count_);
59 }
60
61 // ProfileWriter:
62 bool BookmarkModelIsLoaded() const override {
63 // Profile is ready for writing.
64 return true;
65 }
66
67 bool TemplateURLServiceIsLoaded() const override { return true; }
68
69 void AddBookmarks(const std::vector<ImportedBookmarkEntry>& bookmarks,
70 const base::string16& top_level_folder_name) override {
71 ASSERT_EQ(expected_bookmark_entries_.size(), bookmarks.size());
72 for (size_t i = 0; i < bookmarks.size(); ++i) {
73 EXPECT_NO_FATAL_FAILURE(
74 TestEqualBookmarkEntry(bookmarks[i], expected_bookmark_entries_[i]))
75 << i;
76 ++bookmark_count_;
77 }
78 }
79
80 void AddFavicons(const favicon_base::FaviconUsageDataList& usage) override {
81 // Importer should group the favicon information for each favicon URL.
82 ASSERT_EQ(expected_favicon_groups_.size(), usage.size());
83 for (size_t i = 0; i < expected_favicon_groups_.size(); ++i) {
84 GURL favicon_url(expected_favicon_groups_[i].favicon_url);
85 std::set<GURL> urls;
86 urls.insert(GURL(expected_favicon_groups_[i].site_url));
87
88 bool expected_favicon_url_found = false;
89 for (size_t j = 0; j < usage.size(); ++j) {
90 if (usage[j].favicon_url == favicon_url) {
91 EXPECT_EQ(urls, usage[j].urls);
92 expected_favicon_url_found = true;
93 break;
94 }
95 }
96 EXPECT_TRUE(expected_favicon_url_found);
97 }
98 favicon_count_ += usage.size();
99 }
100
101 private:
102 ~TestObserver() override {}
103
104 // This is the count of bookmark entries observed during the test.
105 size_t bookmark_count_;
106 // This is the expected list of bookmark entries to observe during the test.
107 std::vector<BookmarkInfo> expected_bookmark_entries_;
108 // This is the expected list of favicon groups to observe during the test.
109 std::vector<FaviconGroup> expected_favicon_groups_;
110 // This is the count of favicon groups observed during the test.
111 size_t favicon_count_;
112 };
113
114 bool DecompressDatabase(const base::FilePath& data_path) {
115 base::FilePath output_file = data_path.Append(
116 L"DataStore\\Data\\nouser1\\120712-0049\\DBStore\\spartan.edb");
117 base::FilePath gzip_file = output_file.AddExtension(L".gz");
118 std::string gzip_data;
119 if (!base::ReadFileToString(gzip_file, &gzip_data))
120 return false;
121 if (!compression::GzipUncompress(gzip_data, &gzip_data))
122 return false;
123 return base::WriteFile(output_file, gzip_data.c_str(), gzip_data.size()) >= 0;
124 }
125
126 const char kDummyFaviconImageData[] =
127 "\x42\x4D" // Magic signature 'BM'
128 "\x1E\x00\x00\x00" // File size
129 "\x00\x00\x00\x00" // Reserved
130 "\x1A\x00\x00\x00" // Offset of the pixel data
131 "\x0C\x00\x00\x00" // Header Size
132 "\x01\x00\x01\x00" // Size: 1x1
133 "\x01\x00" // Reserved
134 "\x18\x00" // 24-bits
135 "\x00\xFF\x00\x00"; // The pixel
136
137 } // namespace
138
139 // These tests need to be browser tests in order to be able to run the OOP
140 // import (via ExternalProcessImporterHost) which launches a utility process.
141 class EdgeImporterBrowserTest : public InProcessBrowserTest {
142 protected:
143 void SetUp() override {
144 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
145
146 // This will launch the browser test and thus needs to happen last.
147 InProcessBrowserTest::SetUp();
148 }
149
150 base::ScopedTempDir temp_dir_;
151
152 // Overrides the default registry key for Edge tests.
153 ImporterTestRegistryOverrider test_registry_overrider_;
154 };
155
156 IN_PROC_BROWSER_TEST_F(EdgeImporterBrowserTest, EdgeImporter) {
157 // Only verified to work with ESE library on Windows 8.1 and above.
158 if (base::win::GetVersion() < base::win::VERSION_WIN8_1)
159 return;
160
161 const BookmarkInfo kEdgeBookmarks[] = {
162 {true,
163 2,
164 {"Links", "SubFolderOfLinks"},
165 L"SubLink",
166 "http://www.links-sublink.com/"},
167 {true, 1, {"Links"}, L"TheLink", "http://www.links-thelink.com/"},
168 {false, 0, {}, L"Google Home Page", "http://www.google.com/"},
169 {false, 0, {}, L"TheLink", "http://www.links-thelink.com/"},
170 {false, 1, {"SubFolder"}, L"Title", "http://www.link.com/"},
171 {false, 0, {}, L"WithPortAndQuery", "http://host:8080/cgi?q=query"},
172 {false, 1, {"a"}, L"\x4E2D\x6587", "http://chinese-title-favorite/"},
173 {false, 0, {}, L"SubFolder", "http://www.subfolder.com/"},
174 {false, 0, {}, L"InvalidFavicon", "http://www.invalid-favicon.com/"},
175 };
176 std::vector<BookmarkInfo> bookmark_entries(
177 kEdgeBookmarks, kEdgeBookmarks + arraysize(kEdgeBookmarks));
178
179 const FaviconGroup kEdgeFaviconGroup[] = {
180 {L"http://www.links-sublink.com/favicon.ico",
181 L"http://www.links-sublink.com"},
182 {L"http://www.links-thelink.com/favicon.ico",
183 L"http://www.links-thelink.com"},
184 {L"http://www.google.com/favicon.ico", L"http://www.google.com"},
185 {L"http://www.links-thelink.com/favicon.ico",
186 L"http://www.links-thelink.com"},
187 {L"http://www.link.com/favicon.ico", L"http://www.link.com"},
188 {L"http://host:8080/favicon.ico", L"http://host:8080/cgi?q=query"},
189 {L"http://chinese-title-favorite/favicon.ico",
190 L"http://chinese-title-favorite"},
191 {L"http://www.subfolder.com/favicon.ico", L"http://www.subfolder.com"},
192 };
193
194 std::vector<FaviconGroup> favicon_groups(
195 kEdgeFaviconGroup, kEdgeFaviconGroup + arraysize(kEdgeFaviconGroup));
196
197 base::FilePath data_path;
198 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_path));
199 data_path = data_path.AppendASCII("edge_profile");
200
201 base::FilePath temp_path = temp_dir_.path();
202 ASSERT_TRUE(base::CopyDirectory(data_path, temp_path, true));
203 ASSERT_TRUE(DecompressDatabase(temp_path.AppendASCII("edge_profile")));
204
205 base::string16 key_path(importer::GetEdgeSettingsKey());
206 base::win::RegKey key;
207 ASSERT_EQ(ERROR_SUCCESS,
208 key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_WRITE));
209 key.WriteValue(L"FavoritesESEEnabled", 1);
210 ASSERT_FALSE(importer::IsEdgeFavoritesLegacyMode());
211
212 // Starts to import the above settings.
213 // Deletes itself.
214 ExternalProcessImporterHost* host = new ExternalProcessImporterHost;
215 scoped_refptr<TestObserver> observer(
216 new TestObserver(bookmark_entries, favicon_groups));
217 host->set_observer(observer.get());
218
219 importer::SourceProfile source_profile;
220 source_profile.importer_type = importer::TYPE_EDGE;
221 source_profile.source_path = temp_path.AppendASCII("edge_profile");
222
223 host->StartImportSettings(source_profile, browser()->profile(),
224 importer::FAVORITES, observer.get());
225 base::MessageLoop::current()->Run();
226 }
227
228 IN_PROC_BROWSER_TEST_F(EdgeImporterBrowserTest, EdgeImporterLegacyFallback) {
229 const BookmarkInfo kEdgeBookmarks[] = {
230 {false, 0, {}, L"Google", "http://www.google.com/"}};
231 std::vector<BookmarkInfo> bookmark_entries(
232 kEdgeBookmarks, kEdgeBookmarks + arraysize(kEdgeBookmarks));
233 const FaviconGroup kEdgeFaviconGroup[] = {
234 {L"http://www.google.com/favicon.ico", L"http://www.google.com/"}};
235 std::vector<FaviconGroup> favicon_groups(
236 kEdgeFaviconGroup, kEdgeFaviconGroup + arraysize(kEdgeFaviconGroup));
237
238 base::FilePath data_path;
239 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_path));
240 data_path = data_path.AppendASCII("edge_profile");
241
242 ASSERT_TRUE(base::CopyDirectory(data_path, temp_dir_.path(), true));
243 ASSERT_TRUE(importer::IsEdgeFavoritesLegacyMode());
244
245 // Starts to import the above settings.
246 // Deletes itself.
247 ExternalProcessImporterHost* host = new ExternalProcessImporterHost;
248 scoped_refptr<TestObserver> observer(
249 new TestObserver(bookmark_entries, favicon_groups));
250 host->set_observer(observer.get());
251
252 importer::SourceProfile source_profile;
253 source_profile.importer_type = importer::TYPE_EDGE;
254 base::FilePath source_path = temp_dir_.path().AppendASCII("edge_profile");
255 ASSERT_NE(base::WriteFile(
256 source_path.AppendASCII("Favorites\\Google.url:favicon:$DATA"),
257 kDummyFaviconImageData, sizeof(kDummyFaviconImageData)),
258 -1);
259 source_profile.source_path = source_path;
260
261 host->StartImportSettings(source_profile, browser()->profile(),
262 importer::FAVORITES, observer.get());
263 base::MessageLoop::current()->Run();
264 }
265
266 IN_PROC_BROWSER_TEST_F(EdgeImporterBrowserTest, EdgeImporterNoDatabase) {
267 // Only verified to work with ESE library on Windows 8.1 and above.
268 if (base::win::GetVersion() < base::win::VERSION_WIN8_1)
269 return;
270
271 std::vector<BookmarkInfo> bookmark_entries;
272 std::vector<FaviconGroup> favicon_groups;
273
274 base::string16 key_path(importer::GetEdgeSettingsKey());
275 base::win::RegKey key;
276 ASSERT_EQ(ERROR_SUCCESS,
277 key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_WRITE));
278 key.WriteValue(L"FavoritesESEEnabled", 1);
279 ASSERT_FALSE(importer::IsEdgeFavoritesLegacyMode());
280
281 // Starts to import the above settings.
282 // Deletes itself.
283 ExternalProcessImporterHost* host = new ExternalProcessImporterHost;
284 scoped_refptr<TestObserver> observer(
285 new TestObserver(bookmark_entries, favicon_groups));
286 host->set_observer(observer.get());
287
288 importer::SourceProfile source_profile;
289 source_profile.importer_type = importer::TYPE_EDGE;
290 source_profile.source_path = temp_dir_.path();
291
292 host->StartImportSettings(source_profile, browser()->profile(),
293 importer::FAVORITES, observer.get());
294 base::MessageLoop::current()->Run();
295 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698