• 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 <gtest/gtest.h>
20 #include <unistd.h>
21 
22 #include <cinttypes>
23 #include <cstdio>
24 #include <cstring>
25 #include <ctime>
26 
27 #include "plugin_module_api.h"
28 
29 using namespace testing::ext;
30 
31 namespace {
32 #if defined(__LP64__)
33 const int US_PER_S = 1000000;
34 const int DEFAULT_WAIT = 9;
35 std::vector<OptimizeReport> g_protoXpower;
WriteFunc(WriterStruct * writer,const void * data,size_t size)36 long WriteFunc(WriterStruct* writer, const void* data, size_t size)
37 {
38     if (writer == nullptr || data == nullptr || size <= 0) {
39         return -1;
40     }
41 
42     OptimizeReport info;
43     if (info.ParseFromArray(data, size) <= 0) {
44         return -1;
45     }
46     g_protoXpower.push_back(info);
47     return 0;
48 }
49 
FlushFunc(WriterStruct * writer)50 bool FlushFunc(WriterStruct* writer)
51 {
52     if (writer == nullptr) {
53         return false;
54     }
55     return true;
56 }
57 #endif
58 
59 class XpowerPluginTest : public ::testing::Test {
60 public:
SetUpTestCase()61     static void SetUpTestCase(){};
TearDownTestCase()62     static void TearDownTestCase(){};
63 
SetUp()64     void SetUp() {}
TearDown()65     void TearDown() {}
66 };
67 
68 /**
69  * @tc.name: xpower plugin
70  * @tc.desc: Framework test
71  * @tc.type: FUNC
72  */
73 HWTEST_F(XpowerPluginTest, TestFramework, TestSize.Level1)
74 {
75 #if defined(__LP64__)
76     std::string path = std::string("libxpowerplugin.z.so");
77     void* handle = dlopen(path.c_str(), RTLD_LAZY);
78     EXPECT_NE(handle, nullptr);
79     PluginModuleStruct* plugin = reinterpret_cast<PluginModuleStruct*>(dlsym(handle, "g_pluginModule"));
80     EXPECT_NE(plugin, nullptr);
81     EXPECT_STREQ(plugin->name, "xpower-plugin");
82     g_protoXpower.clear();
83     // set config
84     XpowerConfig config;
85     config.set_bundle_name("com.ohos.sceneboard");
86     config.add_message_type(XpowerMessageType::REAL_BATTERY);
87     config.add_message_type(XpowerMessageType::APP_STATISTIC);
88     int size = config.ByteSizeLong();
89     ASSERT_GT(size, 0);
90     std::vector<uint8_t> configData(size);
91     ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0);
92     // test framework process
93     WriterStruct writer = {WriteFunc, FlushFunc};
94     std::vector<uint8_t> dataBuffer(plugin->resultBufferSizeHint);
95     EXPECT_EQ(plugin->callbacks->onRegisterWriterStruct(&writer), 0);
96     EXPECT_EQ(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), 0);
97     usleep(US_PER_S * DEFAULT_WAIT); // 9s
98     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(4096);
99     EXPECT_NE(plugin->callbacks->onPluginReportResult(buffer.get(), 4096), -1);
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_EQ(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