1 /*
2 * Copyright (c) 2025 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 #include "ui_effect_controller_client.h"
18 #include "ui_effect_manager.h"
19 #include "mock/mock_accesstoken_kit.h"
20 #include "mock/mock_ui_effect_controller_client_stub.h"
21 #include "iremote_object_mocker.h"
22 #include "ui_effect_controller_client.h"
23 #include "ui_effect_controller.h"
24 #include "ui_effect_manager.h"
25
26 namespace OHOS::Rosen {
27
28 class UIEffectControllerTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 };
35
SetUpTestCase()36 void UIEffectControllerTest::SetUpTestCase() {}
37
TearDownTestCase()38 void UIEffectControllerTest::TearDownTestCase() {}
39
SetUp()40 void UIEffectControllerTest::SetUp() {}
41
TearDown()42 void UIEffectControllerTest::TearDown() {}
43
44 namespace {
45 using namespace testing;
46 using namespace testing::ext;
47 HWTEST_F(UIEffectControllerTest, UIEffectControllerClientAll, TestSize.Level1)
48 {
49 UIEffectControllerClient client;
50 EXPECT_EQ(client.id_, UIEFFECT_INVALID_ID);
51 int32_t id = 23423;
52 client.SetId(id);
53 EXPECT_EQ(client.GetId(), id);
54 sptr<UIEffectParams> params = sptr<UIEffectParams>::MakeSptr();
55 client.SetParams(params);
56 EXPECT_EQ(client.params_, params);
57 }
58
59 HWTEST_F(UIEffectControllerTest, UIEffectManagerTestCreateUIEffectController, TestSize.Level1)
60 {
61 sptr<UIEffectControllerClientStubMocker> controllerClient = sptr<UIEffectControllerClientStubMocker>::MakeSptr();
62 sptr<IUIEffectController> controller = nullptr;
63 int32_t id = 1;
64 EXPECT_EQ(UIEffectManager::GetInstance().CreateUIEffectController(controllerClient, controller, id),
65 WMError::WM_ERROR_NULLPTR);
66 bool setParamCalled = false;
67 bool animateToCalled = false;
__anon3c548c770202(int32_t, sptr<UIEffectParams>) 68 UIEffectManager::GetInstance().RegisterUIEffectSetParamsCallback([&setParamCalled](int32_t, sptr<UIEffectParams>) {
69 setParamCalled = true;
70 });
71 EXPECT_EQ(UIEffectManager::GetInstance().CreateUIEffectController(controllerClient, controller, id),
72 WMError::WM_ERROR_NULLPTR);
73 UIEffectManager::GetInstance().RegisterUIEffectAnimateToCallback([&animateToCalled](int32_t,
__anon3c548c770302(int32_t, sptr<UIEffectParams>, sptr<WindowAnimationOption>, sptr<WindowAnimationOption>) 74 sptr<UIEffectParams>, sptr<WindowAnimationOption>, sptr<WindowAnimationOption>) {
75 animateToCalled = true;
76 });
77 EXPECT_CALL(*controllerClient, AsObject()).WillOnce(Return(nullptr));
78 EXPECT_EQ(UIEffectManager::GetInstance().CreateUIEffectController(controllerClient, controller, id),
79 WMError::WM_ERROR_NULLPTR);
80 sptr<RemoteObjectMocker> remoteObjectMocker = sptr<RemoteObjectMocker>::MakeSptr();
81 EXPECT_CALL(*controllerClient, AsObject()).WillRepeatedly(
__anon3c548c770402()82 [weakThis = wptr(remoteObjectMocker)]()-> sptr<RemoteObjectMocker> {
83 auto mocker = weakThis.promote();
84 if (mocker) {
85 return mocker;
86 } else {
87 GTEST_LOG_(INFO) << "SceneSessionMocker:NULL";
88 return nullptr;
89 }
90 });
91 EXPECT_CALL(*remoteObjectMocker, AddDeathRecipient(_)).
__anon3c548c770502(const sptr<IRemoteObject::DeathRecipient>& recipient) 92 WillOnce([](const sptr<IRemoteObject::DeathRecipient>& recipient) {
93 return false;
94 });
95 EXPECT_EQ(UIEffectManager::GetInstance().CreateUIEffectController(controllerClient, controller, id),
96 WMError::WM_ERROR_NULLPTR);
97 EXPECT_CALL(*remoteObjectMocker, AddDeathRecipient(_)).
__anon3c548c770602(const sptr<IRemoteObject::DeathRecipient>& recipient) 98 WillOnce([](const sptr<IRemoteObject::DeathRecipient>& recipient) {
99 return true;
100 });
101 EXPECT_EQ(UIEffectManager::GetInstance().CreateUIEffectController(controllerClient, controller, id),
102 WMError::WM_OK);
103 }
104
105 HWTEST_F(UIEffectControllerTest, UIEffectManagerTestSetUIEffectControllerAliveState, TestSize.Level1)
106 {
107 sptr<UIEffectControllerClient> client = sptr<UIEffectControllerClient>::MakeSptr();
108 sptr<UIEffectController> controller = sptr<UIEffectController>::MakeSptr(0, nullptr, nullptr);
109 sptr<UIEffectControllerClientDeath> death = sptr<UIEffectControllerClientDeath>::MakeSptr(nullptr);
110 controller->isAliveInUI_ = false;
111 UIEffectManager::GetInstance().SetUIEffectControllerAliveState(10000, true);
112 EXPECT_EQ(controller->isAliveInUI_, false);
113 UIEffectManager::GetInstance().UIEffectControllerMap_[100] = std::make_tuple(nullptr, nullptr, nullptr);
114 UIEffectManager::GetInstance().SetUIEffectControllerAliveState(100, true);
115 EXPECT_EQ(controller->isAliveInUI_, false);
116 UIEffectManager::GetInstance().UIEffectControllerMap_[200] = std::make_tuple(controller, client, death);
117 UIEffectManager::GetInstance().SetUIEffectControllerAliveState(200, true);
118 EXPECT_EQ(controller->isAliveInUI_, true);
119 }
120
121 HWTEST_F(UIEffectControllerTest, UIEffectManagerTestEraseController, TestSize.Level1)
122 {
123 sptr<UIEffectControllerClientStubMocker> client = sptr<UIEffectControllerClientStubMocker>::MakeSptr();
124 sptr<UIEffectController> controller = sptr<UIEffectController>::MakeSptr(0, nullptr, nullptr);
125 sptr<UIEffectControllerClientDeath> death = sptr<UIEffectControllerClientDeath>::MakeSptr(nullptr);
126 UIEffectManager::GetInstance().UIEffectControllerMap_.clear();
127 UIEffectManager::GetInstance().UIEffectControllerMap_[100] = std::make_tuple(nullptr, nullptr, nullptr);
128 UIEffectManager::GetInstance().EraseUIEffectController(100);
129 EXPECT_EQ(UIEffectManager::GetInstance().UIEffectControllerMap_.size(), 0);
130 UIEffectManager::GetInstance().UIEffectControllerMap_[100] = std::make_tuple(controller, client, death);
131 EXPECT_CALL(*client, AsObject()).WillRepeatedly(Return(nullptr));
132 UIEffectManager::GetInstance().EraseUIEffectController(100);
133 UIEffectManager::GetInstance().UIEffectControllerMap_[100] = std::make_tuple(controller, client, death);
134 EXPECT_CALL(*client, AsObject()).WillRepeatedly(Return(sptr<RemoteObjectMocker>::MakeSptr()));
135 UIEffectManager::GetInstance().EraseUIEffectController(100);
136 }
137
138 HWTEST_F(UIEffectControllerTest, UIEffectControllerTest, TestSize.Level1)
139 {
140 sptr<UIEffectController> controller = sptr<UIEffectController>::MakeSptr(0, nullptr, nullptr);
141 sptr<UIEffectParams> params = sptr<UIEffectParams>::MakeSptr();
142 sptr<WindowAnimationOption> option = sptr<WindowAnimationOption>::MakeSptr();
143 MockAccesstokenKit::MockIsSystemApp(false);
144 MockAccesstokenKit::MockIsSACalling(false);
145 EXPECT_EQ(controller->SetParams(params), WMError::WM_ERROR_NOT_SYSTEM_APP);
146 EXPECT_EQ(controller->AnimateTo(params, option, option), WMError::WM_ERROR_NOT_SYSTEM_APP);
147 MockAccesstokenKit::MockIsSystemApp(true);
148 MockAccesstokenKit::MockIsSACalling(true);
149 bool setParamCalled = false;
150 controller->setParamsCallback_ = nullptr;
151 EXPECT_EQ(controller->SetParams(params), WMError::WM_ERROR_NULLPTR);
__anon3c548c770702(int32_t, sptr<UIEffectParams>) 152 controller->setParamsCallback_ = [&setParamCalled](int32_t, sptr<UIEffectParams>) {
153 setParamCalled = true;
154 };
155 EXPECT_EQ(controller->SetParams(params), WMError::WM_OK);
156 controller->isAliveInUI_ = false;
157 EXPECT_EQ(controller->AnimateTo(params, option, option), WMError::WM_ERROR_UI_EFFECT_ERROR);
158 controller->isAliveInUI_ = true;
159 controller->animateToCallback_ = nullptr;
160 EXPECT_EQ(controller->AnimateTo(params, option, option), WMError::WM_ERROR_NULLPTR);
161 bool animateToCalled = false;
162 controller->animateToCallback_ = [&animateToCalled](int32_t, sptr<UIEffectParams>, sptr<WindowAnimationOption>,
__anon3c548c770802(int32_t, sptr<UIEffectParams>, sptr<WindowAnimationOption>, sptr<WindowAnimationOption>) 163 sptr<WindowAnimationOption>) {
164 animateToCalled = true;
165 };
166 EXPECT_EQ(controller->AnimateTo(params, option, option), WMError::WM_OK);
167 }
168
169 HWTEST_F(UIEffectControllerTest, UIEffectControllerClientDeathTest, TestSize.Level1)
170 {
171 bool called = false;
__anon3c548c770902() 172 auto eraseFunc = [&called]() {
173 called = true;
174 };
175 sptr<UIEffectControllerClient> client = sptr<UIEffectControllerClient>::MakeSptr();
176 UIEffectControllerClientDeath deathRecipientNull(nullptr);
177 deathRecipientNull.OnRemoteDied(client);
178 EXPECT_EQ(called, false);
179 UIEffectControllerClientDeath deathRecipient(eraseFunc);
180 deathRecipient.OnRemoteDied(nullptr);
181 EXPECT_EQ(called, false);
182 deathRecipient.OnRemoteDied(client);
183 EXPECT_EQ(called, true);
184 }
185 }
186 } // namespace OHOS::Rosen