Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / perf / profile_creators / fast_navigation_profile_extender_unittest.py
blob1b48e4db586e22a1712609179f783af96142da1a
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
3 # found in the LICENSE file.
4 import unittest
6 from profile_creators.fast_navigation_profile_extender import (
7 FastNavigationProfileExtender)
8 from telemetry.testing import options_for_unittests
10 import mock # pylint: disable=import-error
13 class FakeTab(object):
14 pass
17 class FakeTabList(object):
18 def __init__(self):
19 self._tabs = []
21 def New(self):
22 tab = FakeTab()
23 self._tabs.append(tab)
24 return tab
26 def __len__(self):
27 return len(self._tabs)
30 class FakeBrowser(object):
31 def __init__(self):
32 self.tabs = FakeTabList()
35 # Testing private method.
36 # pylint: disable=protected-access
37 class FastNavigationProfileExtenderTest(unittest.TestCase):
38 def testPerformNavigations(self):
39 maximum_batch_size = 15
40 options = options_for_unittests.GetCopy()
41 extender = FastNavigationProfileExtender(options, maximum_batch_size)
43 navigation_urls = []
44 for i in range(extender._NUM_TABS):
45 navigation_urls.append('http://test%s.com' % i)
46 batch_size = 5
47 navigation_urls_batch = navigation_urls[3:3 + batch_size]
49 extender.GetUrlIterator = mock.MagicMock(
50 return_value=iter(navigation_urls_batch))
51 extender.ShouldExitAfterBatchNavigation = mock.MagicMock(return_value=True)
52 extender._WaitForQueuedTabsToLoad = mock.MagicMock()
54 extender._browser = FakeBrowser()
55 extender._BatchNavigateTabs = mock.MagicMock()
57 # Set up a callback to record the tabs and urls in each navigation.
58 callback_tabs_batch = []
59 callback_urls_batch = []
60 def SideEffect(*args, **_):
61 batch = args[0]
62 for tab, url in batch:
63 callback_tabs_batch.append(tab)
64 callback_urls_batch.append(url)
65 extender._BatchNavigateTabs.side_effect = SideEffect
67 # Perform the navigations.
68 extender._PerformNavigations()
70 # Each url in the batch should have been navigated to exactly once.
71 self.assertEqual(set(callback_urls_batch), set(navigation_urls_batch))
73 # The other urls should not have been navigated to.
74 navigation_urls_remaining = (set(navigation_urls) -
75 set(navigation_urls_batch))
76 self.assertFalse(navigation_urls_remaining & set(callback_urls_batch))
78 # The first couple of tabs should have been navigated once. The remaining
79 # tabs should not have been navigated.
80 for i in range(len(extender._browser.tabs)):
81 tab = extender._browser.tabs._tabs[i]
83 if i < batch_size:
84 expected_tab_navigation_count = 1
85 else:
86 expected_tab_navigation_count = 0
88 count = callback_tabs_batch.count(tab)
89 self.assertEqual(count, expected_tab_navigation_count)