OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/metrics/histogram_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/test/histogram_tester.h" | 11 #include "base/test/histogram_tester.h" |
12 #import "ios/chrome/browser/metrics/previous_session_info.h" | 12 #import "ios/chrome/browser/metrics/previous_session_info.h" |
13 #include "ios/chrome/browser/metrics/tab_usage_recorder.h" | 13 #include "ios/chrome/browser/metrics/tab_usage_recorder.h" |
14 #import "ios/chrome/browser/metrics/tab_usage_recorder_delegate.h" | 14 #import "ios/chrome/browser/metrics/tab_usage_recorder_delegate.h" |
15 #import "ios/chrome/browser/tabs/tab.h" | 15 #import "ios/chrome/browser/tabs/tab.h" |
16 #include "ios/testing/ocmock_complex_type_helper.h" | 16 #include "ios/testing/ocmock_complex_type_helper.h" |
17 #import "ios/web/public/test/fakes/test_navigation_manager.h" | 17 #import "ios/web/public/test/fakes/test_navigation_manager.h" |
18 #import "ios/web/public/test/fakes/test_web_state.h" | 18 #import "ios/web/public/test/fakes/test_web_state.h" |
19 #include "ios/web/public/test/test_web_thread.h" | 19 #include "ios/web/public/test/test_web_thread.h" |
20 #import "ios/web/web_state/ui/crw_web_controller.h" | 20 #import "ios/web/web_state/ui/crw_web_controller.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
23 #include "third_party/ocmock/ocmock_extensions.h" | 23 #include "third_party/ocmock/ocmock_extensions.h" |
24 | 24 |
25 #if !defined(__has_feature) || !__has_feature(objc_arc) | 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
26 #error "This file requires ARC support." | 26 #error "This file requires ARC support." |
27 #endif | 27 #endif |
28 | 28 |
29 @interface TURTestTabMock : OCMockComplexTypeHelper { | 29 @interface TURTestTabMock : OCMockComplexTypeHelper { |
30 GURL _url; | |
31 GURL _lastCommittedURL; | 30 GURL _lastCommittedURL; |
32 GURL _visibleURL; | 31 GURL _visibleURL; |
33 web::TestWebState _webState; | 32 web::TestWebState _webState; |
34 } | 33 } |
35 | 34 |
36 @property(nonatomic, assign) const GURL& url; | |
37 @property(nonatomic, assign) const GURL& lastCommittedURL; | 35 @property(nonatomic, assign) const GURL& lastCommittedURL; |
38 @property(nonatomic, assign) const GURL& visibleURL; | 36 @property(nonatomic, assign) const GURL& visibleURL; |
39 @property(nonatomic, readonly) web::WebState* webState; | 37 @property(nonatomic, readonly) web::WebState* webState; |
40 | 38 |
41 @end | 39 @end |
42 | 40 |
43 @implementation TURTestTabMock | 41 @implementation TURTestTabMock |
44 - (const GURL&)url { | |
45 return _url; | |
46 } | |
47 - (void)setUrl:(const GURL&)url { | |
48 _url = url; | |
49 } | |
50 - (const GURL&)lastCommittedURL { | 42 - (const GURL&)lastCommittedURL { |
51 return _lastCommittedURL; | 43 return _lastCommittedURL; |
52 } | 44 } |
53 - (void)setLastCommittedURL:(const GURL&)lastCommittedURL { | 45 - (void)setLastCommittedURL:(const GURL&)lastCommittedURL { |
54 _lastCommittedURL = lastCommittedURL; | 46 _lastCommittedURL = lastCommittedURL; |
55 } | 47 } |
56 - (const GURL&)visibleURL { | 48 - (const GURL&)visibleURL { |
57 return _visibleURL; | 49 return _visibleURL; |
58 } | 50 } |
59 - (void)setVisibleURL:(const GURL&)visibleURL { | 51 - (void)setVisibleURL:(const GURL&)visibleURL { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 nativeUrl_ = GURL("chrome://version"); | 119 nativeUrl_ = GURL("chrome://version"); |
128 } | 120 } |
129 | 121 |
130 id MockTab(bool inMemory) { | 122 id MockTab(bool inMemory) { |
131 id tab_mock = [[TURTestTabMock alloc] | 123 id tab_mock = [[TURTestTabMock alloc] |
132 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; | 124 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
133 id web_controller_mock = | 125 id web_controller_mock = |
134 [OCMockObject mockForClass:[CRWWebController class]]; | 126 [OCMockObject mockForClass:[CRWWebController class]]; |
135 [[[tab_mock stub] andReturn:web_controller_mock] webController]; | 127 [[[tab_mock stub] andReturn:web_controller_mock] webController]; |
136 [[[tab_mock stub] andReturnBool:false] isPrerenderTab]; | 128 [[[tab_mock stub] andReturnBool:false] isPrerenderTab]; |
137 [tab_mock setUrl:webUrl_]; | |
138 [tab_mock setLastCommittedURL:webUrl_]; | 129 [tab_mock setLastCommittedURL:webUrl_]; |
139 [tab_mock setVisibleURL:webUrl_]; | 130 [tab_mock setVisibleURL:webUrl_]; |
140 [[[web_controller_mock stub] andReturnBool:inMemory] isViewAlive]; | 131 [[[web_controller_mock stub] andReturnBool:inMemory] isViewAlive]; |
141 [[web_controller_mock stub] removeObserver:OCMOCK_ANY]; | 132 [[web_controller_mock stub] removeObserver:OCMOCK_ANY]; |
142 return tab_mock; | 133 return tab_mock; |
143 } | 134 } |
144 | 135 |
145 GURL webUrl_; | 136 GURL webUrl_; |
146 GURL nativeUrl_; | 137 GURL nativeUrl_; |
147 std::unique_ptr<base::MessageLoop> loop_; | 138 std::unique_ptr<base::MessageLoop> loop_; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 tab_usage_recorder_->RecordPageLoadStart(tab_mock_a); | 187 tab_usage_recorder_->RecordPageLoadStart(tab_mock_a); |
197 } | 188 } |
198 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); | 189 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); |
199 histogram_tester_->ExpectUniqueSample(kPageLoadsBeforeEvictedTabSelected, | 190 histogram_tester_->ExpectUniqueSample(kPageLoadsBeforeEvictedTabSelected, |
200 kNumReloads, 1); | 191 kNumReloads, 1); |
201 } | 192 } |
202 | 193 |
203 TEST_F(TabUsageRecorderTest, CountNativePageLoadsBeforeEvictedTab) { | 194 TEST_F(TabUsageRecorderTest, CountNativePageLoadsBeforeEvictedTab) { |
204 id tab_mock_a = MockTab(true); | 195 id tab_mock_a = MockTab(true); |
205 id tab_mock_b = MockTab(false); | 196 id tab_mock_b = MockTab(false); |
206 [tab_mock_a setUrl:nativeUrl_]; | |
207 [tab_mock_a setLastCommittedURL:nativeUrl_]; | 197 [tab_mock_a setLastCommittedURL:nativeUrl_]; |
208 [tab_mock_a setVisibleURL:nativeUrl_]; | 198 [tab_mock_a setVisibleURL:nativeUrl_]; |
209 [tab_mock_b setUrl:nativeUrl_]; | |
210 [tab_mock_b setLastCommittedURL:nativeUrl_]; | 199 [tab_mock_b setLastCommittedURL:nativeUrl_]; |
211 [tab_mock_b setVisibleURL:nativeUrl_]; | 200 [tab_mock_b setVisibleURL:nativeUrl_]; |
212 | 201 |
213 // Call reload an arbitrary number of times. | 202 // Call reload an arbitrary number of times. |
214 const int kNumReloads = 4; | 203 const int kNumReloads = 4; |
215 for (int i = 0; i < kNumReloads; i++) { | 204 for (int i = 0; i < kNumReloads; i++) { |
216 tab_usage_recorder_->RecordPageLoadStart(tab_mock_a); | 205 tab_usage_recorder_->RecordPageLoadStart(tab_mock_a); |
217 } | 206 } |
218 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); | 207 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); |
219 histogram_tester_->ExpectTotalCount(kPageLoadsBeforeEvictedTabSelected, 0); | 208 histogram_tester_->ExpectTotalCount(kPageLoadsBeforeEvictedTabSelected, 0); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 | 384 |
396 tab_usage_recorder_->RendererTerminated(tab_mock_b, false); | 385 tab_usage_recorder_->RendererTerminated(tab_mock_b, false); |
397 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); | 386 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); |
398 | 387 |
399 histogram_tester_->ExpectUniqueSample( | 388 histogram_tester_->ExpectUniqueSample( |
400 kSelectedTabHistogramName, | 389 kSelectedTabHistogramName, |
401 TabUsageRecorder::EVICTED_DUE_TO_RENDERER_TERMINATION, 1); | 390 TabUsageRecorder::EVICTED_DUE_TO_RENDERER_TERMINATION, 1); |
402 } | 391 } |
403 | 392 |
404 } // namespace | 393 } // namespace |
OLD | NEW |