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): |
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. |
162 """ | 163 """ |
163 prefs = self._LoadPreferences() | 164 prefs = self._LoadPreferences() |
164 prefs['session']['restore_on_startup'] = startup_type | 165 if startup_type is None: |
166 del prefs['session']['restore_on_startup'] | |
167 else: | |
168 prefs['session']['restore_on_startup'] = startup_type | |
169 | |
165 if startup_urls is not None: | 170 if startup_urls is not None: |
166 prefs['session']['urls_to_restore_on_startup'] = startup_urls | 171 prefs['session']['urls_to_restore_on_startup'] = startup_urls |
167 if homepage is not None: | 172 if homepage is not None: |
168 prefs['homepage'] = homepage | 173 prefs['homepage'] = homepage |
169 self._WritePreferences(prefs) | 174 self._WritePreferences(prefs) |
170 | 175 |
171 def _ChangePinnedTabsPrefs(self, pinned_tabs): | 176 def _ChangePinnedTabsPrefs(self, pinned_tabs): |
172 """Changes the list of pinned tabs. | 177 """Changes the list of pinned tabs. |
173 | 178 |
174 Args: | 179 Args: |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( | 436 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( |
432 self._SESSION_STARTUP_URLS, | 437 self._SESSION_STARTUP_URLS, |
433 startup_urls=['http://www.google.com'])) | 438 startup_urls=['http://www.google.com'])) |
434 # The change must be detected by Protector. | 439 # The change must be detected by Protector. |
435 self.assertTrue(self.GetProtectorState()['showing_change']) | 440 self.assertTrue(self.GetProtectorState()['showing_change']) |
436 # Change the setting manually. | 441 # Change the setting manually. |
437 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_NTP) | 442 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_NTP) |
438 # No longer showing the change. | 443 # No longer showing the change. |
439 self.assertFalse(self.GetProtectorState()['showing_change']) | 444 self.assertFalse(self.GetProtectorState()['showing_change']) |
440 | 445 |
441 def testSessionStartupPrefMigration(self): | 446 def testSessionStartupPrefMigrationFromHomepage(self): |
442 """Test migration from old session.restore_on_startup values (homepage).""" | 447 """Test migration from old session.restore_on_startup values (homepage).""" |
443 # Set startup prefs to restoring last open tabs. | 448 # Set startup prefs to restoring last open tabs. |
444 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) | 449 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) |
445 self.SetPrefs(pyauto.kURLsToRestoreOnStartup, []) | 450 self.SetPrefs(pyauto.kURLsToRestoreOnStartup, []) |
446 new_homepage = 'http://www.google.com/' | 451 new_homepage = 'http://www.google.com/' |
447 # Restart browser with startup prefs set to open homepage (google.com). | 452 # Restart browser with startup prefs set to open homepage (google.com). |
448 self.RestartBrowser( | 453 self.RestartBrowser( |
449 clear_profile=False, | 454 clear_profile=False, |
450 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( | 455 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( |
451 self._SESSION_STARTUP_HOMEPAGE, | 456 self._SESSION_STARTUP_HOMEPAGE, |
452 homepage=new_homepage)) | 457 homepage=new_homepage)) |
453 # The change must be detected by Protector. | 458 # The change must be detected by Protector. |
454 self.assertTrue(self.GetProtectorState()['showing_change']) | 459 self.assertTrue(self.GetProtectorState()['showing_change']) |
455 # Protector must restore old preference values. | 460 # Protector must restore old preference values. |
456 self.assertEqual(self._SESSION_STARTUP_LAST, | 461 self.assertEqual(self._SESSION_STARTUP_LAST, |
457 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | 462 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
458 self.assertEqual([], | 463 self.assertEqual([], |
459 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | 464 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) |
460 self.ApplyProtectorChange() | 465 self.ApplyProtectorChange() |
461 # Now the new preference values are active. | 466 # Now the new preference values are active. |
462 self.assertEqual(self._SESSION_STARTUP_URLS, | 467 self.assertEqual(self._SESSION_STARTUP_URLS, |
463 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | 468 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
464 # Homepage migrated to the list of startup URLs. | 469 # Homepage migrated to the list of startup URLs. |
465 self.assertEqual([new_homepage], | 470 self.assertEqual([new_homepage], |
466 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | 471 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) |
467 # No longer showing the change. | 472 # No longer showing the change. |
468 self.assertFalse(self.GetProtectorState()['showing_change']) | 473 self.assertFalse(self.GetProtectorState()['showing_change']) |
469 | 474 |
475 def testSessionStartupPrefMigrationFromBlank(self): | |
476 """Test migration from session.restore_on_startup being blank, as it would | |
477 be for a user who had m18 or lower, and never changed that preference. | |
478 """ | |
479 # Set startup prefs to restoring last open tabs. | |
480 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) | |
481 self.SetPrefs(pyauto.kURLsToRestoreOnStartup, []) | |
482 # Set the homepage. | |
483 new_homepage = 'http://www.google.com/' | |
484 self.SetPrefs(pyauto.kHomePageIsNewTabPage, False) | |
485 self.SetPrefs(pyauto.kHomePage, new_homepage) | |
486 # Restart browser, clearing the 'restore on startup' pref. | |
487 self.RestartBrowser( | |
488 clear_profile=False, | |
489 pre_launch_hook=lambda: self._ChangeSessionStartupPrefs( | |
490 startup_type=None)) | |
491 # The change must be detected by Protector. | |
492 self.assertTrue(self.GetProtectorState()['showing_change']) | |
493 # Protector must restore old preference values. | |
494 self.assertEqual(self._SESSION_STARTUP_LAST, | |
495 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
496 self.assertEqual([], | |
497 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | |
Nirnimesh
2012/04/14 08:09:55
use assertFalse instead
| |
498 self.ApplyProtectorChange() | |
499 # Now the new preference values are active. | |
500 self.assertEqual(self._SESSION_STARTUP_URLS, | |
501 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
502 # Homepage migrated to the list of startup URLs. | |
503 self.assertEqual([new_homepage], | |
504 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | |
505 # No longer showing the change. | |
506 self.assertFalse(self.GetProtectorState()['showing_change']) | |
507 | |
508 def testSessionStartupPrefNoMigrationOnHomepageChange(self): | |
509 """Test that when the user modifies their homepage in m19+, we don't do the | |
510 preference migration. | |
511 """ | |
512 # Leave the kRestoreOnStartup preference unset so that the default value | |
513 # applies (for m18, that's "open the homepage"), and set the homepage. | |
514 new_homepage = 'http://www.google.com/' | |
515 self.SetPrefs(pyauto.kHomePageIsNewTabPage, False) | |
516 self.SetPrefs(pyauto.kHomePage, new_homepage) | |
517 # Restart browser. | |
518 self.RestartBrowser(clear_profile=False) | |
519 # Now the new preference values are active. | |
520 self.assertEqual(self._SESSION_STARTUP_NTP, | |
521 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | |
522 # kURLsToRestoreOnStartup pref is unchanged. | |
523 self.assertEqual([], | |
524 self.GetPrefsInfo().Prefs(pyauto.kURLsToRestoreOnStartup)) | |
525 | |
470 def testDetectPinnedTabsChangeAndApply(self): | 526 def testDetectPinnedTabsChangeAndApply(self): |
471 """Test for detecting and applying a change to pinned tabs.""" | 527 """Test for detecting and applying a change to pinned tabs.""" |
472 pinned_urls = ['chrome://version/', 'chrome://credits/'] | 528 pinned_urls = ['chrome://version/', 'chrome://credits/'] |
473 self.RestartBrowser( | 529 self.RestartBrowser( |
474 clear_profile=False, | 530 clear_profile=False, |
475 pre_launch_hook=lambda: self._ChangePinnedTabsPrefs(pinned_urls)) | 531 pre_launch_hook=lambda: self._ChangePinnedTabsPrefs(pinned_urls)) |
476 # The change must be detected by Protector. | 532 # The change must be detected by Protector. |
477 self.assertTrue(self.GetProtectorState()['showing_change']) | 533 self.assertTrue(self.GetProtectorState()['showing_change']) |
478 # Protector must restore old preference values. | 534 # Protector must restore old preference values. |
479 self.assertEqual([], | 535 self.assertEqual([], |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | 658 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
603 # Verify that open tabs are consistent with new prefs. | 659 # Verify that open tabs are consistent with new prefs. |
604 info = self.GetBrowserInfo() | 660 info = self.GetBrowserInfo() |
605 self.assertEqual(1, len(info['windows'])) # one window | 661 self.assertEqual(1, len(info['windows'])) # one window |
606 self.assertEqual(1, len(info['windows'][0]['tabs'])) # one tab | 662 self.assertEqual(1, len(info['windows'][0]['tabs'])) # one tab |
607 self.assertEqual(new_url, info['windows'][0]['tabs'][0]['url']) | 663 self.assertEqual(new_url, info['windows'][0]['tabs'][0]['url']) |
608 | 664 |
609 | 665 |
610 if __name__ == '__main__': | 666 if __name__ == '__main__': |
611 pyauto_functional.Main() | 667 pyauto_functional.Main() |
OLD | NEW |