OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import contextlib | 5 import contextlib |
6 from measurements import tab_switching | |
7 import mock | |
8 from page_sets.system_health import multi_tab_stories | |
9 from telemetry import benchmark | |
10 from telemetry import story as story_module | |
11 from telemetry.internal.results import page_test_results | 6 from telemetry.internal.results import page_test_results |
12 from telemetry.testing import page_test_test_case | 7 from telemetry.testing import page_test_test_case |
13 from telemetry.testing import options_for_unittests | 8 |
14 from telemetry.value import histogram | 9 from measurements import tab_switching |
| 10 |
| 11 import mock |
15 | 12 |
16 | 13 |
17 class BrowserForTest(object): | 14 class BrowserForTest(object): |
18 def __init__(self): | 15 def __init__(self): |
19 self.tabs = [] | 16 self.tabs = [] |
20 self.platform = mock.MagicMock() | 17 self.platform = mock.MagicMock() |
21 self.platform.CanMonitorPower = mock.Mock(return_value=False) | 18 self.platform.CanMonitorPower = mock.Mock(return_value=False) |
22 | 19 |
23 def AddTab(self, tab): | 20 def AddTab(self, tab): |
24 tab.browser = self | 21 tab.browser = self |
25 self.tabs.append(tab) | 22 self.tabs.append(tab) |
26 | 23 |
27 | 24 |
28 class StorySetForTest(object): | 25 class StorySetForTest(object): |
29 def __init__(self): | 26 def __init__(self): |
30 self.stories = [] | 27 self.stories = [] |
31 | 28 |
32 def AddStory(self, story): | 29 def AddStory(self, story): |
33 story.story_set = self | 30 story.story_set = self |
34 self.stories.append(story) | 31 self.stories.append(story) |
35 | 32 |
36 INTEGRATION_TEST_TAB_COUNT = 3 | |
37 | |
38 class EmptyMultiTabStory(multi_tab_stories.MultiTabStory): | |
39 NAME = 'multitab:test:empty' | |
40 URL_LIST = ['about:blank'] * INTEGRATION_TEST_TAB_COUNT | |
41 URL = URL_LIST[0] | |
42 | 33 |
43 class TabSwitchingUnittest(page_test_test_case.PageTestTestCase): | 34 class TabSwitchingUnittest(page_test_test_case.PageTestTestCase): |
44 @staticmethod | 35 @staticmethod |
45 def MakeStoryForTest(): | 36 def MakeStoryForTest(): |
46 story = mock.MagicMock() | 37 story = mock.MagicMock() |
47 story.story_set = None | 38 story.story_set = None |
48 return story | 39 return story |
49 | 40 |
50 @staticmethod | 41 @staticmethod |
51 def MakeTabForTest(): | 42 def MakeTabForTest(): |
(...skipping 13 matching lines...) Expand all Loading... |
65 | 56 |
66 # Set up a browser with two tabs open | 57 # Set up a browser with two tabs open |
67 browser = BrowserForTest() | 58 browser = BrowserForTest() |
68 tab_0 = self.MakeTabForTest() | 59 tab_0 = self.MakeTabForTest() |
69 browser.AddTab(tab_0) | 60 browser.AddTab(tab_0) |
70 tab_1 = self.MakeTabForTest() | 61 tab_1 = self.MakeTabForTest() |
71 browser.AddTab(tab_1) | 62 browser.AddTab(tab_1) |
72 | 63 |
73 # Mock histogram result to test _IsDone really works. | 64 # Mock histogram result to test _IsDone really works. |
74 expected_histogram = [ | 65 expected_histogram = [ |
75 # DidNavigateToPage() calls GetHistogram() once | 66 # To get first_histogram for last tab (tab_1). |
76 '{"count": 0, "buckets": []}', | 67 '{"count": 0, "buckets": []}', |
77 # ValidateAndMeasurePage() calls GetHistogram() once | 68 # First _IsDone check for tab_0. Retry. |
| 69 '{"count": 0, "buckets": []}', |
| 70 # Second _IsDone check for tab_0. Retry. |
| 71 '{"count": 0, "buckets": []}', |
| 72 # Third _IsDone check for tab_0. Pass. |
| 73 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
| 74 # To get prev_histogram. End of tab_0 loop. |
| 75 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
| 76 # First _IsDone check for tab_1. Retry. |
| 77 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', |
| 78 # Second _IsDone check for tab_1. Pass. |
| 79 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' |
| 80 '{"low": 2, "high": 3, "count": 1}]}', |
| 81 # To get prev_histogram. End of tab_1 loop. |
78 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' | 82 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' |
79 '{"low": 2, "high": 3, "count": 1}]}', | 83 '{"low": 2, "high": 3, "count": 1}]}', |
80 ] | 84 ] |
81 mock_get_histogram = mock.MagicMock(side_effect=expected_histogram) | 85 mock_get_histogram = mock.MagicMock(side_effect=expected_histogram) |
82 | 86 |
83 with contextlib.nested( | 87 with contextlib.nested( |
84 mock.patch('telemetry.value.histogram_util.GetHistogram', | 88 mock.patch('telemetry.value.histogram_util.GetHistogram', |
85 mock_get_histogram), | 89 mock_get_histogram), |
86 mock.patch('metrics.keychain_metric.KeychainMetric')): | 90 mock.patch('metrics.keychain_metric.KeychainMetric')): |
87 measure.DidNavigateToPage(story_set.stories[0], browser.tabs[-1]) | |
88 measure.ValidateAndMeasurePage(story_set.stories[0], browser.tabs[-1], | 91 measure.ValidateAndMeasurePage(story_set.stories[0], browser.tabs[-1], |
89 page_test_results.PageTestResults()) | 92 page_test_results.PageTestResults()) |
90 self.assertEqual(len(expected_histogram), | 93 self.assertEqual(len(expected_histogram), |
91 len(mock_get_histogram.mock_calls)) | 94 len(mock_get_histogram.mock_calls)) |
92 # The last tab is passed to DidNavigateToPage() and | |
93 # ValidateAndMeasurePage() | |
94 expected_calls = [mock.call(mock.ANY, mock.ANY, t) for t in | 95 expected_calls = [mock.call(mock.ANY, mock.ANY, t) for t in |
95 [browser.tabs[-1]] * 2] | 96 [tab_1] + [tab_0] * 4 + [tab_1] * 3] |
96 self.assertEqual(expected_calls, mock_get_histogram.mock_calls) | 97 self.assertEqual(expected_calls, mock_get_histogram.mock_calls) |
97 | |
98 @benchmark.Enabled('has tabs') | |
99 @benchmark.Disabled('mac-reference') | |
100 @benchmark.Disabled('android') | |
101 def testTabSwitching(self): | |
102 """IT of TabSwitching measurement and multi-tab story""" | |
103 ps = story_module.StorySet() | |
104 ps.AddStory(EmptyMultiTabStory(ps, False)) | |
105 measurement = tab_switching.TabSwitching() | |
106 options = options_for_unittests.GetCopy() | |
107 results = self.RunMeasurement(measurement, ps, options=options) | |
108 self.assertEquals(len(results.failures), 0) | |
109 | |
110 self.assertEquals(len(results.all_summary_values), 1) | |
111 summary = results.all_summary_values[0] | |
112 self.assertIsInstance(summary, histogram.HistogramValue) | |
113 self.assertEquals(summary.name, 'MPArch_RWH_TabSwitchPaintDuration') | |
114 histogram_count = sum([b.count for b in summary.buckets]) | |
115 self.assertEquals(histogram_count, INTEGRATION_TEST_TAB_COUNT) | |
116 histogram_mean = summary.GetRepresentativeNumber() | |
117 self.assertGreater(histogram_mean, 0) | |
OLD | NEW |