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