blob: 0905440a813cbd2028e84cd72839fb6b52735a51 [file] [log] [blame]
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -08001// Copyright 2016 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "buffet/binder_command_proxy.h"
16
17#include <memory>
18
19#include <gtest/gtest.h>
20#include <weave/command.h>
21#include <weave/enum_to_string.h>
22#include <weave/test/mock_command.h>
23#include <weave/test/unittest_utils.h>
24
25#include "common/binder_utils.h"
26
27using weaved::binder_utils::ToString;
28using weaved::binder_utils::ToString16;
29
30namespace buffet {
31
32using ::testing::_;
33using ::testing::Return;
34using ::testing::ReturnRef;
35using ::testing::ReturnRefOfCopy;
36using ::testing::StrictMock;
37
38using weave::test::CreateDictionaryValue;
39using weave::test::IsEqualValue;
40
41namespace {
42
43const char kTestCommandId[] = "cmd_1";
44
45MATCHER_P(EqualToJson, json, "") {
46 auto json_value = CreateDictionaryValue(json);
47 return IsEqualValue(*json_value, arg);
48}
49
50MATCHER_P2(ExpectError, code, message, "") {
51 return arg->GetCode() == code && arg->GetMessage() == message;
52}
53
54} // namespace
55
56class BinderCommandProxyTest : public ::testing::Test {
57 public:
58 void SetUp() override {
59 command_ = std::make_shared<StrictMock<weave::test::MockCommand>>();
60
61 expected_result_dict_.SetInteger("height", 53);
62 expected_result_dict_.SetString("_jumpType", "_withKick");
63 EXPECT_CALL(*command_, GetID())
64 .WillRepeatedly(ReturnRefOfCopy<std::string>(kTestCommandId));
65 EXPECT_CALL(*command_, GetName())
66 .WillRepeatedly(ReturnRefOfCopy<std::string>("robot.jump"));
67 EXPECT_CALL(*command_, GetComponent())
68 .WillRepeatedly(ReturnRefOfCopy<std::string>("myComponent"));
69 EXPECT_CALL(*command_, GetState())
70 .WillRepeatedly(Return(weave::Command::State::kQueued));
71 EXPECT_CALL(*command_, GetOrigin())
72 .WillRepeatedly(Return(weave::Command::Origin::kLocal));
73 EXPECT_CALL(*command_, GetParameters())
74 .WillRepeatedly(ReturnRef(expected_result_dict_));
75 EXPECT_CALL(*command_, GetProgress())
76 .WillRepeatedly(ReturnRef(empty_dict_));
77 EXPECT_CALL(*command_, GetResults())
78 .WillRepeatedly(ReturnRef(empty_dict_));
79
80 proxy_.reset(
81 new BinderCommandProxy{std::weak_ptr<weave::Command>{command_}});
82 }
83
84 BinderCommandProxy* GetCommandProxy() const { return proxy_.get(); }
85
86 weave::Command::State GetCommandState() const {
87 weave::Command::State state;
88 android::String16 state_string;
89 EXPECT_TRUE(GetCommandProxy()->getState(&state_string).isOk());
90 EXPECT_TRUE(StringToEnum(ToString(state_string), &state));
91 return state;
92 }
93
94 weave::Command::Origin GetCommandOrigin() const {
95 weave::Command::Origin origin;
96 android::String16 origin_string;
97 EXPECT_TRUE(GetCommandProxy()->getOrigin(&origin_string).isOk());
98 EXPECT_TRUE(StringToEnum(ToString(origin_string), &origin));
99 return origin;
100 }
101
102 base::DictionaryValue empty_dict_;
103 base::DictionaryValue expected_result_dict_;
104
105 std::shared_ptr<StrictMock<weave::test::MockCommand>> command_;
106 std::unique_ptr<BinderCommandProxy> proxy_;
107};
108
109TEST_F(BinderCommandProxyTest, Init) {
110 android::String16 result;
111 EXPECT_EQ(weave::Command::State::kQueued, GetCommandState());
112 EXPECT_EQ(weave::Command::Origin::kLocal, GetCommandOrigin());
113 EXPECT_TRUE(GetCommandProxy()->getParameters(&result).isOk());
114 EXPECT_EQ(R"({"_jumpType":"_withKick","height":53})", ToString(result));
115 EXPECT_TRUE(GetCommandProxy()->getProgress(&result).isOk());
116 EXPECT_EQ("{}", ToString(result));
117 EXPECT_TRUE(GetCommandProxy()->getResults(&result).isOk());
118 EXPECT_EQ("{}", ToString(result));
119 EXPECT_TRUE(GetCommandProxy()->getName(&result).isOk());
120 EXPECT_EQ("robot.jump", ToString(result));
121 EXPECT_TRUE(GetCommandProxy()->getComponent(&result).isOk());
122 EXPECT_EQ("myComponent", ToString(result));
123 EXPECT_TRUE(GetCommandProxy()->getId(&result).isOk());
124 EXPECT_EQ(kTestCommandId, ToString(result));
125}
126
127TEST_F(BinderCommandProxyTest, SetProgress) {
128 EXPECT_CALL(*command_, SetProgress(EqualToJson("{'progress': 10}"), _))
129 .WillOnce(Return(true));
130 EXPECT_TRUE(
131 GetCommandProxy()->setProgress(ToString16(R"({"progress": 10})")).isOk());
132}
133
134TEST_F(BinderCommandProxyTest, Complete) {
135 EXPECT_CALL(
136 *command_,
137 Complete(
138 EqualToJson("{'foo': 42, 'bar': 'foobar', 'resultList': [1, 2, 3]}"),
139 _))
140 .WillOnce(Return(true));
141 const android::String16 result{
142 R"({"foo": 42, "bar": "foobar", "resultList": [1, 2, 3]})"};
143 EXPECT_TRUE(GetCommandProxy()->complete(result).isOk());
144}
145
146TEST_F(BinderCommandProxyTest, Abort) {
147 EXPECT_CALL(*command_, Abort(ExpectError("foo", "bar"), _))
148 .WillOnce(Return(true));
149 EXPECT_TRUE(
150 GetCommandProxy()->abort(ToString16("foo"), ToString16("bar")).isOk());
151}
152
153TEST_F(BinderCommandProxyTest, Cancel) {
154 EXPECT_CALL(*command_, Cancel(_)).WillOnce(Return(true));
155 EXPECT_TRUE(GetCommandProxy()->cancel().isOk());
156}
157
158TEST_F(BinderCommandProxyTest, Pause) {
159 EXPECT_CALL(*command_, Pause(_)).WillOnce(Return(true));
160 EXPECT_TRUE(GetCommandProxy()->pause().isOk());
161}
162
163} // namespace buffet