1 /*
2  * Copyright (C) 2018 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 #include "aidl_test_client.h"
17 #include "gmock/gmock.h"
18 
19 using android::binder::Status;
20 
21 using android::aidl::tests::ITestService;
22 using android::aidl::tests::ITestServiceDefault;
23 
24 using testing::Eq;
25 
26 static constexpr int32_t kExpectedArgValue = 100;
27 static constexpr int32_t kExpectedReturnValue = 200;
28 
29 static int numCalled = 0;
30 static int32_t gotArgument = 0;
31 
32 struct Def : public ITestServiceDefault {
UnimplementedMethodDef33   Status UnimplementedMethod(int32_t arg, int32_t* _aidl_return) override {
34     numCalled++;
35     gotArgument = arg;
36     *_aidl_return = kExpectedReturnValue;
37     return android::binder::Status::ok();
38   }
39 };
40 
41 /* Make sure the unimplementedMethod behaves as expected before testing the
42  * default impl
43  */
TEST_F(AidlTest,unimplementedMethodImpl)44 TEST_F(AidlTest, unimplementedMethodImpl) {
45   int32_t returned_value;
46   auto status = service->UnimplementedMethod(kExpectedArgValue, &returned_value);
47   ASSERT_FALSE(status.isOk()) << status;
48   ASSERT_EQ(status.exceptionCode(), android::binder::Status::EX_TRANSACTION_FAILED);
49   EXPECT_EQ(status.transactionError(), android::UNKNOWN_TRANSACTION);
50 }
51 
TEST_F(AidlTest,defaultImpl)52 TEST_F(AidlTest, defaultImpl) {
53   android::sp<ITestService> defImpl = android::sp<Def>::make();
54   auto ret = ITestService::setDefaultImpl(std::move(defImpl));
55   ASSERT_TRUE(ret);
56 
57   int32_t returned_value;
58   auto status = service->UnimplementedMethod(kExpectedArgValue, &returned_value);
59   ASSERT_TRUE(status.isOk()) << status;
60   ASSERT_THAT(numCalled, Eq(1));
61   ASSERT_THAT(gotArgument, Eq(kExpectedArgValue));
62   ASSERT_THAT(returned_value, Eq(kExpectedReturnValue));
63 }
64