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 class SessionStubPropertyTest : public testing::Test {
26 public:
27 static void SetUpTestCase();
28 static void TearDownTestCase();
29 void SetUp() override;
30 void TearDown() override;
31
32 private:
33 sptr<SessionStubMocker> session_ = nullptr;
34 };
35
SetUpTestCase()36 void SessionStubPropertyTest::SetUpTestCase() {}
37
TearDownTestCase()38 void SessionStubPropertyTest::TearDownTestCase() {}
39
SetUp()40 void SessionStubPropertyTest::SetUp()
41 {
42 session_ = sptr<SessionStubMocker>::MakeSptr();
43 EXPECT_NE(nullptr, session_);
44
45 EXPECT_CALL(*session_, OnRemoteRequest(_, _, _, _)).WillOnce(Invoke(
46 [&](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) -> int {
47 return session_->SessionStub::OnRemoteRequest(code, data, reply, option);
48 }
49 ));
50 }
51
TearDown()52 void SessionStubPropertyTest::TearDown()
53 {
54 session_ = nullptr;
55 }
56
57 namespace {
58
59 /**
60 * @tc.name: HandleUpdatePropertyByAction01
61 * @tc.desc: No error
62 * @tc.type: FUNC
63 * @tc.require: #I6JLSI
64 */
65 HWTEST_F(SessionStubPropertyTest, HandleUpdatePropertyByAction01, Function | SmallTest | Level2)
66 {
67 EXPECT_CALL(*session_, UpdateSessionPropertyByAction(_, _)).WillOnce(Return(WMError::WM_OK));
68
69 MessageParcel data;
70 MessageParcel reply;
71 MessageOption option{MessageOption::TF_SYNC};
72 data.WriteInterfaceToken(u"OHOS.ISession");
73 data.WriteUint64(static_cast<uint64_t>(WSPropertyChangeAction::ACTION_UPDATE_MAIN_WINDOW_TOPMOST));
74 uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_UPDATE_SESSION_PROPERTY);
75
76 int ret = session_->OnRemoteRequest(code, data, reply, option);
77 ASSERT_EQ(ERR_NONE, ret);
78 }
79
80 /**
81 * @tc.name: HandleUpdatePropertyByAction02
82 * @tc.desc: Invalid data
83 * @tc.type: FUNC
84 * @tc.require: #I6JLSI
85 */
86 HWTEST_F(SessionStubPropertyTest, HandleUpdatePropertyByAction02, Function | SmallTest | Level2)
87 {
88 EXPECT_CALL(*session_, UpdateSessionPropertyByAction(_, _)).Times(0);
89
90 MessageParcel data;
91 MessageParcel reply;
92 MessageOption option{MessageOption::TF_SYNC};
93 data.WriteInterfaceToken(u"OHOS.ISession");
94 const uint32_t invalidData = 0;
95 data.WriteUint32(invalidData);
96 uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_UPDATE_SESSION_PROPERTY);
97
98 int ret = session_->OnRemoteRequest(code, data, reply, option);
99 ASSERT_EQ(ERR_INVALID_DATA, ret);
100 }
101
102 /**
103 * @tc.name: HandleUpdatePropertyByAction03
104 * @tc.desc: No action
105 * @tc.type: FUNC
106 * @tc.require: #I6JLSI
107 */
108 HWTEST_F(SessionStubPropertyTest, HandleUpdatePropertyByAction03, Function | SmallTest | Level2)
109 {
110 EXPECT_CALL(*session_, UpdateSessionPropertyByAction(_, _)).Times(0);
111
112 MessageParcel data;
113 MessageParcel reply;
114 MessageOption option{MessageOption::TF_SYNC};
115 data.WriteInterfaceToken(u"OHOS.ISession");
116 uint32_t code = static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_UPDATE_SESSION_PROPERTY);
117
118 int ret = session_->OnRemoteRequest(code, data, reply, option);
119 ASSERT_EQ(ERR_INVALID_DATA, ret);
120 }
121 }
122 } // namespace Rosen
123 } // namespace OHOS
124