OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ios/chrome/app/application_delegate/metrics_mediator.h" | 5 #import "ios/chrome/app/application_delegate/metrics_mediator.h" |
6 #import "ios/chrome/app/application_delegate/metrics_mediator_testing.h" | 6 #import "ios/chrome/app/application_delegate/metrics_mediator_testing.h" |
7 | 7 |
8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
9 | 9 |
10 #include "base/mac/scoped_block.h" | 10 #include "base/mac/scoped_block.h" |
11 #include "base/mac/scoped_nsobject.h" | |
12 #import "breakpad/src/client/ios/BreakpadController.h" | 11 #import "breakpad/src/client/ios/BreakpadController.h" |
13 #include "components/metrics/metrics_service.h" | 12 #include "components/metrics/metrics_service.h" |
14 #import "ios/chrome/app/application_delegate/startup_information.h" | 13 #import "ios/chrome/app/application_delegate/startup_information.h" |
15 #include "ios/chrome/browser/application_context.h" | 14 #include "ios/chrome/browser/application_context.h" |
16 #import "ios/chrome/browser/metrics/previous_session_info.h" | 15 #import "ios/chrome/browser/metrics/previous_session_info.h" |
17 #import "ios/chrome/browser/metrics/previous_session_info_private.h" | 16 #import "ios/chrome/browser/metrics/previous_session_info_private.h" |
18 #import "ios/chrome/browser/tabs/tab_model.h" | 17 #import "ios/chrome/browser/tabs/tab_model.h" |
19 #import "ios/chrome/browser/ui/main/browser_view_information.h" | 18 #import "ios/chrome/browser/ui/main/browser_view_information.h" |
20 #import "ios/chrome/test/base/scoped_block_swizzler.h" | 19 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
21 #import "ios/chrome/test/ocmock/OCMockObject+BreakpadControllerTesting.h" | 20 #import "ios/chrome/test/ocmock/OCMockObject+BreakpadControllerTesting.h" |
22 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
23 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
24 #import "third_party/ocmock/OCMock/OCMock.h" | 23 #import "third_party/ocmock/OCMock/OCMock.h" |
25 #include "third_party/ocmock/gtest_support.h" | 24 #include "third_party/ocmock/gtest_support.h" |
26 | 25 |
| 26 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 27 #error "This file requires ARC support." |
| 28 #endif |
| 29 |
27 #pragma mark - connectionTypeChanged tests. | 30 #pragma mark - connectionTypeChanged tests. |
28 | 31 |
29 // Mock class for testing MetricsMediator. | 32 // Mock class for testing MetricsMediator. |
30 @interface MetricsMediatorMock : MetricsMediator | 33 @interface MetricsMediatorMock : MetricsMediator |
31 @property(nonatomic) NSInteger reportingValue; | 34 @property(nonatomic) NSInteger reportingValue; |
32 @property(nonatomic) NSInteger breakpadUpload; | 35 @property(nonatomic) NSInteger breakpadUpload; |
33 - (void)reset; | 36 - (void)reset; |
34 - (void)setReporting:(BOOL)enableReporting; | 37 - (void)setReporting:(BOOL)enableReporting; |
35 @end | 38 @end |
36 | 39 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 int getExpectedValue(int number) { | 89 int getExpectedValue(int number) { |
87 if (number > 2 && number < 6) | 90 if (number > 2 && number < 6) |
88 return 0; | 91 return 0; |
89 return 1; | 92 return 1; |
90 } | 93 } |
91 | 94 |
92 // Verifies that connectionTypeChanged correctly enables or disables the | 95 // Verifies that connectionTypeChanged correctly enables or disables the |
93 // uploading in the breakpad and in the metrics service. | 96 // uploading in the breakpad and in the metrics service. |
94 TEST(MetricsMediatorTest, connectionTypeChanged) { | 97 TEST(MetricsMediatorTest, connectionTypeChanged) { |
95 [[PreviousSessionInfo sharedInstance] setIsFirstSessionAfterUpgrade:NO]; | 98 [[PreviousSessionInfo sharedInstance] setIsFirstSessionAfterUpgrade:NO]; |
96 base::scoped_nsobject<MetricsMediatorMock> mock_metrics_helper( | 99 MetricsMediatorMock* mock_metrics_helper = [[MetricsMediatorMock alloc] init]; |
97 [[MetricsMediatorMock alloc] init]); | |
98 | 100 |
99 // Checks all different scenarios. | 101 // Checks all different scenarios. |
100 for (int i = 0; i < 8; ++i) { | 102 for (int i = 0; i < 8; ++i) { |
101 [mock_metrics_helper reset]; | 103 [mock_metrics_helper reset]; |
102 [mock_metrics_helper connectionTypeChanged:getConnectionType(i)]; | 104 [mock_metrics_helper connectionTypeChanged:getConnectionType(i)]; |
103 EXPECT_EQ(getExpectedValue(i), [mock_metrics_helper reportingValue]); | 105 EXPECT_EQ(getExpectedValue(i), [mock_metrics_helper reportingValue]); |
104 EXPECT_EQ(getExpectedValue(i), [mock_metrics_helper breakpadUpload]); | 106 EXPECT_EQ(getExpectedValue(i), [mock_metrics_helper breakpadUpload]); |
105 } | 107 } |
106 | 108 |
107 // Checks that no new ConnectionType has been added. | 109 // Checks that no new ConnectionType has been added. |
(...skipping 11 matching lines...) Expand all Loading... |
119 protected: | 121 protected: |
120 MetricsMediatorLogLaunchTest() : has_been_called_(FALSE) {} | 122 MetricsMediatorLogLaunchTest() : has_been_called_(FALSE) {} |
121 | 123 |
122 void initiateMetricsMediator(BOOL coldStart, int tabCount) { | 124 void initiateMetricsMediator(BOOL coldStart, int tabCount) { |
123 id mainTabModel = [OCMockObject mockForClass:[TabModel class]]; | 125 id mainTabModel = [OCMockObject mockForClass:[TabModel class]]; |
124 [[[mainTabModel stub] andReturnValue:@(tabCount)] count]; | 126 [[[mainTabModel stub] andReturnValue:@(tabCount)] count]; |
125 browser_view_information_ = | 127 browser_view_information_ = |
126 [OCMockObject mockForProtocol:@protocol(BrowserViewInformation)]; | 128 [OCMockObject mockForProtocol:@protocol(BrowserViewInformation)]; |
127 [[[browser_view_information_ stub] andReturn:mainTabModel] mainTabModel]; | 129 [[[browser_view_information_ stub] andReturn:mainTabModel] mainTabModel]; |
128 | 130 |
129 swizzle_block_.reset([^(id self, int numTab) { | 131 swizzle_block_ = [^(id self, int numTab) { |
130 has_been_called_ = YES; | 132 has_been_called_ = YES; |
131 // Tests. | 133 // Tests. |
132 EXPECT_EQ(tabCount, numTab); | 134 EXPECT_EQ(tabCount, numTab); |
133 } copy]); | 135 } copy]; |
134 if (coldStart) { | 136 if (coldStart) { |
135 uma_histogram_swizzler_.reset(new ScopedBlockSwizzler( | 137 uma_histogram_swizzler_.reset(new ScopedBlockSwizzler( |
136 [MetricsMediator class], @selector(recordNumTabAtStartup:), | 138 [MetricsMediator class], @selector(recordNumTabAtStartup:), |
137 swizzle_block_)); | 139 swizzle_block_)); |
138 } else { | 140 } else { |
139 uma_histogram_swizzler_.reset(new ScopedBlockSwizzler( | 141 uma_histogram_swizzler_.reset(new ScopedBlockSwizzler( |
140 [MetricsMediator class], @selector(recordNumTabAtResume:), | 142 [MetricsMediator class], @selector(recordNumTabAtResume:), |
141 swizzle_block_)); | 143 swizzle_block_)); |
142 } | 144 } |
143 } | 145 } |
144 | 146 |
145 void verifySwizzleHasBeenCalled() { EXPECT_TRUE(has_been_called_); } | 147 void verifySwizzleHasBeenCalled() { EXPECT_TRUE(has_been_called_); } |
146 | 148 |
147 id getBrowserViewInformation() { return browser_view_information_; } | 149 id getBrowserViewInformation() { return browser_view_information_; } |
148 | 150 |
149 private: | 151 private: |
150 id browser_view_information_; | 152 id browser_view_information_; |
151 __block BOOL has_been_called_; | 153 __block BOOL has_been_called_; |
152 base::mac::ScopedBlock<logLaunchMetricsBlock> swizzle_block_; | 154 logLaunchMetricsBlock swizzle_block_; |
153 std::unique_ptr<ScopedBlockSwizzler> uma_histogram_swizzler_; | 155 std::unique_ptr<ScopedBlockSwizzler> uma_histogram_swizzler_; |
154 }; | 156 }; |
155 | 157 |
156 // Verifies that the log of the number of open tabs is sent and verifies | 158 // Verifies that the log of the number of open tabs is sent and verifies |
157 TEST_F(MetricsMediatorLogLaunchTest, | 159 TEST_F(MetricsMediatorLogLaunchTest, |
158 logLaunchMetricsWithEnteredBackgroundDate) { | 160 logLaunchMetricsWithEnteredBackgroundDate) { |
159 // Setup. | 161 // Setup. |
160 BOOL coldStart = YES; | 162 BOOL coldStart = YES; |
161 initiateMetricsMediator(coldStart, 23); | 163 initiateMetricsMediator(coldStart, 23); |
162 | 164 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 240 |
239 class MetricsMediatorShutdownTypeTest : public testing::TestWithParam<int> {}; | 241 class MetricsMediatorShutdownTypeTest : public testing::TestWithParam<int> {}; |
240 | 242 |
241 // Verifies that the Breakpad controller gets called appropriately when | 243 // Verifies that the Breakpad controller gets called appropriately when |
242 // processCrashReportsPresentAtStartup is invoked. | 244 // processCrashReportsPresentAtStartup is invoked. |
243 // | 245 // |
244 // This parameterized test receives an int parameter that is the crash log | 246 // This parameterized test receives an int parameter that is the crash log |
245 // count waiting to be processed. | 247 // count waiting to be processed. |
246 TEST_P(MetricsMediatorShutdownTypeTest, ProcessCrashReportsPresentAtStartup) { | 248 TEST_P(MetricsMediatorShutdownTypeTest, ProcessCrashReportsPresentAtStartup) { |
247 // Create a MainController. | 249 // Create a MainController. |
248 base::scoped_nsobject<MetricsMediator> metric_helper( | 250 MetricsMediator* metric_helper = [[MetricsMediator alloc] init]; |
249 [[MetricsMediator alloc] init]); | |
250 | 251 |
251 // Create a mock for BreakpadController and swizzle | 252 // Create a mock for BreakpadController and swizzle |
252 // +[BreakpadController sharedInstance] to return the mock instead of the | 253 // +[BreakpadController sharedInstance] to return the mock instead of the |
253 // normal singleton instance. | 254 // normal singleton instance. |
254 base::scoped_nsobject<id> mock_breakpad_controller( | 255 id mock_breakpad_controller = |
255 [[OCMockObject mockForClass:[BreakpadController class]] retain]); | 256 [OCMockObject mockForClass:[BreakpadController class]]; |
256 | 257 |
257 id implementation_block = ^BreakpadController*(id self) { | 258 id implementation_block = ^BreakpadController*(id self) { |
258 return mock_breakpad_controller.get(); | 259 return mock_breakpad_controller; |
259 }; | 260 }; |
260 ScopedBlockSwizzler breakpad_controller_shared_instance_swizzler( | 261 ScopedBlockSwizzler breakpad_controller_shared_instance_swizzler( |
261 [BreakpadController class], @selector(sharedInstance), | 262 [BreakpadController class], @selector(sharedInstance), |
262 implementation_block); | 263 implementation_block); |
263 | 264 |
264 // Create the mock expectation for calling | 265 // Create the mock expectation for calling |
265 // -[BreakpadController getCrashReportCount]. | 266 // -[BreakpadController getCrashReportCount]. |
266 [mock_breakpad_controller cr_expectGetCrashReportCount:GetParam()]; | 267 [mock_breakpad_controller cr_expectGetCrashReportCount:GetParam()]; |
267 | 268 |
268 // Now call the method under test and verify that the Breakpad controller got | 269 // Now call the method under test and verify that the Breakpad controller got |
269 // called appropriately. | 270 // called appropriately. |
270 [metric_helper processCrashReportsPresentAtStartup]; | 271 [metric_helper processCrashReportsPresentAtStartup]; |
271 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller.get()); | 272 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller); |
272 } | 273 } |
273 | 274 |
274 INSTANTIATE_TEST_CASE_P(/* No InstantiationName */, | 275 INSTANTIATE_TEST_CASE_P(/* No InstantiationName */, |
275 MetricsMediatorShutdownTypeTest, | 276 MetricsMediatorShutdownTypeTest, |
276 ::testing::Values(0, 1, 42)); | 277 ::testing::Values(0, 1, 42)); |
OLD | NEW |