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 telemetry.internal.results import page_test_results | 6 from telemetry.internal.results import page_test_results |
7 from telemetry.testing import page_test_test_case | 7 from telemetry.testing import page_test_test_case |
8 | 8 |
9 from measurements import tab_switching | 9 from measurements import tab_switching |
10 | 10 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 | 56 |
57 # Set up a browser with two tabs open | 57 # Set up a browser with two tabs open |
58 browser = BrowserForTest() | 58 browser = BrowserForTest() |
59 tab_0 = self.MakeTabForTest() | 59 tab_0 = self.MakeTabForTest() |
60 browser.AddTab(tab_0) | 60 browser.AddTab(tab_0) |
61 tab_1 = self.MakeTabForTest() | 61 tab_1 = self.MakeTabForTest() |
62 browser.AddTab(tab_1) | 62 browser.AddTab(tab_1) |
63 | 63 |
64 # Mock histogram result to test _IsDone really works. | 64 # Mock histogram result to test _IsDone really works. |
65 expected_histogram = [ | 65 expected_histogram = [ |
66 # To get first_histogram for last tab (tab_1). | 66 # DidNavigateToPage() calls GetHistogram() once |
67 '{"count": 0, "buckets": []}', | 67 '{"count": 0, "buckets": []}', |
68 # First _IsDone check for tab_0. Retry. | 68 # ValidateAndMeasurePage() calls GetHistogram() once |
vovoy
2017/03/23 09:37:49
_IsDone() check loop is removed in the written tab
| |
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. | |
82 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' | 69 '{"count": 2, "buckets": [{"low": 1, "high": 2, "count": 1},' |
83 '{"low": 2, "high": 3, "count": 1}]}', | 70 '{"low": 2, "high": 3, "count": 1}]}', |
84 ] | 71 ] |
85 mock_get_histogram = mock.MagicMock(side_effect=expected_histogram) | 72 mock_get_histogram = mock.MagicMock(side_effect=expected_histogram) |
nednguyen
2017/03/23 13:15:10
I really like to get away with using mock here bec
vovoy
2017/03/23 16:55:58
I think unittest shall test one thing at a time.
i
nednguyen
2017/03/23 23:49:06
from my experience, these mock tests are mostly in
vovoy
2017/03/24 00:25:51
OK, I will check how smoothness_unittest works.
vovoy
2017/03/24 16:35:30
Done.
| |
86 | 73 |
87 with contextlib.nested( | 74 with contextlib.nested( |
88 mock.patch('telemetry.value.histogram_util.GetHistogram', | 75 mock.patch('telemetry.value.histogram_util.GetHistogram', |
89 mock_get_histogram), | 76 mock_get_histogram), |
90 mock.patch('metrics.keychain_metric.KeychainMetric')): | 77 mock.patch('metrics.keychain_metric.KeychainMetric')): |
78 measure.DidNavigateToPage(story_set.stories[0], browser.tabs[-1]) | |
91 measure.ValidateAndMeasurePage(story_set.stories[0], browser.tabs[-1], | 79 measure.ValidateAndMeasurePage(story_set.stories[0], browser.tabs[-1], |
92 page_test_results.PageTestResults()) | 80 page_test_results.PageTestResults()) |
93 self.assertEqual(len(expected_histogram), | 81 self.assertEqual(len(expected_histogram), |
94 len(mock_get_histogram.mock_calls)) | 82 len(mock_get_histogram.mock_calls)) |
83 # The last tab is passed to DidNavigateToPage() and | |
84 # ValidateAndMeasurePage() | |
95 expected_calls = [mock.call(mock.ANY, mock.ANY, t) for t in | 85 expected_calls = [mock.call(mock.ANY, mock.ANY, t) for t in |
96 [tab_1] + [tab_0] * 4 + [tab_1] * 3] | 86 [browser.tabs[-1]] * 2] |
97 self.assertEqual(expected_calls, mock_get_histogram.mock_calls) | 87 self.assertEqual(expected_calls, mock_get_histogram.mock_calls) |
OLD | NEW |