| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 138 |
| 139 def _WritePreferences(self, prefs): | 139 def _WritePreferences(self, prefs): |
| 140 """Writes new contents to the Preferences file. | 140 """Writes new contents to the Preferences file. |
| 141 | 141 |
| 142 Args: | 142 Args: |
| 143 prefs: dict() with new user preferences as returned by PrefsInfo.Prefs(). | 143 prefs: dict() with new user preferences as returned by PrefsInfo.Prefs(). |
| 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): |
| 149 """Makes the Preferences backup invalid by clearing the signature.""" |
| 150 prefs = self._LoadPreferences() |
| 151 prefs['backup']['_signature'] = 'INVALID' |
| 152 self._WritePreferences(prefs) |
| 153 |
| 148 def _ChangeSessionStartupPrefs(self, startup_type, startup_urls=None, | 154 def _ChangeSessionStartupPrefs(self, startup_type, startup_urls=None, |
| 149 homepage=None): | 155 homepage=None): |
| 150 """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. |
| 151 | 157 |
| 152 Args: | 158 Args: |
| 153 startup_type: int with one of _SESSION_STARTUP_* values. | 159 startup_type: int with one of _SESSION_STARTUP_* values. |
| 154 startup_urls: list(str) with a list of URLs; if None, is left unchanged. | 160 startup_urls: list(str) with a list of URLs; if None, is left unchanged. |
| 155 homepage: unless None, the new value for homepage. | 161 homepage: unless None, the new value for homepage. |
| 156 """ | 162 """ |
| 157 prefs = self._LoadPreferences() | 163 prefs = self._LoadPreferences() |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 self.RestartBrowser(clear_profile=False) | 278 self.RestartBrowser(clear_profile=False) |
| 273 default_search = self._GetDefaultSearchEngine() | 279 default_search = self._GetDefaultSearchEngine() |
| 274 self.assertEqual(old_default_search, default_search) | 280 self.assertEqual(old_default_search, default_search) |
| 275 # No longer showing the change. | 281 # No longer showing the change. |
| 276 self.assertFalse(self.GetProtectorState()['showing_change']) | 282 self.assertFalse(self.GetProtectorState()['showing_change']) |
| 277 | 283 |
| 278 # TODO(ivankr): more hijacking cases (remove the current default search engine, | 284 # TODO(ivankr): more hijacking cases (remove the current default search engine, |
| 279 # add new search engines to the list, invalidate backup, etc). | 285 # add new search engines to the list, invalidate backup, etc). |
| 280 | 286 |
| 281 | 287 |
| 288 class ProtectorPreferencesTest(BaseProtectorTest): |
| 289 """Generic test suite for Preferences protection.""" |
| 290 |
| 291 def testPreferencesBackupInvalid(self): |
| 292 """Test for detecting invalid Preferences backup.""" |
| 293 self.RestartBrowser( |
| 294 clear_profile=False, |
| 295 pre_launch_hook=self._InvalidatePreferencesBackup) |
| 296 # The change must be detected by Protector. |
| 297 self.assertTrue(self.GetProtectorState()['showing_change']) |
| 298 self.DiscardProtectorChange() |
| 299 # Verify that a new tab with settings is opened. |
| 300 info = self.GetBrowserInfo() |
| 301 self.assertEqual(1, len(info['windows'])) # one window |
| 302 self.assertEqual(2, len(info['windows'][0]['tabs'])) # 2 tabs |
| 303 self.assertEqual('chrome://settings/', info['windows'][0]['tabs'][1]['url']) |
| 304 # No longer showing the change. |
| 305 self.assertFalse(self.GetProtectorState()['showing_change']) |
| 306 self.RestartBrowser(clear_profile=False) |
| 307 # Not showing the change after a restart |
| 308 self.assertFalse(self.GetProtectorState()['showing_change']) |
| 309 |
| 310 |
| 282 class ProtectorSessionStartupTest(BaseProtectorTest): | 311 class ProtectorSessionStartupTest(BaseProtectorTest): |
| 283 """Test suite for session startup changes detection with Protector enabled. | 312 """Test suite for session startup changes detection with Protector enabled. |
| 284 """ | 313 """ |
| 285 def testDetectSessionStartupChangeAndApply(self): | 314 def testDetectSessionStartupChangeAndApply(self): |
| 286 """Test for detecting and applying a session startup pref change.""" | 315 """Test for detecting and applying a session startup pref change.""" |
| 287 # Set startup prefs to restoring last open tabs. | 316 # Set startup prefs to restoring last open tabs. |
| 288 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) | 317 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) |
| 289 previous_url = 'chrome://version/' | 318 previous_url = 'chrome://version/' |
| 290 self.NavigateToURL(previous_url) | 319 self.NavigateToURL(previous_url) |
| 291 # Restart browser with startup prefs set to open google.com. | 320 # Restart browser with startup prefs set to open google.com. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | 447 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
| 419 # Verify that open tabs are consistent with new prefs. | 448 # Verify that open tabs are consistent with new prefs. |
| 420 info = self.GetBrowserInfo() | 449 info = self.GetBrowserInfo() |
| 421 self.assertEqual(1, len(info['windows'])) # one window | 450 self.assertEqual(1, len(info['windows'])) # one window |
| 422 self.assertEqual(1, len(info['windows'][0]['tabs'])) # one tab | 451 self.assertEqual(1, len(info['windows'][0]['tabs'])) # one tab |
| 423 self.assertEqual(new_url, info['windows'][0]['tabs'][0]['url']) | 452 self.assertEqual(new_url, info['windows'][0]['tabs'][0]['url']) |
| 424 | 453 |
| 425 | 454 |
| 426 if __name__ == '__main__': | 455 if __name__ == '__main__': |
| 427 pyauto_functional.Main() | 456 pyauto_functional.Main() |
| OLD | NEW |