1 /*
2 * Copyright (c) 2024 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 "mock/mock_session_stub.h"
17 #include "session/host/include/zidl/session_ipc_interface_code.h"
18 #include <gtest/gtest.h>
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace Rosen {
25
26 class SessionStubImmersiveTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32
33 private:
34 sptr<SessionStubMocker> session_ = nullptr;
35 };
36
SetUpTestCase()37 void SessionStubImmersiveTest::SetUpTestCase() {}
38
TearDownTestCase()39 void SessionStubImmersiveTest::TearDownTestCase() {}
40
SetUp()41 void SessionStubImmersiveTest::SetUp()
42 {
43 session_ = sptr<SessionStubMocker>::MakeSptr();
44 EXPECT_NE(nullptr, session_);
45
46 EXPECT_CALL(*session_, OnRemoteRequest(_, _, _, _)).WillOnce(Invoke(
47 [&](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) -> int {
48 return session_->SessionStub::OnRemoteRequest(code, data, reply, option);
49 }
50 ));
51 }
52
TearDown()53 void SessionStubImmersiveTest::TearDown()
54 {
55 session_ = nullptr;
56 }
57
58 namespace {
59
60 /**
61 * @tc.name: HandleGetAvoidAreaByTypeWithInvalidType
62 * @tc.desc: GetAvoidAreaByType with invalid type
63 * @tc.type: FUNC
64 */
65 HWTEST_F(SessionStubImmersiveTest, HandleGetAvoidAreaByTypeWithInvalidType, Function | SmallTest | Level2)
66 {
67 GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAvoidAreaByTypeWithInvalidType start";
68 MessageParcel data;
69 MessageParcel reply;
70 MessageOption option = {MessageOption::TF_SYNC};
71 data.WriteInterfaceToken(u"OHOS.ISession");
72 data.WriteUint32(1111); // invalid type
73 uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_AVOID_AREA);
74 int ret = session_->OnRemoteRequest(code, data, reply, option);
75 ASSERT_EQ(ret, 5);
76 GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAvoidAreaByTypeWithInvalidType end";
77 }
78
79 /**
80 * @tc.name: HandleGetAvoidAreaByTypeWithSystemType
81 * @tc.desc: GetAvoidAreaByType with system type
82 * @tc.type: FUNC
83 */
84 HWTEST_F(SessionStubImmersiveTest, HandleGetAvoidAreaByTypeWithSystemType, Function | SmallTest | Level2)
85 {
86 GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAvoidAreaByTypeWithSystemType start";
87 AvoidArea mockArea;
88 mockArea.topRect_.width_ = 1200;
89 mockArea.topRect_.height_ = 127;
90 EXPECT_CALL(*session_, GetAvoidAreaByType(_, _, _)).WillOnce(Return(mockArea));
91
92 MessageParcel data;
93 MessageParcel reply;
94 MessageOption option = {MessageOption::TF_SYNC};
95 data.WriteInterfaceToken(u"OHOS.ISession");
96 data.WriteUint32(static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM));
97 WSRect rect = {0, 0, 1200, 127};
98 data.WriteInt32(rect.posX_);
99 data.WriteInt32(rect.posY_);
100 data.WriteInt32(rect.width_);
101 data.WriteInt32(rect.height_);
102 data.WriteInt32(16);
103 uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_AVOID_AREA);
104 int ret = session_->OnRemoteRequest(code, data, reply, option);
105
106 ASSERT_EQ(ret, 0);
107 sptr<AvoidArea> retArea = reply.ReadParcelable<AvoidArea>();
108 ASSERT_TRUE(retArea != nullptr);
109 ASSERT_EQ(retArea->topRect_.width_, 1200);
110 ASSERT_EQ(retArea->topRect_.height_, 127);
111 GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAvoidAreaByTypeWithSystemType end";
112 }
113
114 /**
115 * @tc.name: HandleGetAllAvoidAreasNormal
116 * @tc.desc: GetAllAvoidAreas return two Areas
117 * @tc.type: FUNC
118 */
119 HWTEST_F(SessionStubImmersiveTest, HandleGetAllAvoidAreasNormal, Function | SmallTest | Level2)
120 {
121 GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAllAvoidAreasNormal start";
122 EXPECT_CALL(*session_, GetAllAvoidAreas(_)).WillOnce(Invoke(
__anon96b7cb0a0302(std::map<AvoidAreaType, AvoidArea>& avoidAreas) 123 [](std::map<AvoidAreaType, AvoidArea>& avoidAreas) -> WSError {
124 AvoidArea mockArea;
125 mockArea.topRect_.width_ = 1200;
126 mockArea.topRect_.height_ = 127;
127 avoidAreas[AvoidAreaType::TYPE_SYSTEM] = mockArea;
128
129 AvoidArea indArea;
130 indArea.bottomRect_.width_ = 500;
131 indArea.bottomRect_.height_ = 10;
132 avoidAreas[AvoidAreaType::TYPE_NAVIGATION_INDICATOR] = indArea;
133 return WSError::WS_OK;
134 }
135 ));
136
137 MessageParcel data;
138 MessageParcel reply;
139 MessageOption option = {MessageOption::TF_SYNC};
140 data.WriteInterfaceToken(u"OHOS.ISession");
141 data.WriteUint32(static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM));
142 uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_ALL_AVOID_AREAS);
143
144 int ret = session_->OnRemoteRequest(code, data, reply, option);
145 ASSERT_EQ(ret, 0);
146
147 uint32_t areasNum = reply.ReadUint32();
148 ASSERT_EQ(areasNum, 2);
149 for (uint32_t i = 0; i < 2; i++) {
150 uint32_t type = reply.ReadUint32();
151 ASSERT_TRUE((static_cast<AvoidAreaType>(type) == AvoidAreaType::TYPE_SYSTEM) ||
152 (static_cast<AvoidAreaType>(type) == AvoidAreaType::TYPE_NAVIGATION_INDICATOR));
153
154 sptr<AvoidArea> area = reply.ReadParcelable<AvoidArea>();
155 ASSERT_TRUE(area != nullptr);
156 }
157 uint32_t errCode = reply.ReadUint32();
158 ASSERT_EQ(errCode, 0);
159 GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAllAvoidAreasNormal end";
160 }
161
162 /**
163 * @tc.name: HandleGetAllAvoidAreasEmpty
164 * @tc.desc: GetAllAvoidAreas return empty
165 * @tc.type: FUNC
166 */
167 HWTEST_F(SessionStubImmersiveTest, HandleGetAllAvoidAreasEmpty, Function | SmallTest | Level2)
168 {
169 GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAllAvoidAreasEmpty start";
170 EXPECT_CALL(*session_, GetAllAvoidAreas(_)).WillOnce(Return(WSError::WS_OK));
171
172 MessageParcel data;
173 MessageParcel reply;
174 MessageOption option = {MessageOption::TF_SYNC};
175 data.WriteInterfaceToken(u"OHOS.ISession");
176 data.WriteUint32(static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM));
177 uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_ALL_AVOID_AREAS);
178
179 int ret = session_->OnRemoteRequest(code, data, reply, option);
180 ASSERT_EQ(ret, 0);
181
182 uint32_t areasNum = reply.ReadUint32();
183 ASSERT_EQ(areasNum, 0);
184 uint32_t errCode = reply.ReadUint32();
185 ASSERT_EQ(errCode, 0);
186 GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAllAvoidAreasEmpty end";
187 }
188 }
189 } // namespace Rosen
190 } // namespace OHOS
191