1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 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 <climits> 17 #include <gtest/gtest.h> 18 #include <iostream> 19 #include <unistd.h> 20 21 #include "cpu_collector_client.h" 22 23 using namespace testing::ext; 24 using namespace OHOS::HiviewDFX; 25 using namespace OHOS::HiviewDFX::UCollectClient; 26 using namespace OHOS::HiviewDFX::UCollect; 27 28 class CpuCollectorTest : public testing::Test { 29 public: SetUp()30 void SetUp() {}; TearDown()31 void TearDown() {}; SetUpTestCase()32 static void SetUpTestCase() {}; TearDownTestCase()33 static void TearDownTestCase() {}; 34 }; 35 36 #ifdef UNIFIED_COLLECTOR_CPU_ENABLE 37 namespace { 38 constexpr int CPU_USAGE_MIN_VALUE = 0; 39 constexpr int CPU_USAGE_MAX_VALUE = 1; 40 } 41 42 /** 43 * @tc.name: CpuCollectorTest01 44 * @tc.desc: used to test the function of CpuCollector.GetSysCpuUsage; 45 * @tc.type: FUNC 46 */ 47 HWTEST_F(CpuCollectorTest, CpuCollectorTest01, TestSize.Level1) 48 { 49 std::shared_ptr<CpuCollector> collector = CpuCollector::Create(); 50 auto collectResult = collector->GetSysCpuUsage(); 51 ASSERT_TRUE(collectResult.retCode == UcError::SUCCESS); 52 ASSERT_TRUE(collectResult.data >= CPU_USAGE_MIN_VALUE && collectResult.data <= CPU_USAGE_MAX_VALUE); 53 } 54 #else 55 /** 56 * @tc.name: CpuCollectorTest01 57 * @tc.desc: used to test the function of empty CpuCollector 58 * @tc.type: FUNC 59 */ 60 HWTEST_F(CpuCollectorTest, CpuCollectorTest01, TestSize.Level1) 61 { 62 std::shared_ptr<CpuCollector> collector = CpuCollector::Create(); 63 auto collectResult = collector->GetSysCpuUsage(); 64 ASSERT_TRUE(collectResult.retCode == UcError::FEATURE_CLOSED); 65 } 66 #endif 67