OLD | NEW |
(Empty) | |
| 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 |
| 5 from telemetry.core import util |
| 6 from telemetry.results import page_test_results |
| 7 from telemetry.unittest_util import page_test_test_case |
| 8 |
| 9 from measurements import tab_switching |
| 10 |
| 11 # Import Python mock module (https://pypi.python.org/pypi/mock) |
| 12 util.AddDirToPythonPath(util.GetTelemetryDir(), 'third_party', 'mock') |
| 13 import mock # pylint: disable=import-error |
| 14 |
| 15 |
| 16 class BrowserForTest(object): |
| 17 def __init__(self): |
| 18 self.tabs = [] |
| 19 self.platform = mock.MagicMock() |
| 20 self.platform.CanMonitorPower = mock.Mock(return_value=False) |
| 21 |
| 22 def AddTab(self, tab): |
| 23 tab.browser = self |
| 24 self.tabs.append(tab) |
| 25 |
| 26 |
| 27 class PageSetForTest(object): |
| 28 def __init__(self): |
| 29 self.pages = [] |
| 30 |
| 31 def AddPage(self, page): |
| 32 page.page_set = self |
| 33 self.pages.append(page) |
| 34 |
| 35 |
| 36 class TabSwitchingUnittest(page_test_test_case.PageTestTestCase): |
| 37 @staticmethod |
| 38 def MakePageForTest(): |
| 39 page = mock.MagicMock() |
| 40 page.page_set = None |
| 41 return page |
| 42 |
| 43 @staticmethod |
| 44 def MakeTabForTest(): |
| 45 tab = mock.MagicMock() |
| 46 tab.browser = None |
| 47 tab.HasReachedQuiescence = mock.Mock(return_value=True) |
| 48 return tab |
| 49 |
| 50 def testIsDone(self): |
| 51 """Tests ValidateAndMeasurePage, specifically _IsDone check.""" |
| 52 measure = tab_switching.TabSwitching() |
| 53 |
| 54 # For sanity check: #tabs == #pages |
| 55 page_set = PageSetForTest() |
| 56 page_set.AddPage(self.MakePageForTest()) |
| 57 page_set.AddPage(self.MakePageForTest()) |
| 58 |
| 59 # Set up a browser with two tabs open |
| 60 browser = BrowserForTest() |
| 61 tab_0 = self.MakeTabForTest() |
| 62 browser.AddTab(tab_0) |
| 63 tab_1 = self.MakeTabForTest() |
| 64 browser.AddTab(tab_1) |
| 65 |
| 66 # Mock histogram result to test _IsDone really works. |
| 67 mock_get_histogram = mock.MagicMock(side_effect=[ |
| 68 # To get first_histogram for last tab (tab_1). |
| 69 '{"count": 0, "buckets": []}', |
| 70 # First _IsDone check for tab_0. Retry. |
| 71 '{"count": 0, "buckets": []}', |
| 72 # Second _IsDone check for tab_0. Retry. |
| 73 '{"count": 0, "buckets": []}', |
| 74 # Third _IsDone check for tab_0. Pass. |
| 75 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
| 76 # To get prev_histogram. End of tab_0 loop. |
| 77 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
| 78 # First _IsDone check for tab_1. Retry. |
| 79 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
| 80 # Second _IsDone check for tab_1. Pass. |
| 81 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' |
| 82 '{"low": 2, "high": 3, "count": 1}]}', |
| 83 # To get prev_histogram. End of tab_1 loop. |
| 84 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' |
| 85 '{"low": 2, "high": 3, "count": 1}]}', |
| 86 ]) |
| 87 |
| 88 |
| 89 with mock.patch('telemetry.value.histogram_util.GetHistogram', |
| 90 mock_get_histogram): |
| 91 measure.ValidateAndMeasurePage(page_set.pages[0], browser.tabs[-1], |
| 92 page_test_results.PageTestResults()) |
| 93 self.assertEqual(8, len(mock_get_histogram.mock_calls)) |
| 94 expected_calls = [mock.call(mock.ANY, mock.ANY, t) for t in |
| 95 [tab_1] + [tab_0] * 4 + [tab_1] * 3] |
| 96 self.assertEqual(expected_calls, mock_get_histogram.mock_calls) |
OLD | NEW |