1 /*
2  * Copyright (c) 2023 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 "session_proxy.h"
16 
17 #include <gtest/gtest.h>
18 #include "accessibility_event_info.h"
19 #include "iremote_object_mocker.h"
20 #include "mock_message_parcel.h"
21 #include "window_manager_hilog.h"
22 using namespace testing;
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionProxyMockTest"};
28 }
29 class SessionProxyMockTest : public testing::Test {
30   public:
SessionProxyMockTest()31     SessionProxyMockTest() {}
~SessionProxyMockTest()32     ~SessionProxyMockTest() {}
33 };
34 namespace {
35 /**
36  * @tc.name: TransferAccessibilityEvent01
37  * @tc.desc: normal function
38  * @tc.type: FUNC
39  */
HWTEST_F(SessionProxyMockTest, TransferAccessibilityEvent01, Function | SmallTest | Level2)40 HWTEST_F(SessionProxyMockTest, TransferAccessibilityEvent01, Function | SmallTest | Level2)
41 {
42     WLOGI("TransferAccessibilityEvent01 begin");
43     MockMessageParcel::ClearAllErrorFlag();
44     sptr<IRemoteObject> iRemoteObjectMocker = new IRemoteObjectMocker();
45     SessionProxy* sProxy = new(std::nothrow) SessionProxy(iRemoteObjectMocker);
46     Accessibility::AccessibilityEventInfo info;
47     int64_t uiExtensionIdLevel = 0;
48     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
49     WSError res = sProxy->TransferAccessibilityEvent(info, uiExtensionIdLevel);
50     ASSERT_EQ(res, WSError::WS_ERROR_IPC_FAILED);
51     MockMessageParcel::ClearAllErrorFlag();
52     WLOGI("TransferAccessibilityEvent01 end");
53 }
54 
55 /**
56  * @tc.name: TransferAccessibilityEvent02
57  * @tc.desc: normal function
58  * @tc.type: FUNC
59  */
HWTEST_F(SessionProxyMockTest, TransferAccessibilityEvent02, Function | SmallTest | Level2)60 HWTEST_F(SessionProxyMockTest, TransferAccessibilityEvent02, Function | SmallTest | Level2)
61 {
62     WLOGI("TransferAccessibilityEvent02 begin");
63     MockMessageParcel::ClearAllErrorFlag();
64     sptr<IRemoteObject> iRemoteObjectMocker = new IRemoteObjectMocker();
65     SessionProxy* sProxy = new(std::nothrow) SessionProxy(iRemoteObjectMocker);
66     Accessibility::AccessibilityEventInfo info;
67     int64_t uiExtensionIdLevel = 0;
68 
69     MockMessageParcel::SetWriteParcelableErrorFlag(true);
70     WSError res = sProxy->TransferAccessibilityEvent(info, uiExtensionIdLevel);
71     ASSERT_EQ(res, WSError::WS_ERROR_IPC_FAILED);
72     MockMessageParcel::ClearAllErrorFlag();
73     WLOGI("TransferAccessibilityEvent02 end");
74 }
75 
76 /**
77  * @tc.name: TransferAccessibilityEvent02
78  * @tc.desc: normal function
79  * @tc.type: FUNC
80  */
HWTEST_F(SessionProxyMockTest, TransferAccessibilityEvent03, Function | SmallTest | Level2)81 HWTEST_F(SessionProxyMockTest, TransferAccessibilityEvent03, Function | SmallTest | Level2)
82 {
83     WLOGI("TransferAccessibilityEvent03 begin");
84     MockMessageParcel::ClearAllErrorFlag();
85     sptr<IRemoteObject> iRemoteObjectMocker = new IRemoteObjectMocker();
86     SessionProxy* sProxy = new(std::nothrow) SessionProxy(iRemoteObjectMocker);
87     Accessibility::AccessibilityEventInfo info;
88     int64_t uiExtensionIdLevel = 0;
89 
90     MockMessageParcel::SetWriteInt64ErrorFlag(true);
91     WSError res = sProxy->TransferAccessibilityEvent(info, uiExtensionIdLevel);
92     ASSERT_EQ(res, WSError::WS_ERROR_IPC_FAILED);
93 
94     MockMessageParcel::ClearAllErrorFlag();
95     WLOGI("TransferAccessibilityEvent03 end");
96 }
97 
98 /**
99  * @tc.name: UpdateSessionPropertyByAction
100  * @tc.desc: normal function
101  * @tc.type: FUNC
102  */
HWTEST_F(SessionProxyMockTest, UpdateSessionPropertyByAction, Function | SmallTest | Level2)103 HWTEST_F(SessionProxyMockTest, UpdateSessionPropertyByAction, Function | SmallTest | Level2)
104 {
105     MockMessageParcel::ClearAllErrorFlag();
106     sptr<IRemoteObject> iRemoteObjectMocker = new IRemoteObjectMocker();
107     SessionProxy* sessionProxy = new(std::nothrow) SessionProxy(iRemoteObjectMocker);
108     ASSERT_NE(sessionProxy, nullptr);
109     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
110     ASSERT_EQ(WMError::WM_ERROR_IPC_FAILED, sessionProxy->UpdateSessionPropertyByAction(nullptr,
111         WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
112     MockMessageParcel::ClearAllErrorFlag();
113 
114     MockMessageParcel::SetWriteUint32ErrorFlag(true);
115     ASSERT_EQ(WMError::WM_ERROR_IPC_FAILED, sessionProxy->UpdateSessionPropertyByAction(nullptr,
116         WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
117     MockMessageParcel::ClearAllErrorFlag();
118 
119     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
120     ASSERT_NE(property, nullptr);
121     MockMessageParcel::SetWriteBoolErrorFlag(true);
122     ASSERT_EQ(WMError::WM_ERROR_IPC_FAILED, sessionProxy->UpdateSessionPropertyByAction(property,
123         WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
124     MockMessageParcel::ClearAllErrorFlag();
125 
126     MockMessageParcel::SetWriteBoolErrorFlag(false);
127     ASSERT_EQ(WMError::WM_OK, sessionProxy->UpdateSessionPropertyByAction(property,
128         WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
129     MockMessageParcel::ClearAllErrorFlag();
130 
131     MockMessageParcel::SetWriteBoolErrorFlag(true);
132     ASSERT_EQ(WMError::WM_ERROR_IPC_FAILED, sessionProxy->UpdateSessionPropertyByAction(nullptr,
133         WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
134     MockMessageParcel::ClearAllErrorFlag();
135 
136     MockMessageParcel::SetWriteBoolErrorFlag(false);
137     ASSERT_EQ(WMError::WM_OK, sessionProxy->UpdateSessionPropertyByAction(nullptr,
138         WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
139     MockMessageParcel::ClearAllErrorFlag();
140 }
141 } // namespace
142 } // namespace Rosen
143 } // namespace OHOS
144