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/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
8 #include "base/metrics/histogram_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
9 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
10 #include "base/test/histogram_tester.h" | 11 #include "base/test/histogram_tester.h" |
11 #import "ios/chrome/browser/metrics/previous_session_info.h" | 12 #import "ios/chrome/browser/metrics/previous_session_info.h" |
12 #include "ios/chrome/browser/metrics/tab_usage_recorder.h" | 13 #include "ios/chrome/browser/metrics/tab_usage_recorder.h" |
13 #import "ios/chrome/browser/metrics/tab_usage_recorder_delegate.h" | 14 #import "ios/chrome/browser/metrics/tab_usage_recorder_delegate.h" |
14 #import "ios/chrome/browser/tabs/tab.h" | 15 #import "ios/chrome/browser/tabs/tab.h" |
15 #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" |
| 18 #import "ios/web/public/test/fakes/test_web_state.h" |
16 #include "ios/web/public/test/test_web_thread.h" | 19 #include "ios/web/public/test/test_web_thread.h" |
17 #import "ios/web/web_state/ui/crw_web_controller.h" | 20 #import "ios/web/web_state/ui/crw_web_controller.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
20 #include "third_party/ocmock/ocmock_extensions.h" | 23 #include "third_party/ocmock/ocmock_extensions.h" |
21 | 24 |
22 #if !defined(__has_feature) || !__has_feature(objc_arc) | 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
23 #error "This file requires ARC support." | 26 #error "This file requires ARC support." |
24 #endif | 27 #endif |
25 | 28 |
26 @interface TURTestTabMock : OCMockComplexTypeHelper { | 29 @interface TURTestTabMock : OCMockComplexTypeHelper { |
27 GURL _url; | 30 GURL _url; |
| 31 GURL _lastCommittedURL; |
| 32 GURL _visibleURL; |
| 33 web::TestWebState _webState; |
28 } | 34 } |
29 | 35 |
30 @property(nonatomic, assign) const GURL& url; | 36 @property(nonatomic, assign) const GURL& url; |
| 37 @property(nonatomic, assign) const GURL& lastCommittedURL; |
| 38 @property(nonatomic, assign) const GURL& visibleURL; |
| 39 @property(nonatomic, readonly) web::WebState* webState; |
| 40 |
31 @end | 41 @end |
32 | 42 |
33 @implementation TURTestTabMock | 43 @implementation TURTestTabMock |
34 - (const GURL&)url { | 44 - (const GURL&)url { |
35 return _url; | 45 return _url; |
36 } | 46 } |
37 - (void)setUrl:(const GURL&)url { | 47 - (void)setUrl:(const GURL&)url { |
38 _url = url; | 48 _url = url; |
39 } | 49 } |
| 50 - (const GURL&)lastCommittedURL { |
| 51 return _lastCommittedURL; |
| 52 } |
| 53 - (void)setLastCommittedURL:(const GURL&)lastCommittedURL { |
| 54 _lastCommittedURL = lastCommittedURL; |
| 55 } |
| 56 - (const GURL&)visibleURL { |
| 57 return _visibleURL; |
| 58 } |
| 59 - (void)setVisibleURL:(const GURL&)visibleURL { |
| 60 _visibleURL = visibleURL; |
| 61 } |
| 62 - (web::WebState*)webState { |
| 63 if (!_webState.GetNavigationManager()) { |
| 64 _webState.SetNavigationManager( |
| 65 base::MakeUnique<web::TestNavigationManager>()); |
| 66 } |
| 67 return &_webState; |
| 68 } |
40 @end | 69 @end |
41 | 70 |
42 // A mock TabUsageRecorderDelegate which allows the unit tests to control | 71 // A mock TabUsageRecorderDelegate which allows the unit tests to control |
43 // the count of live tabs returned from the |liveTabsCount| delegate method. | 72 // the count of live tabs returned from the |liveTabsCount| delegate method. |
44 @interface MockTabUsageRecorderDelegate : NSObject<TabUsageRecorderDelegate> { | 73 @interface MockTabUsageRecorderDelegate : NSObject<TabUsageRecorderDelegate> { |
45 NSUInteger _tabCount; | 74 NSUInteger _tabCount; |
46 } | 75 } |
47 | 76 |
48 // Sets the live tab count returned from the |liveTabsCount| delegate method. | 77 // Sets the live tab count returned from the |liveTabsCount| delegate method. |
49 - (void)setLiveTabsCount:(NSUInteger)count; | 78 - (void)setLiveTabsCount:(NSUInteger)count; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 128 } |
100 | 129 |
101 id MockTab(bool inMemory) { | 130 id MockTab(bool inMemory) { |
102 id tab_mock = [[TURTestTabMock alloc] | 131 id tab_mock = [[TURTestTabMock alloc] |
103 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; | 132 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
104 id web_controller_mock = | 133 id web_controller_mock = |
105 [OCMockObject mockForClass:[CRWWebController class]]; | 134 [OCMockObject mockForClass:[CRWWebController class]]; |
106 [[[tab_mock stub] andReturn:web_controller_mock] webController]; | 135 [[[tab_mock stub] andReturn:web_controller_mock] webController]; |
107 [[[tab_mock stub] andReturnBool:false] isPrerenderTab]; | 136 [[[tab_mock stub] andReturnBool:false] isPrerenderTab]; |
108 [tab_mock setUrl:webUrl_]; | 137 [tab_mock setUrl:webUrl_]; |
| 138 [tab_mock setLastCommittedURL:webUrl_]; |
| 139 [tab_mock setVisibleURL:webUrl_]; |
109 [[[web_controller_mock stub] andReturnBool:inMemory] isViewAlive]; | 140 [[[web_controller_mock stub] andReturnBool:inMemory] isViewAlive]; |
110 [[web_controller_mock stub] removeObserver:OCMOCK_ANY]; | 141 [[web_controller_mock stub] removeObserver:OCMOCK_ANY]; |
111 return tab_mock; | 142 return tab_mock; |
112 } | 143 } |
113 | 144 |
114 GURL webUrl_; | 145 GURL webUrl_; |
115 GURL nativeUrl_; | 146 GURL nativeUrl_; |
116 std::unique_ptr<base::MessageLoop> loop_; | 147 std::unique_ptr<base::MessageLoop> loop_; |
117 std::unique_ptr<web::TestWebThread> ui_thread_; | 148 std::unique_ptr<web::TestWebThread> ui_thread_; |
118 std::unique_ptr<base::HistogramTester> histogram_tester_; | 149 std::unique_ptr<base::HistogramTester> histogram_tester_; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 } | 197 } |
167 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); | 198 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); |
168 histogram_tester_->ExpectUniqueSample(kPageLoadsBeforeEvictedTabSelected, | 199 histogram_tester_->ExpectUniqueSample(kPageLoadsBeforeEvictedTabSelected, |
169 kNumReloads, 1); | 200 kNumReloads, 1); |
170 } | 201 } |
171 | 202 |
172 TEST_F(TabUsageRecorderTest, CountNativePageLoadsBeforeEvictedTab) { | 203 TEST_F(TabUsageRecorderTest, CountNativePageLoadsBeforeEvictedTab) { |
173 id tab_mock_a = MockTab(true); | 204 id tab_mock_a = MockTab(true); |
174 id tab_mock_b = MockTab(false); | 205 id tab_mock_b = MockTab(false); |
175 [tab_mock_a setUrl:nativeUrl_]; | 206 [tab_mock_a setUrl:nativeUrl_]; |
| 207 [tab_mock_a setLastCommittedURL:nativeUrl_]; |
| 208 [tab_mock_a setVisibleURL:nativeUrl_]; |
176 [tab_mock_b setUrl:nativeUrl_]; | 209 [tab_mock_b setUrl:nativeUrl_]; |
| 210 [tab_mock_b setLastCommittedURL:nativeUrl_]; |
| 211 [tab_mock_b setVisibleURL:nativeUrl_]; |
177 | 212 |
178 // Call reload an arbitrary number of times. | 213 // Call reload an arbitrary number of times. |
179 const int kNumReloads = 4; | 214 const int kNumReloads = 4; |
180 for (int i = 0; i < kNumReloads; i++) { | 215 for (int i = 0; i < kNumReloads; i++) { |
181 tab_usage_recorder_->RecordPageLoadStart(tab_mock_a); | 216 tab_usage_recorder_->RecordPageLoadStart(tab_mock_a); |
182 } | 217 } |
183 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); | 218 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); |
184 histogram_tester_->ExpectTotalCount(kPageLoadsBeforeEvictedTabSelected, 0); | 219 histogram_tester_->ExpectTotalCount(kPageLoadsBeforeEvictedTabSelected, 0); |
185 } | 220 } |
186 | 221 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 395 |
361 tab_usage_recorder_->RendererTerminated(tab_mock_b, false); | 396 tab_usage_recorder_->RendererTerminated(tab_mock_b, false); |
362 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); | 397 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); |
363 | 398 |
364 histogram_tester_->ExpectUniqueSample( | 399 histogram_tester_->ExpectUniqueSample( |
365 kSelectedTabHistogramName, | 400 kSelectedTabHistogramName, |
366 TabUsageRecorder::EVICTED_DUE_TO_RENDERER_TERMINATION, 1); | 401 TabUsageRecorder::EVICTED_DUE_TO_RENDERER_TERMINATION, 1); |
367 } | 402 } |
368 | 403 |
369 } // namespace | 404 } // namespace |
OLD | NEW |