OLD | NEW |
1 # Copyright 2012 The Chromium Authors. All rights reserved. | 1 # Copyright 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | 5 import logging |
6 import os | 6 import os |
7 import shutil | 7 import shutil |
8 import tempfile | 8 import tempfile |
9 import unittest | 9 import unittest |
10 | 10 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 def _GenerateBrowserProfile(number_of_tabs): | 176 def _GenerateBrowserProfile(number_of_tabs): |
177 """ Generate a browser profile which browser had |number_of_tabs| number of | 177 """ Generate a browser profile which browser had |number_of_tabs| number of |
178 tabs opened before it was closed. | 178 tabs opened before it was closed. |
179 Returns: | 179 Returns: |
180 profile_dir: the directory of profile. | 180 profile_dir: the directory of profile. |
181 """ | 181 """ |
182 profile_dir = tempfile.mkdtemp() | 182 profile_dir = tempfile.mkdtemp() |
183 options = options_for_unittests.GetCopy() | 183 options = options_for_unittests.GetCopy() |
184 options.browser_options.output_profile_path = profile_dir | 184 options.browser_options.output_profile_path = profile_dir |
185 browser_to_create = browser_finder.FindBrowser(options) | 185 browser_to_create = browser_finder.FindBrowser(options) |
186 with browser_to_create.Create(options) as browser: | 186 browser_to_create.platform.network_controller.InitializeIfNeeded() |
187 browser.platform.SetHTTPServerDirectories(path.GetUnittestDataDir()) | 187 try: |
188 blank_file_path = os.path.join(path.GetUnittestDataDir(), 'blank.html') | 188 with browser_to_create.Create(options) as browser: |
189 blank_url = browser.platform.http_server.UrlOf(blank_file_path) | 189 browser.platform.SetHTTPServerDirectories(path.GetUnittestDataDir()) |
190 browser.foreground_tab.Navigate(blank_url) | 190 blank_file_path = os.path.join(path.GetUnittestDataDir(), 'blank.html') |
191 browser.foreground_tab.WaitForDocumentReadyStateToBeComplete() | 191 blank_url = browser.platform.http_server.UrlOf(blank_file_path) |
192 for _ in xrange(number_of_tabs - 1): | 192 browser.foreground_tab.Navigate(blank_url) |
193 tab = browser.tabs.New() | 193 browser.foreground_tab.WaitForDocumentReadyStateToBeComplete() |
194 tab.Navigate(blank_url) | 194 for _ in xrange(number_of_tabs - 1): |
195 tab.WaitForDocumentReadyStateToBeComplete() | 195 tab = browser.tabs.New() |
196 return profile_dir | 196 tab.Navigate(blank_url) |
| 197 tab.WaitForDocumentReadyStateToBeComplete() |
| 198 return profile_dir |
| 199 finally: |
| 200 browser_to_create.platform.network_controller.Close() |
197 | 201 |
198 | 202 |
199 class BrowserCreationTest(unittest.TestCase): | 203 class BrowserCreationTest(unittest.TestCase): |
200 def setUp(self): | 204 def setUp(self): |
201 self.mock_browser_backend = mock.MagicMock() | 205 self.mock_browser_backend = mock.MagicMock() |
202 self.mock_platform_backend = mock.MagicMock() | 206 self.mock_platform_backend = mock.MagicMock() |
203 | 207 |
204 def testCleanedUpCalledWhenExceptionRaisedInBrowserCreation(self): | 208 def testCleanedUpCalledWhenExceptionRaisedInBrowserCreation(self): |
205 self.mock_platform_backend.platform.FlushDnsCache.side_effect = ( | 209 self.mock_platform_backend.platform.FlushDnsCache.side_effect = ( |
206 IntentionalException('Boom!')) | 210 IntentionalException('Boom!')) |
(...skipping 19 matching lines...) Expand all Loading... |
226 | 230 |
227 @classmethod | 231 @classmethod |
228 def setUpClass(cls): | 232 def setUpClass(cls): |
229 cls._number_of_tabs = 4 | 233 cls._number_of_tabs = 4 |
230 cls._profile_dir = _GenerateBrowserProfile(cls._number_of_tabs) | 234 cls._profile_dir = _GenerateBrowserProfile(cls._number_of_tabs) |
231 cls._options = options_for_unittests.GetCopy() | 235 cls._options = options_for_unittests.GetCopy() |
232 cls._options.browser_options.AppendExtraBrowserArgs( | 236 cls._options.browser_options.AppendExtraBrowserArgs( |
233 ['--restore-last-session']) | 237 ['--restore-last-session']) |
234 cls._options.browser_options.profile_dir = cls._profile_dir | 238 cls._options.browser_options.profile_dir = cls._profile_dir |
235 cls._browser_to_create = browser_finder.FindBrowser(cls._options) | 239 cls._browser_to_create = browser_finder.FindBrowser(cls._options) |
| 240 cls._browser_to_create.platform.network_controller.InitializeIfNeeded() |
236 | 241 |
237 @decorators.Enabled('has tabs') | 242 @decorators.Enabled('has tabs') |
238 @decorators.Disabled('chromeos', 'win', 'mac') | 243 @decorators.Disabled('chromeos', 'win', 'mac') |
239 # TODO(nednguyen): Enable this test on windowsn platform | 244 # TODO(nednguyen): Enable this test on windowsn platform |
240 def testRestoreBrowserWithMultipleTabs(self): | 245 def testRestoreBrowserWithMultipleTabs(self): |
241 with self._browser_to_create.Create(self._options) as browser: | 246 with self._browser_to_create.Create(self._options) as browser: |
242 # The number of tabs will be self._number_of_tabs + 1 as it includes the | 247 # The number of tabs will be self._number_of_tabs + 1 as it includes the |
243 # old tabs and a new blank tab. | 248 # old tabs and a new blank tab. |
244 expected_number_of_tabs = self._number_of_tabs + 1 | 249 expected_number_of_tabs = self._number_of_tabs + 1 |
245 try: | 250 try: |
246 util.WaitFor(lambda: len(browser.tabs) == expected_number_of_tabs, 10) | 251 util.WaitFor(lambda: len(browser.tabs) == expected_number_of_tabs, 10) |
247 except: | 252 except: |
248 logging.error('Number of tabs is %s' % len(browser.tabs)) | 253 logging.error('Number of tabs is %s' % len(browser.tabs)) |
249 raise | 254 raise |
250 self.assertEquals(expected_number_of_tabs, len(browser.tabs)) | 255 self.assertEquals(expected_number_of_tabs, len(browser.tabs)) |
251 | 256 |
252 @classmethod | 257 @classmethod |
253 def tearDownClass(cls): | 258 def tearDownClass(cls): |
| 259 cls._browser_to_create.platform.network_controller.Close() |
254 shutil.rmtree(cls._profile_dir) | 260 shutil.rmtree(cls._profile_dir) |
255 | 261 |
256 | 262 |
257 class TestBrowserOperationDoNotLeakTempFiles(unittest.TestCase): | 263 class TestBrowserOperationDoNotLeakTempFiles(unittest.TestCase): |
258 | 264 |
259 @decorators.Enabled('win', 'mac', 'linux') | 265 @decorators.Enabled('win', 'mac', 'linux') |
260 @decorators.Isolated | 266 @decorators.Isolated |
261 def testBrowserNotLeakingTempFiles(self): | 267 def testBrowserNotLeakingTempFiles(self): |
262 options = options_for_unittests.GetCopy() | 268 options = options_for_unittests.GetCopy() |
263 browser_to_create = browser_finder.FindBrowser(options) | 269 browser_to_create = browser_finder.FindBrowser(options) |
264 self.assertIsNotNone(browser_to_create) | 270 self.assertIsNotNone(browser_to_create) |
265 before_browser_run_temp_dir_content = os.listdir(tempfile.tempdir) | 271 before_browser_run_temp_dir_content = os.listdir(tempfile.tempdir) |
266 with browser_to_create.Create(options) as browser: | 272 browser_to_create.platform.network_controller.InitializeIfNeeded() |
267 tab = browser.tabs.New() | 273 try: |
268 tab.Navigate('about:blank') | 274 with browser_to_create.Create(options) as browser: |
269 self.assertEquals(2, tab.EvaluateJavaScript('1 + 1')) | 275 tab = browser.tabs.New() |
270 after_browser_run_temp_dir_content = os.listdir(tempfile.tempdir) | 276 tab.Navigate('about:blank') |
271 self.assertEqual(before_browser_run_temp_dir_content, | 277 self.assertEquals(2, tab.EvaluateJavaScript('1 + 1')) |
272 after_browser_run_temp_dir_content) | 278 after_browser_run_temp_dir_content = os.listdir(tempfile.tempdir) |
| 279 self.assertEqual(before_browser_run_temp_dir_content, |
| 280 after_browser_run_temp_dir_content) |
| 281 finally: |
| 282 browser_to_create.platform.network_controller.Close() |
OLD | NEW |