Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 "buffet/dbus_command_proxy.h" |
| 6 | |
| 7 | #include <functional> |
| 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <dbus/mock_bus.h> |
| 12 | #include <dbus/mock_exported_object.h> |
| 13 | #include <dbus/property.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 14 | #include <brillo/dbus/dbus_object.h> |
| 15 | #include <brillo/dbus/dbus_object_test_helpers.h> |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 16 | #include <gtest/gtest.h> |
Vitaly Buka | e2713ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 17 | #include <weave/command.h> |
| 18 | #include <weave/enum_to_string.h> |
Vitaly Buka | ea2f1e2 | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 19 | #include <weave/test/mock_command.h> |
Vitaly Buka | ea2f1e2 | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 20 | #include <weave/test/unittest_utils.h> |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 21 | |
| 22 | #include "buffet/dbus_constants.h" |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 23 | |
| 24 | namespace buffet { |
| 25 | |
Vitaly Buka | 3a58bb9 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 26 | using ::testing::_; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 27 | using ::testing::AnyNumber; |
| 28 | using ::testing::Return; |
| 29 | using ::testing::ReturnRefOfCopy; |
Vitaly Buka | 3a58bb9 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 30 | using ::testing::StrictMock; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 31 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 32 | using brillo::VariantDictionary; |
| 33 | using brillo::dbus_utils::AsyncEventSequencer; |
Vitaly Buka | ea2f1e2 | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 34 | using weave::test::CreateDictionaryValue; |
| 35 | using weave::test::IsEqualValue; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 36 | |
| 37 | namespace { |
| 38 | |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 39 | const char kTestCommandId[] = "cmd_1"; |
| 40 | |
| 41 | MATCHER_P(EqualToJson, json, "") { |
| 42 | auto json_value = CreateDictionaryValue(json); |
| 43 | return IsEqualValue(*json_value, arg); |
| 44 | } |
| 45 | |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 46 | MATCHER_P2(ExpectError, code, message, "") { |
| 47 | return arg->GetCode() == code && arg->GetMessage() == message; |
| 48 | } |
| 49 | |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 50 | } // namespace |
| 51 | |
| 52 | class DBusCommandProxyTest : public ::testing::Test { |
| 53 | public: |
| 54 | void SetUp() override { |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 55 | command_ = std::make_shared<StrictMock<weave::test::MockCommand>>(); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 56 | // Set up a mock DBus bus object. |
| 57 | dbus::Bus::Options options; |
| 58 | options.bus_type = dbus::Bus::SYSTEM; |
| 59 | bus_ = new dbus::MockBus(options); |
| 60 | // By default, don't worry about threading assertions. |
| 61 | EXPECT_CALL(*bus_, AssertOnOriginThread()).Times(AnyNumber()); |
| 62 | EXPECT_CALL(*bus_, AssertOnDBusThread()).Times(AnyNumber()); |
| 63 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 64 | EXPECT_CALL(*command_, GetID()) |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 65 | .WillOnce(ReturnRefOfCopy<std::string>(kTestCommandId)); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 66 | // Use WillRepeatedly because GetName is used for logging. |
| 67 | EXPECT_CALL(*command_, GetName()) |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 68 | .WillRepeatedly(ReturnRefOfCopy<std::string>("robot.jump")); |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 69 | EXPECT_CALL(*command_, GetState()) |
| 70 | .WillRepeatedly(Return(weave::Command::State::kQueued)); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 71 | EXPECT_CALL(*command_, GetOrigin()) |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 72 | .WillOnce(Return(weave::Command::Origin::kLocal)); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 73 | EXPECT_CALL(*command_, MockGetParameters()) |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 74 | .WillOnce(ReturnRefOfCopy<std::string>(R"({ |
| 75 | 'height': 53, |
| 76 | '_jumpType': '_withKick' |
| 77 | })")); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 78 | EXPECT_CALL(*command_, MockGetProgress()) |
| 79 | .WillRepeatedly(ReturnRefOfCopy<std::string>("{}")); |
| 80 | EXPECT_CALL(*command_, MockGetResults()) |
Vitaly Buka | baf7a95 | 2015-10-10 01:44:45 -0700 | [diff] [blame] | 81 | .WillRepeatedly(ReturnRefOfCopy<std::string>("{}")); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 82 | |
| 83 | // Set up a mock ExportedObject to be used with the DBus command proxy. |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 84 | std::string cmd_path = buffet::dbus_constants::kCommandServicePathPrefix; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 85 | cmd_path += kTestCommandId; |
| 86 | const dbus::ObjectPath kCmdObjPath(cmd_path); |
| 87 | // Use a mock exported object for the exported object manager. |
| 88 | mock_exported_object_command_ = |
| 89 | new dbus::MockExportedObject(bus_.get(), kCmdObjPath); |
| 90 | EXPECT_CALL(*bus_, GetExportedObject(kCmdObjPath)) |
| 91 | .Times(AnyNumber()) |
| 92 | .WillRepeatedly(Return(mock_exported_object_command_.get())); |
| 93 | EXPECT_CALL(*mock_exported_object_command_, ExportMethod(_, _, _, _)) |
| 94 | .Times(AnyNumber()); |
| 95 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 96 | proxy_.reset(new DBusCommandProxy{ |
| 97 | nullptr, bus_, std::weak_ptr<weave::Command>{command_}, cmd_path}); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 98 | GetCommandProxy()->RegisterAsync( |
| 99 | AsyncEventSequencer::GetDefaultCompletionAction()); |
| 100 | } |
| 101 | |
| 102 | void TearDown() override { |
| 103 | EXPECT_CALL(*mock_exported_object_command_, Unregister()).Times(1); |
| 104 | bus_ = nullptr; |
| 105 | } |
| 106 | |
| 107 | DBusCommandProxy* GetCommandProxy() const { return proxy_.get(); } |
| 108 | |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 109 | com::android::Weave::CommandAdaptor* GetCommandAdaptor() const { |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 110 | return &GetCommandProxy()->dbus_adaptor_; |
| 111 | } |
| 112 | |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 113 | com::android::Weave::CommandInterface* GetCommandInterface() const { |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 114 | // DBusCommandProxy also implements CommandInterface. |
| 115 | return GetCommandProxy(); |
| 116 | } |
| 117 | |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 118 | weave::Command::State GetCommandState() const { |
| 119 | weave::Command::State state; |
| 120 | EXPECT_TRUE(StringToEnum(GetCommandAdaptor()->GetState(), &state)); |
| 121 | return state; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | scoped_refptr<dbus::MockExportedObject> mock_exported_object_command_; |
| 125 | scoped_refptr<dbus::MockBus> bus_; |
| 126 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 127 | std::shared_ptr<StrictMock<weave::test::MockCommand>> command_; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 128 | std::unique_ptr<DBusCommandProxy> proxy_; |
| 129 | }; |
| 130 | |
| 131 | TEST_F(DBusCommandProxyTest, Init) { |
| 132 | VariantDictionary params = { |
| 133 | {"height", int32_t{53}}, {"_jumpType", std::string{"_withKick"}}, |
| 134 | }; |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 135 | EXPECT_EQ(weave::Command::State::kQueued, GetCommandState()); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 136 | EXPECT_EQ(params, GetCommandAdaptor()->GetParameters()); |
| 137 | EXPECT_EQ(VariantDictionary{}, GetCommandAdaptor()->GetProgress()); |
| 138 | EXPECT_EQ(VariantDictionary{}, GetCommandAdaptor()->GetResults()); |
| 139 | EXPECT_EQ("robot.jump", GetCommandAdaptor()->GetName()); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 140 | EXPECT_EQ(kTestCommandId, GetCommandAdaptor()->GetId()); |
| 141 | } |
| 142 | |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 143 | TEST_F(DBusCommandProxyTest, SetProgress) { |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 144 | EXPECT_CALL(*command_, SetProgress(EqualToJson("{'progress': 10}"), _)) |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 145 | .WillOnce(Return(true)); |
| 146 | EXPECT_TRUE( |
| 147 | GetCommandInterface()->SetProgress(nullptr, {{"progress", int32_t{10}}})); |
| 148 | } |
| 149 | |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 150 | TEST_F(DBusCommandProxyTest, Complete) { |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 151 | EXPECT_CALL( |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 152 | *command_, |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 153 | Complete( |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 154 | EqualToJson("{'foo': 42, 'bar': 'foobar', 'resultList': [1, 2, 3]}"), |
| 155 | _)) |
| 156 | .WillOnce(Return(true)); |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 157 | EXPECT_TRUE(GetCommandInterface()->Complete( |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 158 | nullptr, VariantDictionary{{"foo", int32_t{42}}, |
| 159 | {"bar", std::string{"foobar"}}, |
| 160 | {"resultList", std::vector<int>{1, 2, 3}}})); |
| 161 | } |
| 162 | |
| 163 | TEST_F(DBusCommandProxyTest, Abort) { |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 164 | EXPECT_CALL(*command_, Abort(ExpectError("foo", "bar"), _)) |
| 165 | .WillOnce(Return(true)); |
| 166 | EXPECT_TRUE(GetCommandInterface()->Abort(nullptr, "foo", "bar")); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | TEST_F(DBusCommandProxyTest, Cancel) { |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 170 | EXPECT_CALL(*command_, Cancel(_)).WillOnce(Return(true)); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 171 | EXPECT_TRUE(GetCommandInterface()->Cancel(nullptr)); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 174 | } // namespace buffet |