• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. 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 #include "xpower_plugin.h"
16 
17 #include <dlfcn.h>
18 #include <fcntl.h>
19 #include <hwext/gtest-ext.h>
20 #include <hwext/gtest-tag.h>
21 #include <unistd.h>
22 
23 #include <cinttypes>
24 #include <cstdio>
25 #include <cstring>
26 #include <ctime>
27 
28 #include "plugin_module_api.h"
29 
30 using namespace testing::ext;
31 
32 namespace {
33 #if defined(__LP64__)
34 const std::string DEFAULT_TEST_PATH("/system/lib64/");
35 const int US_PER_S = 1000000;
36 const int DEFAULT_WAIT = 9;
37 std::vector<OptimizeReport> g_protoXpower;
WriteFunc(WriterStruct * writer,const void * data,size_t size)38 long WriteFunc(WriterStruct* writer, const void* data, size_t size)
39 {
40     if (writer == nullptr || data == nullptr || size <= 0) {
41         return -1;
42     }
43 
44     OptimizeReport info;
45     if (info.ParseFromArray(data, size) <= 0) {
46         return -1;
47     }
48     g_protoXpower.push_back(info);
49     return 0;
50 }
51 
FlushFunc(WriterStruct * writer)52 bool FlushFunc(WriterStruct* writer)
53 {
54     if (writer == nullptr) {
55         return false;
56     }
57     return true;
58 }
59 #endif
60 
61 class XpowerPluginTest : public ::testing::Test {
62 public:
SetUpTestCase()63     static void SetUpTestCase(){};
TearDownTestCase()64     static void TearDownTestCase(){};
65 
SetUp()66     void SetUp() {}
TearDown()67     void TearDown() {}
68 };
69 
70 /**
71  * @tc.name: xpower plugin
72  * @tc.desc: Framework test
73  * @tc.type: FUNC
74  */
75 HWTEST_F(XpowerPluginTest, TestFramework, TestSize.Level1)
76 {
77 #if defined(__LP64__)
78     std::string path = DEFAULT_TEST_PATH + std::string("libxpowerplugin.z.so");
79     void* handle = dlopen(path.c_str(), RTLD_LAZY);
80     EXPECT_NE(handle, nullptr);
81     PluginModuleStruct* plugin = reinterpret_cast<PluginModuleStruct*>(dlsym(handle, "g_pluginModule"));
82     EXPECT_NE(plugin, nullptr);
83     EXPECT_STREQ(plugin->name, "xpower-plugin");
84     g_protoXpower.clear();
85     // set config
86     XpowerConfig config;
87     config.set_bundle_name("com.ohos.sceneboard");
88     config.add_message_type(XpowerMessageType::REAL_BATTERY);
89     config.add_message_type(XpowerMessageType::APP_STATISTIC);
90     int size = config.ByteSizeLong();
91     ASSERT_GT(size, 0);
92     std::vector<uint8_t> configData(size);
93     ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0);
94     // test framework process
95     WriterStruct writer = {WriteFunc, FlushFunc};
96     std::vector<uint8_t> dataBuffer(plugin->resultBufferSizeHint);
97     EXPECT_EQ(plugin->callbacks->onRegisterWriterStruct(&writer), 0);
98     EXPECT_EQ(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), 0);
99     usleep(US_PER_S * DEFAULT_WAIT); // 9s
100     EXPECT_EQ(plugin->callbacks->onPluginSessionStop(), 0);
101 
102     // test proto data
103     int vectSize = g_protoXpower.size();
104     EXPECT_TRUE(vectSize >= 0);
105 #endif
106 }
107 
108 /**
109  * @tc.name: xpower plugin
110  * @tc.desc: start fail test
111  * @tc.type: FUNC
112  * @tc.require: issueI5UGTK
113  */
114 HWTEST_F(XpowerPluginTest, TestStartFail, TestSize.Level1)
115 {
116 #if defined(__LP64__)
117     XpowerConfig config;
118     XpowerPlugin plugin;
119     WriterStruct writer = {WriteFunc, FlushFunc};
120     // set config
121     config.set_bundle_name("");
122     config.add_message_type(XpowerMessageType::REAL_BATTERY);
123     // test plugin process
124     plugin.SetWriter(&writer);
125     // serialize
126     int size = config.ByteSizeLong();
127     ASSERT_GT(size, 0);
128     std::vector<uint8_t> configData(size);
129     ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0);
130     // start
131     EXPECT_NE(plugin.Start(configData.data(), configData.size()), 0);
132     EXPECT_NE(plugin.Start(nullptr, configData.size()), 0);
133     EXPECT_NE(plugin.Start(configData.data(), 0), 0);
134 #endif
135 }
136 
137 /**
138  * @tc.name: xpower plugin
139  * @tc.desc: message queue test
140  * @tc.type: FUNC
141  * @tc.require: issueI5UGTK
142  */
143 HWTEST_F(XpowerPluginTest, TestMessageQueue, TestSize.Level1)
144 {
145     const int msgQueueSize = 2000;
146     std::unique_ptr<PowerMessageQueue> dataQueuePtr = std::make_unique<PowerMessageQueue>(msgQueueSize);
147     auto rawData = std::make_shared<PowerOptimizeData>();
148     rawData->messageType = OptimizeMessageType::MESSAGE_REAL_BATTERY;
149     dataQueuePtr->PushBack(rawData);
150     EXPECT_GE(dataQueuePtr->Size(), 0);
151     EXPECT_TRUE(!dataQueuePtr->Empty());
152     std::shared_ptr<PowerOptimizeData> result = nullptr;
153     const uint32_t waitDuration = 100;
154     EXPECT_TRUE(dataQueuePtr->WaitAndPop(result, std::chrono::milliseconds(waitDuration)));
155     EXPECT_TRUE(result != nullptr);
156     const int batchSize = 5;
157     std::vector<std::shared_ptr<PowerOptimizeData>> araryData(batchSize); // 5: the size of std::vector;
158     EXPECT_FALSE(dataQueuePtr->WaitAndPopBatch(araryData, std::chrono::milliseconds(waitDuration), batchSize));
159     for (size_t i = 0; i < 3; i++) {
160         auto rawData = std::make_shared<PowerOptimizeData>();
161         rawData->messageType = OptimizeMessageType::MESSAGE_REAL_BATTERY;
162         rawData->length = i;
163         dataQueuePtr->PushBack(rawData);
164     }
165     EXPECT_TRUE(dataQueuePtr->WaitAndPopBatch(araryData, std::chrono::milliseconds(waitDuration), batchSize));
166     dataQueuePtr->ShutDown();
167 }
168 
169 } // namespace
170