• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, TestSize.Level1)
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, TestSize.Level1)
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, TestSize.Level1)
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, TestSize.Level1)
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(
111         WMError::WM_ERROR_IPC_FAILED,
112         sessionProxy->UpdateSessionPropertyByAction(nullptr, WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
113     MockMessageParcel::ClearAllErrorFlag();
114 
115     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
116     MockMessageParcel::SetWriteBoolErrorFlag(true);
117     ASSERT_EQ(
118         WMError::WM_ERROR_IPC_FAILED,
119         sessionProxy->UpdateSessionPropertyByAction(property, WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
120     MockMessageParcel::ClearAllErrorFlag();
121 
122     MockMessageParcel::SetWriteBoolErrorFlag(false);
123     ASSERT_EQ(
124         WMError::WM_OK,
125         sessionProxy->UpdateSessionPropertyByAction(property, WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
126     MockMessageParcel::ClearAllErrorFlag();
127 
128     MockMessageParcel::SetWriteBoolErrorFlag(true);
129     ASSERT_EQ(
130         WMError::WM_ERROR_IPC_FAILED,
131         sessionProxy->UpdateSessionPropertyByAction(nullptr, WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
132     MockMessageParcel::ClearAllErrorFlag();
133 
134     MockMessageParcel::SetWriteBoolErrorFlag(false);
135     ASSERT_EQ(
136         WMError::WM_OK,
137         sessionProxy->UpdateSessionPropertyByAction(nullptr, WSPropertyChangeAction::ACTION_UPDATE_KEEP_SCREEN_ON));
138     MockMessageParcel::ClearAllErrorFlag();
139 }
140 
141 /**
142  * @tc.name: HandlePendingSessionActivation01
143  * @tc.desc: normal function
144  * @tc.type: FUNC
145  */
146 HWTEST_F(SessionProxyMockTest, PendingSessionActivation01, TestSize.Level1)
147 {
148     GTEST_LOG_(INFO) << "SessionProxyMockTest::PendingSessionActivation01 start";
149     sptr<IRemoteObject> iRemoteObjectMocker = sptr<IRemoteObjectMocker>::MakeSptr();
150     sptr<SessionProxy> sProxy = sptr<SessionProxy>::MakeSptr(iRemoteObjectMocker);
151     sptr<AAFwk::SessionInfo> abilitySessionInfo = sptr<AAFwk::SessionInfo>::MakeSptr();
152     ASSERT_NE(abilitySessionInfo, nullptr);
153     AAFwk::Want want;
154     abilitySessionInfo->want = want;
155     abilitySessionInfo->requestCode = 0;
156     abilitySessionInfo->persistentId = 12;
157     abilitySessionInfo->uiAbilityId = 9;
158     abilitySessionInfo->callingTokenId = 1;
159     abilitySessionInfo->requestId = 2;
160     abilitySessionInfo->reuse = false;
161     abilitySessionInfo->canStartAbilityFromBackground = true;
162     abilitySessionInfo->isAtomicService = true;
163     abilitySessionInfo->isBackTransition = false;
164     abilitySessionInfo->needClearInNotShowRecent = false;
165     abilitySessionInfo->callerToken = sptr<MockIRemoteObject>::MakeSptr();
166     abilitySessionInfo->instanceKey = "PendingSessionActivation01";
167     abilitySessionInfo->isFromIcon = false;
168     abilitySessionInfo->specifiedFlag = "specifiedFlag";
169     abilitySessionInfo->reuseDelegatorWindow = true;
170     abilitySessionInfo->scenarios = 1;
171     auto res = sProxy->PendingSessionActivation(abilitySessionInfo);
172     EXPECT_EQ(res, WSError::WS_OK);
173     GTEST_LOG_(INFO) << "SessionProxyMockTest::PendingSessionActivation01 end";
174 }
175 } // namespace
176 } // namespace Rosen
177 } // namespace OHOS
178