OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 import unittest | 4 import unittest |
5 | 5 |
6 from profile_creators.fast_navigation_profile_extender import ( | 6 from profile_creators.fast_navigation_profile_extender import ( |
7 FastNavigationProfileExtender) | 7 FastNavigationProfileExtender) |
8 from telemetry.core import util | 8 from telemetry.core import util |
9 | 9 |
10 util.AddDirToPythonPath(util.GetTelemetryDir(), 'third_party', 'mock') | 10 util.AddDirToPythonPath(util.GetTelemetryDir(), 'third_party', 'mock') |
11 import mock | 11 import mock |
12 | 12 |
13 | 13 |
14 class FakeTab(object): | 14 class FakeTab(object): |
15 pass | 15 pass |
16 | 16 |
17 | 17 |
| 18 class FakeTabList(object): |
| 19 def __init__(self): |
| 20 self._tabs = [] |
| 21 |
| 22 def New(self): |
| 23 tab = FakeTab() |
| 24 self._tabs.append(tab) |
| 25 return tab |
| 26 |
| 27 def __len__(self): |
| 28 return len(self._tabs) |
| 29 |
| 30 |
18 class FakeBrowser(object): | 31 class FakeBrowser(object): |
19 def __init__(self, tab_count): | 32 def __init__(self): |
20 self.tabs = [] | 33 self.tabs = FakeTabList() |
21 for _ in range(tab_count): | |
22 self.tabs.append(FakeTab()) | |
23 | 34 |
24 | 35 |
25 # Testing private method. | 36 # Testing private method. |
26 # pylint: disable=protected-access | 37 # pylint: disable=protected-access |
27 class FastNavigationProfileExtenderTest(unittest.TestCase): | 38 class FastNavigationProfileExtenderTest(unittest.TestCase): |
28 def testPerformNavigations(self): | 39 def testPerformNavigations(self): |
29 maximum_batch_size = 15 | 40 maximum_batch_size = 15 |
30 extender = FastNavigationProfileExtender(maximum_batch_size) | 41 extender = FastNavigationProfileExtender(maximum_batch_size) |
31 | 42 |
32 navigation_urls = [] | 43 navigation_urls = [] |
33 for i in range(extender._NUM_TABS): | 44 for i in range(extender._NUM_TABS): |
34 navigation_urls.append('http://test%s.com' % i) | 45 navigation_urls.append('http://test%s.com' % i) |
35 batch_size = 5 | 46 batch_size = 5 |
36 navigation_urls_batch = navigation_urls[3:3 + batch_size] | 47 navigation_urls_batch = navigation_urls[3:3 + batch_size] |
37 | 48 |
38 extender.GetUrlIterator = mock.MagicMock( | 49 extender.GetUrlIterator = mock.MagicMock( |
39 return_value=iter(navigation_urls_batch)) | 50 return_value=iter(navigation_urls_batch)) |
40 extender.ShouldExitAfterBatchNavigation = mock.MagicMock(return_value=True) | 51 extender.ShouldExitAfterBatchNavigation = mock.MagicMock(return_value=True) |
41 extender._WaitForQueuedTabsToLoad = mock.MagicMock() | 52 extender._WaitForQueuedTabsToLoad = mock.MagicMock() |
42 | 53 |
43 extender._browser = FakeBrowser(extender._NUM_TABS) | 54 extender._browser = FakeBrowser() |
44 extender._BatchNavigateTabs = mock.MagicMock() | 55 extender._BatchNavigateTabs = mock.MagicMock() |
45 | 56 |
46 # Set up a callback to record the tabs and urls in each navigation. | 57 # Set up a callback to record the tabs and urls in each navigation. |
47 callback_tabs_batch = [] | 58 callback_tabs_batch = [] |
48 callback_urls_batch = [] | 59 callback_urls_batch = [] |
49 def SideEffect(*args, **_): | 60 def SideEffect(*args, **_): |
50 batch = args[0] | 61 batch = args[0] |
51 for tab, url in batch: | 62 for tab, url in batch: |
52 callback_tabs_batch.append(tab) | 63 callback_tabs_batch.append(tab) |
53 callback_urls_batch.append(url) | 64 callback_urls_batch.append(url) |
54 extender._BatchNavigateTabs.side_effect = SideEffect | 65 extender._BatchNavigateTabs.side_effect = SideEffect |
55 | 66 |
56 # Perform the navigations. | 67 # Perform the navigations. |
57 extender._PerformNavigations() | 68 extender._PerformNavigations() |
58 | 69 |
59 # Each url in the batch should have been navigated to exactly once. | 70 # Each url in the batch should have been navigated to exactly once. |
60 self.assertEqual(set(callback_urls_batch), set(navigation_urls_batch)) | 71 self.assertEqual(set(callback_urls_batch), set(navigation_urls_batch)) |
61 | 72 |
62 # The other urls should not have been navigated to. | 73 # The other urls should not have been navigated to. |
63 navigation_urls_remaining = (set(navigation_urls) - | 74 navigation_urls_remaining = (set(navigation_urls) - |
64 set(navigation_urls_batch)) | 75 set(navigation_urls_batch)) |
65 self.assertFalse(navigation_urls_remaining & set(callback_urls_batch)) | 76 self.assertFalse(navigation_urls_remaining & set(callback_urls_batch)) |
66 | 77 |
67 # The first couple of tabs should have been navigated once. The remaining | 78 # The first couple of tabs should have been navigated once. The remaining |
68 # tabs should not have been navigated. | 79 # tabs should not have been navigated. |
69 for i in range(len(extender._browser.tabs)): | 80 for i in range(len(extender._browser.tabs)): |
70 tab = extender._browser.tabs[i] | 81 tab = extender._browser.tabs._tabs[i] |
71 | 82 |
72 if i < batch_size: | 83 if i < batch_size: |
73 expected_tab_navigation_count = 1 | 84 expected_tab_navigation_count = 1 |
74 else: | 85 else: |
75 expected_tab_navigation_count = 0 | 86 expected_tab_navigation_count = 0 |
76 | 87 |
77 count = callback_tabs_batch.count(tab) | 88 count = callback_tabs_batch.count(tab) |
78 self.assertEqual(count, expected_tab_navigation_count) | 89 self.assertEqual(count, expected_tab_navigation_count) |
OLD | NEW |