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