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 <gtest/gtest.h>
17
18 #include "iremote_object_mocker.h"
19 #include "modal_system_ui_extension.h"
20 #include "mock_message_parcel.h"
21 #include "wm_common.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 class ModalSystemUiExtensionTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 };
35
SetUpTestCase()36 void ModalSystemUiExtensionTest::SetUpTestCase()
37 {
38 }
39
TearDownTestCase()40 void ModalSystemUiExtensionTest::TearDownTestCase()
41 {
42 }
43
SetUp()44 void ModalSystemUiExtensionTest::SetUp()
45 {
46 }
47
TearDown()48 void ModalSystemUiExtensionTest::TearDown()
49 {
50 }
51
52 namespace {
53 /**
54 * @tc.name: ModalSystemUiExtensionConnection01
55 * @tc.desc: connect modal system ui_extension
56 * @tc.type: FUNC
57 */
58 HWTEST_F(ModalSystemUiExtensionTest, ModalSystemUiExtensionConnection01, Function | SmallTest | Level2)
59 {
60 auto connection = new(std::nothrow)ModalSystemUiExtension();
61 if (connection == nullptr) {
62 return;
63 }
64 OHOS::AAFwk::Want want;
65 ASSERT_FALSE(connection->CreateModalUIExtension(want));
66 delete connection;
67 }
68
69 /**
70 * @tc.name: ToString
71 * @tc.desc: ToString
72 * @tc.type: FUNC
73 */
74 HWTEST_F(ModalSystemUiExtensionTest, ToString, Function | SmallTest | Level2)
75 {
76 AAFwk::WantParams wantParams;
77 std::string ret = ModalSystemUiExtension::ToString(wantParams);
78 ASSERT_EQ("{}", ret);
79 }
80
81 /**
82 * @tc.name: DialogAbilityConnectionOnAbilityConnectDone
83 * @tc.desc: DialogAbilityConnectionOnAbilityConnectDone
84 * @tc.type: FUNC
85 */
86 HWTEST_F(ModalSystemUiExtensionTest, DialogAbilityConnectionOnAbilityConnectDone, Function | SmallTest | Level2)
87 {
88 AAFwk::Want want;
89 auto connection = sptr<ModalSystemUiExtension::DialogAbilityConnection>::MakeSptr(want);
90 ASSERT_NE(connection, nullptr);
91 AppExecFwk::ElementName element;
92
93 connection->OnAbilityConnectDone(element, nullptr, 0);
94
95 auto remoteObject = sptr<MockIRemoteObject>::MakeSptr();
96 connection->OnAbilityConnectDone(element, remoteObject, 0);
97
98 remoteObject->sendRequestResult_ = 1;
99 connection->OnAbilityConnectDone(element, remoteObject, 0);
100 static constexpr uint32_t WAIT_TIME_MICROSECONDS = 5200000;
101 usleep(WAIT_TIME_MICROSECONDS);
102 }
103
104 /**
105 * @tc.name: DialogAbilityConnectionOnAbilityDisconnectDone
106 * @tc.desc: DialogAbilityConnectionOnAbilityDisconnectDone
107 * @tc.type: FUNC
108 */
109 HWTEST_F(ModalSystemUiExtensionTest, DialogAbilityConnectionOnAbilityDisconnectDone, Function | SmallTest | Level2)
110 {
111 AAFwk::Want want;
112 auto connection = sptr<ModalSystemUiExtension::DialogAbilityConnection>::MakeSptr(want);
113 ASSERT_NE(connection, nullptr);
114 AppExecFwk::ElementName element;
115 connection->OnAbilityDisconnectDone(element, 0);
116 }
117
118 /**
119 * @tc.name: DialogAbilityConnectionSendWant
120 * @tc.desc: DialogAbilityConnectionSendWant
121 * @tc.type: FUNC
122 */
123 HWTEST_F(ModalSystemUiExtensionTest, DialogAbilityConnectionSendWant, Function | SmallTest | Level2)
124 {
125 AAFwk::Want want;
126 auto connection = sptr<ModalSystemUiExtension::DialogAbilityConnection>::MakeSptr(want);
127 auto remoteObject = sptr<MockIRemoteObject>::MakeSptr();
128
129 MockMessageParcel::SetWriteInt32ErrorFlag(true);
130 EXPECT_FALSE(connection->SendWant(remoteObject));
131 MockMessageParcel::ClearAllErrorFlag();
132
133 MockMessageParcel::SetWriteString16ErrorFlag(true);
134 EXPECT_FALSE(connection->SendWant(remoteObject));
135 MockMessageParcel::ClearAllErrorFlag();
136
137 EXPECT_TRUE(connection->SendWant(remoteObject));
138
139 remoteObject->sendRequestResult_ = 1;
140 EXPECT_FALSE(connection->SendWant(remoteObject));
141 }
142 }
143 } // Rosen
144 } // OHOS