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
|
#!/usr/bin/python
# Copyright (c) 2010 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']
self._bookmark_folder_items = ['Google', u'Google \ub3c4\uc11c']
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.
"""
zf = zipfile.ZipFile(profile_zip)
# Make base.
pushd = os.getcwd()
try:
if not os.path.isdir(dir):
os.mkdir(dir)
os.chdir(dir)
# Extract files.
for info in zf.infolist():
name = info.filename
if name.endswith('/'): # It's a directory.
if not os.path.isdir(name):
os.makedirs(name)
else: # It's a file.
dir = os.path.dirname(name)
if dir and not os.path.isdir(dir):
os.makedirs(dir)
out = open(name, 'wb')
out.write(zf.read(name))
out.close()
# Set permissions.
os.chmod(name, 0777)
finally:
os.chdir(pushd)
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):
"""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.
"""
confirmed_titles = set()
bookmarks = self.GetBookmarkModel()
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):
"""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)
self._CheckForBookmarks(self._bookmark_folder_items, False)
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
# Tests.
def testFirefoxImportFromPrefs(self):
"""Verify importing 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)
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
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 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()))
if __name__ == '__main__':
pyauto_functional.Main()
|