1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17
18 #include <iremote_broker.h>
19 #include <iremote_object.h>
20
21 #include "agent_death_recipient.h"
22 #include "color_parser.h"
23 #include "iremote_object_mocker.h"
24 #include "perform_reporter.h"
25 #include "singleton_container.h"
26 #include "surface_reader_handler_impl.h"
27 #include "sys_cap_util.h"
28 #include "wm_common.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace OHOS {
34 namespace Rosen {
35 class TestClass {
36 public:
37 std::string name = "testClass";
38 };
39
40 class UtilsAllTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp() override;
45 void TearDown() override;
46
47 std::map<std::string, int32_t> oldStringMap_;
48 std::map<int32_t, SingletonContainer::Singleton> oldSingletonMap_;
49 std::map<int32_t, std::set<int32_t>> oldDependencySetMap_;
50 };
51
SetUpTestCase()52 void UtilsAllTest::SetUpTestCase() {}
53
TearDownTestCase()54 void UtilsAllTest::TearDownTestCase() {}
55
SetUp()56 void UtilsAllTest::SetUp() {}
57
TearDown()58 void UtilsAllTest::TearDown() {}
59
60 namespace {
61 /**
62 * @tc.name: ADROnRemoteDied01
63 * @tc.desc: test AgentDeathRecipient::OnRemoteDied
64 * @tc.type: FUNC
65 */
66 HWTEST_F(UtilsAllTest, ADROnRemoteDied01, TestSize.Level1)
67 {
68 sptr<AgentDeathRecipient> deathRecipient = new AgentDeathRecipient(nullptr);
69
70 deathRecipient->OnRemoteDied(nullptr);
71
72 sptr<MockIRemoteObject> remoteObj = new MockIRemoteObject();
73 deathRecipient->OnRemoteDied(remoteObj);
74 ASSERT_EQ(0, remoteObj->count_);
75
__anon823e55100202(sptr<IRemoteObject>& remote) 76 deathRecipient->callback_ = [&remoteObj](sptr<IRemoteObject>& remote) { remoteObj->count_ = 1; };
77 deathRecipient->OnRemoteDied(remoteObj);
78 ASSERT_EQ(1, remoteObj->count_);
79 }
80
81 /**
82 * @tc.name: PRCount01
83 * @tc.desc: test PerformReporter::count
84 * @tc.type: FUNC
85 */
86 HWTEST_F(UtilsAllTest, PRCount01, TestSize.Level1)
87 {
88 std::vector<int64_t> timeSpiltsMs = { 0, 1, 2 };
89 PerformReporter reporter = PerformReporter("test", timeSpiltsMs);
90
91 reporter.count(0);
92 ASSERT_EQ(1, reporter.totalCount_);
93 reporter.timeSplitCount_.clear();
94 ASSERT_EQ(1, reporter.totalCount_);
95 }
96
97 /**
98 * @tc.name: SCAddSingleton01
99 * @tc.desc: test SingletonContainer::AddSingleton
100 * @tc.type: FUNC
101 */
102 HWTEST_F(UtilsAllTest, SCAddSingleton01, TestSize.Level1)
103 {
104 auto& singletonContainer = SingletonContainer::GetInstance();
105
106 singletonContainer.AddSingleton("test", nullptr);
107 auto testId = singletonContainer.stringMap["test"];
108 singletonContainer.AddSingleton("test", nullptr);
109 ASSERT_EQ(testId, singletonContainer.stringMap["test"]);
110 singletonContainer.AddSingleton("test2", nullptr);
111 ASSERT_EQ(testId + 1, singletonContainer.stringMap["test2"]);
112
113 auto testId2 = singletonContainer.stringMap["test2"];
114 singletonContainer.singletonMap.erase(testId);
115 singletonContainer.singletonMap.erase(testId2);
116 singletonContainer.stringMap.erase("test");
117 singletonContainer.stringMap.erase("test2");
118 }
119
120 /**
121 * @tc.name: SCSetSingleton01
122 * @tc.desc: test SingletonContainer::AddSingleton
123 * @tc.type: FUNC
124 */
125 HWTEST_F(UtilsAllTest, SCSetSingleton01, TestSize.Level1)
126 {
127 auto& singletonContainer = SingletonContainer::GetInstance();
128
129 TestClass* testObj = new TestClass();
130
131 singletonContainer.SetSingleton("test", testObj);
132 auto testId = singletonContainer.stringMap["test"];
133 auto instance = singletonContainer.GetSingleton("test2");
134 ASSERT_EQ(instance, nullptr);
135
136 instance = singletonContainer.GetSingleton("test");
137 ASSERT_NE(instance, nullptr);
138 ASSERT_EQ(static_cast<TestClass*>(instance)->name, "testClass");
139
140 singletonContainer.SetSingleton("test", nullptr);
141 instance = singletonContainer.GetSingleton("test");
142 ASSERT_EQ(instance, nullptr);
143
144 singletonContainer.singletonMap.erase(testId);
145 singletonContainer.stringMap.erase("test");
146 delete testObj;
147 }
148
149 /**
150 * @tc.name: SCDependOn01
151 * @tc.desc: test SingletonContainer::DependOn
152 * @tc.type: FUNC
153 */
154 HWTEST_F(UtilsAllTest, SCDependOn01, TestSize.Level1)
155 {
156 auto& singletonContainer = SingletonContainer::GetInstance();
157
158 singletonContainer.AddSingleton("test", nullptr);
159
160 ASSERT_EQ(nullptr, singletonContainer.DependOn("test", "test"));
161
162 auto id = singletonContainer.stringMap["test"];
163 auto& testSet = singletonContainer.dependencySetMap[id];
164 ASSERT_EQ(1, testSet.size());
165
166 ASSERT_EQ(nullptr, singletonContainer.DependOn("test", "test"));
167 id = singletonContainer.stringMap["test"];
168 auto& testSet2 = singletonContainer.dependencySetMap[id];
169 ASSERT_EQ(1, testSet2.size());
170
171 singletonContainer.singletonMap.erase(id);
172 singletonContainer.stringMap.erase("test");
173 id = singletonContainer.dependencySetMap.erase(id);
174 }
175
176 /**
177 * @tc.name: SRHOnImageAvailable01
178 * @tc.desc: test SurfaceReaderHandlerImpl::OnImageAvailable
179 * @tc.type: FUNC
180 */
181 HWTEST_F(UtilsAllTest, SRHOnImageAvailable, TestSize.Level1)
182 {
183 sptr<SurfaceReaderHandlerImpl> surfaceReaderHandlerImpl = new (std::nothrow) SurfaceReaderHandlerImpl();
184 surfaceReaderHandlerImpl->flag_ = false;
185 surfaceReaderHandlerImpl->OnImageAvailable(nullptr);
186 ASSERT_EQ(true, surfaceReaderHandlerImpl->flag_);
187 surfaceReaderHandlerImpl->flag_ = true;
188 surfaceReaderHandlerImpl->OnImageAvailable(nullptr);
189 ASSERT_EQ(true, surfaceReaderHandlerImpl->flag_);
190 }
191
192 /**
193 * @tc.name: SRHGetPixelMap01
194 * @tc.desc: test SurfaceReaderHandlerImpl::GetPixelMap
195 * @tc.type: FUNC
196 */
197 HWTEST_F(UtilsAllTest, SRHGetPixelMap, TestSize.Level1)
198 {
199 sptr<SurfaceReaderHandlerImpl> surfaceReaderHandlerImpl = new (std::nothrow) SurfaceReaderHandlerImpl();
200 surfaceReaderHandlerImpl->flag_ = false;
201 surfaceReaderHandlerImpl->GetPixelMap();
202 ASSERT_EQ(false, surfaceReaderHandlerImpl->flag_);
203 surfaceReaderHandlerImpl->flag_ = true;
204 surfaceReaderHandlerImpl->GetPixelMap();
205 }
206
207 /**
208 * @tc.name: SysCapUtilGetClientName
209 * @tc.desc: test SysCapUtil::GetClientName
210 * @tc.type: FUNC
211 */
212 HWTEST_F(UtilsAllTest, SysCapUtilGetClientName, TestSize.Level1)
213 {
214 ASSERT_NE("", SysCapUtil::GetClientName());
215 }
216
217 /**
218 * @tc.name: ConvertErrorToCode
219 * @tc.desc: test ConvertErrorToCode
220 * @tc.type: FUNC
221 */
222 HWTEST_F(UtilsAllTest, ConvertErrorToCode, TestSize.Level1)
223 {
224 EXPECT_EQ(ConvertErrorToCode(WMError::WM_OK), WmErrorCode::WM_OK);
225 EXPECT_EQ(ConvertErrorToCode(WMError::WM_ERROR_FB_RESTORE_MAIN_WINDOW_FAILED),
226 WmErrorCode::WM_ERROR_FB_RESTORE_MAIN_WINDOW_FAILED);
227 WMError error = static_cast<WMError>(-1);
228 EXPECT_EQ(ConvertErrorToCode(error), WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
229 }
230
231 /**
232 * @tc.name: IsValidColorNoAlpha
233 * @tc.desc: test IsValidColorNoAlpha
234 * @tc.type: FUNC
235 */
236 HWTEST_F(UtilsAllTest, IsValidColorNoAlpha, TestSize.Level1)
237 {
238 EXPECT_TRUE(ColorParser::IsValidColorNoAlpha("#008EF5"));
239 EXPECT_TRUE(ColorParser::IsValidColorNoAlpha("#FF008EF5"));
240 EXPECT_FALSE(ColorParser::IsValidColorNoAlpha("InvalidColor"));
241 EXPECT_FALSE(ColorParser::IsValidColorNoAlpha("#009HG5"));
242 EXPECT_FALSE(ColorParser::IsValidColorNoAlpha("##009FF5"));
243 EXPECT_FALSE(ColorParser::IsValidColorNoAlpha("#08EF5"));
244 EXPECT_FALSE(ColorParser::IsValidColorNoAlpha("#80008EF5"));
245 }
246 } // namespace
247 } // namespace Rosen
248 } // namespace OHOS