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
9 from telemetry
.unittest_util
import options_for_unittests
11 util
.AddDirToPythonPath(util
.GetTelemetryDir(), 'third_party', 'mock')
12 import mock
# pylint: disable=import-error
15 class FakeTab(object):
19 class FakeTabList(object):
25 self
._tabs
.append(tab
)
29 return len(self
._tabs
)
32 class FakeBrowser(object):
34 self
.tabs
= FakeTabList()
37 # Testing private method.
38 # pylint: disable=protected-access
39 class FastNavigationProfileExtenderTest(unittest
.TestCase
):
40 def testPerformNavigations(self
):
41 maximum_batch_size
= 15
42 options
= options_for_unittests
.GetCopy()
43 extender
= FastNavigationProfileExtender(options
, maximum_batch_size
)
46 for i
in range(extender
._NUM
_TABS
):
47 navigation_urls
.append('http://test%s.com' % i
)
49 navigation_urls_batch
= navigation_urls
[3:3 + batch_size
]
51 extender
.GetUrlIterator
= mock
.MagicMock(
52 return_value
=iter(navigation_urls_batch
))
53 extender
.ShouldExitAfterBatchNavigation
= mock
.MagicMock(return_value
=True)
54 extender
._WaitForQueuedTabsToLoad
= mock
.MagicMock()
56 extender
._browser
= FakeBrowser()
57 extender
._BatchNavigateTabs
= mock
.MagicMock()
59 # Set up a callback to record the tabs and urls in each navigation.
60 callback_tabs_batch
= []
61 callback_urls_batch
= []
62 def SideEffect(*args
, **_
):
64 for tab
, url
in batch
:
65 callback_tabs_batch
.append(tab
)
66 callback_urls_batch
.append(url
)
67 extender
._BatchNavigateTabs
.side_effect
= SideEffect
69 # Perform the navigations.
70 extender
._PerformNavigations
()
72 # Each url in the batch should have been navigated to exactly once.
73 self
.assertEqual(set(callback_urls_batch
), set(navigation_urls_batch
))
75 # The other urls should not have been navigated to.
76 navigation_urls_remaining
= (set(navigation_urls
) -
77 set(navigation_urls_batch
))
78 self
.assertFalse(navigation_urls_remaining
& set(callback_urls_batch
))
80 # The first couple of tabs should have been navigated once. The remaining
81 # tabs should not have been navigated.
82 for i
in range(len(extender
._browser
.tabs
)):
83 tab
= extender
._browser
.tabs
._tabs
[i
]
86 expected_tab_navigation_count
= 1
88 expected_tab_navigation_count
= 0
90 count
= callback_tabs_batch
.count(tab
)
91 self
.assertEqual(count
, expected_tab_navigation_count
)