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 | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 20 | #include "buffet/dbus_constants.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 21 | |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 22 | namespace buffet { |
| 23 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 24 | using ::testing::AnyNumber; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 25 | using ::testing::Invoke; |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 26 | using ::testing::Return; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 27 | using ::testing::_; |
| 28 | |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 29 | using chromeos::VariantDictionary; |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 30 | using chromeos::dbus_utils::AsyncEventSequencer; |
| 31 | using unittests::CreateDictionaryValue; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 32 | |
| 33 | namespace { |
| 34 | |
| 35 | const char kTestCommandCategoty[] = "test_command_category"; |
| 36 | const char kTestCommandId[] = "cmd_1"; |
| 37 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 38 | } // namespace |
| 39 | |
| 40 | class DBusCommandProxyTest : public ::testing::Test { |
| 41 | public: |
| 42 | void SetUp() override { |
| 43 | // Set up a mock DBus bus object. |
| 44 | dbus::Bus::Options options; |
| 45 | options.bus_type = dbus::Bus::SYSTEM; |
| 46 | bus_ = new dbus::MockBus(options); |
| 47 | // By default, don't worry about threading assertions. |
| 48 | EXPECT_CALL(*bus_, AssertOnOriginThread()).Times(AnyNumber()); |
| 49 | EXPECT_CALL(*bus_, AssertOnDBusThread()).Times(AnyNumber()); |
| 50 | |
| 51 | // Command instance. |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 52 | // TODO(antonm): Test results. |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 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 | } |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 66 | }, |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 67 | 'results': { |
| 68 | 'foo': { |
| 69 | 'type': 'integer' |
| 70 | }, |
| 71 | 'bar': { |
| 72 | 'type': 'string' |
| 73 | } |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 74 | }, |
| 75 | 'progress': { |
| 76 | 'progress': { |
| 77 | 'type': 'integer', |
| 78 | 'minimum': 0, |
| 79 | 'maximum': 100 |
| 80 | } |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 81 | } |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | })"); |
| 85 | CHECK(dict_.LoadCommands(*json, kTestCommandCategoty, nullptr, nullptr)) |
| 86 | << "Failed to parse test command dictionary"; |
| 87 | |
| 88 | json = CreateDictionaryValue(R"({ |
| 89 | 'name': 'robot.jump', |
| 90 | 'parameters': { |
| 91 | 'height': 53, |
| 92 | '_jumpType': '_withKick' |
| 93 | } |
| 94 | })"); |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 95 | command_instance_ = |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 96 | CommandInstance::FromJson(json.get(), "local", dict_, nullptr, nullptr); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 97 | command_instance_->SetID(kTestCommandId); |
| 98 | |
| 99 | // Set up a mock ExportedObject to be used with the DBus command proxy. |
| 100 | std::string cmd_path = dbus_constants::kCommandServicePathPrefix; |
| 101 | cmd_path += kTestCommandId; |
| 102 | const dbus::ObjectPath kCmdObjPath(cmd_path); |
| 103 | // Use a mock exported object for the exported object manager. |
| 104 | mock_exported_object_command_ = |
| 105 | new dbus::MockExportedObject(bus_.get(), kCmdObjPath); |
| 106 | EXPECT_CALL(*bus_, GetExportedObject(kCmdObjPath)).Times(AnyNumber()) |
| 107 | .WillRepeatedly(Return(mock_exported_object_command_.get())); |
| 108 | EXPECT_CALL(*mock_exported_object_command_, |
| 109 | ExportMethod(_, _, _, _)).Times(AnyNumber()); |
| 110 | |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 111 | std::unique_ptr<CommandProxyInterface> command_proxy( |
| 112 | new DBusCommandProxy(nullptr, bus_, command_instance_.get(), cmd_path)); |
| 113 | command_instance_->AddProxy(std::move(command_proxy)); |
| 114 | GetCommandProxy()->RegisterAsync( |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 115 | AsyncEventSequencer::GetDefaultCompletionAction()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void TearDown() override { |
| 119 | EXPECT_CALL(*mock_exported_object_command_, Unregister()).Times(1); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 120 | command_instance_.reset(); |
| 121 | dict_.Clear(); |
| 122 | bus_ = nullptr; |
| 123 | } |
| 124 | |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 125 | DBusCommandProxy* GetCommandProxy() const { |
Mike Frysinger | 42e3a72 | 2014-11-15 06:48:08 -0500 | [diff] [blame] | 126 | CHECK_EQ(command_instance_->proxies_.size(), 1U); |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 127 | return static_cast<DBusCommandProxy*>(command_instance_->proxies_[0].get()); |
| 128 | } |
| 129 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 130 | org::chromium::Buffet::CommandAdaptor* GetCommandAdaptor() const { |
| 131 | return &GetCommandProxy()->dbus_adaptor_; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 134 | org::chromium::Buffet::CommandInterface* GetCommandInterface() const { |
| 135 | // DBusCommandProxy also implements CommandInterface. |
| 136 | return GetCommandProxy(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 139 | std::unique_ptr<CommandInstance> command_instance_; |
| 140 | CommandDictionary dict_; |
| 141 | |
| 142 | scoped_refptr<dbus::MockExportedObject> mock_exported_object_command_; |
| 143 | scoped_refptr<dbus::MockBus> bus_; |
| 144 | }; |
| 145 | |
| 146 | TEST_F(DBusCommandProxyTest, Init) { |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 147 | VariantDictionary params = { |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 148 | {"height", int32_t{53}}, |
| 149 | {"_jumpType", std::string{"_withKick"}}, |
| 150 | }; |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 151 | EXPECT_EQ(CommandInstance::kStatusQueued, GetCommandAdaptor()->GetStatus()); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 152 | EXPECT_EQ(params, GetCommandAdaptor()->GetParameters()); |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 153 | EXPECT_EQ(VariantDictionary{}, GetCommandAdaptor()->GetProgress()); |
| 154 | EXPECT_EQ(VariantDictionary{}, GetCommandAdaptor()->GetResults()); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 155 | EXPECT_EQ("robot.jump", GetCommandAdaptor()->GetName()); |
| 156 | EXPECT_EQ(kTestCommandCategoty, GetCommandAdaptor()->GetCategory()); |
| 157 | EXPECT_EQ(kTestCommandId, GetCommandAdaptor()->GetId()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | TEST_F(DBusCommandProxyTest, SetProgress) { |
| 161 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(2); |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 162 | EXPECT_TRUE( |
| 163 | GetCommandInterface()->SetProgress(nullptr, {{"progress", int32_t{10}}})); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 164 | EXPECT_EQ(CommandInstance::kStatusInProgress, |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 165 | GetCommandAdaptor()->GetStatus()); |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 166 | |
| 167 | VariantDictionary progress{{"progress", int32_t{10}}}; |
| 168 | EXPECT_EQ(progress, GetCommandAdaptor()->GetProgress()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | TEST_F(DBusCommandProxyTest, SetProgress_OutOfRange) { |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 172 | EXPECT_FALSE(GetCommandInterface()->SetProgress( |
| 173 | nullptr, {{"progress", int32_t{110}}})); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 174 | EXPECT_EQ(CommandInstance::kStatusQueued, GetCommandAdaptor()->GetStatus()); |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 175 | EXPECT_EQ(VariantDictionary{}, GetCommandAdaptor()->GetProgress()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 178 | TEST_F(DBusCommandProxyTest, SetResults) { |
| 179 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1); |
| 180 | const VariantDictionary results = { |
| 181 | {"foo", int32_t{42}}, |
| 182 | {"bar", std::string{"foobar"}}, |
| 183 | }; |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 184 | EXPECT_TRUE(GetCommandInterface()->SetResults(nullptr, results)); |
| 185 | EXPECT_EQ(results, GetCommandAdaptor()->GetResults()); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST_F(DBusCommandProxyTest, SetResults_UnknownProperty) { |
| 189 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(0); |
| 190 | const VariantDictionary results = { |
| 191 | {"quux", int32_t{42}}, |
| 192 | }; |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 193 | EXPECT_FALSE(GetCommandInterface()->SetResults(nullptr, results)); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 194 | } |
| 195 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 196 | TEST_F(DBusCommandProxyTest, Abort) { |
| 197 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 198 | GetCommandInterface()->Abort(); |
| 199 | EXPECT_EQ(CommandInstance::kStatusAborted, |
| 200 | GetCommandAdaptor()->GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | TEST_F(DBusCommandProxyTest, Cancel) { |
| 204 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 205 | GetCommandInterface()->Cancel(); |
Alex Vakulenko | db22124 | 2015-03-13 14:02:46 -0700 | [diff] [blame] | 206 | EXPECT_EQ(CommandInstance::kStatusCancelled, |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 207 | GetCommandAdaptor()->GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | TEST_F(DBusCommandProxyTest, Done) { |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 211 | // 1 property update: |
| 212 | // status: queued -> done |
| 213 | EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 214 | GetCommandInterface()->Done(); |
| 215 | EXPECT_EQ(CommandInstance::kStatusDone, GetCommandAdaptor()->GetStatus()); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // namespace buffet |