blob: 18dd8e07186e21769c937a3c81736398d5577591 [file] [log] [blame]
Alex Vakulenko4866ac92014-08-20 12:53:33 -07001// 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
Vitaly Bukaf9d50292015-07-27 16:08:51 -07005#include "buffet/dbus_command_proxy.h"
Alex Deymof6cbe322014-11-10 19:55:35 -08006
Alex Vakulenko4866ac92014-08-20 12:53:33 -07007#include <functional>
8#include <memory>
Vitaly Buka8d8d2192015-07-21 22:25:09 -07009#include <vector>
Alex Vakulenko4866ac92014-08-20 12:53:33 -070010
11#include <dbus/mock_bus.h>
12#include <dbus/mock_exported_object.h>
13#include <dbus/property.h>
14#include <chromeos/dbus/dbus_object.h>
Alex Vakulenko5c7bf012014-10-30 16:28:38 -070015#include <chromeos/dbus/dbus_object_test_helpers.h>
Alex Vakulenko4866ac92014-08-20 12:53:33 -070016#include <gtest/gtest.h>
Vitaly Buka7b382ac2015-08-03 13:50:01 -070017#include <weave/command.h>
18#include <weave/enum_to_string.h>
19#include <weave/mock_command.h>
20#include <weave/mock_commands.h>
21#include <weave/unittest_utils.h>
Alex Vakulenko4866ac92014-08-20 12:53:33 -070022
Alex Vakulenko420e49f2014-12-01 17:53:27 -080023#include "buffet/dbus_constants.h"
Vitaly Buka15f59092015-07-24 16:54:32 -070024
Vitaly Bukaf9d50292015-07-27 16:08:51 -070025namespace buffet {
Vitaly Buka32005de2015-05-01 12:33:31 -070026
Alex Vakulenko4866ac92014-08-20 12:53:33 -070027using ::testing::AnyNumber;
Vitaly Buka32005de2015-05-01 12:33:31 -070028using ::testing::Return;
Vitaly Bukafc42b312015-07-24 14:04:31 -070029using ::testing::ReturnRefOfCopy;
Alex Vakulenko4866ac92014-08-20 12:53:33 -070030using ::testing::_;
31
Alex Vakulenko576c9792014-09-22 16:49:45 -070032using chromeos::VariantDictionary;
Vitaly Buka32005de2015-05-01 12:33:31 -070033using chromeos::dbus_utils::AsyncEventSequencer;
Vitaly Bukaf9d50292015-07-27 16:08:51 -070034using weave::unittests::CreateDictionaryValue;
35using weave::unittests::IsEqualValue;
Alex Vakulenko4866ac92014-08-20 12:53:33 -070036
37namespace {
38
39const char kTestCommandCategoty[] = "test_command_category";
40const char kTestCommandId[] = "cmd_1";
41
Vitaly Bukafc42b312015-07-24 14:04:31 -070042MATCHER_P(EqualToJson, json, "") {
43 auto json_value = CreateDictionaryValue(json);
Vitaly Bukaf9d50292015-07-27 16:08:51 -070044 return IsEqualValue(*json_value, arg);
Vitaly Bukafc42b312015-07-24 14:04:31 -070045}
46
Alex Vakulenko4866ac92014-08-20 12:53:33 -070047} // namespace
48
49class DBusCommandProxyTest : public ::testing::Test {
50 public:
51 void SetUp() override {
52 // Set up a mock DBus bus object.
53 dbus::Bus::Options options;
54 options.bus_type = dbus::Bus::SYSTEM;
55 bus_ = new dbus::MockBus(options);
56 // By default, don't worry about threading assertions.
57 EXPECT_CALL(*bus_, AssertOnOriginThread()).Times(AnyNumber());
58 EXPECT_CALL(*bus_, AssertOnDBusThread()).Times(AnyNumber());
59
Vitaly Bukafc42b312015-07-24 14:04:31 -070060 EXPECT_CALL(command_, GetID())
61 .WillOnce(ReturnRefOfCopy<std::string>(kTestCommandId));
62 // Use WillRepeatedly becase GetName is used for logging.
63 EXPECT_CALL(command_, GetName())
64 .WillRepeatedly(ReturnRefOfCopy<std::string>("robot.jump"));
65 EXPECT_CALL(command_, GetCategory())
66 .WillOnce(ReturnRefOfCopy<std::string>(kTestCommandCategoty));
Vitaly Bukaf9d50292015-07-27 16:08:51 -070067 EXPECT_CALL(command_, GetStatus())
68 .WillOnce(Return(weave::CommandStatus::kQueued));
69 EXPECT_CALL(command_, GetOrigin())
70 .WillOnce(Return(weave::CommandOrigin::kLocal));
Vitaly Bukafc42b312015-07-24 14:04:31 -070071 EXPECT_CALL(command_, MockGetParameters())
72 .WillOnce(ReturnRefOfCopy<std::string>(R"({
73 'height': 53,
74 '_jumpType': '_withKick'
75 })"));
76 EXPECT_CALL(command_, MockGetProgress())
77 .WillOnce(ReturnRefOfCopy<std::string>("{}"));
78 EXPECT_CALL(command_, MockGetResults())
79 .WillOnce(ReturnRefOfCopy<std::string>("{}"));
Alex Vakulenko4866ac92014-08-20 12:53:33 -070080
81 // Set up a mock ExportedObject to be used with the DBus command proxy.
Vitaly Bukab6f015a2015-07-09 14:59:23 -070082 std::string cmd_path = buffet::kCommandServicePathPrefix;
Alex Vakulenko4866ac92014-08-20 12:53:33 -070083 cmd_path += kTestCommandId;
84 const dbus::ObjectPath kCmdObjPath(cmd_path);
85 // Use a mock exported object for the exported object manager.
86 mock_exported_object_command_ =
87 new dbus::MockExportedObject(bus_.get(), kCmdObjPath);
Vitaly Bukaa647c852015-07-06 14:51:01 -070088 EXPECT_CALL(*bus_, GetExportedObject(kCmdObjPath))
89 .Times(AnyNumber())
Alex Vakulenko4866ac92014-08-20 12:53:33 -070090 .WillRepeatedly(Return(mock_exported_object_command_.get()));
Vitaly Bukaa647c852015-07-06 14:51:01 -070091 EXPECT_CALL(*mock_exported_object_command_, ExportMethod(_, _, _, _))
92 .Times(AnyNumber());
Alex Vakulenko4866ac92014-08-20 12:53:33 -070093
Vitaly Bukafc42b312015-07-24 14:04:31 -070094 proxy_.reset(new DBusCommandProxy(nullptr, bus_, &command_, cmd_path));
Anton Muhinb66a9302014-11-10 22:15:22 +040095 GetCommandProxy()->RegisterAsync(
Alex Vakulenkof6b38712014-09-03 16:23:38 -070096 AsyncEventSequencer::GetDefaultCompletionAction());
Alex Vakulenko4866ac92014-08-20 12:53:33 -070097 }
98
99 void TearDown() override {
100 EXPECT_CALL(*mock_exported_object_command_, Unregister()).Times(1);
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700101 bus_ = nullptr;
102 }
103
Vitaly Bukafc42b312015-07-24 14:04:31 -0700104 DBusCommandProxy* GetCommandProxy() const { return proxy_.get(); }
Anton Muhinb66a9302014-11-10 22:15:22 +0400105
Alex Vakulenko420e49f2014-12-01 17:53:27 -0800106 org::chromium::Buffet::CommandAdaptor* GetCommandAdaptor() const {
107 return &GetCommandProxy()->dbus_adaptor_;
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700108 }
109
Alex Vakulenko420e49f2014-12-01 17:53:27 -0800110 org::chromium::Buffet::CommandInterface* GetCommandInterface() const {
111 // DBusCommandProxy also implements CommandInterface.
112 return GetCommandProxy();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700113 }
114
Vitaly Bukaf9d50292015-07-27 16:08:51 -0700115 weave::CommandStatus GetCommandStatus() const {
116 weave::CommandStatus status;
Vitaly Buka15f59092015-07-24 16:54:32 -0700117 EXPECT_TRUE(StringToEnum(GetCommandAdaptor()->GetStatus(), &status));
118 return status;
119 }
120
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700121 scoped_refptr<dbus::MockExportedObject> mock_exported_object_command_;
122 scoped_refptr<dbus::MockBus> bus_;
Vitaly Bukafc42b312015-07-24 14:04:31 -0700123
Vitaly Bukaf9d50292015-07-27 16:08:51 -0700124 weave::unittests::MockCommand command_;
Vitaly Bukafc42b312015-07-24 14:04:31 -0700125 std::unique_ptr<DBusCommandProxy> proxy_;
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700126};
127
128TEST_F(DBusCommandProxyTest, Init) {
Alex Vakulenko576c9792014-09-22 16:49:45 -0700129 VariantDictionary params = {
Vitaly Bukaa647c852015-07-06 14:51:01 -0700130 {"height", int32_t{53}}, {"_jumpType", std::string{"_withKick"}},
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700131 };
Vitaly Bukaf9d50292015-07-27 16:08:51 -0700132 EXPECT_EQ(weave::CommandStatus::kQueued, GetCommandStatus());
Alex Vakulenko420e49f2014-12-01 17:53:27 -0800133 EXPECT_EQ(params, GetCommandAdaptor()->GetParameters());
Vitaly Buka4129dfa2015-04-29 12:16:58 -0700134 EXPECT_EQ(VariantDictionary{}, GetCommandAdaptor()->GetProgress());
135 EXPECT_EQ(VariantDictionary{}, GetCommandAdaptor()->GetResults());
Alex Vakulenko420e49f2014-12-01 17:53:27 -0800136 EXPECT_EQ("robot.jump", GetCommandAdaptor()->GetName());
137 EXPECT_EQ(kTestCommandCategoty, GetCommandAdaptor()->GetCategory());
138 EXPECT_EQ(kTestCommandId, GetCommandAdaptor()->GetId());
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700139}
140
Vitaly Bukafc42b312015-07-24 14:04:31 -0700141TEST_F(DBusCommandProxyTest, OnProgressChanged) {
142 EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1);
143 EXPECT_CALL(command_, MockGetProgress())
144 .WillOnce(ReturnRefOfCopy<std::string>("{'progress': 10}"));
145 proxy_->OnProgressChanged();
146 EXPECT_EQ((VariantDictionary{{"progress", int32_t{10}}}),
147 GetCommandAdaptor()->GetProgress());
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700148}
149
Vitaly Bukafc42b312015-07-24 14:04:31 -0700150TEST_F(DBusCommandProxyTest, OnResultsChanged) {
151 EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1);
152 EXPECT_CALL(command_, MockGetResults())
153 .WillOnce(ReturnRefOfCopy<std::string>(
154 "{'foo': 42, 'bar': 'foobar', 'resultList': [1, 2, 3]}"));
155 proxy_->OnResultsChanged();
156
157 EXPECT_EQ((VariantDictionary{{"foo", int32_t{42}},
158 {"bar", std::string{"foobar"}},
159 {"resultList", std::vector<int>{1, 2, 3}}}),
160 GetCommandAdaptor()->GetResults());
161}
162
163TEST_F(DBusCommandProxyTest, OnStatusChanged) {
164 EXPECT_CALL(*mock_exported_object_command_, SendSignal(_)).Times(1);
165 EXPECT_CALL(command_, GetStatus())
Vitaly Bukaf9d50292015-07-27 16:08:51 -0700166 .WillOnce(Return(weave::CommandStatus::kInProgress));
Vitaly Bukafc42b312015-07-24 14:04:31 -0700167 proxy_->OnStatusChanged();
Vitaly Bukaf9d50292015-07-27 16:08:51 -0700168 EXPECT_EQ(weave::CommandStatus::kInProgress, GetCommandStatus());
Vitaly Bukafc42b312015-07-24 14:04:31 -0700169}
170
171TEST_F(DBusCommandProxyTest, SetProgress) {
172 EXPECT_CALL(command_, SetProgress(EqualToJson("{'progress': 10}"), _))
173 .WillOnce(Return(true));
174 EXPECT_TRUE(
175 GetCommandInterface()->SetProgress(nullptr, {{"progress", int32_t{10}}}));
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700176}
177
Anton Muhincfde8692014-11-25 03:36:59 +0400178TEST_F(DBusCommandProxyTest, SetResults) {
Vitaly Bukafc42b312015-07-24 14:04:31 -0700179 EXPECT_CALL(
180 command_,
181 SetResults(
182 EqualToJson("{'foo': 42, 'bar': 'foobar', 'resultList': [1, 2, 3]}"),
183 _))
184 .WillOnce(Return(true));
185 EXPECT_TRUE(GetCommandInterface()->SetResults(
186 nullptr, VariantDictionary{{"foo", int32_t{42}},
187 {"bar", std::string{"foobar"}},
188 {"resultList", std::vector<int>{1, 2, 3}}}));
Anton Muhincfde8692014-11-25 03:36:59 +0400189}
190
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700191TEST_F(DBusCommandProxyTest, Abort) {
Vitaly Bukafc42b312015-07-24 14:04:31 -0700192 EXPECT_CALL(command_, Abort());
Alex Vakulenko420e49f2014-12-01 17:53:27 -0800193 GetCommandInterface()->Abort();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700194}
195
196TEST_F(DBusCommandProxyTest, Cancel) {
Vitaly Bukafc42b312015-07-24 14:04:31 -0700197 EXPECT_CALL(command_, Cancel());
Alex Vakulenko420e49f2014-12-01 17:53:27 -0800198 GetCommandInterface()->Cancel();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700199}
200
201TEST_F(DBusCommandProxyTest, Done) {
Vitaly Bukafc42b312015-07-24 14:04:31 -0700202 EXPECT_CALL(command_, Done());
Alex Vakulenko420e49f2014-12-01 17:53:27 -0800203 GetCommandInterface()->Done();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700204}
205
Vitaly Bukaf9d50292015-07-27 16:08:51 -0700206} // namespace buffet