• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(_, _, _, _))
47         .WillOnce(Invoke([&](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) -> int {
48             return session_->SessionStub::OnRemoteRequest(code, data, reply, option);
49         }));
50 }
51 
TearDown()52 void SessionStubImmersiveTest::TearDown()
53 {
54     session_ = nullptr;
55 }
56 
57 namespace {
58 
59 /**
60  * @tc.name: HandleGetAvoidAreaByTypeWithInvalidType
61  * @tc.desc: GetAvoidAreaByType with invalid type
62  * @tc.type: FUNC
63  */
64 HWTEST_F(SessionStubImmersiveTest, HandleGetAvoidAreaByTypeWithInvalidType, TestSize.Level1)
65 {
66     GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAvoidAreaByTypeWithInvalidType start";
67     MessageParcel data;
68     MessageParcel reply;
69     MessageOption option = { MessageOption::TF_SYNC };
70     data.WriteInterfaceToken(u"OHOS.ISession");
71     data.WriteUint32(1111); // invalid type
72     uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_AVOID_AREA);
73     int ret = session_->OnRemoteRequest(code, data, reply, option);
74     ASSERT_EQ(ret, 5);
75     GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAvoidAreaByTypeWithInvalidType end";
76 }
77 
78 /**
79  * @tc.name: HandleGetAvoidAreaByTypeWithSystemType
80  * @tc.desc: GetAvoidAreaByType with system type
81  * @tc.type: FUNC
82  */
83 HWTEST_F(SessionStubImmersiveTest, HandleGetAvoidAreaByTypeWithSystemType, TestSize.Level1)
84 {
85     GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAvoidAreaByTypeWithSystemType start";
86     AvoidArea mockArea;
87     mockArea.topRect_.width_ = 1200;
88     mockArea.topRect_.height_ = 127;
89     EXPECT_CALL(*session_, GetAvoidAreaByType(_, _, _)).WillOnce(Return(mockArea));
90 
91     MessageParcel data;
92     MessageParcel reply;
93     MessageOption option = { MessageOption::TF_SYNC };
94     data.WriteInterfaceToken(u"OHOS.ISession");
95     data.WriteUint32(static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM));
96     WSRect rect = { 0, 0, 1200, 127 };
97     data.WriteInt32(rect.posX_);
98     data.WriteInt32(rect.posY_);
99     data.WriteInt32(rect.width_);
100     data.WriteInt32(rect.height_);
101     uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_AVOID_AREA);
102     int ret = session_->OnRemoteRequest(code, data, reply, option);
103 
104     ASSERT_EQ(ret, 0);
105     sptr<AvoidArea> retArea = reply.ReadParcelable<AvoidArea>();
106     ASSERT_TRUE(retArea != nullptr);
107     ASSERT_EQ(retArea->topRect_.width_, 1200);
108     ASSERT_EQ(retArea->topRect_.height_, 127);
109     GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAvoidAreaByTypeWithSystemType end";
110 }
111 
112 /**
113  * @tc.name: HandleGetAllAvoidAreasNormal
114  * @tc.desc: GetAllAvoidAreas return two Areas
115  * @tc.type: FUNC
116  */
117 HWTEST_F(SessionStubImmersiveTest, HandleGetAllAvoidAreasNormal, TestSize.Level1)
118 {
119     GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAllAvoidAreasNormal start";
120     EXPECT_CALL(*session_, GetAllAvoidAreas(_))
__anond22a2eac0302(std::map<AvoidAreaType, AvoidArea>& avoidAreas) 121         .WillOnce(Invoke([](std::map<AvoidAreaType, AvoidArea>& avoidAreas) -> WSError {
122             AvoidArea mockArea;
123             mockArea.topRect_.width_ = 1200;
124             mockArea.topRect_.height_ = 127;
125             avoidAreas[AvoidAreaType::TYPE_SYSTEM] = mockArea;
126 
127             AvoidArea indArea;
128             indArea.bottomRect_.width_ = 500;
129             indArea.bottomRect_.height_ = 10;
130             avoidAreas[AvoidAreaType::TYPE_NAVIGATION_INDICATOR] = indArea;
131             return WSError::WS_OK;
132         }));
133 
134     MessageParcel data;
135     MessageParcel reply;
136     MessageOption option = { MessageOption::TF_SYNC };
137     data.WriteInterfaceToken(u"OHOS.ISession");
138     data.WriteUint32(static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM));
139     uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_ALL_AVOID_AREAS);
140 
141     int ret = session_->OnRemoteRequest(code, data, reply, option);
142     ASSERT_EQ(ret, 0);
143 
144     uint32_t areasNum = reply.ReadUint32();
145     ASSERT_EQ(areasNum, 2);
146     for (uint32_t i = 0; i < 2; i++) {
147         uint32_t type = reply.ReadUint32();
148         ASSERT_TRUE((static_cast<AvoidAreaType>(type) == AvoidAreaType::TYPE_SYSTEM) ||
149                     (static_cast<AvoidAreaType>(type) == AvoidAreaType::TYPE_NAVIGATION_INDICATOR));
150 
151         sptr<AvoidArea> area = reply.ReadParcelable<AvoidArea>();
152         ASSERT_TRUE(area != nullptr);
153     }
154     uint32_t errCode = reply.ReadUint32();
155     ASSERT_EQ(errCode, 0);
156     GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAllAvoidAreasNormal end";
157 }
158 
159 /**
160  * @tc.name: HandleGetAllAvoidAreasEmpty
161  * @tc.desc: GetAllAvoidAreas return empty
162  * @tc.type: FUNC
163  */
164 HWTEST_F(SessionStubImmersiveTest, HandleGetAllAvoidAreasEmpty, TestSize.Level1)
165 {
166     GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAllAvoidAreasEmpty start";
167     EXPECT_CALL(*session_, GetAllAvoidAreas(_)).WillOnce(Return(WSError::WS_OK));
168 
169     MessageParcel data;
170     MessageParcel reply;
171     MessageOption option = { MessageOption::TF_SYNC };
172     data.WriteInterfaceToken(u"OHOS.ISession");
173     data.WriteUint32(static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM));
174     uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_ALL_AVOID_AREAS);
175 
176     int ret = session_->OnRemoteRequest(code, data, reply, option);
177     ASSERT_EQ(ret, 0);
178 
179     uint32_t areasNum = reply.ReadUint32();
180     ASSERT_EQ(areasNum, 0);
181     uint32_t errCode = reply.ReadUint32();
182     ASSERT_EQ(errCode, 0);
183     GTEST_LOG_(INFO) << "SessionStubImmersiveTest::HandleGetAllAvoidAreasEmpty end";
184 }
185 } // namespace
186 } // namespace Rosen
187 } // namespace OHOS
188