Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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/Gtest.h> |
| 17 | #include <aidl/Vintf.h> |
| 18 | |
| 19 | #include <aidl/android/hardware/memtrack/DeviceInfo.h> |
| 20 | #include <aidl/android/hardware/memtrack/IMemtrack.h> |
| 21 | #include <aidl/android/hardware/memtrack/MemtrackType.h> |
| 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
| 24 | |
| 25 | using aidl::android::hardware::memtrack::DeviceInfo; |
| 26 | using aidl::android::hardware::memtrack::IMemtrack; |
| 27 | using aidl::android::hardware::memtrack::MemtrackRecord; |
| 28 | using aidl::android::hardware::memtrack::MemtrackType; |
| 29 | |
| 30 | class MemtrackAidlTest : public testing::TestWithParam<std::string> { |
| 31 | public: |
| 32 | virtual void SetUp() override { |
| 33 | const auto instance = GetParam(); |
| 34 | ASSERT_TRUE(AServiceManager_isDeclared(instance.c_str())); |
| 35 | auto memtrackBinder = ndk::SpAIBinder(AServiceManager_waitForService(instance.c_str())); |
| 36 | memtrack_ = IMemtrack::fromBinder(memtrackBinder); |
| 37 | ASSERT_NE(memtrack_, nullptr); |
| 38 | } |
| 39 | |
| 40 | std::shared_ptr<IMemtrack> memtrack_; |
| 41 | }; |
| 42 | |
| 43 | TEST_P(MemtrackAidlTest, GetMemoryInvalidPid) { |
| 44 | int pid = -1; |
| 45 | MemtrackType type = MemtrackType::OTHER; |
| 46 | std::vector<MemtrackRecord> records; |
| 47 | |
| 48 | auto status = memtrack_->getMemory(pid, type, &records); |
| 49 | |
| 50 | EXPECT_EQ(status.getExceptionCode(), EX_ILLEGAL_ARGUMENT); |
| 51 | } |
| 52 | |
| 53 | TEST_P(MemtrackAidlTest, GetMemoryInvalidType) { |
| 54 | int pid = 1; |
| 55 | MemtrackType type = MemtrackType::NUM_TYPES; |
| 56 | std::vector<MemtrackRecord> records; |
| 57 | |
| 58 | auto status = memtrack_->getMemory(pid, type, &records); |
| 59 | |
| 60 | EXPECT_EQ(status.getExceptionCode(), EX_UNSUPPORTED_OPERATION); |
| 61 | } |
| 62 | |
| 63 | TEST_P(MemtrackAidlTest, GetMemory) { |
| 64 | int pid = 1; |
| 65 | MemtrackType type = MemtrackType::OTHER; |
| 66 | std::vector<MemtrackRecord> records; |
| 67 | |
| 68 | auto status = memtrack_->getMemory(pid, type, &records); |
| 69 | |
| 70 | EXPECT_TRUE(status.isOk()); |
| 71 | } |
| 72 | |
| 73 | TEST_P(MemtrackAidlTest, GetGpuDeviceInfo) { |
| 74 | std::vector<DeviceInfo> device_info; |
| 75 | |
| 76 | auto status = memtrack_->getGpuDeviceInfo(&device_info); |
| 77 | |
| 78 | EXPECT_TRUE(status.isOk()); |
| 79 | } |
| 80 | |
| 81 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MemtrackAidlTest); |
| 82 | INSTANTIATE_TEST_SUITE_P(PerInstance, MemtrackAidlTest, |
| 83 | testing::ValuesIn(android::getAidlHalInstanceNames(IMemtrack::descriptor)), |
| 84 | android::PrintInstanceNameToString); |
| 85 | |
| 86 | int main(int argc, char** argv) { |
| 87 | ::testing::InitGoogleTest(&argc, argv); |
| 88 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 89 | ABinderProcess_startThreadPool(); |
| 90 | return RUN_ALL_TESTS(); |
| 91 | } |