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 FakeBrowser(object): | 18 class FakeBrowser(object): |
19 def __init__(self, tab_count): | 19 def __init__(self, tab_count): |
20 self.tabs = [] | 20 self.tabs = [] |
21 for _ in range(tab_count): | 21 for _ in range(tab_count): |
22 self.tabs.append(FakeTab()) | 22 self.tabs.append(FakeTab()) |
23 | 23 |
24 | 24 |
25 # Testing private method. | 25 # Testing private method. |
26 # pylint: disable=protected-access | 26 # pylint: disable=protected-access |
27 class FastNavigationProfileExtenderTest(unittest.TestCase): | 27 class FastNavigationProfileExtenderTest(unittest.TestCase): |
28 def testPerformNavigations(self): | 28 def testPerformNavigations(self): |
29 extender = FastNavigationProfileExtender() | 29 extender = FastNavigationProfileExtender() |
30 num_urls = extender._NUM_PARALLEL_PAGES * 3 + 4 | 30 extender._NUM_TABS = 15 |
31 num_tabs = extender._NUM_TABS | |
32 | 31 |
33 navigation_urls = [] | 32 navigation_urls = [] |
34 for i in range(num_urls): | 33 for i in range(extender._NUM_TABS): |
35 navigation_urls.append('http://test%s.com' % i) | 34 navigation_urls.append('http://test%s.com' % i) |
| 35 batch_size = 5 |
| 36 navigation_urls_batch = navigation_urls[3:3 + batch_size] |
36 | 37 |
37 extender._navigation_urls = navigation_urls | 38 extender.GetUrlsToNavigate = mock.MagicMock( |
38 extender._browser = FakeBrowser(num_tabs) | 39 return_value=navigation_urls_batch) |
| 40 extender.ShouldExit = mock.MagicMock(return_value=True) |
39 extender._WaitForQueuedTabsToLoad = mock.MagicMock() | 41 extender._WaitForQueuedTabsToLoad = mock.MagicMock() |
| 42 |
| 43 extender._browser = FakeBrowser(extender._NUM_TABS) |
40 extender._BatchNavigateTabs = mock.MagicMock() | 44 extender._BatchNavigateTabs = mock.MagicMock() |
41 | 45 |
42 # Set up a callback to record the tabs and urls in each navigation. | 46 # Set up a callback to record the tabs and urls in each navigation. |
43 batch_callback_tabs = [] | 47 callback_tabs_batch = [] |
44 batch_callback_urls = [] | 48 callback_urls_batch = [] |
45 def SideEffect(*args, **_): | 49 def SideEffect(*args, **_): |
46 batch = args[0] | 50 batch = args[0] |
47 for tab, url in batch: | 51 for tab, url in batch: |
48 batch_callback_tabs.append(tab) | 52 callback_tabs_batch.append(tab) |
49 batch_callback_urls.append(url) | 53 callback_urls_batch.append(url) |
50 extender._BatchNavigateTabs.side_effect = SideEffect | 54 extender._BatchNavigateTabs.side_effect = SideEffect |
51 | 55 |
52 # Perform the navigations. | 56 # Perform the navigations. |
53 extender._PerformNavigations() | 57 extender._PerformNavigations() |
54 | 58 |
55 # Each url should have been navigated to exactly once. | 59 # Each url in the batch should have been navigated to exactly once. |
56 self.assertEqual(set(batch_callback_urls), set(navigation_urls)) | 60 self.assertEqual(set(callback_urls_batch), set(navigation_urls_batch)) |
57 | 61 |
58 # The first 4 tabs should have been navigated 4 times. The remaining tabs | 62 # The other urls should not have been navigated to. |
59 # should have been navigated 3 times. | 63 navigation_urls_remaining = (set(navigation_urls) - |
60 num_navigations_per_tab = 3 | 64 set(navigation_urls_batch)) |
61 num_tabs_with_one_extra_navigation = 4 | 65 self.assertFalse(navigation_urls_remaining & set(callback_urls_batch)) |
| 66 |
| 67 # The first couple of tabs should have been navigated once. The remaining |
| 68 # tabs should not have been navigated. |
62 for i in range(len(extender._browser.tabs)): | 69 for i in range(len(extender._browser.tabs)): |
63 tab = extender._browser.tabs[i] | 70 tab = extender._browser.tabs[i] |
64 | 71 |
65 expected_tab_navigation_count = num_navigations_per_tab | 72 if i < batch_size: |
66 if i < num_tabs_with_one_extra_navigation: | 73 expected_tab_navigation_count = 1 |
67 expected_tab_navigation_count += 1 | 74 else: |
| 75 expected_tab_navigation_count = 0 |
68 | 76 |
69 count = batch_callback_tabs.count(tab) | 77 count = callback_tabs_batch.count(tab) |
70 self.assertEqual(count, expected_tab_navigation_count) | 78 self.assertEqual(count, expected_tab_navigation_count) |
OLD | NEW |