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.
6 from profile_creators
.fast_navigation_profile_extender
import (
7 FastNavigationProfileExtender
)
8 from telemetry
.core
import util
10 util
.AddDirToPythonPath(util
.GetTelemetryDir(), 'third_party', 'mock')
14 class FakeTab(object):
18 class FakeTabList(object):
24 self
._tabs
.append(tab
)
28 return len(self
._tabs
)
31 class FakeBrowser(object):
33 self
.tabs
= FakeTabList()
36 # Testing private method.
37 # pylint: disable=protected-access
38 class FastNavigationProfileExtenderTest(unittest
.TestCase
):
39 def testPerformNavigations(self
):
40 maximum_batch_size
= 15
41 extender
= FastNavigationProfileExtender(maximum_batch_size
)
44 for i
in range(extender
._NUM
_TABS
):
45 navigation_urls
.append('http://test%s.com' % i
)
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
, **_
):
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
]
84 expected_tab_navigation_count
= 1
86 expected_tab_navigation_count
= 0
88 count
= callback_tabs_batch
.count(tab
)
89 self
.assertEqual(count
, expected_tab_navigation_count
)