1e0dac50fSopenharmony_ci/* 2e0dac50fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License. 5e0dac50fSopenharmony_ci * You may obtain a copy of the License at 6e0dac50fSopenharmony_ci * 7e0dac50fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e0dac50fSopenharmony_ci * 9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and 13e0dac50fSopenharmony_ci * limitations under the License. 14e0dac50fSopenharmony_ci */ 15e0dac50fSopenharmony_ci 16e0dac50fSopenharmony_ci#include <gtest/gtest.h> 17e0dac50fSopenharmony_ci 18e0dac50fSopenharmony_ci#include <iremote_broker.h> 19e0dac50fSopenharmony_ci#include <iremote_object.h> 20e0dac50fSopenharmony_ci 21e0dac50fSopenharmony_ci#include "agent_death_recipient.h" 22e0dac50fSopenharmony_ci#include "iremote_object_mocker.h" 23e0dac50fSopenharmony_ci#include "singleton_container.h" 24e0dac50fSopenharmony_ci#include "perform_reporter.h" 25e0dac50fSopenharmony_ci#include "surface_reader_handler_impl.h" 26e0dac50fSopenharmony_ci#include "sys_cap_util.h" 27e0dac50fSopenharmony_ci 28e0dac50fSopenharmony_ciusing namespace testing; 29e0dac50fSopenharmony_ciusing namespace testing::ext; 30e0dac50fSopenharmony_ci 31e0dac50fSopenharmony_cinamespace OHOS { 32e0dac50fSopenharmony_cinamespace Rosen { 33e0dac50fSopenharmony_ciclass TestClass { 34e0dac50fSopenharmony_cipublic: 35e0dac50fSopenharmony_ci std::string name = "testClass"; 36e0dac50fSopenharmony_ci}; 37e0dac50fSopenharmony_ci 38e0dac50fSopenharmony_ciclass UtilsAllTest : public testing::Test { 39e0dac50fSopenharmony_cipublic: 40e0dac50fSopenharmony_ci static void SetUpTestCase(); 41e0dac50fSopenharmony_ci static void TearDownTestCase(); 42e0dac50fSopenharmony_ci void SetUp() override; 43e0dac50fSopenharmony_ci void TearDown() override; 44e0dac50fSopenharmony_ci 45e0dac50fSopenharmony_ci std::map<std::string, int32_t> oldStringMap_; 46e0dac50fSopenharmony_ci std::map<int32_t, SingletonContainer::Singleton> oldSingletonMap_; 47e0dac50fSopenharmony_ci std::map<int32_t, std::set<int32_t>> oldDependencySetMap_; 48e0dac50fSopenharmony_ci}; 49e0dac50fSopenharmony_ci 50e0dac50fSopenharmony_civoid UtilsAllTest::SetUpTestCase() 51e0dac50fSopenharmony_ci{ 52e0dac50fSopenharmony_ci} 53e0dac50fSopenharmony_ci 54e0dac50fSopenharmony_civoid UtilsAllTest::TearDownTestCase() 55e0dac50fSopenharmony_ci{ 56e0dac50fSopenharmony_ci} 57e0dac50fSopenharmony_ci 58e0dac50fSopenharmony_civoid UtilsAllTest::SetUp() 59e0dac50fSopenharmony_ci{ 60e0dac50fSopenharmony_ci} 61e0dac50fSopenharmony_ci 62e0dac50fSopenharmony_civoid UtilsAllTest::TearDown() 63e0dac50fSopenharmony_ci{ 64e0dac50fSopenharmony_ci} 65e0dac50fSopenharmony_ci 66e0dac50fSopenharmony_cinamespace { 67e0dac50fSopenharmony_ci/** 68e0dac50fSopenharmony_ci * @tc.name: ADROnRemoteDied01 69e0dac50fSopenharmony_ci * @tc.desc: test AgentDeathRecipient::OnRemoteDied 70e0dac50fSopenharmony_ci * @tc.type: FUNC 71e0dac50fSopenharmony_ci */ 72e0dac50fSopenharmony_ciHWTEST_F(UtilsAllTest, ADROnRemoteDied01, Function | SmallTest | Level2) 73e0dac50fSopenharmony_ci{ 74e0dac50fSopenharmony_ci sptr<AgentDeathRecipient> deathRecipient = new AgentDeathRecipient(nullptr); 75e0dac50fSopenharmony_ci 76e0dac50fSopenharmony_ci deathRecipient->OnRemoteDied(nullptr); 77e0dac50fSopenharmony_ci 78e0dac50fSopenharmony_ci sptr<MockIRemoteObject> remoteObj = new MockIRemoteObject(); 79e0dac50fSopenharmony_ci deathRecipient->OnRemoteDied(remoteObj); 80e0dac50fSopenharmony_ci ASSERT_EQ(0, remoteObj->count_); 81e0dac50fSopenharmony_ci 82e0dac50fSopenharmony_ci deathRecipient->callback_ = [&remoteObj](sptr<IRemoteObject>& remote) { 83e0dac50fSopenharmony_ci remoteObj->count_ = 1; 84e0dac50fSopenharmony_ci }; 85e0dac50fSopenharmony_ci deathRecipient->OnRemoteDied(remoteObj); 86e0dac50fSopenharmony_ci ASSERT_EQ(1, remoteObj->count_); 87e0dac50fSopenharmony_ci} 88e0dac50fSopenharmony_ci/** 89e0dac50fSopenharmony_ci * @tc.name: PRCount01 90e0dac50fSopenharmony_ci * @tc.desc: test PerformReporter::count 91e0dac50fSopenharmony_ci * @tc.type: FUNC 92e0dac50fSopenharmony_ci */ 93e0dac50fSopenharmony_ciHWTEST_F(UtilsAllTest, PRCount01, Function | SmallTest | Level2) 94e0dac50fSopenharmony_ci{ 95e0dac50fSopenharmony_ci std::vector<int64_t> timeSpiltsMs = {0, 1, 2}; 96e0dac50fSopenharmony_ci PerformReporter reporter = PerformReporter("test", timeSpiltsMs); 97e0dac50fSopenharmony_ci 98e0dac50fSopenharmony_ci reporter.count(0); 99e0dac50fSopenharmony_ci ASSERT_EQ(1, reporter.totalCount_); 100e0dac50fSopenharmony_ci reporter.timeSplitCount_.clear(); 101e0dac50fSopenharmony_ci ASSERT_EQ(1, reporter.totalCount_); 102e0dac50fSopenharmony_ci} 103e0dac50fSopenharmony_ci/** 104e0dac50fSopenharmony_ci * @tc.name: SCAddSingleton01 105e0dac50fSopenharmony_ci * @tc.desc: test SingletonContainer::AddSingleton 106e0dac50fSopenharmony_ci * @tc.type: FUNC 107e0dac50fSopenharmony_ci */ 108e0dac50fSopenharmony_ciHWTEST_F(UtilsAllTest, SCAddSingleton01, Function | SmallTest | Level2) 109e0dac50fSopenharmony_ci{ 110e0dac50fSopenharmony_ci auto& singletonContainer = SingletonContainer::GetInstance(); 111e0dac50fSopenharmony_ci 112e0dac50fSopenharmony_ci singletonContainer.AddSingleton("test", nullptr); 113e0dac50fSopenharmony_ci auto testId = singletonContainer.stringMap["test"]; 114e0dac50fSopenharmony_ci singletonContainer.AddSingleton("test", nullptr); 115e0dac50fSopenharmony_ci ASSERT_EQ(testId, singletonContainer.stringMap["test"]); 116e0dac50fSopenharmony_ci singletonContainer.AddSingleton("test2", nullptr); 117e0dac50fSopenharmony_ci ASSERT_EQ(testId + 1, singletonContainer.stringMap["test2"]); 118e0dac50fSopenharmony_ci 119e0dac50fSopenharmony_ci auto testId2 = singletonContainer.stringMap["test2"]; 120e0dac50fSopenharmony_ci singletonContainer.singletonMap.erase(testId); 121e0dac50fSopenharmony_ci singletonContainer.singletonMap.erase(testId2); 122e0dac50fSopenharmony_ci singletonContainer.stringMap.erase("test"); 123e0dac50fSopenharmony_ci singletonContainer.stringMap.erase("test2"); 124e0dac50fSopenharmony_ci} 125e0dac50fSopenharmony_ci 126e0dac50fSopenharmony_ci/** 127e0dac50fSopenharmony_ci * @tc.name: SCSetSingleton01 128e0dac50fSopenharmony_ci * @tc.desc: test SingletonContainer::AddSingleton 129e0dac50fSopenharmony_ci * @tc.type: FUNC 130e0dac50fSopenharmony_ci */ 131e0dac50fSopenharmony_ciHWTEST_F(UtilsAllTest, SCSetSingleton01, Function | SmallTest | Level2) 132e0dac50fSopenharmony_ci{ 133e0dac50fSopenharmony_ci auto& singletonContainer = SingletonContainer::GetInstance(); 134e0dac50fSopenharmony_ci 135e0dac50fSopenharmony_ci TestClass* testObj = new TestClass(); 136e0dac50fSopenharmony_ci 137e0dac50fSopenharmony_ci singletonContainer.SetSingleton("test", testObj); 138e0dac50fSopenharmony_ci auto testId = singletonContainer.stringMap["test"]; 139e0dac50fSopenharmony_ci auto instance = singletonContainer.GetSingleton("test2"); 140e0dac50fSopenharmony_ci ASSERT_EQ(instance, nullptr); 141e0dac50fSopenharmony_ci 142e0dac50fSopenharmony_ci instance = singletonContainer.GetSingleton("test"); 143e0dac50fSopenharmony_ci ASSERT_NE(instance, nullptr); 144e0dac50fSopenharmony_ci ASSERT_EQ(static_cast<TestClass*>(instance)->name, "testClass"); 145e0dac50fSopenharmony_ci 146e0dac50fSopenharmony_ci singletonContainer.SetSingleton("test", nullptr); 147e0dac50fSopenharmony_ci instance = singletonContainer.GetSingleton("test"); 148e0dac50fSopenharmony_ci ASSERT_EQ(instance, nullptr); 149e0dac50fSopenharmony_ci 150e0dac50fSopenharmony_ci singletonContainer.singletonMap.erase(testId); 151e0dac50fSopenharmony_ci singletonContainer.stringMap.erase("test"); 152e0dac50fSopenharmony_ci delete testObj; 153e0dac50fSopenharmony_ci} 154e0dac50fSopenharmony_ci/** 155e0dac50fSopenharmony_ci * @tc.name: SCDependOn01 156e0dac50fSopenharmony_ci * @tc.desc: test SingletonContainer::DependOn 157e0dac50fSopenharmony_ci * @tc.type: FUNC 158e0dac50fSopenharmony_ci */ 159e0dac50fSopenharmony_ciHWTEST_F(UtilsAllTest, SCDependOn01, Function | SmallTest | Level2) 160e0dac50fSopenharmony_ci{ 161e0dac50fSopenharmony_ci auto& singletonContainer = SingletonContainer::GetInstance(); 162e0dac50fSopenharmony_ci 163e0dac50fSopenharmony_ci singletonContainer.AddSingleton("test", nullptr); 164e0dac50fSopenharmony_ci 165e0dac50fSopenharmony_ci ASSERT_EQ(nullptr, singletonContainer.DependOn("test", "test")); 166e0dac50fSopenharmony_ci 167e0dac50fSopenharmony_ci auto id = singletonContainer.stringMap["test"]; 168e0dac50fSopenharmony_ci auto& testSet = singletonContainer.dependencySetMap[id]; 169e0dac50fSopenharmony_ci ASSERT_EQ(1, testSet.size()); 170e0dac50fSopenharmony_ci 171e0dac50fSopenharmony_ci ASSERT_EQ(nullptr, singletonContainer.DependOn("test", "test")); 172e0dac50fSopenharmony_ci id = singletonContainer.stringMap["test"]; 173e0dac50fSopenharmony_ci auto& testSet2 = singletonContainer.dependencySetMap[id]; 174e0dac50fSopenharmony_ci ASSERT_EQ(1, testSet2.size()); 175e0dac50fSopenharmony_ci 176e0dac50fSopenharmony_ci singletonContainer.singletonMap.erase(id); 177e0dac50fSopenharmony_ci singletonContainer.stringMap.erase("test"); 178e0dac50fSopenharmony_ci id = singletonContainer.dependencySetMap.erase(id); 179e0dac50fSopenharmony_ci} 180e0dac50fSopenharmony_ci/** 181e0dac50fSopenharmony_ci * @tc.name: SRHOnImageAvailable01 182e0dac50fSopenharmony_ci * @tc.desc: test SurfaceReaderHandlerImpl::OnImageAvailable 183e0dac50fSopenharmony_ci * @tc.type: FUNC 184e0dac50fSopenharmony_ci */ 185e0dac50fSopenharmony_ciHWTEST_F(UtilsAllTest, SRHOnImageAvailable, Function | SmallTest | Level2) 186e0dac50fSopenharmony_ci{ 187e0dac50fSopenharmony_ci sptr<SurfaceReaderHandlerImpl> surfaceReaderHandlerImpl = new (std::nothrow)SurfaceReaderHandlerImpl(); 188e0dac50fSopenharmony_ci surfaceReaderHandlerImpl->flag_ = false; 189e0dac50fSopenharmony_ci surfaceReaderHandlerImpl->OnImageAvailable(nullptr); 190e0dac50fSopenharmony_ci ASSERT_EQ(true, surfaceReaderHandlerImpl->flag_); 191e0dac50fSopenharmony_ci surfaceReaderHandlerImpl->flag_ = true; 192e0dac50fSopenharmony_ci surfaceReaderHandlerImpl->OnImageAvailable(nullptr); 193e0dac50fSopenharmony_ci ASSERT_EQ(true, surfaceReaderHandlerImpl->flag_); 194e0dac50fSopenharmony_ci} 195e0dac50fSopenharmony_ci/** 196e0dac50fSopenharmony_ci * @tc.name: SRHGetPixelMap01 197e0dac50fSopenharmony_ci * @tc.desc: test SurfaceReaderHandlerImpl::GetPixelMap 198e0dac50fSopenharmony_ci * @tc.type: FUNC 199e0dac50fSopenharmony_ci */ 200e0dac50fSopenharmony_ciHWTEST_F(UtilsAllTest, SRHGetPixelMap, Function | SmallTest | Level2) 201e0dac50fSopenharmony_ci{ 202e0dac50fSopenharmony_ci sptr<SurfaceReaderHandlerImpl> surfaceReaderHandlerImpl = new (std::nothrow)SurfaceReaderHandlerImpl(); 203e0dac50fSopenharmony_ci surfaceReaderHandlerImpl->flag_ = false; 204e0dac50fSopenharmony_ci surfaceReaderHandlerImpl->GetPixelMap(); 205e0dac50fSopenharmony_ci ASSERT_EQ(false, surfaceReaderHandlerImpl->flag_); 206e0dac50fSopenharmony_ci surfaceReaderHandlerImpl->flag_ = true; 207e0dac50fSopenharmony_ci surfaceReaderHandlerImpl->GetPixelMap(); 208e0dac50fSopenharmony_ci} 209e0dac50fSopenharmony_ci/** 210e0dac50fSopenharmony_ci * @tc.name: SysCapUtilGetClientName 211e0dac50fSopenharmony_ci * @tc.desc: test SysCapUtil::GetClientName 212e0dac50fSopenharmony_ci * @tc.type: FUNC 213e0dac50fSopenharmony_ci */ 214e0dac50fSopenharmony_ciHWTEST_F(UtilsAllTest, SysCapUtilGetClientName, Function | SmallTest | Level2) 215e0dac50fSopenharmony_ci{ 216e0dac50fSopenharmony_ci ASSERT_NE("", SysCapUtil::GetClientName()); 217e0dac50fSopenharmony_ci} 218e0dac50fSopenharmony_ci} 219e0dac50fSopenharmony_ci} // namespace Rosen 220e0dac50fSopenharmony_ci} // namespace OHOS