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 17 #include <unordered_set> 18 19 #include <android/hardware/atrace/1.0/IAtraceDevice.h> 20 21 #include <gtest/gtest.h> 22 #include <hidl/GtestPrinter.h> 23 #include <hidl/ServiceManagement.h> 24 25 using ::android::sp; 26 using ::android::hardware::hidl_string; 27 using ::android::hardware::hidl_vec; 28 using ::android::hardware::Return; 29 using ::android::hardware::atrace::V1_0::IAtraceDevice; 30 using ::android::hardware::atrace::V1_0::Status; 31 32 /** 33 * There is no expected behaviour that can be tested so these tests check the 34 * HAL doesn't crash with different execution orders. 35 */ 36 struct AtraceHidlTest : public ::testing::TestWithParam<std::string> { 37 virtual void SetUp() override { 38 atrace = IAtraceDevice::getService(GetParam()); 39 ASSERT_NE(atrace, nullptr); 40 } 41 42 sp<IAtraceDevice> atrace; 43 }; 44 45 hidl_vec<hidl_string> getVendorCategoryName(sp<IAtraceDevice> atrace) { 46 std::unordered_set<std::string> categories_set; 47 Return<void> ret = atrace->listCategories([&](const auto& list) { 48 for (const auto& category : list) { 49 std::string name = category.name; 50 if (categories_set.count(name)) { 51 ADD_FAILURE() << "Duplicate category: " << name; 52 } else { 53 categories_set.emplace(name); 54 } 55 } 56 }); 57 if (!ret.isOk()) { 58 ADD_FAILURE(); 59 } 60 hidl_vec<hidl_string> categories; 61 categories.resize(categories_set.size()); 62 std::size_t i = 0; 63 for (auto& c : categories_set) { 64 categories[i++] = c; 65 } 66 return categories; 67 } 68 69 /* list categories from vendors. */ 70 TEST_P(AtraceHidlTest, listCategories) { 71 hidl_vec<hidl_string> vnd_categories = getVendorCategoryName(atrace); 72 EXPECT_NE(0, vnd_categories.size()); 73 } 74 75 /* enable categories. */ 76 TEST_P(AtraceHidlTest, enableCategories) { 77 hidl_vec<hidl_string> vnd_categories = getVendorCategoryName(atrace); 78 // empty Category with ERROR_INVALID_ARGUMENT 79 hidl_vec<hidl_string> empty_categories; 80 auto ret = atrace->enableCategories(empty_categories); 81 ASSERT_TRUE(ret.isOk()); 82 EXPECT_EQ(Status::ERROR_INVALID_ARGUMENT, ret); 83 // non-empty categories SUCCESS 84 ret = atrace->enableCategories(vnd_categories); 85 ASSERT_TRUE(ret.isOk()); 86 EXPECT_EQ(Status::SUCCESS, ret); 87 } 88 89 /* enable categories. */ 90 TEST_P(AtraceHidlTest, disableAllCategories) { 91 auto ret = atrace->disableAllCategories(); 92 ASSERT_TRUE(ret.isOk()); 93 EXPECT_EQ(Status::SUCCESS, ret); 94 } 95 96 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AtraceHidlTest); 97 INSTANTIATE_TEST_SUITE_P( 98 PerInstance, AtraceHidlTest, 99 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IAtraceDevice::descriptor)), 100 android::hardware::PrintInstanceNameToString); 101