OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import re | |
7 | |
8 import pyauto_functional | |
9 import pyauto | |
10 | |
11 | |
12 class MultiprofileTest(pyauto.PyUITest): | |
13 """Tests for Multi-Profile / Multi-users""" | |
14 | |
15 _RESTORE_STARTUP_URL_VALUE = 4 | |
16 _RESTORE_LASTOPEN_URL_VALUE = 1 | |
17 _RESTORE_DEFAULT_URL_VALUE = 0 | |
18 | |
19 def Debug(self): | |
20 """Test method for experimentation. | |
21 | |
22 This method will not run automatically. | |
23 """ | |
24 while True: | |
25 raw_input('Hit <enter> to dump info.. ') | |
26 self.pprint(self.GetMultiProfileInfo()) | |
27 | |
28 def _GetSearchEngineWithKeyword(self, keyword, windex=0): | |
29 """Get search engine info and return an element that matches keyword. | |
30 | |
31 Args: | |
32 keyword: Search engine keyword field. | |
33 windex: The window index, default is 0. | |
34 | |
35 Returns: | |
36 A search engine info dict or None. | |
37 """ | |
38 match_list = ([x for x in self.GetSearchEngineInfo(windex=windex) | |
39 if x['keyword'] == keyword]) | |
40 if match_list: | |
41 return match_list[0] | |
42 return None | |
43 | |
44 def _SetPreferences(self, dict, windex=0): | |
45 """Sets preferences settings. | |
46 | |
47 Args: | |
48 _dict: Dictionary of key preferences and its value to be set. | |
49 windex: The window index, defaults to 0 (the first window). | |
50 """ | |
51 for key in dict.iterkeys(): | |
52 self.SetPrefs(key, dict[key], windex=windex) | |
53 | |
54 def _SetStartUpPage(self, url, windex=0): | |
55 """Set start up page. | |
56 | |
57 Args: | |
58 url: URL of the page to be set as start up page. | |
59 windex: The window index, default is 0. | |
60 """ | |
61 _dict = {pyauto.kURLsToRestoreOnStartup: [url], | |
62 pyauto.kRestoreOnStartup: self._RESTORE_STARTUP_URL_VALUE} | |
63 self._SetPreferences(_dict, windex=windex) | |
64 prefs_info = self.GetPrefsInfo(windex=windex).Prefs( | |
65 pyauto.kURLsToRestoreOnStartup) | |
66 self.assertTrue(url in prefs_info) | |
67 | |
68 def _SetHomePage(self, url, windex=0): | |
69 """Create new profile and set home page. | |
70 | |
71 Args: | |
72 url: URL of the page to be set as home page | |
73 windex: The window index, default is 0. | |
74 """ | |
75 _dict = {pyauto.kHomePage: url, | |
76 pyauto.kHomePageIsNewTabPage: False, pyauto.kShowHomeButton: True, | |
77 pyauto.kRestoreOnStartup: self._RESTORE_DEFAULT_URL_VALUE} | |
78 self._SetPreferences(_dict, windex=windex) | |
79 self.assertTrue(url in | |
80 self.GetPrefsInfo(windex=windex).Prefs(pyauto.kHomePage)) | |
81 | |
82 def _SetSessionRestoreURLs(self, set_restore, windex=0): | |
83 """Create new profile and set home page. | |
84 | |
85 Args: | |
86 set_restore: Value of action of start up. | |
87 windex: The window index, default is 0. | |
88 """ | |
89 self.NavigateToURL('http://www.google.com/', windex) | |
90 self.AppendTab(pyauto.GURL('http://news.google.com/'), windex) | |
91 num_tabs = self.GetTabCount(windex) | |
92 dict = {pyauto.kRestoreOnStartup: set_restore} | |
93 self._SetPreferences(dict, windex=windex) | |
94 | |
95 def _AddSearchEngine(self, title, keyword, url, windex=0): | |
96 """Add search engine. | |
97 | |
98 Args: | |
99 title: Name for search engine. | |
100 keyword: Keyword, used to initiate a custom search from omnibox. | |
101 url: URL template for this search engine's query. | |
102 windex: The window index, default is 0. | |
103 """ | |
104 self.AddSearchEngine(title, keyword, url, windex=windex) | |
105 name = self._GetSearchEngineWithKeyword(keyword, windex=windex) | |
106 self.assertTrue(name) | |
107 | |
108 def _AssertStartUpPage(self, url, profile='Default'): | |
109 """Asserts start up page for given profile. | |
110 | |
111 Args: | |
112 url: URL of the page to be set as start up page | |
113 profile: The profile name, defaults to 'Default'. | |
114 """ | |
115 self.AppendBrowserLaunchSwitch('--profile-directory=' + profile) | |
116 self.RestartBrowser(clear_profile=False) | |
117 info = self.GetBrowserInfo() | |
118 self.assertEqual(url, info['windows'][0]['tabs'][0]['url'].rstrip('/')) | |
119 self.assertTrue(url in | |
120 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | |
121 | |
122 def _AssertHomePage(self, url, profile='Default'): | |
123 """Asserts home page for given profile. | |
124 | |
125 Args: | |
126 url: URL of the page to be set as home page | |
127 profile: The profile name, defaults to 'Dafault'. | |
128 """ | |
129 self.AppendBrowserLaunchSwitch('--profile-directory=' + profile) | |
130 self.RestartBrowser(clear_profile=False) | |
131 self.assertTrue(url in self.GetPrefsInfo().Prefs(pyauto.kHomePage)) | |
132 | |
133 def _AssertDefaultSearchEngine(self, search_engine, profile='Default'): | |
134 """Asserts default search engine for given profile. | |
135 | |
136 Args: | |
137 search_engine: Name of default search engine. | |
138 profile: The profile name, defaults to 'Default'. | |
139 """ | |
140 self.AppendBrowserLaunchSwitch('--profile-directory=' + profile) | |
141 self.RestartBrowser(clear_profile=False) | |
142 name = self._GetSearchEngineWithKeyword(search_engine) | |
143 self.assertTrue(name['is_default']) | |
144 self.SetOmniboxText('test search') | |
145 self.OmniboxAcceptInput() | |
146 self.assertTrue(re.search(search_engine, self.GetActiveTabURL().spec())) | |
147 | |
148 def _AssertSessionRestore(self, url_list, set_restore, num_tabs=1, | |
149 profile='Default'): | |
150 """Asserts urls when session is set to restored or set default. | |
151 | |
152 Args: | |
153 url_list: List of URL to be restored. | |
154 set_restore: Value of action of start up. | |
155 num_tabs: Number of tabs to be restored, default is 1. | |
156 profile: The profile name, defaults to 'Default'. | |
157 """ | |
158 self.AppendBrowserLaunchSwitch('--profile-directory=' + profile) | |
159 self.RestartBrowser(clear_profile=False) | |
160 self.assertEqual(num_tabs, self.GetTabCount()) | |
161 self.assertEqual(self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup), | |
162 set_restore) | |
163 tab_index = 0 | |
164 while (tab_index < num_tabs): | |
165 self.ActivateTab(tab_index) | |
166 self.assertEqual(url_list[tab_index], self.GetActiveTabURL().spec()) | |
167 tab_index += 1 | |
168 | |
169 def testBasic(self): | |
170 """Multi-profile windows can open.""" | |
171 self.assertEqual(1, self.GetBrowserWindowCount()) | |
172 self.assertTrue(self.GetMultiProfileInfo()['enabled'], | |
173 msg='Multi-profile is not enabled') | |
174 self.OpenNewBrowserWindowWithNewProfile() | |
175 # Verify multi-profile info. | |
176 multi_profile = self.GetMultiProfileInfo() | |
177 self.assertEqual(2, len(multi_profile['profiles'])) | |
178 new_profile = multi_profile['profiles'][1] | |
179 self.assertTrue(new_profile['name']) | |
180 | |
181 # Verify browser windows. | |
182 self.assertEqual(2, self.GetBrowserWindowCount(), | |
183 msg='New browser window did not open') | |
184 info = self.GetBrowserInfo() | |
185 new_profile_window = info['windows'][1] | |
186 self.assertEqual('Profile 1', new_profile_window['profile_path']) | |
187 self.assertEqual(1, len(new_profile_window['tabs'])) | |
188 self.assertEqual('chrome://newtab/', new_profile_window['tabs'][0]['url']) | |
189 | |
190 def test20NewProfiles(self): | |
191 """Verify we can create 20 new profiles.""" | |
192 for index in range(1, 21): | |
193 self.OpenNewBrowserWindowWithNewProfile() | |
194 multi_profile = self.GetMultiProfileInfo() | |
195 self.assertEqual(index + 1, len(multi_profile['profiles']), | |
196 msg='Expected %d profiles after adding %d new users. Got %d' % ( | |
197 index + 1, index, len(multi_profile['profiles']))) | |
198 | |
199 def testStartUpPageOptionInMultiProfile(self): | |
200 """Test startup page for Multi-profile windows.""" | |
201 self.assertTrue(self.GetMultiProfileInfo()['enabled'], | |
202 msg='Multi-profile is not enabled') | |
203 # Launch browser with new Profile 1, set startup page to 'www.google.com'. | |
204 self.OpenNewBrowserWindowWithNewProfile() | |
205 self._SetStartUpPage('http://www.google.com', windex=1) | |
206 # Launch browser with new Profile 2, set startup page to 'www.yahoo.com'. | |
207 self.OpenNewBrowserWindowWithNewProfile() | |
208 # Verify start up page for Profile 2 is still newtab page. | |
209 info = self.GetBrowserInfo() | |
210 self.assertEqual('chrome://newtab/', info['windows'][2]['tabs'][0]['url']) | |
211 self._SetStartUpPage('http://www.yahoo.com', windex=2) | |
212 # Exit Profile 1 / Profile 2 | |
213 self.CloseBrowserWindow(2) | |
214 self.CloseBrowserWindow(1) | |
215 # Relaunch Browser with Profile 2, verify startup page. | |
216 self._AssertStartUpPage('http://www.yahoo.com', profile='Profile 2') | |
217 # Relaunch Browser with Profile 1, verify startup page. | |
218 self._AssertStartUpPage('http://www.google.com', profile='Profile 1') | |
219 | |
220 def testHomePageOptionMultiProfile(self): | |
221 """Test Home page for Multi-profile windows.""" | |
222 self.assertTrue(self.GetMultiProfileInfo()['enabled'], | |
223 msg='Multi-profile is not enabled') | |
224 # Launch browser with new Profile 1, set homepage to 'www.google.com'. | |
225 self.OpenNewBrowserWindowWithNewProfile() | |
226 self._SetHomePage('http://www.google.com', windex=1) | |
227 # Launch browser with new Profile 2, set homepage to 'www.yahoo.com'. | |
228 self.OpenNewBrowserWindowWithNewProfile() | |
229 self._SetHomePage('http://www.yahoo.com', windex=2) | |
230 # Exit Profile 1 / Profile 2 | |
231 self.CloseBrowserWindow(2) | |
232 self.CloseBrowserWindow(1) | |
233 # Relaunch Browser with Profile 2, verify startup page. | |
234 self._AssertHomePage('http://www.yahoo.com', profile='Profile 2') | |
235 # Relaunch Browser with Profile 1, verify startup page. | |
236 self._AssertHomePage('http://www.google.com', profile='Profile 1') | |
237 | |
238 def testSessionRestoreInMultiProfile(self): | |
239 """Test session restore preference for Multi-profile windows.""" | |
240 self.assertTrue(self.GetMultiProfileInfo()['enabled'], | |
241 msg='Multi-profile is not enabled') | |
242 # Launch browser with new Profile 1, set pref to restore session on | |
243 # startup. | |
244 self.OpenNewBrowserWindowWithNewProfile() | |
245 self._SetSessionRestoreURLs(self._RESTORE_LASTOPEN_URL_VALUE, windex=1) | |
246 # Launch browser with new Profile 2, do not set session restore pref. | |
247 self.OpenNewBrowserWindowWithNewProfile() | |
248 self._SetSessionRestoreURLs(self._RESTORE_DEFAULT_URL_VALUE, windex=2) | |
249 # Exit Profile 1 / Profile 2 | |
250 self.CloseBrowserWindow(2) | |
251 self.CloseBrowserWindow(1) | |
252 # Relaunch Browser with Profile 1, verify session restores on startup. | |
253 url_list = ['http://www.google.com/', 'http://news.google.com/'] | |
254 self._AssertSessionRestore(url_list, self._RESTORE_LASTOPEN_URL_VALUE, | |
255 num_tabs=2, profile='Profile 1') | |
256 # Relaunch Browser with Profile 2, verify session does not get restored. | |
257 url_list = ['chrome://newtab/'] | |
258 self._AssertSessionRestore(url_list, self._RESTORE_DEFAULT_URL_VALUE, | |
259 num_tabs=1, profile='Profile 2') | |
260 | |
261 def testMakeSearchEngineDefaultInMultiprofile(self): | |
262 """Test adding and making a search engine default for Multi-profiles.""" | |
263 self.assertTrue(self.GetMultiProfileInfo()['enabled'], | |
264 msg='Multi-profile is not enabled') | |
265 # Launch browser with new Profile 1, add search engine to 'Hulu'. | |
266 self.OpenNewBrowserWindowWithNewProfile() | |
267 self._AddSearchEngine('Hulu', 'hulu.com', | |
268 'http://www.hulu.com/search?query=%s&ref=os&src={referrer:source?}', 1) | |
269 self.MakeSearchEngineDefault('hulu.com', windex=1) | |
270 # Launch browser with new Profile 2, add search engine to 'Youtube'. | |
271 self.OpenNewBrowserWindowWithNewProfile() | |
272 self._AddSearchEngine('YouTube Video Search', 'youtube.com', | |
273 'http://www.youtube.com/results?search_query=%s&page={startPage?}'+ | |
274 '&utm_source=opensearch', 2) | |
275 self.MakeSearchEngineDefault('youtube.com', windex=2) | |
276 # Exit Profile 1 / Profile 2 | |
277 self.CloseBrowserWindow(2) | |
278 self.CloseBrowserWindow(1) | |
279 # Relaunch Browser with Profile 1, verify default search engine as 'Hulu'. | |
280 self._AssertDefaultSearchEngine('hulu.com', profile='Profile 1') | |
281 # Relaunch Browser with Profile 2, verify default search engine as | |
282 # 'Youtube'. | |
283 self._AssertDefaultSearchEngine('youtube.com', profile='Profile 2') | |
284 | |
285 def testDeleteSearchEngineInMultiprofile(self): | |
286 """Test adding then deleting a search engine for Multi-profiles.""" | |
287 self.assertTrue(self.GetMultiProfileInfo()['enabled'], | |
288 msg='Multi-profile is not enabled') | |
289 # Launch browser with new Profile 1, add 'foo.com' as new search engine. | |
290 self.OpenNewBrowserWindowWithNewProfile() | |
291 self._AddSearchEngine('foo', 'foo.com', 'http://foo/?q=%s', windex=1) | |
292 # Launch browser with new Profile 2, add 'foo.com' as new search engine. | |
293 self.OpenNewBrowserWindowWithNewProfile() | |
294 self._AddSearchEngine('foo', 'foo.com', 'http://foo/?q=%s', windex=2) | |
295 # Delete search engine 'foo.com' from Profile 1 and exit. | |
296 self.DeleteSearchEngine('foo.com', windex=1) | |
297 self.CloseBrowserWindow(2) | |
298 self.CloseBrowserWindow(1) | |
299 # Relaunch Browser with Profile 1, verify search engine 'foo.com' | |
300 # is deleted. | |
301 self.AppendBrowserLaunchSwitch('--profile-directory=Profile 1') | |
302 self.RestartBrowser(clear_profile=False) | |
303 foo = self._GetSearchEngineWithKeyword('foo.com') | |
304 self.assertFalse(foo) | |
305 # Relaunch Browser with Profile 2, verify search engine 'foo.com' | |
306 # is not deleted. | |
307 self.AppendBrowserLaunchSwitch('--profile-directory=Profile 2') | |
308 self.RestartBrowser(clear_profile=False) | |
309 foo = self._GetSearchEngineWithKeyword('foo.com') | |
310 self.assertTrue(foo) | |
311 | |
312 | |
313 if __name__ == '__main__': | |
314 pyauto_functional.Main() | |
OLD | NEW |