OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/mac/scoped_nsobject.h" |
| 6 #import "breakpad/src/client/ios/BreakpadController.h" |
| 7 #import "ios/chrome/browser/crash_report/breakpad_helper.h" |
| 8 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
| 9 #import "ios/chrome/test/ocmock/OCMockObject+BreakpadControllerTesting.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/platform_test.h" |
| 12 #import "third_party/ocmock/OCMock/OCMock.h" |
| 13 #include "third_party/ocmock/gtest_support.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const int kCrashReportCount = 3; |
| 18 NSString* const kUploadedInRecoveryMode = @"uploaded_in_recovery_mode"; |
| 19 |
| 20 class BreakpadHelperTest : public PlatformTest { |
| 21 public: |
| 22 void SetUp() override { |
| 23 PlatformTest::SetUp(); |
| 24 |
| 25 mock_breakpad_controller_.reset( |
| 26 [[OCMockObject mockForClass:[BreakpadController class]] retain]); |
| 27 |
| 28 // Swizzle +[BreakpadController sharedInstance] to return |
| 29 // |mock_breakpad_controller_| instead of the normal singleton instance. |
| 30 id implementation_block = ^BreakpadController*(id self) { |
| 31 return mock_breakpad_controller_.get(); |
| 32 }; |
| 33 breakpad_controller_shared_instance_swizzler_.reset(new ScopedBlockSwizzler( |
| 34 [BreakpadController class], @selector(sharedInstance), |
| 35 implementation_block)); |
| 36 } |
| 37 |
| 38 void TearDown() override { |
| 39 [[mock_breakpad_controller_ stub] stop]; |
| 40 breakpad_helper::SetEnabled(false); |
| 41 |
| 42 PlatformTest::TearDown(); |
| 43 } |
| 44 |
| 45 protected: |
| 46 base::scoped_nsobject<id> mock_breakpad_controller_; |
| 47 std::unique_ptr<ScopedBlockSwizzler> |
| 48 breakpad_controller_shared_instance_swizzler_; |
| 49 }; |
| 50 |
| 51 TEST_F(BreakpadHelperTest, BrowserState) { |
| 52 // Check that we do not overflow the size of a breakpad record. |
| 53 breakpad_helper::SetCurrentOrientation(3, 7); |
| 54 } |
| 55 |
| 56 TEST_F(BreakpadHelperTest, HorizontalSizeClass) { |
| 57 // Check that we do not overflow the size of a breakpad record. |
| 58 breakpad_helper::SetCurrentHorizontalSizeClass(2); |
| 59 } |
| 60 |
| 61 TEST_F(BreakpadHelperTest, GetCrashReportCount) { |
| 62 [mock_breakpad_controller_ cr_expectGetCrashReportCount:kCrashReportCount]; |
| 63 |
| 64 // Verify that breakpad_helper::GetCrashReportCount() returns the |
| 65 // crash report count that we arranged to pass to the result block that was |
| 66 // passed to -[BreakpadController getCrashReportCount:]. |
| 67 EXPECT_EQ(kCrashReportCount, breakpad_helper::GetCrashReportCount()); |
| 68 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 69 } |
| 70 |
| 71 TEST_F(BreakpadHelperTest, HasReportToUpload) { |
| 72 [mock_breakpad_controller_ cr_expectGetCrashReportCount:kCrashReportCount]; |
| 73 EXPECT_TRUE(breakpad_helper::HasReportToUpload()); |
| 74 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 75 |
| 76 [mock_breakpad_controller_ cr_expectGetCrashReportCount:0]; |
| 77 EXPECT_FALSE(breakpad_helper::HasReportToUpload()); |
| 78 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 79 } |
| 80 |
| 81 TEST_F(BreakpadHelperTest, IsUploadingEnabled) { |
| 82 // Test when crash reporter is disabled. |
| 83 breakpad_helper::SetEnabled(false); |
| 84 EXPECT_FALSE(breakpad_helper::IsUploadingEnabled()); |
| 85 |
| 86 breakpad_helper::SetUploadingEnabled(false); |
| 87 EXPECT_FALSE(breakpad_helper::IsUploadingEnabled()); |
| 88 |
| 89 breakpad_helper::SetUploadingEnabled(true); |
| 90 EXPECT_FALSE(breakpad_helper::IsUploadingEnabled()); |
| 91 |
| 92 // Test when crash reporter is enabled. |
| 93 [[mock_breakpad_controller_ expect] start:NO]; |
| 94 breakpad_helper::SetEnabled(true); |
| 95 EXPECT_FALSE(breakpad_helper::IsUploadingEnabled()); |
| 96 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 97 |
| 98 [[mock_breakpad_controller_ expect] setUploadingEnabled:NO]; |
| 99 breakpad_helper::SetUploadingEnabled(false); |
| 100 EXPECT_FALSE(breakpad_helper::IsUploadingEnabled()); |
| 101 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 102 |
| 103 [[mock_breakpad_controller_ expect] setUploadingEnabled:YES]; |
| 104 breakpad_helper::SetUploadingEnabled(true); |
| 105 EXPECT_TRUE(breakpad_helper::IsUploadingEnabled()); |
| 106 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 107 } |
| 108 |
| 109 TEST_F(BreakpadHelperTest, StartUploadingReportsInRecoveryMode) { |
| 110 // Test when crash reporter is disabled. |
| 111 breakpad_helper::SetEnabled(false); |
| 112 breakpad_helper::StartUploadingReportsInRecoveryMode(); |
| 113 |
| 114 // Test when crash reporter is enabled. |
| 115 [[mock_breakpad_controller_ expect] start:NO]; |
| 116 breakpad_helper::SetEnabled(true); |
| 117 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 118 |
| 119 [[mock_breakpad_controller_ expect] stop]; |
| 120 [[mock_breakpad_controller_ expect] |
| 121 setParametersToAddAtUploadTime:[OCMArg checkWithBlock:^(id value) { |
| 122 return [value isKindOfClass:[NSDictionary class]] && |
| 123 [value[kUploadedInRecoveryMode] isEqualToString:@"yes"] |
| 124 ? YES |
| 125 : NO; |
| 126 }]]; |
| 127 [[mock_breakpad_controller_ expect] setUploadInterval:1]; |
| 128 [[mock_breakpad_controller_ expect] start:NO]; |
| 129 [[mock_breakpad_controller_ expect] setUploadingEnabled:YES]; |
| 130 breakpad_helper::StartUploadingReportsInRecoveryMode(); |
| 131 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 132 } |
| 133 |
| 134 TEST_F(BreakpadHelperTest, RestoreDefaultConfiguration) { |
| 135 // Test when crash reporter is disabled. |
| 136 breakpad_helper::SetEnabled(false); |
| 137 breakpad_helper::RestoreDefaultConfiguration(); |
| 138 |
| 139 // Test when crash reporter is enabled. |
| 140 [[mock_breakpad_controller_ expect] start:NO]; |
| 141 breakpad_helper::SetEnabled(true); |
| 142 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 143 |
| 144 [[mock_breakpad_controller_ expect] stop]; |
| 145 [[mock_breakpad_controller_ expect] resetConfiguration]; |
| 146 [[mock_breakpad_controller_ expect] start:NO]; |
| 147 [[mock_breakpad_controller_ expect] setUploadingEnabled:NO]; |
| 148 breakpad_helper::RestoreDefaultConfiguration(); |
| 149 EXPECT_OCMOCK_VERIFY(mock_breakpad_controller_.get()); |
| 150 } |
| 151 |
| 152 } // namespace |
OLD | NEW |