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