OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include <utility> |
| 12 |
| 13 #include "webrtc/api/rtcerror.h" |
| 14 #include "webrtc/test/gtest.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 const int kDefaultMoveOnlyIntValue = 0xbadf00d; |
| 19 |
| 20 // Class that has no copy constructor, ensuring that RTCErrorOr can |
| 21 struct MoveOnlyInt { |
| 22 MoveOnlyInt() {} |
| 23 explicit MoveOnlyInt(int value) : value(value) {} |
| 24 MoveOnlyInt(const MoveOnlyInt& other) = delete; |
| 25 MoveOnlyInt(MoveOnlyInt&& other) = default; |
| 26 MoveOnlyInt& operator=(MoveOnlyInt&& other) = default; |
| 27 |
| 28 int value = kDefaultMoveOnlyIntValue; |
| 29 }; |
| 30 |
| 31 // Same as above. Used to test conversion from RTCErrorOr<A> to RTCErrorOr<B> |
| 32 // when A can be converted to B. |
| 33 struct MoveOnlyInt2 { |
| 34 MoveOnlyInt2() {} |
| 35 explicit MoveOnlyInt2(int value) : value(value) {} |
| 36 MoveOnlyInt2(const MoveOnlyInt2& other) = delete; |
| 37 MoveOnlyInt2(MoveOnlyInt2&& other) = default; |
| 38 MoveOnlyInt2& operator=(MoveOnlyInt2&& other) = default; |
| 39 |
| 40 explicit MoveOnlyInt2(MoveOnlyInt&& other) : value(other.value) {} |
| 41 MoveOnlyInt2& operator=(MoveOnlyInt&& other) { |
| 42 value = other.value; |
| 43 return *this; |
| 44 } |
| 45 |
| 46 int value = kDefaultMoveOnlyIntValue; |
| 47 }; |
| 48 |
| 49 } // namespace |
| 50 |
| 51 namespace webrtc { |
| 52 |
| 53 // Simple test for ostream operator for RTCErrorType. |
| 54 TEST(RTCErrorTypeTest, OstreamOperator) { |
| 55 std::ostringstream oss; |
| 56 oss << webrtc::RTCErrorType::NONE << ' ' |
| 57 << webrtc::RTCErrorType::INVALID_PARAMETER << ' ' |
| 58 << webrtc::RTCErrorType::INTERNAL_ERROR; |
| 59 EXPECT_EQ("NONE INVALID_PARAMETER INTERNAL_ERROR", oss.str()); |
| 60 } |
| 61 |
| 62 // Test that the default constructor creates a "no error" error. |
| 63 TEST(RTCErrorTest, DefaultConstructor) { |
| 64 RTCError e; |
| 65 EXPECT_EQ(RTCErrorType::NONE, e.type()); |
| 66 EXPECT_EQ(std::string(), e.message()); |
| 67 EXPECT_TRUE(e.ok()); |
| 68 } |
| 69 |
| 70 TEST(RTCErrorTest, NormalConstructors) { |
| 71 RTCError a(RTCErrorType::INVALID_PARAMETER); |
| 72 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, a.type()); |
| 73 EXPECT_EQ(std::string(), a.message()); |
| 74 |
| 75 // Constructor that takes const char* message. |
| 76 RTCError b(RTCErrorType::UNSUPPORTED_PARAMETER, "foobar"); |
| 77 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, b.type()); |
| 78 EXPECT_EQ(std::string("foobar"), b.message()); |
| 79 |
| 80 // Constructor that takes std::string message. |
| 81 RTCError c(RTCErrorType::INVALID_RANGE, std::string("new")); |
| 82 EXPECT_EQ(RTCErrorType::INVALID_RANGE, c.type()); |
| 83 EXPECT_EQ(std::string("new"), c.message()); |
| 84 } |
| 85 |
| 86 TEST(RTCErrorTest, MoveConstructor) { |
| 87 // Static string. |
| 88 RTCError a(RTCErrorType::INVALID_PARAMETER, "foo"); |
| 89 RTCError b(std::move(a)); |
| 90 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, b.type()); |
| 91 EXPECT_EQ(std::string("foo"), b.message()); |
| 92 |
| 93 // Non-static string. |
| 94 RTCError c(RTCErrorType::UNSUPPORTED_PARAMETER, std::string("bar")); |
| 95 RTCError d(std::move(c)); |
| 96 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, d.type()); |
| 97 EXPECT_EQ(std::string("bar"), d.message()); |
| 98 } |
| 99 |
| 100 TEST(RTCErrorTest, MoveAssignment) { |
| 101 // Try all combinations of "is static string"/"is non-static string" moves. |
| 102 RTCError e(RTCErrorType::INVALID_PARAMETER, "foo"); |
| 103 |
| 104 e = RTCError(RTCErrorType::UNSUPPORTED_PARAMETER, "bar"); |
| 105 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, e.type()); |
| 106 EXPECT_EQ(std::string("bar"), e.message()); |
| 107 |
| 108 e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("baz")); |
| 109 EXPECT_EQ(std::string("baz"), e.message()); |
| 110 |
| 111 e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("another")); |
| 112 EXPECT_EQ(std::string("another"), e.message()); |
| 113 |
| 114 e = RTCError(RTCErrorType::SYNTAX_ERROR, "last"); |
| 115 EXPECT_EQ(std::string("last"), e.message()); |
| 116 } |
| 117 |
| 118 // Test that the error returned by RTCError::OK() is a "no error" error. |
| 119 TEST(RTCErrorTest, OKConstant) { |
| 120 RTCError ok = RTCError::OK(); |
| 121 EXPECT_EQ(RTCErrorType::NONE, ok.type()); |
| 122 EXPECT_EQ(std::string(), ok.message()); |
| 123 EXPECT_TRUE(ok.ok()); |
| 124 } |
| 125 |
| 126 // Test that "error.ok()" behaves as expected. |
| 127 TEST(RTCErrorTest, OkMethod) { |
| 128 RTCError success; |
| 129 RTCError failure(RTCErrorType::INTERNAL_ERROR); |
| 130 EXPECT_TRUE(success.ok()); |
| 131 EXPECT_FALSE(failure.ok()); |
| 132 } |
| 133 |
| 134 // Test that a message can be set using either static const strings or |
| 135 // std::strings. |
| 136 TEST(RTCErrorTest, SetMessage) { |
| 137 RTCError e; |
| 138 // Try all combinations of "is static string"/"is non-static string" calls. |
| 139 e.set_message("foo"); |
| 140 EXPECT_EQ(std::string("foo"), e.message()); |
| 141 |
| 142 e.set_message("bar"); |
| 143 EXPECT_EQ(std::string("bar"), e.message()); |
| 144 |
| 145 e.set_message(std::string("string")); |
| 146 EXPECT_EQ(std::string("string"), e.message()); |
| 147 |
| 148 e.set_message(std::string("more")); |
| 149 EXPECT_EQ(std::string("more"), e.message()); |
| 150 |
| 151 e.set_message("love to test"); |
| 152 EXPECT_EQ(std::string("love to test"), e.message()); |
| 153 } |
| 154 |
| 155 // Test that the default constructor creates an "INTERNAL_ERROR". |
| 156 TEST(RTCErrorOrTest, DefaultConstructor) { |
| 157 RTCErrorOr<MoveOnlyInt> e; |
| 158 EXPECT_EQ(RTCErrorType::INTERNAL_ERROR, e.error().type()); |
| 159 } |
| 160 |
| 161 // Test that an RTCErrorOr can be implicitly constructed from a value. |
| 162 TEST(RTCErrorOrTest, ImplicitValueConstructor) { |
| 163 RTCErrorOr<MoveOnlyInt> e = [] { return MoveOnlyInt(100); }(); |
| 164 EXPECT_EQ(100, e.value().value); |
| 165 } |
| 166 |
| 167 // Test that an RTCErrorOr can be implicitly constructed from an RTCError. |
| 168 TEST(RTCErrorOrTest, ImplicitErrorConstructor) { |
| 169 RTCErrorOr<MoveOnlyInt> e = [] { |
| 170 return RTCError(RTCErrorType::SYNTAX_ERROR); |
| 171 }(); |
| 172 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, e.error().type()); |
| 173 } |
| 174 |
| 175 TEST(RTCErrorOrTest, MoveConstructor) { |
| 176 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5)); |
| 177 RTCErrorOr<MoveOnlyInt> b(std::move(a)); |
| 178 EXPECT_EQ(5, b.value().value); |
| 179 } |
| 180 |
| 181 TEST(RTCErrorOrTest, MoveAssignment) { |
| 182 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5)); |
| 183 RTCErrorOr<MoveOnlyInt> b(MoveOnlyInt(10)); |
| 184 a = std::move(b); |
| 185 EXPECT_EQ(10, a.value().value); |
| 186 } |
| 187 |
| 188 TEST(RTCErrorOrTest, ConversionConstructor) { |
| 189 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(1)); |
| 190 RTCErrorOr<MoveOnlyInt2> b(std::move(a)); |
| 191 } |
| 192 |
| 193 TEST(RTCErrorOrTest, ConversionAssignment) { |
| 194 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5)); |
| 195 RTCErrorOr<MoveOnlyInt2> b(MoveOnlyInt2(10)); |
| 196 b = std::move(a); |
| 197 EXPECT_EQ(5, b.value().value); |
| 198 } |
| 199 |
| 200 TEST(RTCErrorOrTest, OkMethod) { |
| 201 RTCErrorOr<int> success(1337); |
| 202 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR); |
| 203 EXPECT_TRUE(success.ok()); |
| 204 EXPECT_FALSE(error.ok()); |
| 205 } |
| 206 |
| 207 TEST(RTCErrorOrTest, MoveError) { |
| 208 RTCErrorOr<int> e({RTCErrorType::SYNTAX_ERROR, "message"}); |
| 209 RTCError err = e.MoveError(); |
| 210 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, err.type()); |
| 211 EXPECT_EQ(std::string("message"), err.message()); |
| 212 } |
| 213 |
| 214 TEST(RTCErrorOrTest, MoveValue) { |
| 215 RTCErrorOr<MoveOnlyInt> e(MoveOnlyInt(88)); |
| 216 MoveOnlyInt value = e.MoveValue(); |
| 217 EXPECT_EQ(88, value.value); |
| 218 } |
| 219 |
| 220 // Death tests. |
| 221 // Disabled on Android because death tests misbehave on Android, see |
| 222 // base/test/gtest_util.h. |
| 223 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 224 |
| 225 TEST(RTCErrorOrDeathTest, ConstructWithOkError) { |
| 226 EXPECT_DEATH(RTCErrorOr<int> err = RTCError::OK(), ""); |
| 227 } |
| 228 |
| 229 TEST(RTCErrorOrDeathTest, DereferenceErrorValue) { |
| 230 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR); |
| 231 EXPECT_DEATH(error.value(), ""); |
| 232 } |
| 233 |
| 234 TEST(RTCErrorOrDeathTest, MoveErrorValue) { |
| 235 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR); |
| 236 EXPECT_DEATH(error.MoveValue(), ""); |
| 237 } |
| 238 |
| 239 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 240 |
| 241 } // namespace webrtc |
OLD | NEW |