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 <VtsHalHidlTargetTestBase.h>
22 #include <VtsHalHidlTargetTestEnvBase.h>
23
24 using ::android::sp;
25 using ::android::hardware::hidl_string;
26 using ::android::hardware::hidl_vec;
27 using ::android::hardware::Return;
28 using ::android::hardware::atrace::V1_0::IAtraceDevice;
29 using ::android::hardware::atrace::V1_0::Status;
30
31 // Test environment for Boot HIDL HAL.
32 class AtraceHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
33 public:
34 // get the test environment singleton
Instance()35 static AtraceHidlEnvironment* Instance() {
36 static AtraceHidlEnvironment* instance = new AtraceHidlEnvironment;
37 return instance;
38 }
39
registerTestServices()40 virtual void registerTestServices() override { registerTestService<IAtraceDevice>(); }
41
42 private:
AtraceHidlEnvironment()43 AtraceHidlEnvironment() {}
44 };
45
46 /**
47 * There is no expected behaviour that can be tested so these tests check the
48 * HAL doesn't crash with different execution orders.
49 */
50 struct AtraceHidlTest : public ::testing::VtsHalHidlTargetTestBase {
SetUpAtraceHidlTest51 virtual void SetUp() override {
52 atrace = ::testing::VtsHalHidlTargetTestBase::getService<IAtraceDevice>(
53 AtraceHidlEnvironment::Instance()->getServiceName<IAtraceDevice>());
54 ASSERT_NE(atrace, nullptr);
55 }
56
57 sp<IAtraceDevice> atrace;
58 };
59
getVendorCategoryName(sp<IAtraceDevice> atrace)60 hidl_vec<hidl_string> getVendorCategoryName(sp<IAtraceDevice> atrace) {
61 std::unordered_set<std::string> categories_set;
62 Return<void> ret = atrace->listCategories([&](const auto& list) {
63 for (const auto& category : list) {
64 std::string name = category.name;
65 if (categories_set.count(name)) {
66 ADD_FAILURE() << "Duplicate category: " << name;
67 } else {
68 categories_set.emplace(name);
69 }
70 }
71 });
72 if (!ret.isOk()) {
73 ADD_FAILURE();
74 }
75 hidl_vec<hidl_string> categories;
76 categories.resize(categories_set.size());
77 std::size_t i = 0;
78 for (auto& c : categories_set) {
79 categories[i++] = c;
80 }
81 return categories;
82 }
83
84 /* list categories from vendors. */
TEST_F(AtraceHidlTest,listCategories)85 TEST_F(AtraceHidlTest, listCategories) {
86 hidl_vec<hidl_string> vnd_categories = getVendorCategoryName(atrace);
87 EXPECT_NE(0, vnd_categories.size());
88 }
89
90 /* enable categories. */
TEST_F(AtraceHidlTest,enableCategories)91 TEST_F(AtraceHidlTest, enableCategories) {
92 hidl_vec<hidl_string> vnd_categories = getVendorCategoryName(atrace);
93 // empty Category with ERROR_INVALID_ARGUMENT
94 hidl_vec<hidl_string> empty_categories;
95 auto ret = atrace->enableCategories(empty_categories);
96 ASSERT_TRUE(ret.isOk());
97 EXPECT_EQ(Status::ERROR_INVALID_ARGUMENT, ret);
98 // non-empty categories SUCCESS
99 ret = atrace->enableCategories(vnd_categories);
100 ASSERT_TRUE(ret.isOk());
101 EXPECT_EQ(Status::SUCCESS, ret);
102 }
103
104 /* enable categories. */
TEST_F(AtraceHidlTest,disableAllCategories)105 TEST_F(AtraceHidlTest, disableAllCategories) {
106 auto ret = atrace->disableAllCategories();
107 ASSERT_TRUE(ret.isOk());
108 EXPECT_EQ(Status::SUCCESS, ret);
109 }
110
main(int argc,char ** argv)111 int main(int argc, char** argv) {
112 ::testing::AddGlobalTestEnvironment(AtraceHidlEnvironment::Instance());
113 ::testing::InitGoogleTest(&argc, argv);
114 AtraceHidlEnvironment::Instance()->init(&argc, argv);
115 int status = RUN_ALL_TESTS();
116 ALOGI("Test result = %d", status);
117 return status;
118 }
119