1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ConnectedClient.h"
18 #include "MockVehicleCallback.h"
19 
20 #include <aidl/android/hardware/automotive/vehicle/IVehicleCallback.h>
21 
22 #include <gtest/gtest.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace automotive {
27 namespace vehicle {
28 
29 using ::aidl::android::hardware::automotive::vehicle::GetValueResult;
30 using ::aidl::android::hardware::automotive::vehicle::GetValueResults;
31 using ::aidl::android::hardware::automotive::vehicle::IVehicleCallback;
32 using ::aidl::android::hardware::automotive::vehicle::SetValueResult;
33 using ::aidl::android::hardware::automotive::vehicle::SetValueResults;
34 using ::aidl::android::hardware::automotive::vehicle::StatusCode;
35 using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
36 
37 class ConnectedClientTest : public testing::Test {
38   public:
SetUp()39     void SetUp() override {
40         mCallback = ndk::SharedRefBase::make<MockVehicleCallback>();
41         mCallbackClient = IVehicleCallback::fromBinder(mCallback->asBinder());
42         // timeout: 1s.
43         int64_t timeout = 1000000000;
44         mPool = std::make_shared<PendingRequestPool>(timeout);
45     }
46 
getCallbackClient()47     std::shared_ptr<IVehicleCallback> getCallbackClient() { return mCallbackClient; }
48 
getCallback()49     MockVehicleCallback* getCallback() { return mCallback.get(); }
50 
getPool()51     std::shared_ptr<PendingRequestPool> getPool() { return mPool; }
52 
53   protected:
54     using GetValuesClient = GetSetValuesClient<GetValueResult, GetValueResults>;
55     using SetValuesClient = GetSetValuesClient<SetValueResult, SetValueResults>;
56 
57   private:
58     std::shared_ptr<MockVehicleCallback> mCallback;
59     std::shared_ptr<IVehicleCallback> mCallbackClient;
60     std::shared_ptr<PendingRequestPool> mPool;
61 };
62 
TEST_F(ConnectedClientTest,testSendGetValueResults)63 TEST_F(ConnectedClientTest, testSendGetValueResults) {
64     std::vector<GetValueResult> results = {{
65                                                    .requestId = 0,
66                                                    .status = StatusCode::OK,
67                                                    .prop =
68                                                            VehiclePropValue{
69                                                                    .prop = 0,
70                                                            },
71                                            },
72                                            {
73                                                    .requestId = 1,
74                                                    .status = StatusCode::OK,
75                                                    .prop =
76                                                            VehiclePropValue{
77                                                                    .prop = 1,
78                                                            },
79                                            }};
80 
81     GetValuesClient client(getPool(), getCallbackClient());
82 
83     auto resultsCopy = results;
84     client.sendResults(std::move(resultsCopy));
85 
86     auto maybeGetValueResults = getCallback()->nextGetValueResults();
87     ASSERT_TRUE(maybeGetValueResults.has_value());
88     ASSERT_EQ(maybeGetValueResults.value().payloads, results);
89 }
90 
TEST_F(ConnectedClientTest,testSendGetValueResultsSeparately)91 TEST_F(ConnectedClientTest, testSendGetValueResultsSeparately) {
92     std::vector<GetValueResult> results = {{
93                                                    .requestId = 0,
94                                                    .status = StatusCode::OK,
95                                                    .prop =
96                                                            VehiclePropValue{
97                                                                    .prop = 0,
98                                                            },
99                                            },
100                                            {
101                                                    .requestId = 1,
102                                                    .status = StatusCode::OK,
103                                                    .prop =
104                                                            VehiclePropValue{
105                                                                    .prop = 1,
106                                                            },
107                                            }};
108 
109     GetValuesClient client(getPool(), getCallbackClient());
110 
111     client.sendResultsSeparately(results);
112 
113     for (auto& result : results) {
114         auto maybeGetValueResults = getCallback()->nextGetValueResults();
115         EXPECT_TRUE(maybeGetValueResults.has_value());
116         if (!maybeGetValueResults.has_value()) {
117             continue;
118         }
119         EXPECT_EQ(maybeGetValueResults.value().payloads, std::vector<GetValueResult>({result}));
120     }
121 }
122 
TEST_F(ConnectedClientTest,testGetValuesGnResultCallback)123 TEST_F(ConnectedClientTest, testGetValuesGnResultCallback) {
124     std::vector<GetValueResult> results = {{
125                                                    .requestId = 0,
126                                                    .status = StatusCode::OK,
127                                                    .prop =
128                                                            VehiclePropValue{
129                                                                    .prop = 0,
130                                                            },
131                                            },
132                                            {
133                                                    .requestId = 1,
134                                                    .status = StatusCode::OK,
135                                                    .prop =
136                                                            VehiclePropValue{
137                                                                    .prop = 1,
138                                                            },
139                                            }};
140 
141     GetValuesClient client(getPool(), getCallbackClient());
142 
143     client.addRequests({0, 1});
144 
145     (*(client.getResultCallback()))(results);
146 
147     auto maybeGetValueResults = getCallback()->nextGetValueResults();
148     ASSERT_TRUE(maybeGetValueResults.has_value());
149     ASSERT_EQ(maybeGetValueResults.value().payloads, results);
150 }
151 
TEST_F(ConnectedClientTest,testSendSetValueResults)152 TEST_F(ConnectedClientTest, testSendSetValueResults) {
153     std::vector<SetValueResult> results = {{
154                                                    .requestId = 0,
155                                                    .status = StatusCode::OK,
156                                            },
157                                            {
158                                                    .requestId = 1,
159                                                    .status = StatusCode::OK,
160                                            }};
161 
162     SetValuesClient client(getPool(), getCallbackClient());
163 
164     auto resultsCopy = results;
165     client.sendResults(std::move(resultsCopy));
166 
167     auto maybeSetValueResults = getCallback()->nextSetValueResults();
168     ASSERT_TRUE(maybeSetValueResults.has_value());
169     ASSERT_EQ(maybeSetValueResults.value().payloads, results);
170 }
171 
TEST_F(ConnectedClientTest,testSendSetValueResultsSeparately)172 TEST_F(ConnectedClientTest, testSendSetValueResultsSeparately) {
173     std::vector<SetValueResult> results = {{
174                                                    .requestId = 0,
175                                                    .status = StatusCode::OK,
176                                            },
177                                            {
178                                                    .requestId = 1,
179                                                    .status = StatusCode::OK,
180                                            }};
181 
182     SetValuesClient client(getPool(), getCallbackClient());
183 
184     client.sendResultsSeparately(results);
185 
186     for (auto& result : results) {
187         auto maybeSetValueResults = getCallback()->nextSetValueResults();
188         EXPECT_TRUE(maybeSetValueResults.has_value());
189         if (!maybeSetValueResults.has_value()) {
190             continue;
191         }
192         EXPECT_EQ(maybeSetValueResults.value().payloads, std::vector<SetValueResult>({result}));
193     }
194 }
195 
TEST_F(ConnectedClientTest,testSetValuesGetResultCallback)196 TEST_F(ConnectedClientTest, testSetValuesGetResultCallback) {
197     std::vector<SetValueResult> results = {{
198                                                    .requestId = 0,
199                                                    .status = StatusCode::OK,
200                                            },
201                                            {
202                                                    .requestId = 1,
203                                                    .status = StatusCode::OK,
204                                            }};
205 
206     SetValuesClient client(getPool(), getCallbackClient());
207 
208     client.addRequests({0, 1});
209 
210     (*(client.getResultCallback()))(results);
211 
212     auto maybeSetValueResults = getCallback()->nextSetValueResults();
213     ASSERT_TRUE(maybeSetValueResults.has_value());
214     ASSERT_EQ(maybeSetValueResults.value().payloads, results);
215 }
216 
217 }  // namespace vehicle
218 }  // namespace automotive
219 }  // namespace hardware
220 }  // namespace android
221