blob: 2eaa03e762f64d89c7f70fe6ec8eb3a6db5fadd4 [file] [log] [blame]
Wei Wangc45ef0e2018-09-21 15:44:31 -07001/*
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
nelsonlie203faf2019-10-29 14:56:03 +080021#include <gtest/gtest.h>
22#include <hidl/GtestPrinter.h>
23#include <hidl/ServiceManagement.h>
Wei Wangc45ef0e2018-09-21 15:44:31 -070024
25using ::android::sp;
26using ::android::hardware::hidl_string;
27using ::android::hardware::hidl_vec;
28using ::android::hardware::Return;
29using ::android::hardware::atrace::V1_0::IAtraceDevice;
30using ::android::hardware::atrace::V1_0::Status;
31
Wei Wangc45ef0e2018-09-21 15:44:31 -070032/**
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 */
nelsonlie203faf2019-10-29 14:56:03 +080036struct AtraceHidlTest : public ::testing::TestWithParam<std::string> {
Wei Wangc45ef0e2018-09-21 15:44:31 -070037 virtual void SetUp() override {
nelsonlie203faf2019-10-29 14:56:03 +080038 atrace = IAtraceDevice::getService(GetParam());
Wei Wangc45ef0e2018-09-21 15:44:31 -070039 ASSERT_NE(atrace, nullptr);
40 }
41
42 sp<IAtraceDevice> atrace;
43};
44
45hidl_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. */
nelsonlie203faf2019-10-29 14:56:03 +080070TEST_P(AtraceHidlTest, listCategories) {
Wei Wangc45ef0e2018-09-21 15:44:31 -070071 hidl_vec<hidl_string> vnd_categories = getVendorCategoryName(atrace);
72 EXPECT_NE(0, vnd_categories.size());
73}
74
75/* enable categories. */
nelsonlie203faf2019-10-29 14:56:03 +080076TEST_P(AtraceHidlTest, enableCategories) {
Wei Wangc45ef0e2018-09-21 15:44:31 -070077 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. */
nelsonlie203faf2019-10-29 14:56:03 +080090TEST_P(AtraceHidlTest, disableAllCategories) {
Wei Wangc45ef0e2018-09-21 15:44:31 -070091 auto ret = atrace->disableAllCategories();
92 ASSERT_TRUE(ret.isOk());
93 EXPECT_EQ(Status::SUCCESS, ret);
94}
95
nelsonlie203faf2019-10-29 14:56:03 +080096INSTANTIATE_TEST_SUITE_P(
97 PerInstance, AtraceHidlTest,
98 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IAtraceDevice::descriptor)),
99 android::hardware::PrintInstanceNameToString);