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 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
| |
17 def CanMonitorPower(self): | |
18 return False | |
19 | |
20 | |
21 class FakeBrowser(object): | |
22 def __init__(self): | |
23 self.tabs = [] | |
24 self.platform = FakePlatform() | |
25 | |
26 def AddTab(self, tab): | |
27 tab.browser = self | |
28 self.tabs.append(tab) | |
29 | |
30 | |
31 class FakeTab(object): | |
32 """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.
| |
33 def __init__(self): | |
34 self.browser = None | |
35 | |
36 def HasReachedQuiescence(self): | |
37 return True | |
38 | |
39 def Activate(self): | |
40 pass | |
41 | |
42 | |
43 class FakePage(object): | |
44 def __init__(self, url): | |
45 self.url = url | |
46 self.page_set = None | |
47 | |
48 | |
49 class FakePageSet(object): | |
50 def __init__(self): | |
51 self.pages = [] | |
52 | |
53 def AddPage(self, page): | |
54 page.page_set = self | |
55 self.pages.append(page) | |
56 | |
57 | |
58 class TabSwitchingUnittest(page_test_test_case.PageTestTestCase): | |
59 def testIsDone(self): | |
60 """Tests ValidateAndMeasurePage, specifically _IsDone check.""" | |
61 measure = tab_switching.TabSwitching() | |
62 | |
63 # 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.
| |
64 page_set = FakePageSet() | |
65 page_set.AddPage(FakePage('http://fake.com/1')) | |
66 page_set.AddPage(FakePage('http://fake.com/2')) | |
67 | |
68 browser = FakeBrowser() | |
69 tab_0 = FakeTab() | |
70 tab_1 = FakeTab() | |
71 browser.AddTab(tab_0) | |
72 browser.AddTab(tab_1) | |
73 | |
74 results = page_test_results.PageTestResults() | |
75 | |
76 # Mock histogram result to test _IsDone really works. | |
77 mock_get_histogram = mock.MagicMock(side_effect=[ | |
78 # To get first_histogram for last tab (tab_1). | |
79 '{"count": 0, "buckets": []}', | |
80 # First _IsDone check for tab_0. Retry. | |
81 '{"count": 0, "buckets": []}', | |
82 # Second _IsDone check for tab_0. Retry. | |
83 '{"count": 0, "buckets": []}', | |
84 # Third _IsDone check for tab_0. Pass. | |
85 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', | |
86 # To get prev_histogram. End of tab_0 loop. | |
87 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', | |
88 # First _IsDone check for tab_1. Retry. | |
89 '{"count": 1, "buckets": [{"low": 1, "high": 2, "count": 1}]}', | |
90 # Second _IsDone check for tab_1. Pass. | |
91 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' | |
92 '{"low": 2, "high": 3, "count": 1}]}', | |
93 # To get prev_histogram. End of tab_1 loop. | |
94 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' | |
95 '{"low": 2, "high": 3, "count": 1}]}', | |
96 ]) | |
97 | |
98 | |
99 with mock.patch('telemetry.value.histogram_util.GetHistogram', | |
100 mock_get_histogram): | |
101 measure.ValidateAndMeasurePage(page_set.pages[0], tab_1, results) | |
102 self.assertEqual(8, len(mock_get_histogram.mock_calls)) | |
103 expected_calls = [mock.call(mock.ANY, mock.ANY, t) for t in | |
104 [tab_1] + [tab_0] * 4 + [tab_1] * 3] | |
105 self.assertEqual(expected_calls, mock_get_histogram.mock_calls) | |
OLD | NEW |