Index: tools/perf/measurements/tab_switching_unittest.py |
diff --git a/tools/perf/measurements/tab_switching_unittest.py b/tools/perf/measurements/tab_switching_unittest.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ae01f86fab761e762fe8bb3d20cc056e03665657 |
--- /dev/null |
+++ b/tools/perf/measurements/tab_switching_unittest.py |
@@ -0,0 +1,96 @@ |
+# 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. |
+ |
+from telemetry.core import util |
+from telemetry.internal.results import page_test_results |
+from telemetry.unittest_util import page_test_test_case |
+ |
+from measurements import tab_switching |
+ |
+# Import Python mock module (https://pypi.python.org/pypi/mock) |
+util.AddDirToPythonPath(util.GetTelemetryDir(), 'third_party', 'mock') |
+import mock # pylint: disable=import-error |
+ |
+ |
+class BrowserForTest(object): |
+ def __init__(self): |
+ self.tabs = [] |
+ self.platform = mock.MagicMock() |
+ self.platform.CanMonitorPower = mock.Mock(return_value=False) |
+ |
+ def AddTab(self, tab): |
+ tab.browser = self |
+ self.tabs.append(tab) |
+ |
+ |
+class PageSetForTest(object): |
+ def __init__(self): |
+ self.pages = [] |
+ |
+ def AddPage(self, page): |
+ page.page_set = self |
+ self.pages.append(page) |
+ |
+ |
+class TabSwitchingUnittest(page_test_test_case.PageTestTestCase): |
+ @staticmethod |
+ def MakePageForTest(): |
+ page = mock.MagicMock() |
+ page.page_set = None |
+ return page |
+ |
+ @staticmethod |
+ def MakeTabForTest(): |
+ tab = mock.MagicMock() |
+ tab.browser = None |
+ tab.HasReachedQuiescence = mock.Mock(return_value=True) |
+ return tab |
+ |
+ def testIsDone(self): |
+ """Tests ValidateAndMeasurePage, specifically _IsDone check.""" |
+ measure = tab_switching.TabSwitching() |
+ |
+ # For sanity check: #tabs == #pages |
+ page_set = PageSetForTest() |
+ page_set.AddPage(self.MakePageForTest()) |
+ page_set.AddPage(self.MakePageForTest()) |
+ |
+ # Set up a browser with two tabs open |
+ browser = BrowserForTest() |
+ tab_0 = self.MakeTabForTest() |
+ browser.AddTab(tab_0) |
+ tab_1 = self.MakeTabForTest() |
+ browser.AddTab(tab_1) |
+ |
+ # Mock histogram result to test _IsDone really works. |
+ mock_get_histogram = mock.MagicMock(side_effect=[ |
+ # To get first_histogram for last tab (tab_1). |
+ '{"count": 0, "buckets": []}', |
+ # First _IsDone check for tab_0. Retry. |
+ '{"count": 0, "buckets": []}', |
+ # Second _IsDone check for tab_0. Retry. |
+ '{"count": 0, "buckets": []}', |
+ # Third _IsDone check for tab_0. Pass. |
+ '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
+ # To get prev_histogram. End of tab_0 loop. |
+ '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
+ # First _IsDone check for tab_1. Retry. |
+ '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
+ # Second _IsDone check for tab_1. Pass. |
+ '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' |
+ '{"low": 2, "high": 3, "count": 1}]}', |
+ # To get prev_histogram. End of tab_1 loop. |
+ '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' |
+ '{"low": 2, "high": 3, "count": 1}]}', |
+ ]) |
+ |
+ |
+ with mock.patch('telemetry.value.histogram_util.GetHistogram', |
+ mock_get_histogram): |
+ measure.ValidateAndMeasurePage(page_set.pages[0], browser.tabs[-1], |
+ page_test_results.PageTestResults()) |
+ self.assertEqual(8, len(mock_get_histogram.mock_calls)) |
+ expected_calls = [mock.call(mock.ANY, mock.ANY, t) for t in |
+ [tab_1] + [tab_0] * 4 + [tab_1] * 3] |
+ self.assertEqual(expected_calls, mock_get_histogram.mock_calls) |