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