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