• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "mock/mock_message_parcel.h"
16 #include "ui_effect_controller_client.h"
17 #include "ui_effect_controller.h"
18 #include "ui_effect_controller_proxy.h"
19 #include "ui_effect_controller_client_proxy.h"
20 #include "iremote_object_mocker.h"
21 
22 bool g_setWriteStrongParcelableErrorFlag = false;
23 int32_t g_setWriteStrongParcelableErrorIndex = 0;
24 int32_t g_setWriteStrongParcelableCurrentIndex = -1;
25 
ResetWriteStrongParcelableState()26 void ResetWriteStrongParcelableState()
27 {
28     g_setWriteStrongParcelableErrorFlag = false;
29     g_setWriteStrongParcelableErrorIndex = 0;
30     g_setWriteStrongParcelableCurrentIndex = -1;
31 }
32 
SetWriteStrongParcelableErrorFlag(bool flag)33 void SetWriteStrongParcelableErrorFlag(bool flag)
34 {
35     g_setWriteStrongParcelableErrorFlag = flag;
36 }
37 
SetWriteStrongParcelableErrorIndex(int32_t num)38 void SetWriteStrongParcelableErrorIndex(int32_t num)
39 {
40     g_setWriteStrongParcelableErrorIndex = num;
41 }
42 
43 namespace OHOS {
WriteStrongParcelable(const sptr<Parcelable> & object)44 bool Parcel::WriteStrongParcelable(const sptr<Parcelable>& object)
45 {
46     g_setWriteStrongParcelableCurrentIndex++;
47     if (g_setWriteStrongParcelableErrorFlag &&
48         g_setWriteStrongParcelableCurrentIndex == g_setWriteStrongParcelableErrorIndex) {
49         return false;
50     }
51     return true;
52 }
53 } // namespace OHOS
54 
55 namespace OHOS::Rosen {
56 class UIEffectControllerProxyTest : public testing::Test {
57 public:
58     static void SetUpTestCase();
59     static void TearDownTestCase();
60     void SetUp() override;
61     void TearDown() override;
62 };
63 
SetUpTestCase()64 void UIEffectControllerProxyTest::SetUpTestCase() {}
65 
TearDownTestCase()66 void UIEffectControllerProxyTest::TearDownTestCase() {}
67 
SetUp()68 void UIEffectControllerProxyTest::SetUp()
69 {
70     ResetWriteStrongParcelableState();
71     MockMessageParcel::ClearAllErrorFlag();
72 }
73 
TearDown()74 void UIEffectControllerProxyTest::TearDown() {}
75 
76 namespace {
77 using namespace testing;
78 using namespace testing::ext;
79 // 需要ENABLE_MOCK_READ_UINT32 启用, Remote() == nullptr 可以通过设置nullptr来覆盖
80 HWTEST_F(UIEffectControllerProxyTest, UIEffectControllerProxySetParam, TestSize.Level1)
81 {
82     sptr<MockIRemoteObject> iRemoteObjectMocker = sptr<MockIRemoteObject>::MakeSptr();
83     sptr<UIEffectControllerProxy> proxy =
84         sptr<UIEffectControllerProxy>::MakeSptr(iRemoteObjectMocker);
85     ASSERT_NE(proxy, nullptr);
86     sptr<UIEffectParams> params = sptr<UIEffectParams>::MakeSptr();
87     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
88     EXPECT_EQ(proxy->SetParams(params), WMError::WM_ERROR_IPC_FAILED);
89     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(false);
90     MockMessageParcel::ClearAllErrorFlag();
91     SetWriteStrongParcelableErrorFlag(true);
92     SetWriteStrongParcelableErrorIndex(0);
93     EXPECT_EQ(proxy->SetParams(nullptr), WMError::WM_ERROR_IPC_FAILED);
94     SetWriteStrongParcelableErrorFlag(false);
95     iRemoteObjectMocker->SetRequestResult(10);
96     EXPECT_EQ(proxy->SetParams(params), WMError::WM_ERROR_IPC_FAILED);
97     iRemoteObjectMocker->SetRequestResult(0);
98     MockMessageParcel::SetReadUint32ErrorFlag(true);
99     EXPECT_EQ(proxy->SetParams(params), WMError::WM_ERROR_IPC_FAILED);
100     MockMessageParcel::SetReadUint32ErrorFlag(false);
101     proxy->SetParams(params);
102     sptr<UIEffectControllerProxy> proxyInValid =
103         sptr<UIEffectControllerProxy>::MakeSptr(nullptr);
104     EXPECT_EQ(proxyInValid->SetParams(params), WMError::WM_ERROR_IPC_FAILED);
105 }
106 
107 HWTEST_F(UIEffectControllerProxyTest, UIEffectControllerProxyAnimateTo, TestSize.Level1)
108 {
109     sptr<MockIRemoteObject> iRemoteObjectMocker = sptr<MockIRemoteObject>::MakeSptr();
110     sptr<UIEffectControllerProxy> proxy =
111         sptr<UIEffectControllerProxy>::MakeSptr(iRemoteObjectMocker);
112     ASSERT_NE(proxy, nullptr);
113     MockMessageParcel::ClearAllErrorFlag();
114     sptr<UIEffectParams> params = sptr<UIEffectParams>::MakeSptr();
115     sptr<WindowAnimationOption> config = sptr<WindowAnimationOption>::MakeSptr();
116     sptr<WindowAnimationOption> interruptOption = sptr<WindowAnimationOption>::MakeSptr();
117     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
118     EXPECT_EQ(proxy->AnimateTo(params, config, interruptOption), WMError::WM_ERROR_IPC_FAILED);
119     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(false);
120     // index 0 failed
121     ResetWriteStrongParcelableState();
122     SetWriteStrongParcelableErrorFlag(true);
123     SetWriteStrongParcelableErrorIndex(0);
124     EXPECT_EQ(proxy->AnimateTo(params, config, interruptOption), WMError::WM_ERROR_IPC_FAILED);
125     SetWriteStrongParcelableErrorFlag(false);
126     // index 1 failed
127     ResetWriteStrongParcelableState();
128     SetWriteStrongParcelableErrorFlag(true);
129     SetWriteStrongParcelableErrorIndex(1);
130     EXPECT_EQ(proxy->AnimateTo(params, config, interruptOption), WMError::WM_ERROR_IPC_FAILED);
131     SetWriteStrongParcelableErrorFlag(false);
132     // index 2 bool failed
133     MockMessageParcel::SetWriteBoolErrorFlag(true);
134     EXPECT_EQ(proxy->AnimateTo(params, config, interruptOption), WMError::WM_ERROR_IPC_FAILED);
135     MockMessageParcel::SetWriteBoolErrorFlag(false);
136     // index 2 failed
137     ResetWriteStrongParcelableState();
138     SetWriteStrongParcelableErrorFlag(true);
139     SetWriteStrongParcelableErrorIndex(2);
140     EXPECT_EQ(proxy->AnimateTo(params, config, interruptOption), WMError::WM_ERROR_IPC_FAILED);
141     SetWriteStrongParcelableErrorFlag(false);
142     // index 2 nullptr
143     proxy->AnimateTo(params, config, nullptr);
144     // send request failed
145     iRemoteObjectMocker->SetRequestResult(10);
146     EXPECT_EQ(proxy->AnimateTo(params, config, interruptOption), WMError::WM_ERROR_IPC_FAILED);
147     iRemoteObjectMocker->SetRequestResult(0);
148     // read result failed
149     MockMessageParcel::SetReadUint32ErrorFlag(true);
150     EXPECT_EQ(proxy->AnimateTo(params, config, interruptOption), WMError::WM_ERROR_IPC_FAILED);
151     MockMessageParcel::SetReadUint32ErrorFlag(false);
152     // read result ok
153     proxy->AnimateTo(params, config, interruptOption);
154     sptr<UIEffectControllerProxy> proxyInValid =
155         sptr<UIEffectControllerProxy>::MakeSptr(nullptr);
156     EXPECT_EQ(proxyInValid->AnimateTo(params, config, interruptOption), WMError::WM_ERROR_IPC_FAILED);
157 }
158 
159 HWTEST_F(UIEffectControllerProxyTest, UIEffectControllerClientProxySetParam, TestSize.Level1)
160 {
161     sptr<MockIRemoteObject> iRemoteObjectMocker = sptr<MockIRemoteObject>::MakeSptr();
162     sptr<UIEffectControllerClientProxy> proxy =
163         sptr<UIEffectControllerClientProxy>::MakeSptr(iRemoteObjectMocker);
164     ASSERT_NE(proxy, nullptr);
165     MockMessageParcel::ClearAllErrorFlag();
166     sptr<UIEffectParams> params = sptr<UIEffectParams>::MakeSptr();
167     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
168     EXPECT_EQ(proxy->SetParams(params), WMError::WM_ERROR_IPC_FAILED);
169     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(false);
170     ResetWriteStrongParcelableState();
171     SetWriteStrongParcelableErrorFlag(true);
172     SetWriteStrongParcelableErrorIndex(0);
173     EXPECT_EQ(proxy->SetParams(nullptr), WMError::WM_ERROR_IPC_FAILED);
174     SetWriteStrongParcelableErrorFlag(false);
175     iRemoteObjectMocker->SetRequestResult(10);
176     EXPECT_EQ(proxy->SetParams(params), WMError::WM_ERROR_IPC_FAILED);
177     iRemoteObjectMocker->SetRequestResult(0);
178     EXPECT_EQ(proxy->SetParams(params), WMError::WM_OK);
179     sptr<UIEffectControllerClientProxy> proxyInValid =
180         sptr<UIEffectControllerClientProxy>::MakeSptr(nullptr);
181     EXPECT_EQ(proxyInValid->SetParams(params), WMError::WM_ERROR_IPC_FAILED);
182 }
183 }
184 } // namespace OHOS::Rosen