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