1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 18 #include "hitrace_ops.h" 19 #include "trace_ops.h" 20 21 namespace { 22 using FTRACE_NS::HitraceOps; 23 using FTRACE_NS::TraceOps; 24 using testing::ext::TestSize; 25 26 class TraceOpsTest : public ::testing::Test { 27 protected: SetUp()28 void SetUp() override {} TearDown()29 void TearDown() override {} 30 }; 31 32 /* 33 * @tc.name: GetTraceType 34 * @tc.desc: test HitraceOps::GetTraceType with normal case. 35 * @tc.type: FUNC 36 */ 37 HWTEST_F(TraceOpsTest, GetTraceType, TestSize.Level1) 38 { 39 std::unique_ptr<TraceOps> traceOps = std::make_unique<HitraceOps>(); 40 EXPECT_TRUE(traceOps->GetTraceType() == TraceOps::TraceType::HITRACE); 41 } 42 43 /* 44 * @tc.name: GetCommand 45 * @tc.desc: test HitraceOps::GetCommand with normal case. 46 * @tc.type: FUNC 47 */ 48 HWTEST_F(TraceOpsTest, GetCommand, TestSize.Level1) 49 { 50 std::unique_ptr<TraceOps> traceOps = std::make_unique<HitraceOps>(); 51 EXPECT_STREQ(traceOps->GetCommand().c_str(), "/system/bin/hitrace"); 52 } 53 54 /* 55 * @tc.name: HasCategoryNormal 56 * @tc.desc: test HitraceOps::HasCategory with normal case. 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(TraceOpsTest, HasCategoryNormal, TestSize.Level1) 60 { 61 std::unique_ptr<TraceOps> traceOps = std::make_unique<HitraceOps>(); 62 EXPECT_TRUE(traceOps->HasCategory("ability")); 63 EXPECT_TRUE(traceOps->HasCategory("idle")); 64 } 65 66 /* 67 * @tc.name: HasCategoryFalse 68 * @tc.desc: test HitraceOps::HasCategory with false case. 69 * @tc.type: FUNC 70 */ 71 HWTEST_F(TraceOpsTest, HasCategoryFalse, TestSize.Level1) 72 { 73 std::unique_ptr<TraceOps> traceOps = std::make_unique<HitraceOps>(); 74 EXPECT_FALSE(traceOps->HasCategory("12345")); 75 76 traceOps = std::make_unique<TraceOps>("/system/bin/hitrace", "4567", TraceOps::TraceType::HITRACE); 77 EXPECT_FALSE(traceOps->HasCategory("sched")); 78 79 traceOps = std::make_unique<TraceOps>("1234", "hitrace", TraceOps::TraceType::HITRACE); 80 EXPECT_FALSE(traceOps->HasCategory("ability")); 81 82 traceOps = std::make_unique<TraceOps>("/system/bin/hitrace", "4567", TraceOps::TraceType::UNKNOW); 83 EXPECT_FALSE(traceOps->HasCategory("idle")); 84 85 traceOps = std::make_unique<TraceOps>("/system/bin/hitrace", "hitrace", TraceOps::TraceType::UNKNOW); 86 EXPECT_FALSE(traceOps->HasCategory("idle")); 87 } 88 89 /* 90 * @tc.name: EnableCategoriesNormal 91 * @tc.desc: test HitraceOps::EnableCategories with normal case. 92 * @tc.type: FUNC 93 */ 94 HWTEST_F(TraceOpsTest, EnableCategoriesNormal, TestSize.Level1) 95 { 96 std::unique_ptr<TraceOps> traceOps = std::make_unique<HitraceOps>(); 97 std::vector<std::string> categories; 98 categories.push_back("sched"); 99 traceOps->DisableCategories(); 100 EXPECT_TRUE(traceOps->EnableCategories(categories)); 101 categories.push_back("ability"); 102 traceOps->DisableCategories(); 103 EXPECT_TRUE(traceOps->EnableCategories(categories)); 104 categories.push_back("idle"); 105 traceOps->DisableCategories(); 106 EXPECT_TRUE(traceOps->EnableCategories(categories)); 107 } 108 109 /* 110 * @tc.name: EnableCategoriesFalse 111 * @tc.desc: test HitraceOps::EnableCategories with false case. 112 * @tc.type: FUNC 113 */ 114 HWTEST_F(TraceOpsTest, EnableCategoriesFalse, TestSize.Level1) 115 { 116 std::unique_ptr<TraceOps> traceOps = std::make_unique<HitraceOps>(); 117 std::vector<std::string> categories; 118 EXPECT_FALSE(traceOps->EnableCategories(categories)); 119 120 categories.push_back("sched"); 121 traceOps = std::make_unique<TraceOps>("/system/bin/hitrace", "4567", TraceOps::TraceType::HITRACE); 122 EXPECT_FALSE(traceOps->EnableCategories(categories)); 123 124 categories.push_back("ability"); 125 traceOps = std::make_unique<TraceOps>("1234", "hitrace", TraceOps::TraceType::HITRACE); 126 EXPECT_FALSE(traceOps->EnableCategories(categories)); 127 128 categories.push_back("idle"); 129 traceOps = std::make_unique<TraceOps>("/system/bin/hitrace", "4567", TraceOps::TraceType::UNKNOW); 130 EXPECT_FALSE(traceOps->EnableCategories(categories)); 131 132 categories.push_back("1234"); 133 traceOps = std::make_unique<TraceOps>("/system/bin/hitrace", "hitrace", TraceOps::TraceType::UNKNOW); 134 EXPECT_FALSE(traceOps->EnableCategories(categories)); 135 } 136 137 /* 138 * @tc.name: DisableCategoriesNormal 139 * @tc.desc: test HitraceOps::DisableCategories with normal case. 140 * @tc.type: FUNC 141 */ 142 HWTEST_F(TraceOpsTest, DisableCategoriesNormal, TestSize.Level1) 143 { 144 std::unique_ptr<TraceOps> traceOps = std::make_unique<HitraceOps>(); 145 EXPECT_TRUE(traceOps->DisableCategories()); 146 } 147 148 /* 149 * @tc.name: DisableCategoriesFalse 150 * @tc.desc: test HitraceOps::DisableCategories with false case. 151 * @tc.type: FUNC 152 */ 153 HWTEST_F(TraceOpsTest, DisableCategoriesFalse, TestSize.Level1) 154 { 155 std::unique_ptr<TraceOps> traceOps = std::make_unique<TraceOps>("1234", "hitrace", TraceOps::TraceType::HITRACE); 156 EXPECT_FALSE(traceOps->DisableCategories()); 157 158 traceOps = std::make_unique<TraceOps>("/system/bin/hitrace", "4567", TraceOps::TraceType::UNKNOW); 159 EXPECT_FALSE(traceOps->DisableCategories()); 160 } 161 162 /* 163 * @tc.name: IsSupportedNormal 164 * @tc.desc: test HitraceOps::IsSupported with normal case. 165 * @tc.type: FUNC 166 */ 167 HWTEST_F(TraceOpsTest, IsSupportedNormal, TestSize.Level1) 168 { 169 std::unique_ptr<TraceOps> traceOps = std::make_unique<HitraceOps>(); 170 EXPECT_TRUE(traceOps->IsSupported()); 171 } 172 173 /* 174 * @tc.name: IsSupportedFalse 175 * @tc.desc: test HitraceOps::IsSupported with false case. 176 * @tc.type: FUNC 177 */ 178 HWTEST_F(TraceOpsTest, IsSupportedFalse, TestSize.Level1) 179 { 180 std::unique_ptr<TraceOps> traceOps = std::make_unique<TraceOps>("1234", "hitrace", TraceOps::TraceType::HITRACE); 181 EXPECT_FALSE(traceOps->IsSupported()); 182 183 traceOps = std::make_unique<TraceOps>("/system/bin/hitrace", "4567", TraceOps::TraceType::UNKNOW); 184 EXPECT_TRUE(traceOps->IsSupported()); 185 186 traceOps = std::make_unique<TraceOps>("1234", "4567", TraceOps::TraceType::HITRACE); 187 EXPECT_FALSE(traceOps->IsSupported()); 188 189 traceOps = std::make_unique<TraceOps>("1234", "4567", TraceOps::TraceType::UNKNOW); 190 EXPECT_FALSE(traceOps->IsSupported()); 191 } 192 } // namespace 193