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..819b7722c7c3f08eb560d22ef51a8c3f4e2417b4 |
--- /dev/null |
+++ b/tools/perf/measurements/tab_switching_unittest.py |
@@ -0,0 +1,105 @@ |
+# 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.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 FakePlatform(object): |
eakuefner
2015/06/18 17:23:29
Since you're already importing the mock library, c
deanliao
2015/06/22 06:37:07
Except Browser and PageSet, I use MagicMock for th
|
+ def CanMonitorPower(self): |
+ return False |
+ |
+ |
+class FakeBrowser(object): |
+ def __init__(self): |
+ self.tabs = [] |
+ self.platform = FakePlatform() |
+ |
+ def AddTab(self, tab): |
+ tab.browser = self |
+ self.tabs.append(tab) |
+ |
+ |
+class FakeTab(object): |
+ """Used to mock a browser tab.""" |
eakuefner
2015/06/18 17:23:29
nit: Why docstring here? It seems like this is as
deanliao
2015/06/22 06:37:07
Removed.
|
+ def __init__(self): |
+ self.browser = None |
+ |
+ def HasReachedQuiescence(self): |
+ return True |
+ |
+ def Activate(self): |
+ pass |
+ |
+ |
+class FakePage(object): |
+ def __init__(self, url): |
+ self.url = url |
+ self.page_set = None |
+ |
+ |
+class FakePageSet(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): |
+ def testIsDone(self): |
+ """Tests ValidateAndMeasurePage, specifically _IsDone check.""" |
+ measure = tab_switching.TabSwitching() |
+ |
+ # For sanity check: #tabs == #page_sets |
eakuefner
2015/06/18 17:23:29
Should be #tabs == #pages, right?
deanliao
2015/06/22 06:37:07
Done.
|
+ page_set = FakePageSet() |
+ page_set.AddPage(FakePage('http://fake.com/1')) |
+ page_set.AddPage(FakePage('http://fake.com/2')) |
+ |
+ browser = FakeBrowser() |
+ tab_0 = FakeTab() |
+ tab_1 = FakeTab() |
+ browser.AddTab(tab_0) |
+ browser.AddTab(tab_1) |
+ |
+ results = page_test_results.PageTestResults() |
+ |
+ # 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], tab_1, results) |
+ 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) |