1 /*
2 * Copyright (c) 2024-2025 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 "cpu_storage_test.h"
17
18 #include <memory>
19
20 #include "cpu_storage.h"
21 #include "file_util.h"
22 #include "rdb_predicates.h"
23 #include "time_util.h"
24 #ifdef POWER_MANAGER_ENABLE
25 #include "power_status_manager.h"
26 #endif
27 #include "gmock/gmock-matchers.h"
28
29 using namespace testing::ext;
30 using namespace OHOS::HiviewDFX;
31 using namespace OHOS::NativeRdb;
32
33 namespace {
34 const std::string CPU_COLLECTION_TABLE_NAME = "unified_collection_cpu";
35 const std::string DB_PATH = "/data/test/cpu_storage";
36 const std::string DB_FILE_DIR = "/cpu/";
37 const std::string DB_FILE_PREIFIX = "cpu_stat_";
38 const std::string DB_FILE_SUFFIX = ".db";
39 const std::string TIME_STAMP_FORMAT = "%Y%m%d";
40 constexpr int32_t DB_VERSION = 2;
41
GenerateDbFileName()42 std::string GenerateDbFileName()
43 {
44 std::string name;
45 std::string formattedTsStr = TimeUtil::TimestampFormatToDate(std::time(nullptr), TIME_STAMP_FORMAT);
46 name.append(DB_FILE_PREIFIX).append(formattedTsStr).append(DB_FILE_SUFFIX);
47 return name;
48 }
49 }
50
SetUpTestCase()51 void CpuStorageTest::SetUpTestCase()
52 {
53 }
54
TearDownTestCase()55 void CpuStorageTest::TearDownTestCase()
56 {
57 FileUtil::ForceRemoveDirectory(DB_PATH);
58 }
59
SetUp()60 void CpuStorageTest::SetUp()
61 {
62 platform.GetPluginMap();
63 }
64
TearDown()65 void CpuStorageTest::TearDown()
66 {
67 }
68
69 /**
70 * @tc.name: CpuStorageTest001
71 * @tc.desc: CpuStorage init test
72 * @tc.type: FUNC
73 * @tc.require: issueI5NULM
74 */
75 HWTEST_F(CpuStorageTest, CpuStorageTest001, TestSize.Level3)
76 {
77 FileUtil::RemoveFile(DB_PATH);
78 std::shared_ptr cpuStorage = std::make_shared<CpuStorage>(DB_PATH);
79 ASSERT_NE(cpuStorage, nullptr);
80 std::string dbFileName = GenerateDbFileName();
81 std::string dbFile = std::string(DB_PATH);
82 dbFile.append(DB_FILE_DIR).append(dbFileName);
83 ASSERT_TRUE(FileUtil::FileExists(dbFile));
84 }
85
86 /**
87 * @tc.name: CpuStorageTest002
88 * @tc.desc: CpuStorage store&report test
89 * @tc.type: FUNC
90 * @tc.require: issueI5NULM
91 */
92 HWTEST_F(CpuStorageTest, CpuStorageTest002, TestSize.Level3)
93 {
94 FileUtil::RemoveFile(DB_PATH);
95 CpuStorage cpuStorage(DB_PATH);
96 ProcessCpuStatInfo processCpuStatInfo = {
97 .pid = 1,
98 .procName = "init",
99 .cpuLoad = 0.0004 // 0.0004 : not meet store condition
100 };
101 cpuStorage.StoreProcessDatas({processCpuStatInfo});
102 cpuStorage.Report();
103 RdbPredicates predicates(CPU_COLLECTION_TABLE_NAME);
104 std::vector<std::string> columns;
105 std::string dbFileName = GenerateDbFileName();
106 std::string dbFile = std::string(DB_PATH);
107 dbFile.append(DB_FILE_DIR).append(dbFileName);
108 RdbStoreConfig config(dbFile);
109 config.SetSecurityLevel(SecurityLevel::S1);
110 auto ret = E_OK;
111 CpuStorageDbCallback callback;
112 auto dbStore = RdbHelper::GetRdbStore(config, DB_VERSION, callback, ret);
113 std::shared_ptr<ResultSet> allVersions = dbStore->Query(predicates, columns);
114 ASSERT_NE(allVersions, nullptr);
115 ASSERT_NE(allVersions->GoToFirstRow(), E_OK);
116 processCpuStatInfo.cpuLoad = 1;
117 cpuStorage.StoreProcessDatas({processCpuStatInfo});
118 allVersions = dbStore->Query(predicates, columns);
119 ASSERT_NE(allVersions, nullptr);
120 ASSERT_EQ(allVersions->GoToFirstRow(), E_OK);
121 }
122
123 /**
124 * @tc.name: CpuStorageTest003
125 * @tc.desc: CpuStorage as RdbOpenCallback
126 * @tc.type: FUNC
127 * @tc.require: issueI5NULM
128 */
129 HWTEST_F(CpuStorageTest, CpuStorageTest003, TestSize.Level3)
130 {
131 FileUtil::RemoveFile(DB_PATH);
132 std::string dbFileName = GenerateDbFileName();
133 std::string dbFile = std::string(DB_PATH);
134 dbFile.append(DB_FILE_DIR).append(dbFileName);
135 RdbStoreConfig config(dbFile);
136 config.SetSecurityLevel(SecurityLevel::S1);
137 auto ret = E_OK;
138 CpuStorageDbCallback callback;
139 auto dbStore = RdbHelper::GetRdbStore(config, DB_VERSION, callback, ret);
140 ASSERT_EQ(ret, E_OK);
141 ret = callback.OnCreate(*dbStore);
142 ASSERT_EQ(ret, E_OK);
143 ret = callback.OnUpgrade(*dbStore, 2, 3); // test db upgrade from version 2 to version 3
144 ASSERT_EQ(ret, E_OK);
145 }
146
147 /**
148 * @tc.name: CpuStorageTest004
149 * @tc.desc: CpuStorage test PowerStatusManager
150 * @tc.type: FUNC
151 * @tc.require: issueI5NULM
152 */
153 #ifdef POWER_MANAGER_ENABLE
154 HWTEST_F(CpuStorageTest, CpuStorageTest004, TestSize.Level3)
155 {
156 int32_t powerState = UCollectUtil::PowerStatusManager::GetInstance().GetPowerState();
157 ASSERT_THAT(powerState, testing::AnyOf(UCollectUtil::SCREEN_OFF, UCollectUtil::SCREEN_ON));
158 UCollectUtil::PowerStatusManager::GetInstance().SetPowerState(UCollectUtil::SCREEN_ON);
159 int32_t powerState2 = UCollectUtil::PowerStatusManager::GetInstance().GetPowerState();
160 ASSERT_EQ(powerState2, UCollectUtil::SCREEN_ON);
161 UCollectUtil::PowerStatusManager::GetInstance().SetPowerState(UCollectUtil::SCREEN_OFF);
162 int32_t powerState3 = UCollectUtil::PowerStatusManager::GetInstance().GetPowerState();
163 ASSERT_EQ(powerState3, UCollectUtil::SCREEN_OFF);
164 }
165 #endif
166