1 /*
2 * Copyright (C) 2023 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/Gtest.h>
18 #include <aidl/Vintf.h>
19 #include <aidl/android/frameworks/location/altitude/AddMslAltitudeToLocationRequest.h>
20 #include <aidl/android/frameworks/location/altitude/AddMslAltitudeToLocationResponse.h>
21 #include <aidl/android/frameworks/location/altitude/GetGeoidHeightRequest.h>
22 #include <aidl/android/frameworks/location/altitude/GetGeoidHeightResponse.h>
23 #include <aidl/android/frameworks/location/altitude/IAltitudeService.h>
24 #include <android/binder_manager.h>
25 #include <android/binder_process.h>
26
27 using ::aidl::android::frameworks::location::altitude::AddMslAltitudeToLocationRequest;
28 using ::aidl::android::frameworks::location::altitude::AddMslAltitudeToLocationResponse;
29 using ::aidl::android::frameworks::location::altitude::GetGeoidHeightRequest;
30 using ::aidl::android::frameworks::location::altitude::GetGeoidHeightResponse;
31 using ::aidl::android::frameworks::location::altitude::IAltitudeService;
32 using ::android::getAidlHalInstanceNames;
33 using ::android::PrintInstanceNameToString;
34 using ndk::SpAIBinder;
35 using ::testing::Eq;
36 using ::testing::InitGoogleTest;
37 using ::testing::TestWithParam;
38 using ::testing::ValuesIn;
39
40 class AltitudeServiceTest : public TestWithParam<std::string> {
41 public:
SetUp()42 void SetUp() override {
43 SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str()));
44 service = IAltitudeService::fromBinder(binder);
45 ASSERT_NE(service, nullptr);
46 }
47 std::shared_ptr<IAltitudeService> service;
48 };
49
TEST_P(AltitudeServiceTest,TestAddMslAltitudeToLocation)50 TEST_P(AltitudeServiceTest, TestAddMslAltitudeToLocation) {
51 // Test known location near Hawaii.
52 AddMslAltitudeToLocationRequest request;
53 request.latitudeDegrees = 19.545519;
54 request.longitudeDegrees = -155.998774;
55 request.altitudeMeters = -1;
56 request.verticalAccuracyMeters = 1;
57
58 AddMslAltitudeToLocationResponse response;
59 service->addMslAltitudeToLocation(request, &response);
60 ASSERT_NEAR(response.mslAltitudeMeters, -19.2359, 2);
61 ASSERT_NEAR(response.mslAltitudeAccuracyMeters, 1.05f, 0.5f);
62 int32_t ver = 0;
63 auto ret = service->getInterfaceVersion(&ver);
64 ASSERT_TRUE(ret.isOk()) << ret;
65 if (ver >= 2) {
66 // In V2 we now have a "success" boolean in the response.
67 ASSERT_TRUE(response.success);
68 }
69 }
70
TEST_P(AltitudeServiceTest,TestGetGeoidHeight)71 TEST_P(AltitudeServiceTest, TestGetGeoidHeight) {
72 int32_t ver = 0;
73 auto ret = service->getInterfaceVersion(&ver);
74 ASSERT_TRUE(ret.isOk()) << ret;
75 if (ver < 2) GTEST_SKIP() << "getGeoidHeight is introduced in V2";
76 // Test known location near Hawaii.
77 GetGeoidHeightRequest request;
78 request.latitudeDegrees = 19.545519;
79 request.longitudeDegrees = -155.998774;
80
81 GetGeoidHeightResponse response;
82 service->getGeoidHeight(request, &response);
83 ASSERT_NEAR(response.geoidHeightMeters, 18.2359, 2);
84 ASSERT_NEAR(response.geoidHeightErrorMeters, 0.27f, 0.2f);
85 ASSERT_NEAR(response.expirationDistanceMeters, 2828, 1000);
86 ASSERT_NEAR(response.additionalGeoidHeightErrorMeters, 0.707f, 0.5f);
87 ASSERT_TRUE(response.success);
88 }
89
90 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AltitudeServiceTest);
91 INSTANTIATE_TEST_SUITE_P(AltitudeService, AltitudeServiceTest,
92 ValuesIn(getAidlHalInstanceNames(IAltitudeService::descriptor)),
93 PrintInstanceNameToString);
94
main(int argc,char ** argv)95 int main(int argc, char** argv) {
96 InitGoogleTest(&argc, argv);
97 ABinderProcess_setThreadPoolMaxThreadCount(1);
98 ABinderProcess_startThreadPool();
99 return RUN_ALL_TESTS();
100 }
101