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