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 def testPreferencesBackupInvalidChangeDismissedOnEdit(self): |
| 311 """Test that editing protected prefs dismisses the invalid backup bubble.""" |
| 312 self.RestartBrowser( |
| 313 clear_profile=False, |
| 314 pre_launch_hook=self._InvalidatePreferencesBackup) |
| 315 # The change must be detected by Protector. |
| 316 self.assertTrue(self.GetProtectorState()['showing_change']) |
| 317 # Change some protected setting manually. |
| 318 self.SetPrefs(pyauto.kHomePage, 'http://example.com/') |
| 319 # No longer showing the change. |
| 320 self.assertFalse(self.GetProtectorState()['showing_change']) |
| 321 |
| 322 |
282 class ProtectorSessionStartupTest(BaseProtectorTest): | 323 class ProtectorSessionStartupTest(BaseProtectorTest): |
283 """Test suite for session startup changes detection with Protector enabled. | 324 """Test suite for session startup changes detection with Protector enabled. |
284 """ | 325 """ |
285 def testDetectSessionStartupChangeAndApply(self): | 326 def testDetectSessionStartupChangeAndApply(self): |
286 """Test for detecting and applying a session startup pref change.""" | 327 """Test for detecting and applying a session startup pref change.""" |
287 # Set startup prefs to restoring last open tabs. | 328 # Set startup prefs to restoring last open tabs. |
288 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) | 329 self.SetPrefs(pyauto.kRestoreOnStartup, self._SESSION_STARTUP_LAST) |
289 previous_url = 'chrome://version/' | 330 previous_url = 'chrome://version/' |
290 self.NavigateToURL(previous_url) | 331 self.NavigateToURL(previous_url) |
291 # Restart browser with startup prefs set to open google.com. | 332 # Restart browser with startup prefs set to open google.com. |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) | 476 self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup)) |
436 # Verify that open tabs are consistent with new prefs. | 477 # Verify that open tabs are consistent with new prefs. |
437 info = self.GetBrowserInfo() | 478 info = self.GetBrowserInfo() |
438 self.assertEqual(1, len(info['windows'])) # one window | 479 self.assertEqual(1, len(info['windows'])) # one window |
439 self.assertEqual(1, len(info['windows'][0]['tabs'])) # one tab | 480 self.assertEqual(1, len(info['windows'][0]['tabs'])) # one tab |
440 self.assertEqual(new_url, info['windows'][0]['tabs'][0]['url']) | 481 self.assertEqual(new_url, info['windows'][0]['tabs'][0]['url']) |
441 | 482 |
442 | 483 |
443 if __name__ == '__main__': | 484 if __name__ == '__main__': |
444 pyauto_functional.Main() | 485 pyauto_functional.Main() |
OLD | NEW |