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