Backed out changeset 0c01a856e4c3 (bug 1870427) as requested by Emilio CLOSED TREE
[gecko.git] / third_party / libwebrtc / api / rtc_error_unittest.cc
blob68ca2f5a51d7a6c9bb3280ef8a35b41e536b49dc
1 /*
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
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 */
11 #include "api/rtc_error.h"
13 #include <string>
14 #include <utility>
16 #include "absl/strings/string_view.h"
17 #include "rtc_base/checks.h"
18 #include "test/gtest.h"
20 namespace webrtc {
21 namespace {
23 constexpr int kDefaultMoveOnlyIntValue = 0xbadf00d;
25 // Class that has no copy constructor, ensuring that RTCErrorOr can
26 struct MoveOnlyInt {
27 MoveOnlyInt() {}
28 explicit MoveOnlyInt(int value) : value(value) {}
29 MoveOnlyInt(const MoveOnlyInt& other) = delete;
30 MoveOnlyInt& operator=(const MoveOnlyInt& other) = delete;
31 MoveOnlyInt(MoveOnlyInt&& other) : value(other.value) {}
32 MoveOnlyInt& operator=(MoveOnlyInt&& other) {
33 value = other.value;
34 return *this;
37 int value = kDefaultMoveOnlyIntValue;
40 // Same as above. Used to test conversion from RTCErrorOr<A> to RTCErrorOr<B>
41 // when A can be converted to B.
42 struct MoveOnlyInt2 {
43 MoveOnlyInt2() {}
44 explicit MoveOnlyInt2(int value) : value(value) {}
45 MoveOnlyInt2(const MoveOnlyInt2& other) = delete;
46 MoveOnlyInt2& operator=(const MoveOnlyInt2& other) = delete;
47 MoveOnlyInt2(MoveOnlyInt2&& other) : value(other.value) {}
48 MoveOnlyInt2& operator=(MoveOnlyInt2&& other) {
49 value = other.value;
50 return *this;
53 explicit MoveOnlyInt2(MoveOnlyInt&& other) : value(other.value) {}
54 MoveOnlyInt2& operator=(MoveOnlyInt&& other) {
55 value = other.value;
56 return *this;
59 int value = kDefaultMoveOnlyIntValue;
62 // Test that the default constructor creates a "no error" error.
63 TEST(RTCErrorTest, DefaultConstructor) {
64 RTCError e;
65 EXPECT_EQ(e.type(), RTCErrorType::NONE);
66 EXPECT_STREQ(e.message(), "");
67 EXPECT_TRUE(e.ok());
70 TEST(RTCErrorTest, NormalConstructors) {
71 RTCError a(RTCErrorType::INVALID_PARAMETER);
72 EXPECT_EQ(a.type(), RTCErrorType::INVALID_PARAMETER);
73 EXPECT_STREQ(a.message(), "");
75 // Constructor that takes const char* message.
76 RTCError b(RTCErrorType::UNSUPPORTED_PARAMETER, "foobar");
77 EXPECT_EQ(b.type(), RTCErrorType::UNSUPPORTED_PARAMETER);
78 EXPECT_STREQ(b.message(), "foobar");
80 // Constructor that takes absl::string_view message.
81 RTCError c(RTCErrorType::SYNTAX_ERROR, absl::string_view("baz"));
82 EXPECT_EQ(c.type(), RTCErrorType::SYNTAX_ERROR);
83 EXPECT_STREQ(c.message(), "baz");
85 // Constructor that takes std::string message.
86 RTCError d(RTCErrorType::INVALID_RANGE, std::string("new"));
87 EXPECT_EQ(d.type(), RTCErrorType::INVALID_RANGE);
88 EXPECT_STREQ(d.message(), "new");
91 TEST(RTCErrorTest, MoveConstructor) {
92 // Static string.
93 RTCError a(RTCErrorType::INVALID_PARAMETER, "foo");
94 RTCError b(std::move(a));
95 EXPECT_EQ(b.type(), RTCErrorType::INVALID_PARAMETER);
96 EXPECT_STREQ(b.message(), "foo");
98 // Non-static string.
99 RTCError c(RTCErrorType::UNSUPPORTED_PARAMETER, std::string("bar"));
100 RTCError d(std::move(c));
101 EXPECT_EQ(d.type(), RTCErrorType::UNSUPPORTED_PARAMETER);
102 EXPECT_STREQ(d.message(), "bar");
105 TEST(RTCErrorTest, MoveAssignment) {
106 // Try all combinations of "is static string"/"is non-static string" moves.
107 RTCError e(RTCErrorType::INVALID_PARAMETER, "foo");
109 e = RTCError(RTCErrorType::UNSUPPORTED_PARAMETER, "bar");
110 EXPECT_EQ(e.type(), RTCErrorType::UNSUPPORTED_PARAMETER);
111 EXPECT_STREQ(e.message(), "bar");
113 e = RTCError(RTCErrorType::SYNTAX_ERROR, absl::string_view("baz"));
114 EXPECT_STREQ(e.message(), "baz");
116 e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("another"));
117 EXPECT_STREQ(e.message(), "another");
120 // Test that the error returned by RTCError::OK() is a "no error" error.
121 TEST(RTCErrorTest, OKConstant) {
122 RTCError ok = RTCError::OK();
123 EXPECT_EQ(ok.type(), RTCErrorType::NONE);
124 EXPECT_STREQ(ok.message(), "");
125 EXPECT_TRUE(ok.ok());
128 // Test that "error.ok()" behaves as expected.
129 TEST(RTCErrorTest, OkMethod) {
130 RTCError success;
131 RTCError failure(RTCErrorType::INTERNAL_ERROR);
132 EXPECT_TRUE(success.ok());
133 EXPECT_FALSE(failure.ok());
136 // Test that a message can be set using either static const strings or
137 // std::strings.
138 TEST(RTCErrorTest, SetMessage) {
139 RTCError e;
140 e.set_message("foo");
141 EXPECT_STREQ(e.message(), "foo");
143 e.set_message(absl::string_view("bar"));
144 EXPECT_STREQ(e.message(), "bar");
146 e.set_message(std::string("string"));
147 EXPECT_STREQ(e.message(), "string");
150 // Test that the default constructor creates an "INTERNAL_ERROR".
151 TEST(RTCErrorOrTest, DefaultConstructor) {
152 RTCErrorOr<MoveOnlyInt> e;
153 EXPECT_EQ(e.error().type(), RTCErrorType::INTERNAL_ERROR);
156 // Test that an RTCErrorOr can be implicitly constructed from a value.
157 TEST(RTCErrorOrTest, ImplicitValueConstructor) {
158 RTCErrorOr<MoveOnlyInt> e = [] { return MoveOnlyInt(100); }();
159 EXPECT_EQ(e.value().value, 100);
162 // Test that an RTCErrorOr can be implicitly constructed from an RTCError.
163 TEST(RTCErrorOrTest, ImplicitErrorConstructor) {
164 RTCErrorOr<MoveOnlyInt> e = [] {
165 return RTCError(RTCErrorType::SYNTAX_ERROR);
166 }();
167 EXPECT_EQ(e.error().type(), RTCErrorType::SYNTAX_ERROR);
170 TEST(RTCErrorOrTest, MoveConstructor) {
171 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
172 RTCErrorOr<MoveOnlyInt> b(std::move(a));
173 EXPECT_EQ(b.value().value, 5);
176 TEST(RTCErrorOrTest, MoveAssignment) {
177 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
178 RTCErrorOr<MoveOnlyInt> b(MoveOnlyInt(10));
179 a = std::move(b);
180 EXPECT_EQ(a.value().value, 10);
183 TEST(RTCErrorOrTest, ConversionConstructor) {
184 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(1));
185 RTCErrorOr<MoveOnlyInt2> b(std::move(a));
188 TEST(RTCErrorOrTest, ConversionAssignment) {
189 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
190 RTCErrorOr<MoveOnlyInt2> b(MoveOnlyInt2(10));
191 b = std::move(a);
192 EXPECT_EQ(b.value().value, 5);
195 TEST(RTCErrorOrTest, OkMethod) {
196 RTCErrorOr<int> success(1337);
197 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
198 EXPECT_TRUE(success.ok());
199 EXPECT_FALSE(error.ok());
202 TEST(RTCErrorOrTest, MoveError) {
203 RTCErrorOr<int> e({RTCErrorType::SYNTAX_ERROR, "message"});
204 RTCError err = e.MoveError();
205 EXPECT_EQ(err.type(), RTCErrorType::SYNTAX_ERROR);
206 EXPECT_STREQ(err.message(), "message");
209 TEST(RTCErrorOrTest, MoveValue) {
210 RTCErrorOr<MoveOnlyInt> e(MoveOnlyInt(88));
211 MoveOnlyInt value = e.MoveValue();
212 EXPECT_EQ(value.value, 88);
215 // Death tests.
216 // Disabled on Android because death tests misbehave on Android, see
217 // base/test/gtest_util.h.
218 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
220 TEST(RTCErrorOrDeathTest, ConstructWithOkError) {
221 RTCErrorOr<int> err;
222 EXPECT_DEATH(err = RTCError::OK(), "");
225 TEST(RTCErrorOrDeathTest, DereferenceErrorValue) {
226 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
227 EXPECT_DEATH(error.value(), "");
230 TEST(RTCErrorOrDeathTest, MoveErrorValue) {
231 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
232 EXPECT_DEATH(error.MoveValue(), "");
235 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
237 } // namespace
238 } // namespace webrtc