summaryrefslogtreecommitdiffstats
path: root/tools/perf/profile_creators/fast_navigation_profile_extender_unittest.py
blob: 86e6c81a82d446d332f6159c0eb4b44b6733f1f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest

from profile_creators.fast_navigation_profile_extender import (
    FastNavigationProfileExtender)
from telemetry.core import util
from telemetry.unittest_util import options_for_unittests

util.AddDirToPythonPath(util.GetTelemetryDir(), 'third_party', 'mock')
import mock  # pylint: disable=import-error


class FakeTab(object):
  pass


class FakeTabList(object):
  def __init__(self):
    self._tabs = []

  def New(self):
    tab = FakeTab()
    self._tabs.append(tab)
    return tab

  def __len__(self):
    return len(self._tabs)


class FakeBrowser(object):
  def __init__(self):
    self.tabs = FakeTabList()


# Testing private method.
# pylint: disable=protected-access
class FastNavigationProfileExtenderTest(unittest.TestCase):
  def testPerformNavigations(self):
    maximum_batch_size = 15
    options = options_for_unittests.GetCopy()
    extender = FastNavigationProfileExtender(options, maximum_batch_size)

    navigation_urls = []
    for i in range(extender._NUM_TABS):
      navigation_urls.append('http://test%s.com' % i)
    batch_size = 5
    navigation_urls_batch = navigation_urls[3:3 + batch_size]

    extender.GetUrlIterator = mock.MagicMock(
        return_value=iter(navigation_urls_batch))
    extender.ShouldExitAfterBatchNavigation = mock.MagicMock(return_value=True)
    extender._WaitForQueuedTabsToLoad = mock.MagicMock()

    extender._browser = FakeBrowser()
    extender._BatchNavigateTabs = mock.MagicMock()

    # Set up a callback to record the tabs and urls in each navigation.
    callback_tabs_batch = []
    callback_urls_batch = []
    def SideEffect(*args, **_):
      batch = args[0]
      for tab, url in batch:
        callback_tabs_batch.append(tab)
        callback_urls_batch.append(url)
    extender._BatchNavigateTabs.side_effect = SideEffect

    # Perform the navigations.
    extender._PerformNavigations()

    # Each url in the batch should have been navigated to exactly once.
    self.assertEqual(set(callback_urls_batch), set(navigation_urls_batch))

    # The other urls should not have been navigated to.
    navigation_urls_remaining = (set(navigation_urls) -
        set(navigation_urls_batch))
    self.assertFalse(navigation_urls_remaining & set(callback_urls_batch))

    # The first couple of tabs should have been navigated once. The remaining
    # tabs should not have been navigated.
    for i in range(len(extender._browser.tabs)):
      tab = extender._browser.tabs._tabs[i]

      if i < batch_size:
        expected_tab_navigation_count = 1
      else:
        expected_tab_navigation_count = 0

      count = callback_tabs_batch.count(tab)
      self.assertEqual(count, expected_tab_navigation_count)