OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/app/application_delegate/metrics_mediator.h" |
| 6 #import "ios/chrome/app/application_delegate/metrics_mediator_testing.h" |
| 7 |
| 8 #import <Foundation/Foundation.h> |
| 9 |
| 10 #include "base/mac/scoped_block.h" |
| 11 #include "base/mac/scoped_nsobject.h" |
| 12 #import "breakpad/src/client/ios/BreakpadController.h" |
| 13 #include "components/metrics/metrics_service.h" |
| 14 #import "ios/chrome/app/application_delegate/startup_information.h" |
| 15 #include "ios/chrome/browser/application_context.h" |
| 16 #import "ios/chrome/browser/metrics/previous_session_info.h" |
| 17 #import "ios/chrome/browser/metrics/previous_session_info_private.h" |
| 18 #import "ios/chrome/browser/tabs/tab_model.h" |
| 19 #import "ios/chrome/browser/ui/main/browser_view_information.h" |
| 20 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
| 21 #import "ios/chrome/test/ocmock/OCMockObject+BreakpadControllerTesting.h" |
| 22 #include "net/base/network_change_notifier.h" |
| 23 #include "testing/platform_test.h" |
| 24 #import "third_party/ocmock/OCMock/OCMock.h" |
| 25 #include "third_party/ocmock/gtest_support.h" |
| 26 |
| 27 #pragma mark - connectionTypeChanged tests. |
| 28 |
| 29 // Mock class for testing MetricsMediator. |
| 30 @interface MetricsMediatorMock : MetricsMediator |
| 31 @property(nonatomic) NSInteger reportingValue; |
| 32 @property(nonatomic) NSInteger breakpadUpload; |
| 33 - (void)reset; |
| 34 - (void)setReporting:(BOOL)enableReporting; |
| 35 @end |
| 36 |
| 37 @implementation MetricsMediatorMock |
| 38 @synthesize reportingValue = _reportingValue; |
| 39 @synthesize breakpadUpload = _breakpadUpload; |
| 40 |
| 41 - (void)reset { |
| 42 _reportingValue = -1; |
| 43 _breakpadUpload = -1; |
| 44 } |
| 45 - (void)setBreakpadUploadingEnabled:(BOOL)enableUploading { |
| 46 _breakpadUpload = enableUploading ? 1 : 0; |
| 47 } |
| 48 - (void)setReporting:(BOOL)enableReporting { |
| 49 _reportingValue = enableReporting ? 1 : 0; |
| 50 } |
| 51 - (BOOL)isMetricsReportingEnabledWifiOnly { |
| 52 return YES; |
| 53 } |
| 54 - (BOOL)areMetricsEnabled { |
| 55 return YES; |
| 56 } |
| 57 |
| 58 @end |
| 59 |
| 60 // Gives the differents net::NetworkChangeNotifier::ConnectionType based on |
| 61 // scenario number. |
| 62 net::NetworkChangeNotifier::ConnectionType getConnectionType(int number) { |
| 63 switch (number) { |
| 64 case 0: |
| 65 return net::NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 66 case 1: |
| 67 return net::NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 68 case 2: |
| 69 return net::NetworkChangeNotifier::CONNECTION_WIFI; |
| 70 case 3: |
| 71 return net::NetworkChangeNotifier::CONNECTION_2G; |
| 72 case 4: |
| 73 return net::NetworkChangeNotifier::CONNECTION_3G; |
| 74 case 5: |
| 75 return net::NetworkChangeNotifier::CONNECTION_4G; |
| 76 case 6: |
| 77 return net::NetworkChangeNotifier::CONNECTION_NONE; |
| 78 case 7: |
| 79 return net::NetworkChangeNotifier::CONNECTION_BLUETOOTH; |
| 80 default: |
| 81 return net::NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 82 } |
| 83 } |
| 84 |
| 85 // Gives the differents expected value based on scenario number. |
| 86 int getExpectedValue(int number) { |
| 87 if (number > 2 && number < 6) |
| 88 return 0; |
| 89 return 1; |
| 90 } |
| 91 |
| 92 // Verifies that connectionTypeChanged correctly enables or disables the |
| 93 // uploading in the breakpad and in the metrics service. |
| 94 TEST(MetricsMediatorTest, connectionTypeChanged) { |
| 95 [[PreviousSessionInfo sharedInstance] setIsFirstSessionAfterUpgrade:NO]; |
| 96 base::scoped_nsobject<MetricsMediatorMock> mock_metrics_helper( |
| 97 [[MetricsMediatorMock alloc] init]); |
| 98 |
| 99 // Checks all different scenarios. |
| 100 for (int i = 0; i < 8; ++i) { |
| 101 [mock_metrics_helper reset]; |
| 102 [mock_metrics_helper connectionTypeChanged:getConnectionType(i)]; |
| 103 EXPECT_EQ(getExpectedValue(i), [mock_metrics_helper reportingValue]); |
| 104 EXPECT_EQ(getExpectedValue(i), [mock_metrics_helper breakpadUpload]); |
| 105 } |
| 106 |
| 107 // Checks that no new ConnectionType has been added. |
| 108 EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_BLUETOOTH, |
| 109 net::NetworkChangeNotifier::CONNECTION_LAST); |
| 110 } |
| 111 |
| 112 #pragma mark - logLaunchMetrics tests. |
| 113 |
| 114 // A block that takes as arguments the caller and the arguments from |
| 115 // UserActivityHandler +handleStartupParameters and returns nothing. |
| 116 typedef void (^logLaunchMetricsBlock)(id, const char*, int); |
| 117 |
| 118 class MetricsMediatorLogLaunchTest : public PlatformTest { |
| 119 protected: |
| 120 MetricsMediatorLogLaunchTest() : has_been_called_(FALSE) {} |
| 121 |
| 122 void initiateMetricsMediator(BOOL coldStart, int tabCount) { |
| 123 id mainTabModel = [OCMockObject mockForClass:[TabModel class]]; |
| 124 [[[mainTabModel stub] andReturnValue:@(tabCount)] count]; |
| 125 browser_view_information_ = |
| 126 [OCMockObject mockForProtocol:@protocol(BrowserViewInformation)]; |
| 127 [[[browser_view_information_ stub] andReturn:mainTabModel] mainTabModel]; |
| 128 |
| 129 swizzle_block_.reset([^(id self, int numTab) { |
| 130 has_been_called_ = YES; |
| 131 // Tests. |
| 132 EXPECT_EQ(tabCount, numTab); |
| 133 } copy]); |
| 134 if (coldStart) { |
| 135 uma_histogram_swizzler_.reset(new ScopedBlockSwizzler( |
| 136 [MetricsMediator class], @selector(recordNumTabAtStartup:), |
| 137 swizzle_block_)); |
| 138 } else { |
| 139 uma_histogram_swizzler_.reset(new ScopedBlockSwizzler( |
| 140 [MetricsMediator class], @selector(recordNumTabAtResume:), |
| 141 swizzle_block_)); |
| 142 } |
| 143 } |
| 144 |
| 145 void verifySwizzleHasBeenCalled() { EXPECT_TRUE(has_been_called_); } |
| 146 |
| 147 id getBrowserViewInformation() { return browser_view_information_; } |
| 148 |
| 149 private: |
| 150 id browser_view_information_; |
| 151 __block BOOL has_been_called_; |
| 152 base::mac::ScopedBlock<logLaunchMetricsBlock> swizzle_block_; |
| 153 std::unique_ptr<ScopedBlockSwizzler> uma_histogram_swizzler_; |
| 154 }; |
| 155 |
| 156 // Verifies that the log of the number of open tabs is sent and verifies |
| 157 TEST_F(MetricsMediatorLogLaunchTest, |
| 158 logLaunchMetricsWithEnteredBackgroundDate) { |
| 159 // Setup. |
| 160 BOOL coldStart = YES; |
| 161 initiateMetricsMediator(coldStart, 23); |
| 162 |
| 163 const NSTimeInterval kFirstUserActionTimeout = 30.0; |
| 164 |
| 165 id startupInformation = |
| 166 [OCMockObject niceMockForProtocol:@protocol(StartupInformation)]; |
| 167 [[[startupInformation stub] andReturnValue:@(coldStart)] isColdStart]; |
| 168 [[startupInformation expect] |
| 169 expireFirstUserActionRecorderAfterDelay:kFirstUserActionTimeout]; |
| 170 |
| 171 id currentTabModel = [OCMockObject mockForClass:[TabModel class]]; |
| 172 [[[currentTabModel stub] andReturn:nil] currentTab]; |
| 173 |
| 174 id browserViewInformation = getBrowserViewInformation(); |
| 175 [[[browserViewInformation stub] andReturn:currentTabModel] currentTabModel]; |
| 176 |
| 177 [[NSUserDefaults standardUserDefaults] |
| 178 setObject:[NSDate date] |
| 179 forKey:metrics_mediator::kAppEnteredBackgroundDateKey]; |
| 180 |
| 181 // Action. |
| 182 [MetricsMediator |
| 183 logLaunchMetricsWithStartupInformation:startupInformation |
| 184 browserViewInformation:getBrowserViewInformation()]; |
| 185 |
| 186 // Tests. |
| 187 NSDate* dateStored = [[NSUserDefaults standardUserDefaults] |
| 188 objectForKey:metrics_mediator::kAppEnteredBackgroundDateKey]; |
| 189 EXPECT_EQ(nil, dateStored); |
| 190 verifySwizzleHasBeenCalled(); |
| 191 EXPECT_OCMOCK_VERIFY(startupInformation); |
| 192 } |
| 193 |
| 194 // Verifies that +logLaunchMetrics logs of the number of open tabs and nothing |
| 195 // more if the background date is not set; |
| 196 TEST_F(MetricsMediatorLogLaunchTest, logLaunchMetricsNoBackgroundDate) { |
| 197 // Setup. |
| 198 BOOL coldStart = NO; |
| 199 initiateMetricsMediator(coldStart, 32); |
| 200 |
| 201 id startupInformation = |
| 202 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 203 [[[startupInformation stub] andReturnValue:@(coldStart)] isColdStart]; |
| 204 |
| 205 [[NSUserDefaults standardUserDefaults] |
| 206 removeObjectForKey:metrics_mediator::kAppEnteredBackgroundDateKey]; |
| 207 |
| 208 // Action. |
| 209 [MetricsMediator |
| 210 logLaunchMetricsWithStartupInformation:startupInformation |
| 211 browserViewInformation:getBrowserViewInformation()]; |
| 212 |
| 213 // Tests. |
| 214 verifySwizzleHasBeenCalled(); |
| 215 } |
| 216 |
| 217 // Tests that +logDateInUserDefaults logs the date in UserDefaults. |
| 218 TEST(MetricsMediatorNoFixtureTest, logDateInUserDefaultsTest) { |
| 219 // Setup. |
| 220 [[NSUserDefaults standardUserDefaults] |
| 221 removeObjectForKey:metrics_mediator::kAppEnteredBackgroundDateKey]; |
| 222 |
| 223 NSDate* lastAppClose = [[NSUserDefaults standardUserDefaults] |
| 224 objectForKey:metrics_mediator::kAppEnteredBackgroundDateKey]; |
| 225 |
| 226 ASSERT_EQ(nil, lastAppClose); |
| 227 |
| 228 // Action. |
| 229 [MetricsMediator logDateInUserDefaults]; |
| 230 |
| 231 // Setup. |
| 232 lastAppClose = [[NSUserDefaults standardUserDefaults] |
| 233 objectForKey:metrics_mediator::kAppEnteredBackgroundDateKey]; |
| 234 EXPECT_NE(nil, lastAppClose); |
| 235 } |
| 236 |
| 237 #pragma mark - processCrashReportsPresentAtStartup tests. |
| 238 |
| 239 class MetricsMediatorShutdownTypeTest : public testing::TestWithParam<int> {}; |
| 240 |
| 241 // Verifies that the Breakpad controller gets called appropriately when |
| 242 // processCrashReportsPresentAtStartup is invoked. |
| 243 // |
| 244 // This parameterized test receives an int parameter that is the crash log |
| 245 // count waiting to be processed. |
| 246 TEST_P(MetricsMediatorShutdownTypeTest, ProcessCrashReportsPresentAtStartup) { |
| 247 // Create a MainController. |
| 248 base::scoped_nsobject<MetricsMediator> metric_helper( |
| 249 [[MetricsMediator alloc] init]); |
| 250 |
| 251 // Create a mock for BreakpadController and swizzle |
| 252 // +[BreakpadController sharedInstance] to return the mock instead of the |
| 253 // normal singleton instance. |
| 254 base::scoped_nsobject<id> mock_breakpad_controller( |
| 255 [[OCMockObject mockForClass:[BreakpadController class]] retain]); |
| 256 |
| 257 id implementation_block = ^BreakpadController*(id self) { |
| 258 return mock_breakpad_controller.get(); |
| 259 }; |
| 260 ScopedBlockSwizzler breakpad_controller_shared_instance_swizzler( |
| 261 [BreakpadController class], @selector(sharedInstance), |
| 262 implementation_block); |
| 263 |
| 264 // Create the mock expectation for calling |
| 265 // -[BreakpadController getCrashReportCount]. |
| 266 [mock_breakpad_controller cr_expectGetCrashReportCount:GetParam()]; |
| 267 |
| 268 // Now call the method under test and verify that the Breakpad controller got |
| 269 // called appropriately. |
| 270 [metric_helper processCrashReportsPresentAtStartup]; |
| 271 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller.get()); |
| 272 } |
| 273 |
| 274 INSTANTIATE_TEST_CASE_P(/* No InstantiationName */, |
| 275 MetricsMediatorShutdownTypeTest, |
| 276 ::testing::Values(0, 1, 42)); |
OLD | NEW |