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