1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import glob
import logging
import optparse
import os
import shutil
import sys
import tempfile
import zipfile
import pyauto_functional # Must be imported before pyauto
import pyauto
import pyauto_utils
class ImportsTest(pyauto.PyUITest):
"""Import settings from other browsers.
Import settings tables below show which items get imported on first run and
via preferences for different browsers and operating systems.
Bookmarks History SearchEngines Passwords Homepage
Firefox:
Win/FRUI N Y N N Y
Win/Prefs Y Y Y Y N
Mac&Lin/FRUI Y Y Y Y Y
Mac&Lin/Prefs Y Y Y Y N
Safari:
Mac/FRUI Y Y Y Y N
Mac/Prefs Y Y Y Y N
"""
def setUp(self):
self._to_import = ['ALL']
if pyauto.PyUITest.IsMac():
self._firefox_profiles_path = os.path.join(
os.environ['HOME'], 'Library','Application Support','Firefox')
self._firefox_test_profile = os.path.abspath(os.path.join(
pyauto.PyUITest.DataDir(), 'import', 'firefox', 'macwin.zip'))
self._safari_profiles_path = os.path.join(
os.environ['HOME'], 'Library', 'Safari')
# Don't import passwords to avoid Keychain popups. See crbug.com/49378.
self._to_import = ['HISTORY', 'FAVORITES', 'SEARCH_ENGINES', 'HOME_PAGE']
elif pyauto.PyUITest.IsWin():
self._firefox_profiles_path = os.path.join(
os.getenv('APPDATA'), 'Mozilla', 'Firefox')
self._firefox_test_profile = os.path.abspath(os.path.join(
pyauto.PyUITest.DataDir(), 'import', 'firefox', 'macwin.zip'))
else: # Linux
self._firefox_profiles_path = os.path.join(
os.environ['HOME'], '.mozilla', 'firefox')
self._firefox_test_profile = os.path.abspath(os.path.join(
pyauto.PyUITest.DataDir(), 'import', 'firefox', 'linux.zip'))
# Expected items for tests.
self._history_items = ['Google', 'Google News', u'Google \ub3c4\uc11c']
self._bookmark_bar_items = ['Google News', 'Google', u'Google \ub3c4\uc11c']
self._bookmark_folder_items = []
self._password_items = ['etouchqa@gmail.com', 'macqa05']
self._home_page = 'http://news.google.com/'
self._safari_replacer = None
self._firefox_replacer = None
pyauto.PyUITest.setUp(self)
def tearDown(self):
pyauto.PyUITest.tearDown(self)
# Delete any replacers to restore the original profiles.
if self._safari_replacer:
del self._safari_replacer
if self._firefox_replacer:
del self._firefox_replacer
def _UnzipProfileToDir(self, profile_zip, dir):
"""Unzip |profile_zip| into directory |dir|.
Creates |dir| if it doesn't exist.
"""
if not os.path.isdir(dir):
os.makedirs(dir)
zf = zipfile.ZipFile(profile_zip)
for name in zf.namelist():
full_path = os.path.join(dir, name)
if name.endswith('/'):
if not os.path.isdir(full_path):
os.makedirs(full_path)
else:
zf.extract(name, dir)
os.chmod(full_path, 0777)
def _SwapFirefoxProfile(self):
"""Swaps the test Firefox profile with the original one."""
self._firefox_replacer = pyauto_utils.ExistingPathReplacer(
self._firefox_profiles_path)
self._UnzipProfileToDir(self._firefox_test_profile,
self._firefox_profiles_path)
def _SwapSafariProfile(self):
"""Swaps the test Safari profile with the original one."""
self._safari_replacer = pyauto_utils.ExistingPathReplacer(
self._safari_profiles_path)
self._UnzipProfileToDir(
os.path.join(self.DataDir(), 'import', 'safari', 'mac.zip'),
self._safari_profiles_path)
def _CheckForBookmarks(self, bookmark_titles, bookmark_bar, window_index=0):
"""Checks that the given bookmarks exist.
Args:
bookmark_titles: A set of bookmark title strings.
bookmark_bar: True if the bookmarks are part of the bookmark bar.
False otherwise.
window_index: The window index, default is 0.
"""
bookmarks = self.GetBookmarkModel(window_index)
if bookmark_bar:
node = bookmarks.BookmarkBar()
else:
node = bookmarks.Other()
for title in bookmark_titles:
self.assertTrue([x for x in bookmark_titles \
if bookmarks.FindByTitle(title, [node])])
def _BookmarkDuplicatesExist(self, bookmark_titles):
"""Returns true if any of the bookmark titles are duplicated.
Args:
bookmark_titles: A list of bookmark title strings.
"""
bookmarks = self.GetBookmarkModel()
for title in bookmark_titles:
if len(bookmarks.FindByTitle(title)) > 1:
return True
return False
def _CheckForHistory(self, history_titles):
"""Verifies that the given list of history items are in the history.
Args:
history_titles: A list of history title strings.
"""
history = self.GetHistoryInfo().History()
# History import automation is broken - crbug.com/63001
return
for title in history_titles:
self.assertTrue([x for x in history if x['title'] == title])
def _CheckForPasswords(self, usernames):
"""Check that password items exist for the given usernames."""
# Password import automation does not work on Mac. See crbug.com/52124.
if self.IsMac():
return
passwords = self.GetSavedPasswords()
for username in usernames:
self.assertTrue([x for x in passwords if x['username_value'] == username])
def _CheckDefaults(self, bookmarks, history, passwords, home_page,
search_engines, window_index=0):
"""Checks the defaults for each of the possible import items.
All arguments are True if they should be checked, False otherwise."""
if bookmarks:
self._CheckForBookmarks(self._bookmark_bar_items, True, window_index)
self._CheckForBookmarks(self._bookmark_folder_items, False, window_index)
if history:
self._CheckForHistory(self._history_items)
if passwords:
self._CheckForPasswords(self._password_items)
if home_page:
self.assertEqual(self._home_page, self.GetPrefsInfo().Prefs()['homepage'])
# TODO(alyssad): Test for search engines after a hook is added.
# See crbug.com/52009.
def _CanRunFirefoxTests(self):
"""Determine whether we can run firefox imports.
On windows, checks if firefox is installed. Always True on other platforms.
"""
if self.IsWin():
ff_installed = os.path.exists(os.path.join(
os.getenv('ProgramFiles'), 'Mozilla Firefox', 'firefox.exe'))
if not ff_installed:
logging.warn('Firefox not installed.')
return ff_installed
# TODO(nirnimesh): Anything else to be done on other platforms?
return True
def _ImportFromFirefox(self, bookmarks, history, passwords, home_page,
search_engines, window_index=0):
"""Verify importing individual Firefox data through preferences"""
if not self._CanRunFirefoxTests():
logging.warn('Not running firefox import tests.')
return
self._SwapFirefoxProfile()
self.ImportSettings('Mozilla Firefox', False, self._to_import, window_index)
self._CheckDefaults(bookmarks, history, passwords, home_page,
search_engines, window_index)
def _GetProfilePath(self):
"""Get profile paths when multiprofile windows are open.
Returns:
profile: Path for multiprofiles.
"""
profiles_list = self.GetMultiProfileInfo()['profiles']
profile1_path = profile2_path = default_path = None
for profile in profiles_list:
if profile['path'].find('Profile 1') != -1:
profile1_path = profile['path']
elif profile['path'].find('Profile 2') != -1:
profile2_path = profile['path']
elif profile['path'].find('Default') != -1:
default_path = profile['path']
return default_path, profile1_path, profile2_path
# Tests.
def testFirefoxImportFromPrefs(self):
"""Verify importing Firefox data through preferences."""
if not self._CanRunFirefoxTests():
logging.warn('Not running firefox import tests.')
return
if self.IsWinVista(): # Broken on vista. crbug.com/89768
return
self._SwapFirefoxProfile()
self.ImportSettings('Mozilla Firefox', False, self._to_import)
self._CheckDefaults(bookmarks=True, history=True, passwords=True,
home_page=False, search_engines=True)
def testFirefoxFirstRun(self):
"""Verify importing from Firefox on the first run.
For Win, only history and homepage will only be imported.
Mac and Linux can import history, homepage, and bookmarks.
"""
if not self._CanRunFirefoxTests():
logging.warn('Not running firefox import tests.')
return
self._SwapFirefoxProfile()
self.ImportSettings('Mozilla Firefox', True, self._to_import)
non_win = not self.IsWin()
self._CheckDefaults(bookmarks=non_win, history=True, passwords=True,
home_page=non_win, search_engines=True)
def testImportFirefoxDataTwice(self):
"""Verify importing Firefox data twice.
Bookmarks should be duplicated, but history and passwords should not.
"""
if not self._CanRunFirefoxTests():
logging.warn('Not running firefox import tests.')
return
if self.IsWinVista(): # Broken on vista. crbug.com/89768
return
self._SwapFirefoxProfile()
self.ImportSettings('Mozilla Firefox', False, self._to_import)
num_history_orig = len(self.GetHistoryInfo().History())
num_passwords_orig = len(self.GetSavedPasswords())
# Re-import and check for duplicates.
self.ImportSettings('Mozilla Firefox', False, self._to_import)
self.assertTrue(self._BookmarkDuplicatesExist(
self._bookmark_bar_items + self._bookmark_folder_items))
self.assertEqual(num_history_orig, len(self.GetHistoryInfo().History()))
self.assertEqual(num_passwords_orig, len(self.GetSavedPasswords()))
def testImportFirefoxBookmarksFromPrefs(self):
"""Verify importing Firefox bookmarks through preferences."""
self._ImportFromFirefox(bookmarks=True, history=False, passwords=False,
home_page=False, search_engines=False)
def testImportFirefoxHistoryFromPrefs(self):
"""Verify importing Firefox history through preferences."""
self._ImportFromFirefox(bookmarks=False, history=True, passwords=False,
home_page=False, search_engines=False)
def testImportFirefoxPasswordsFromPrefs(self):
"""Verify importing Firefox passwords through preferences."""
if self.IsWinVista(): # Broken on vista. crbug.com/89768
return
self._ImportFromFirefox(bookmarks=False, history=False, passwords=True,
home_page=False, search_engines=False)
def testImportFirefoxSearchEnginesFromPrefs(self):
"""Verify importing Firefox search engines through preferences."""
self._ImportFromFirefox(bookmarks=False, history=False, passwords=False,
home_page=False, search_engines=True)
def testImportFromFirefoxAndSafari(self):
"""Verify importing from Firefox and then Safari."""
# This test is for Mac only.
if not self.IsMac():
return
self._SwapSafariProfile()
self._SwapFirefoxProfile()
self.ImportSettings('Mozilla Firefox', False, self._to_import)
self.ImportSettings('Safari', False, self._to_import)
self._CheckDefaults(bookmarks=True, history=True, passwords=True,
home_page=False, search_engines=True)
self.assertTrue(self._BookmarkDuplicatesExist(
self._bookmark_bar_items + self._bookmark_folder_items))
def testSafariImportFromPrefs(self):
"""Verify importing Safari data through preferences."""
# This test is Mac only.
if not self.IsMac():
return
self._SwapSafariProfile()
self.ImportSettings('Safari', False, self._to_import)
self._CheckDefaults(bookmarks=True, history=True, passwords=False,
home_page=False, search_engines=True)
def testSafariFirstRun(self):
"""Verify importing Safari data on the first run."""
# This test is Mac only.
if not self.IsMac():
return
self._SwapSafariProfile()
self.ImportSettings('Safari', False, self._to_import)
self._CheckDefaults(bookmarks=True, history=True, passwords=False,
home_page=False, search_engines=False)
def testImportSafariDataTwice(self):
"""Verify importing Safari data twice.
Bookmarks should be duplicated, but history and passwords should not."""
# This test is Mac only.
if not self.IsMac():
return
self._SwapSafariProfile()
self.ImportSettings('Safari', False, self._to_import)
num_history_orig = len(self.GetHistoryInfo().History())
num_passwords_orig = len(self.GetSavedPasswords())
# Re-import and check for duplicates.
self.ImportSettings('Safari', False, self._to_import)
self.assertTrue(self._BookmarkDuplicatesExist(
self._bookmark_bar_items + self._bookmark_folder_items))
self.assertEqual(num_history_orig, len(self.GetHistoryInfo().History()))
self.assertEqual(num_passwords_orig, len(self.GetSavedPasswords()))
def testFireFoxImportBookmarksMultiProfile(self):
"""Verify importing Firefox bookmarks through preferences.
Bookmarks are imported from Firefox through the preferences for multiple
profiles."""
# Create new profile, import bookmarks from firefox.
self.OpenNewBrowserWindowWithNewProfile()
self._ImportFromFirefox(bookmarks=True, history=False,
passwords=False, home_page=False,
search_engines=False, window_index=1)
# Create new profile, add 'BING', 'DB' as bookmark.
self.OpenNewBrowserWindowWithNewProfile()
bookmarks = self.GetBookmarkModel(2)
bar_id = bookmarks.BookmarkBar()['id']
self.AddBookmarkURL(bar_id, 0, 'BING', 'http://www.bing.com/', 2)
self.AddBookmarkURL(bar_id, 0, 'DB', 'http://www.oracle.com/', 2)
default_path, profile1_path, profile2_path = self._GetProfilePath()
# Close profile1/profile2 windows.
self.CloseBrowserWindow(2)
self.CloseBrowserWindow(1)
# Launch profile2.
self.OpenProfileWindow(path=profile2_path)
# Verify bookmark imported from firefox 'GoogleNews' in
# profile2 is not present.
bookmarks = self.GetBookmarkModel(1)
node = bookmarks.FindByTitle('GoogleNews')
self.assertEqual(0, len(node))
# Assert if 'BING' is present.
node = bookmarks.FindByTitle('BING')
self.assertEqual(1, len(node))
if __name__ == '__main__':
pyauto_functional.Main()
|