Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -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 | |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "buffet/commands/dbus_command_proxy.h" |
| 6 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 7 | #include <functional> |
| 8 | #include <memory> |
| 9 | |
| 10 | #include <dbus/mock_bus.h> |
| 11 | #include <dbus/mock_exported_object.h> |
| 12 | #include <dbus/property.h> |
| 13 | #include <chromeos/dbus/dbus_object.h> |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/dbus_object_test_helpers.h> |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
| 16 | |
| 17 | #include "buffet/commands/command_dictionary.h" |
| 18 | #include "buffet/commands/command_instance.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 19 | #include "buffet/commands/unittest_utils.h" |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 20 | #include "buffet/libbuffet/dbus_constants.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 21 | |
| 22 | using ::testing::AnyNumber; |
| 23 | using ::testing::Return; |
| 24 | using ::testing::Invoke; |
| 25 | using ::testing::_; |
| 26 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 27 | using buffet::unittests::CreateDictionaryValue; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 28 | using chromeos::dbus_utils::AsyncEventSequencer; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 29 | using chromeos::dbus_utils::ExportedObjectManager; |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 30 | using chromeos::VariantDictionary; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 31 | |
| 32 | namespace buffet { |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | const char kTestCommandCategoty[] = "test_command_category"; |
| 37 | const char kTestCommandId[] = "cmd_1"; |
| 38 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 39 | } // namespace |
| 40 | |
| 41 | class DBusCommandProxyTest : public ::testing::Test { |
| 42 | public: |
| 43 | void SetUp() override { |
| 44 | // Set up a mock DBus bus object. |
| 45 | dbus::Bus::Options options; |
| 46 | options.bus_type = dbus::Bus::SYSTEM; |
| 47 | bus_ = new dbus::MockBus(options); |
| 48 | // By default, don't worry about threading assertions. |
| 49 | EXPECT_CALL(*bus_, AssertOnOriginThread()).Times(AnyNumber()); |
| 50 | EXPECT_CALL(*bus_, AssertOnDBusThread()).Times(AnyNumber()); |
| 51 | |
| 52 | // Command instance. |
| 53 | auto json = CreateDictionaryValue(R"({ |
| 54 | 'robot': { |
| 55 | 'jump': { |
| 56 | 'parameters': { |
| 57 | 'height': { |
| 58 | 'type': 'integer', |
| 59 | 'minimum': 0, |
| 60 | 'maximum': 100 |
| 61 | }, |
| 62 | '_jumpType': { |
| 63 | 'type': 'string', |
| 64 | 'enum': ['_withAirFlip', '_withSpin', '_withKick'] |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | })"); |
| 70 | CHECK(dict_.LoadCommands(*json, kTestCommandCategoty, nullptr, nullptr)) |
| 71 | << "Failed to parse test command dictionary"; |
| 72 | |
| 73 | json = CreateDictionaryValue(R"({ |
| 74 | 'name': 'robot.jump', |
| 75 | 'parameters': { |
| 76 | 'height': 53, |
| 77 | '_jumpType': '_withKick' |
| 78 | } |
| 79 | })"); |
| 80 | command_instance_ = CommandInstance::FromJson(json.get(), dict_, nullptr); |
| 81 | command_instance_->SetID(kTestCommandId); |
| 82 | |
| 83 | // Set up a mock ExportedObject to be used with the DBus command proxy. |
| 84 | std::string cmd_path = dbus_constants::kCommandServicePathPrefix; |
| 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)).Times(AnyNumber()) |
| 91 | .WillRepeatedly(Return(mock_exported_object_command_.get())); |
| 92 | EXPECT_CALL(*mock_exported_object_command_, |
| 93 | ExportMethod(_, _, _, _)).Times(AnyNumber()); |
| 94 | |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 95 | std::unique_ptr<CommandProxyInterface> command_proxy( |
| 96 | new DBusCommandProxy(nullptr, bus_, command_instance_.get(), cmd_path)); |
| 97 | command_instance_->AddProxy(std::move(command_proxy)); |
| 98 | GetCommandProxy()->RegisterAsync( |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 99 | AsyncEventSequencer::GetDefaultCompletionAction()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void TearDown() override { |
| 103 | EXPECT_CALL(*mock_exported_object_command_, Unregister()).Times(1); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 104 | command_instance_.reset(); |
| 105 | dict_.Clear(); |
| 106 | bus_ = nullptr; |
| 107 | } |
| 108 | |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 109 | DBusCommandProxy* GetCommandProxy() const { |
Mike Frysinger | 42e3a72 | 2014-11-15 06:48:08 -0500 | [diff] [blame] | 110 | CHECK_EQ(command_instance_->proxies_.size(), 1U); |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 111 | return static_cast<DBusCommandProxy*>(command_instance_->proxies_[0].get()); |
| 112 | } |
| 113 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 114 | chromeos::dbus_utils::DBusObject* GetProxyDBusObject() { |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 115 | return &GetCommandProxy()->dbus_object_; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | std::string GetStatus() const { |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 119 | return GetCommandProxy()->status_.value(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int32_t GetProgress() const { |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 123 | return GetCommandProxy()->progress_.value(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 126 | VariantDictionary GetParameters() const { |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 127 | return GetCommandProxy()->parameters_.value(); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 130 | std::unique_ptr<dbus::Response> CallMethod( |
| 131 | const std::string& method_name, |
| 132 | const std::function<void(dbus::MessageWriter*)>& param_callback) { |
| 133 | dbus::MethodCall method_call(dbus_constants::kCommandInterface, |
| 134 | method_name); |
| 135 | method_call.SetSerial(1234); |
| 136 | dbus::MessageWriter writer(&method_call); |
| 137 | if (param_callback) |
| 138 | param_callback(&writer); |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 139 | return chromeos::dbus_utils::testing::CallMethod(*GetProxyDBusObject(), |
| 140 | &method_call); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static bool IsResponseError(const std::unique_ptr<dbus::Response>& response) { |
| 144 | return (response->GetMessageType() == dbus::Message::MESSAGE_ERROR); |
| 145 | } |
| 146 | |
| 147 | static void VerifyResponse( |
| 148 | const std::unique_ptr<dbus::Response>& response, |
| 149 | const std::function<void(dbus::MessageReader*)>& result_callback) { |
| 150 | EXPECT_FALSE(IsResponseError(response)); |
| 151 | dbus::MessageReader reader(response.get()); |
| 152 | if (result_callback) |
| 153 | result_callback(&reader); |
| 154 | EXPECT_FALSE(reader.HasMoreData()); |
| 155 | } |
| 156 | |
| 157 | template<typename T> |
| 158 | T GetPropertyValue(const std::string& property_name) { |
| 159 | dbus::MethodCall method_call(dbus::kPropertiesInterface, |
| 160 | dbus::kPropertiesGet); |
| 161 | method_call.SetSerial(1234); |
| 162 | dbus::MessageWriter writer(&method_call); |
| 163 | writer.AppendString(dbus_constants::kCommandInterface); |
| 164 | writer.AppendString(property_name); |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 165 | auto response = chromeos::dbus_utils::testing::CallMethod( |
| 166 | *GetProxyDBusObject(), &method_call); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 167 | T value{}; |
| 168 | VerifyResponse(response, [&value](dbus::MessageReader* reader) { |
| 169 | EXPECT_TRUE(chromeos::dbus_utils::PopValueFromReader(reader, &value)); |
| 170 | }); |
| 171 | return value; |
| 172 | } |
| 173 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 174 | std::unique_ptr<CommandInstance> command_instance_; |
| 175 | CommandDictionary dict_; |
| 176 | |
| 177 | scoped_refptr<dbus::MockExportedObject> mock_exported_object_command_; |
| 178 | scoped_refptr<dbus::MockBus> bus_; |
| 179 | }; |
| 180 | |
| 181 | TEST_F(DBusCommandProxyTest, Init) { |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 182 | VariantDictionary params = { |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 183 | {"height", int32_t{53}}, |
| 184 | {"_jumpType", std::string{"_withKick"}}, |
| 185 | }; |
| 186 | EXPECT_EQ(CommandInstance::kStatusQueued, GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 187 | EXPECT_EQ(0, GetProgress()); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 188 | EXPECT_EQ(params, GetParameters()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 189 | EXPECT_EQ("robot.jump", |
| 190 | GetPropertyValue<std::string>(dbus_constants::kCommandName)); |
| 191 | EXPECT_EQ(kTestCommandCategoty, |
| 192 | GetPropertyValue<std::string>(dbus_constants::kCommandCategory)); |
| 193 | EXPECT_EQ(kTestCommandId, |
| 194 | GetPropertyValue<std::string>(dbus_constants::kCommandId)); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 195 | EXPECT_EQ(CommandInstance::kStatusQueued, |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 196 | GetPropertyValue<std::string>(dbus_constants::kCommandStatus)); |
| 197 | EXPECT_EQ(0, GetPropertyValue<int32_t>(dbus_constants::kCommandProgress)); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 198 | EXPECT_EQ(params, |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 199 | GetPropertyValue<VariantDictionary>( |
| 200 | dbus_constants::kCommandParameters)); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | TEST_F(DBusCommandProxyTest, SetProgress) { |
| 204 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(2); |
| 205 | auto response = CallMethod(dbus_constants::kCommandSetProgress, |
| 206 | [](dbus::MessageWriter* writer) { |
| 207 | writer->AppendInt32(10); |
| 208 | }); |
| 209 | VerifyResponse(response, {}); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 210 | EXPECT_EQ(CommandInstance::kStatusInProgress, GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 211 | EXPECT_EQ(10, GetProgress()); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 212 | EXPECT_EQ(CommandInstance::kStatusInProgress, |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 213 | GetPropertyValue<std::string>(dbus_constants::kCommandStatus)); |
| 214 | EXPECT_EQ(10, GetPropertyValue<int32_t>(dbus_constants::kCommandProgress)); |
| 215 | } |
| 216 | |
| 217 | TEST_F(DBusCommandProxyTest, SetProgress_OutOfRange) { |
| 218 | auto response = CallMethod(dbus_constants::kCommandSetProgress, |
| 219 | [](dbus::MessageWriter* writer) { |
| 220 | writer->AppendInt32(110); |
| 221 | }); |
| 222 | EXPECT_TRUE(IsResponseError(response)); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 223 | EXPECT_EQ(CommandInstance::kStatusQueued, GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 224 | EXPECT_EQ(0, GetProgress()); |
| 225 | } |
| 226 | |
| 227 | TEST_F(DBusCommandProxyTest, Abort) { |
| 228 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1); |
| 229 | auto response = CallMethod(dbus_constants::kCommandAbort, {}); |
| 230 | VerifyResponse(response, {}); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 231 | EXPECT_EQ(CommandInstance::kStatusAborted, GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | TEST_F(DBusCommandProxyTest, Cancel) { |
| 235 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1); |
| 236 | auto response = CallMethod(dbus_constants::kCommandCancel, {}); |
| 237 | VerifyResponse(response, {}); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 238 | EXPECT_EQ(CommandInstance::kStatusCanceled, GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | TEST_F(DBusCommandProxyTest, Done) { |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 242 | // 3 property updates: |
| 243 | // status: queued -> inProgress |
| 244 | // progress: 0 -> 100 |
| 245 | // status: inProgress -> done |
| 246 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(3); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 247 | auto response = CallMethod(dbus_constants::kCommandDone, {}); |
| 248 | VerifyResponse(response, {}); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 249 | EXPECT_EQ(CommandInstance::kStatusDone, GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 250 | EXPECT_EQ(100, GetProgress()); |
| 251 | } |
| 252 | |
| 253 | } // namespace buffet |