Peter Qiu | 69d2521 | 2015-11-19 13:13:25 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2011 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #include "apmanager/error.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include <brillo/errors/error.h> |
| 22 | #include <brillo/errors/error_codes.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #if defined(__ANDROID__) |
| 26 | #include <dbus/service_constants.h> |
| 27 | #else |
| 28 | #include <chromeos/dbus/service_constants.h> |
| 29 | #endif // __ANDROID__ |
| 30 | |
| 31 | using std::string; |
| 32 | using testing::Test; |
| 33 | |
| 34 | namespace apmanager { |
| 35 | |
| 36 | class ErrorTest : public Test { |
| 37 | public: |
| 38 | ErrorTest() {} |
| 39 | |
| 40 | void PopulateError(Error* error, Error::Type type) { |
| 41 | error->type_ = type; |
| 42 | } |
| 43 | |
| 44 | void PopulateError(Error* error, Error::Type type, string message) { |
| 45 | error->type_ = type; |
| 46 | error->message_ = message; |
| 47 | } |
| 48 | |
| 49 | void VerifyDBusError(Error::Type type, const string& expected_error_code) { |
| 50 | static const std::string kMessage = "Test error message"; |
| 51 | Error e; |
| 52 | PopulateError(&e, type, kMessage); |
| 53 | brillo::ErrorPtr dbus_error; |
| 54 | EXPECT_TRUE(e.ToDBusError(&dbus_error)); |
| 55 | EXPECT_NE(nullptr, dbus_error.get()); |
| 56 | EXPECT_EQ(brillo::errors::dbus::kDomain, dbus_error->GetDomain()); |
| 57 | EXPECT_EQ(expected_error_code, dbus_error->GetCode()); |
| 58 | EXPECT_EQ(kMessage, dbus_error->GetMessage()); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | TEST_F(ErrorTest, Constructor) { |
| 63 | Error e; |
| 64 | EXPECT_EQ(Error::kSuccess, e.type()); |
| 65 | } |
| 66 | |
| 67 | TEST_F(ErrorTest, Reset) { |
| 68 | Error e; |
| 69 | PopulateError(&e, Error::kInternalError); |
| 70 | EXPECT_TRUE(e.IsFailure()); |
| 71 | e.Reset(); |
| 72 | EXPECT_TRUE(e.IsSuccess()); |
| 73 | } |
| 74 | |
| 75 | TEST_F(ErrorTest, ToDBusError) { |
| 76 | brillo::ErrorPtr dbus_error; |
| 77 | |
| 78 | // No error. |
| 79 | EXPECT_EQ(nullptr, dbus_error.get()); |
| 80 | EXPECT_FALSE(Error().ToDBusError(&dbus_error)); |
| 81 | EXPECT_EQ(nullptr, dbus_error.get()); |
| 82 | |
| 83 | VerifyDBusError(Error::kInternalError, kErrorInternalError); |
| 84 | VerifyDBusError(Error::kInvalidArguments, kErrorInvalidArguments); |
| 85 | VerifyDBusError(Error::kInvalidConfiguration, kErrorInvalidConfiguration); |
| 86 | } |
| 87 | |
| 88 | } // namespace shill |