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