OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import pyauto_functional # Must be imported first | 6 import pyauto_functional # Must be imported first |
7 import pyauto | 7 import pyauto |
8 import test_utils | 8 import test_utils |
9 | 9 |
10 import json | 10 import json |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 """ | 144 """ |
145 with open(os.path.join(self._profile_path, 'Preferences'), 'w') as f: | 145 with open(os.path.join(self._profile_path, 'Preferences'), 'w') as f: |
146 json.dump(prefs, f) | 146 json.dump(prefs, f) |
147 | 147 |
148 def _InvalidatePreferencesBackup(self): | 148 def _InvalidatePreferencesBackup(self): |
149 """Makes the Preferences backup invalid by clearing the signature.""" | 149 """Makes the Preferences backup invalid by clearing the signature.""" |
150 prefs = self._LoadPreferences() | 150 prefs = self._LoadPreferences() |
151 prefs['backup']['_signature'] = 'INVALID' | 151 prefs['backup']['_signature'] = 'INVALID' |
152 self._WritePreferences(prefs) | 152 self._WritePreferences(prefs) |
153 | 153 |
154 def _ChangeSessionStartupPrefs(self, startup_type, startup_urls=None, | 154 def _ChangeSessionStartupPrefs(self, startup_type=None, startup_urls=None, |
155 homepage=None): | 155 homepage=None, delete_migrated_pref=False): |
156 """Changes the session startup type and the list of URLs to load on startup. | 156 """Changes the session startup type and the list of URLs to load on startup. |
157 | 157 |
158 Args: | 158 Args: |
159 startup_type: int with one of _SESSION_STARTUP_* values. | 159 startup_type: int with one of _SESSION_STARTUP_* values. If startup_type |
160 is None, then it deletes the preference. | |
160 startup_urls: list(str) with a list of URLs; if None, is left unchanged. | 161 startup_urls: list(str) with a list of URLs; if None, is left unchanged. |
161 homepage: unless None, the new value for homepage. | 162 homepage: unless None, the new value for homepage. |
163 delete_migrated_pref: Whether we should delete the preference which says | |
164 we've already migrated the startup_type preference. | |
162 """ | 165 """ |
163 prefs = self._LoadPreferences() | 166 prefs = self._LoadPreferences() |
164 prefs['session']['restore_on_startup'] = startup_type | 167 if startup_type is None: |
168 del prefs['session']['restore_on_startup'] | |
169 else: | |
170 prefs['session']['restore_on_startup'] = startup_type | |
171 | |
165 if startup_urls is not None: | 172 if startup_urls is not None: |
166 prefs['session']['urls_to_restore_on_startup'] = startup_urls | 173 prefs['session']['urls_to_restore_on_startup'] = startup_urls |
167 if homepage is not None: | 174 if homepage is not None: |
168 prefs['homepage'] = homepage | 175 prefs['homepage'] = homepage |
176 if delete_migrated_pref: | |
177 del prefs['session']['restore_on_startup_migrated'] | |
169 self._WritePreferences(prefs) | 178 self._WritePreferences(prefs) |
170 | 179 |
171 def _ChangePinnedTabsPrefs(self, pinned_tabs): | 180 def _ChangePinnedTabsPrefs(self, pinned_tabs): |
172 """Changes the list of pinned tabs. | 181 """Changes the list of pinned tabs. |
173 | 182 |
174 Args: | 183 Args: |
175 pinned_tabs: list(str) with a list of pinned tabs URLs. | 184 pinned_tabs: list(str) with a list of pinned tabs URLs. |
176 """ | 185 """ |
177 prefs = self._LoadPreferences() | 186 prefs = self._LoadPreferences() |
178 prefs['pinned_tabs'] = [] | 187 prefs['pinned_tabs'] = [] |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( | 440 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( |
432 self._SESSION_STARTUP_URLS, | 441 self._SESSION_STARTUP_URLS, |
433 startup_urls=['http://www.google.com'])) | 442 startup_urls=['http://www.google.com'])) |
434 # The change must be detected by Protector. | 443 # The change must be detected by Protector. |
435 self.assertTrue(self.GetProtectorState()['showing_change']) | 444 self.assertTrue(self.GetProtectorState()['showing_change']) |
436 # Change the setting manually. | 445 # Change the setting manually. |
437 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_NTP) | 446 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_NTP) |
438 # No longer showing the change. | 447 # No longer showing the change. |
439 self.assertFalse(self.GetProtectorState()['showing_change']) | 448 self.assertFalse(self.GetProtectorState()['showing_change']) |
440 | 449 |
441 def testSessionStartupPrefMigration(self): | 450 def testSessionStartupPrefMigrationFromHomepage(self): |
442 """Test migration from old session.restore_on_startup values (homepage).""" | 451 """Test migration from old session.restore_on_startup values (homepage).""" |
443 # Set startup prefs to restoring last open tabs. | 452 # Set startup prefs to restoring last open tabs. |
444 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) | 453 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) |
445 self.SetPrefs(pyauto.kURLsToRestoreOnStartup, []) | 454 self.SetPrefs(pyauto.kURLsToRestoreOnStartup, []) |
446 new_homepage = 'http://www.google.com/' | 455 new_homepage = 'http://www.google.com/' |
447 # Restart browser with startup prefs set to open homepage (google.com). | 456 # Restart browser with startup prefs set to open homepage (google.com). |
448 self.RestartBrowser( | 457 self.RestartBrowser( |
449 clear_profile=False, | 458 clear_profile=False, |
450 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( | 459 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( |
451 self._SESSION_STARTUP_HOMEPAGE, | 460 self._SESSION_STARTUP_HOMEPAGE, |
452 homepage=new_homepage)) | 461 homepage=new_homepage, |
462 delete_migrated_pref=True)) | |
453 # The change must be detected by Protector. | 463 # The change must be detected by Protector. |
454 self.assertTrue(self.GetProtectorState()['showing_change']) | 464 self.assertTrue(self.GetProtectorState()['showing_change']) |
455 # Protector must restore old preference values. | 465 # Protector must restore old preference values. |
456 self.assertEqual(self._SESSION_STARTUP_LAST, | 466 self.assertEqual(self._SESSION_STARTUP_LAST, |
457 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | 467 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
458 self.assertEqual([], | 468 self.assertEqual([], |
459 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | 469 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) |
460 self.ApplyProtectorChange() | 470 self.ApplyProtectorChange() |
461 # Now the new preference values are active. | 471 # Now the new preference values are active. |
462 self.assertEqual(self._SESSION_STARTUP_URLS, | 472 self.assertEqual(self._SESSION_STARTUP_URLS, |
463 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | 473 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
464 # Homepage migrated to the list of startup URLs. | 474 # Homepage migrated to the list of startup URLs. |
465 self.assertEqual([new_homepage], | 475 self.assertEqual([new_homepage], |
466 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | 476 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) |
467 # No longer showing the change. | 477 # No longer showing the change. |
468 self.assertFalse(self.GetProtectorState()['showing_change']) | 478 self.assertFalse(self.GetProtectorState()['showing_change']) |
469 | 479 |
480 def testSessionStartupPrefMigrationFromBlank(self): | |
481 """Test migration from session.restore_on_startup being blank, as it would | |
Ivan Korotkov
2012/04/17 09:27:51
Please align pydoc as in the rest of the file, i.e
Tyler Breisacher (Chromium)
2012/04/17 19:54:33
Done.
| |
482 be for a user who had m18 or lower, and never changed that preference. | |
483 """ | |
484 # Set startup prefs to restoring last open tabs. | |
485 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) | |
486 self.SetPrefs(pyauto.kURLsToRestoreOnStartup, []) | |
487 # Set the homepage. | |
488 new_homepage = 'http://www.google.com/' | |
489 self.SetPrefs(pyauto.kHomePageIsNewTabPage, False) | |
490 self.SetPrefs(pyauto.kHomePage, new_homepage) | |
491 # Restart browser, clearing the 'restore on startup' pref, to simulate a | |
492 # user coming from m18 and having left it on the default value. | |
493 self.RestartBrowser( | |
494 clear_profile=False, | |
495 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( | |
496 startup_type=None, | |
497 delete_migrated_pref=True)) | |
498 # The change must be detected by Protector. | |
499 self.assertTrue(self.GetProtectorState()['showing_change']) | |
500 # Protector must restore old preference values. | |
501 self.assertEqual(self._SESSION_STARTUP_LAST, | |
502 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
503 self.assertEqual([], | |
504 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | |
505 self.ApplyProtectorChange() | |
506 # Now the new preference values are active. | |
507 self.assertEqual(self._SESSION_STARTUP_URLS, | |
508 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
509 # Homepage migrated to the list of startup URLs. | |
510 self.assertEqual([new_homepage], | |
511 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | |
512 # No longer showing the change. | |
513 self.assertFalse(self.GetProtectorState()['showing_change']) | |
514 | |
515 def testSessionStartupPrefNoMigrationOnHomepageChange(self): | |
516 """Test that when the user modifies their homepage in m19+, we don't do the | |
517 preference migration. | |
518 """ | |
519 # Initially, the default value is selected for kRestoreOnStartup. | |
520 self.assertEqual(self._SESSION_STARTUP_NTP, | |
521 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
522 # Set the homepage, but leave kRestoreOnStartup unchanged. | |
523 new_homepage = 'http://www.google.com/' | |
524 self.SetPrefs(pyauto.kHomePageIsNewTabPage, False) | |
525 self.SetPrefs(pyauto.kHomePage, new_homepage) | |
526 # Restart browser. | |
527 self.RestartBrowser(clear_profile=False) | |
528 # Now the new preference values are active. | |
Ivan Korotkov
2012/04/17 09:27:51
Please add assert that protector isn't showing a b
Tyler Breisacher (Chromium)
2012/04/17 19:54:33
Done.
| |
529 self.assertEqual(self._SESSION_STARTUP_NTP, | |
530 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
531 # kURLsToRestoreOnStartup pref is unchanged. | |
532 self.assertEqual([], | |
533 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | |
534 | |
470 def testDetectPinnedTabsChangeAndApply(self): | 535 def testDetectPinnedTabsChangeAndApply(self): |
471 """Test for detecting and applying a change to pinned tabs.""" | 536 """Test for detecting and applying a change to pinned tabs.""" |
472 pinned_urls = ['chrome://version/', 'chrome://credits/'] | 537 pinned_urls = ['chrome://version/', 'chrome://credits/'] |
473 self.RestartBrowser( | 538 self.RestartBrowser( |
474 clear_profile=False, | 539 clear_profile=False, |
475 pre_launch_hook=lambda: self._ChangePinnedTabsPrefs(pinned_urls)) | 540 pre_launch_hook=lambda: self._ChangePinnedTabsPrefs(pinned_urls)) |
476 # The change must be detected by Protector. | 541 # The change must be detected by Protector. |
477 self.assertTrue(self.GetProtectorState()['showing_change']) | 542 self.assertTrue(self.GetProtectorState()['showing_change']) |
478 # Protector must restore old preference values. | 543 # Protector must restore old preference values. |
479 self.assertEqual([], | 544 self.assertEqual([], |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | 667 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
603 # Verify that open tabs are consistent with new prefs. | 668 # Verify that open tabs are consistent with new prefs. |
604 info = self.GetBrowserInfo() | 669 info = self.GetBrowserInfo() |
605 self.assertEqual(1, len(info['windows'])) # one window | 670 self.assertEqual(1, len(info['windows'])) # one window |
606 self.assertEqual(1, len(info['windows'][0]['tabs'])) # one tab | 671 self.assertEqual(1, len(info['windows'][0]['tabs'])) # one tab |
607 self.assertEqual(new_url, info['windows'][0]['tabs'][0]['url']) | 672 self.assertEqual(new_url, info['windows'][0]['tabs'][0]['url']) |
608 | 673 |
609 | 674 |
610 if __name__ == '__main__': | 675 if __name__ == '__main__': |
611 pyauto_functional.Main() | 676 pyauto_functional.Main() |
OLD | NEW |