| 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/mac/scoped_nsobject.h" | |
| 8 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 9 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
| 10 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 11 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
| 12 #import "ios/chrome/browser/metrics/previous_session_info.h" | 11 #import "ios/chrome/browser/metrics/previous_session_info.h" |
| 13 #include "ios/chrome/browser/metrics/tab_usage_recorder.h" | 12 #include "ios/chrome/browser/metrics/tab_usage_recorder.h" |
| 14 #import "ios/chrome/browser/metrics/tab_usage_recorder_delegate.h" | 13 #import "ios/chrome/browser/metrics/tab_usage_recorder_delegate.h" |
| 15 #import "ios/chrome/browser/tabs/tab.h" | 14 #import "ios/chrome/browser/tabs/tab.h" |
| 16 #include "ios/testing/ocmock_complex_type_helper.h" | 15 #include "ios/testing/ocmock_complex_type_helper.h" |
| 17 #include "ios/web/public/test/test_web_thread.h" | 16 #include "ios/web/public/test/test_web_thread.h" |
| 18 #import "ios/web/web_state/ui/crw_web_controller.h" | 17 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/platform_test.h" | 19 #include "testing/platform_test.h" |
| 21 #include "third_party/ocmock/ocmock_extensions.h" | 20 #include "third_party/ocmock/ocmock_extensions.h" |
| 22 | 21 |
| 22 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 23 #error "This file requires ARC support." |
| 24 #endif |
| 25 |
| 23 @interface TURTestTabMock : OCMockComplexTypeHelper { | 26 @interface TURTestTabMock : OCMockComplexTypeHelper { |
| 24 GURL _url; | 27 GURL _url; |
| 25 } | 28 } |
| 26 | 29 |
| 27 @property(nonatomic, assign) const GURL& url; | 30 @property(nonatomic, assign) const GURL& url; |
| 28 @end | 31 @end |
| 29 | 32 |
| 30 @implementation TURTestTabMock | 33 @implementation TURTestTabMock |
| 31 - (const GURL&)url { | 34 - (const GURL&)url { |
| 32 return _url; | 35 return _url; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 id MockTab(bool inMemory) { | 101 id MockTab(bool inMemory) { |
| 99 id tab_mock = [[TURTestTabMock alloc] | 102 id tab_mock = [[TURTestTabMock alloc] |
| 100 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; | 103 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
| 101 id web_controller_mock = | 104 id web_controller_mock = |
| 102 [OCMockObject mockForClass:[CRWWebController class]]; | 105 [OCMockObject mockForClass:[CRWWebController class]]; |
| 103 [[[tab_mock stub] andReturn:web_controller_mock] webController]; | 106 [[[tab_mock stub] andReturn:web_controller_mock] webController]; |
| 104 [[[tab_mock stub] andReturnBool:false] isPrerenderTab]; | 107 [[[tab_mock stub] andReturnBool:false] isPrerenderTab]; |
| 105 [tab_mock setUrl:webUrl_]; | 108 [tab_mock setUrl:webUrl_]; |
| 106 [[[web_controller_mock stub] andReturnBool:inMemory] isViewAlive]; | 109 [[[web_controller_mock stub] andReturnBool:inMemory] isViewAlive]; |
| 107 [[web_controller_mock stub] removeObserver:OCMOCK_ANY]; | 110 [[web_controller_mock stub] removeObserver:OCMOCK_ANY]; |
| 108 return [tab_mock autorelease]; | 111 return tab_mock; |
| 109 } | 112 } |
| 110 | 113 |
| 111 GURL webUrl_; | 114 GURL webUrl_; |
| 112 GURL nativeUrl_; | 115 GURL nativeUrl_; |
| 113 std::unique_ptr<base::MessageLoop> loop_; | 116 std::unique_ptr<base::MessageLoop> loop_; |
| 114 std::unique_ptr<web::TestWebThread> ui_thread_; | 117 std::unique_ptr<web::TestWebThread> ui_thread_; |
| 115 std::unique_ptr<base::HistogramTester> histogram_tester_; | 118 std::unique_ptr<base::HistogramTester> histogram_tester_; |
| 116 std::unique_ptr<TabUsageRecorderForTesting> tab_usage_recorder_; | 119 std::unique_ptr<TabUsageRecorderForTesting> tab_usage_recorder_; |
| 117 }; | 120 }; |
| 118 | 121 |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // Should record nothing. | 308 // Should record nothing. |
| 306 tab_usage_recorder_->RecordPageLoadStart(tab_mock_b); | 309 tab_usage_recorder_->RecordPageLoadStart(tab_mock_b); |
| 307 histogram_tester_->ExpectTotalCount(kTimeAfterLastRestore, 1); | 310 histogram_tester_->ExpectTotalCount(kTimeAfterLastRestore, 1); |
| 308 } | 311 } |
| 309 | 312 |
| 310 // Verifies that metrics are recorded correctly when a renderer terminates. | 313 // Verifies that metrics are recorded correctly when a renderer terminates. |
| 311 TEST_F(TabUsageRecorderTest, RendererTerminated) { | 314 TEST_F(TabUsageRecorderTest, RendererTerminated) { |
| 312 Tab* terminated_tab = MockTab(false); | 315 Tab* terminated_tab = MockTab(false); |
| 313 | 316 |
| 314 // Set up the delegate to return |kAliveTabsCountAtRenderTermination|. | 317 // Set up the delegate to return |kAliveTabsCountAtRenderTermination|. |
| 315 base::scoped_nsobject<MockTabUsageRecorderDelegate> delegate( | 318 MockTabUsageRecorderDelegate* delegate = |
| 316 [[MockTabUsageRecorderDelegate alloc] init]); | 319 [[MockTabUsageRecorderDelegate alloc] init]; |
| 317 [delegate setLiveTabsCount:kAliveTabsCountAtRendererTermination]; | 320 [delegate setLiveTabsCount:kAliveTabsCountAtRendererTermination]; |
| 318 tab_usage_recorder_->SetDelegate(delegate); | 321 tab_usage_recorder_->SetDelegate(delegate); |
| 319 | 322 |
| 320 base::TimeTicks now = base::TimeTicks::Now(); | 323 base::TimeTicks now = base::TimeTicks::Now(); |
| 321 | 324 |
| 322 // Add |kExpiredTimesAddedCount| expired timestamps and one recent timestamp | 325 // Add |kExpiredTimesAddedCount| expired timestamps and one recent timestamp |
| 323 // to the termination timestamp list. | 326 // to the termination timestamp list. |
| 324 for (int seconds = kExpiredTimesAddedCount; seconds > 0; seconds--) { | 327 for (int seconds = kExpiredTimesAddedCount; seconds > 0; seconds--) { |
| 325 int expired_time_delta = kSecondsBeforeRendererTermination + seconds; | 328 int expired_time_delta = kSecondsBeforeRendererTermination + seconds; |
| 326 tab_usage_recorder_->AddTimeToDeque( | 329 tab_usage_recorder_->AddTimeToDeque( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 357 | 360 |
| 358 tab_usage_recorder_->RendererTerminated(tab_mock_b, false); | 361 tab_usage_recorder_->RendererTerminated(tab_mock_b, false); |
| 359 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); | 362 tab_usage_recorder_->RecordTabSwitched(tab_mock_a, tab_mock_b); |
| 360 | 363 |
| 361 histogram_tester_->ExpectUniqueSample( | 364 histogram_tester_->ExpectUniqueSample( |
| 362 kSelectedTabHistogramName, | 365 kSelectedTabHistogramName, |
| 363 TabUsageRecorder::EVICTED_DUE_TO_RENDERER_TERMINATION, 1); | 366 TabUsageRecorder::EVICTED_DUE_TO_RENDERER_TERMINATION, 1); |
| 364 } | 367 } |
| 365 | 368 |
| 366 } // namespace | 369 } // namespace |
| OLD | NEW |