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

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: Fix another clang problem 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/ie_importer_test_registry_overrider_win.h"
23 #include "chrome/common/importer/imported_bookmark_entry.h"
24 #include "chrome/common/importer/importer_bridge.h"
25 #include "chrome/common/importer/importer_data_types.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());
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 IE registry keys like favorites,
153 // settings, password store, etc.
154 IEImporterTestRegistryOverrider test_registry_overrider_;
155 };
156
157 IN_PROC_BROWSER_TEST_F(EdgeImporterBrowserTest, EdgeImporter) {
158 // Only verified to work with ESE library on Windows 8.1 and above.
159 if (base::win::GetVersion() < base::win::VERSION_WIN8_1)
160 return;
161
162 const BookmarkInfo kEdgeBookmarks[] = {
163 {true,
164 2,
165 {"Links", "SubFolderOfLinks"},
166 L"SubLink",
167 "http://www.links-sublink.com/"},
168 {true, 1, {"Links"}, L"TheLink", "http://www.links-thelink.com/"},
169 {false, 0, {}, L"Google Home Page", "http://www.google.com/"},
170 {false, 0, {}, L"TheLink", "http://www.links-thelink.com/"},
171 {false, 1, {"SubFolder"}, L"Title", "http://www.link.com/"},
172 {false, 0, {}, L"WithPortAndQuery", "http://host:8080/cgi?q=query"},
173 {false, 1, {"a"}, L"\x4E2D\x6587", "http://chinese-title-favorite/"},
174 {false, 0, {}, L"SubFolder", "http://www.subfolder.com/"},
175 {false, 0, {}, L"InvalidFavicon", "http://www.invalid-favicon.com/"},
176 };
177 std::vector<BookmarkInfo> bookmark_entries(
178 kEdgeBookmarks, kEdgeBookmarks + arraysize(kEdgeBookmarks));
179
180 const FaviconGroup kEdgeFaviconGroup[] = {
181 {L"http://www.links-sublink.com/favicon.ico",
182 L"http://www.links-sublink.com"},
183 {L"http://www.links-thelink.com/favicon.ico",
184 L"http://www.links-thelink.com"},
185 {L"http://www.google.com/favicon.ico", L"http://www.google.com"},
186 {L"http://www.links-thelink.com/favicon.ico",
187 L"http://www.links-thelink.com"},
188 {L"http://www.link.com/favicon.ico", L"http://www.link.com"},
189 {L"http://host:8080/favicon.ico", L"http://host:8080/cgi?q=query"},
190 {L"http://chinese-title-favorite/favicon.ico",
191 L"http://chinese-title-favorite"},
192 {L"http://www.subfolder.com/favicon.ico", L"http://www.subfolder.com"},
193 };
194
195 std::vector<FaviconGroup> favicon_groups(
196 kEdgeFaviconGroup, kEdgeFaviconGroup + arraysize(kEdgeFaviconGroup));
197
198 base::FilePath data_path;
199 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_path));
200 data_path = data_path.AppendASCII("edge_profile");
201
202 base::FilePath temp_path = temp_dir_.path();
203 ASSERT_TRUE(base::CopyDirectory(data_path, temp_path, true));
204 ASSERT_TRUE(DecompressDatabase(temp_path.AppendASCII("edge_profile")));
205
206 base::string16 key_path(importer::GetEdgeSettingsKey());
207 base::win::RegKey key;
208 ASSERT_EQ(ERROR_SUCCESS,
209 key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_WRITE));
210 key.WriteValue(L"FavoritesESEEnabled", 1);
211 ASSERT_FALSE(importer::IsEdgeFavoritesLegacyMode());
212
213 // Starts to import the above settings.
214 // Deletes itself.
215 ExternalProcessImporterHost* host = new ExternalProcessImporterHost;
216 scoped_refptr<TestObserver> observer(
217 new TestObserver(bookmark_entries, favicon_groups));
218 host->set_observer(observer.get());
219
220 importer::SourceProfile source_profile;
221 source_profile.importer_type = importer::TYPE_EDGE;
222 source_profile.source_path = temp_path.AppendASCII("edge_profile");
223
224 host->StartImportSettings(source_profile, browser()->profile(),
225 importer::FAVORITES, observer.get());
226 base::MessageLoop::current()->Run();
227 }
228
229 IN_PROC_BROWSER_TEST_F(EdgeImporterBrowserTest, EdgeImporterLegacyFallback) {
230 const BookmarkInfo kEdgeBookmarks[] = {
231 {false, 0, {}, L"Google", "http://www.google.com/"}};
232 std::vector<BookmarkInfo> bookmark_entries(
233 kEdgeBookmarks, kEdgeBookmarks + arraysize(kEdgeBookmarks));
234 const FaviconGroup kEdgeFaviconGroup[] = {
235 {L"http://www.google.com/favicon.ico", L"http://www.google.com/"}};
236 std::vector<FaviconGroup> favicon_groups(
237 kEdgeFaviconGroup, kEdgeFaviconGroup + arraysize(kEdgeFaviconGroup));
238
239 base::FilePath data_path;
240 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_path));
241 data_path = data_path.AppendASCII("edge_profile");
242
243 ASSERT_TRUE(base::CopyDirectory(data_path, temp_dir_.path(), true));
244 ASSERT_TRUE(importer::IsEdgeFavoritesLegacyMode());
245
246 // Starts to import the above settings.
247 // Deletes itself.
248 ExternalProcessImporterHost* host = new ExternalProcessImporterHost;
249 scoped_refptr<TestObserver> observer(
250 new TestObserver(bookmark_entries, favicon_groups));
251 host->set_observer(observer.get());
252
253 importer::SourceProfile source_profile;
254 source_profile.importer_type = importer::TYPE_EDGE;
255 base::FilePath source_path = temp_dir_.path().AppendASCII("edge_profile");
256 ASSERT_NE(base::WriteFile(
257 source_path.AppendASCII("Favorites\\Google.url:favicon:$DATA"),
258 kDummyFaviconImageData, sizeof(kDummyFaviconImageData)),
259 -1);
260 source_profile.source_path = source_path;
261
262 host->StartImportSettings(source_profile, browser()->profile(),
263 importer::FAVORITES, observer.get());
264 base::MessageLoop::current()->Run();
265 }
266
267 IN_PROC_BROWSER_TEST_F(EdgeImporterBrowserTest, EdgeImporterNoDatabase) {
268 // Only verified to work with ESE library on Windows 8.1 and above.
269 if (base::win::GetVersion() < base::win::VERSION_WIN8_1)
270 return;
271
272 std::vector<BookmarkInfo> bookmark_entries;
273 std::vector<FaviconGroup> favicon_groups;
274
275 base::string16 key_path(importer::GetEdgeSettingsKey());
276 base::win::RegKey key;
277 ASSERT_EQ(ERROR_SUCCESS,
278 key.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_WRITE));
279 key.WriteValue(L"FavoritesESEEnabled", 1);
280 ASSERT_FALSE(importer::IsEdgeFavoritesLegacyMode());
281
282 // Starts to import the above settings.
283 // Deletes itself.
284 ExternalProcessImporterHost* host = new ExternalProcessImporterHost;
285 scoped_refptr<TestObserver> observer(
286 new TestObserver(bookmark_entries, favicon_groups));
287 host->set_observer(observer.get());
288
289 importer::SourceProfile source_profile;
290 source_profile.importer_type = importer::TYPE_EDGE;
291 source_profile.source_path = temp_dir_.path();
292
293 host->StartImportSettings(source_profile, browser()->profile(),
294 importer::FAVORITES, observer.get());
295 base::MessageLoop::current()->Run();
296 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698