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 "aidl_test_client.h"
18
19 #include <android/aidl/tests/nested/INestedService.h>
20 #include <binder/IServiceManager.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <utils/String16.h>
24
25 using android::IBinder;
26 using android::sp;
27 using android::String16;
28 using android::aidl::tests::nested::INestedService;
29 using android::aidl::tests::nested::ParcelableWithNested;
30 using android::binder::Status;
31 using NestedResult = android::aidl::tests::nested::INestedService::Result;
32 using NestedStatus = android::aidl::tests::nested::ParcelableWithNested::Status;
33 using std::optional;
34 using std::pair;
35 using std::string;
36 using std::vector;
37 using testing::Eq;
38 using testing::Optional;
39
TEST_F(AidlTest,NestedService)40 TEST_F(AidlTest, NestedService) {
41 sp<INestedService> nestedService =
42 android::waitForService<INestedService>(INestedService::descriptor);
43 ASSERT_NE(nullptr, nestedService);
44
45 ParcelableWithNested p;
46 p.status = NestedStatus::OK;
47 NestedResult r;
48 // OK -> NOT_OK
49 EXPECT_TRUE(nestedService->flipStatus(p, &r).isOk());
50 EXPECT_EQ(r.status, NestedStatus::NOT_OK);
51
52 // NOT_OK -> OK with callback nested interface
53 struct Callback : INestedService::BnCallback {
54 optional<NestedStatus> result;
55 Status done(NestedStatus st) override {
56 result = st;
57 return Status::ok();
58 }
59 };
60 sp<Callback> cb = new Callback;
61 EXPECT_TRUE(nestedService->flipStatusWithCallback(r.status, cb).isOk());
62 EXPECT_THAT(cb->result, Optional(NestedStatus::OK));
63
64 // android::enum_ranges<>
65 vector<NestedStatus> values{android::enum_range<NestedStatus>().begin(),
66 android::enum_range<NestedStatus>().end()};
67 EXPECT_EQ(values, vector<NestedStatus>({NestedStatus::OK, NestedStatus::NOT_OK}));
68
69 // toString()
70 EXPECT_EQ(toString(NestedStatus::NOT_OK), "NOT_OK");
71 }
72