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