1 /*
2 * Copyright (c) 2022-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 #include <string>
18 #include <vector>
19 #include "func_code.h"
20 #include "managed_event.h"
21 #define private public
22 #include "enterprise_conn_manager.h"
23 #undef private
24
25 using namespace testing::ext;
26 using namespace testing;
27
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 constexpr int32_t DEFAULT_USERID = 100;
32 class EnterpriseConnManagerTest : public testing::Test {
33 protected:
34 void SetUp() override;
35
36 void TearDown() override;
37
38 std::shared_ptr<EnterpriseConnManager> enterpriseConnManagerTest {nullptr};
39 };
40
SetUp()41 void EnterpriseConnManagerTest::SetUp()
42 {
43 enterpriseConnManagerTest = std::make_shared<EnterpriseConnManager>();
44 }
45
TearDown()46 void EnterpriseConnManagerTest::TearDown()
47 {
48 if (enterpriseConnManagerTest) {
49 enterpriseConnManagerTest.reset();
50 }
51 }
52
53 /**
54 * @tc.name: TestAdminConnectAbility
55 * @tc.desc: Test EnterpriseConnManager::CreateAdminConnection func.
56 * @tc.type: FUNC
57 */
58 HWTEST_F(EnterpriseConnManagerTest, TestAdminConnectAbility, TestSize.Level1)
59 {
60 std::string bundleName{"com.edm.test.demo"};
61 std::string abilityName{"com.edm.test.demo.Ability"};
62 AAFwk::Want connectWant;
63 connectWant.SetElementName(bundleName, abilityName);
64 std::shared_ptr<EnterpriseConnManager> manager = DelayedSingleton<EnterpriseConnManager>::GetInstance();
65 sptr<IEnterpriseConnection> connection = manager->CreateAdminConnection(connectWant,
66 IEnterpriseAdmin::COMMAND_ON_ADMIN_ENABLED, DEFAULT_USERID);
67 bool ret = manager->ConnectAbility(connection);
68 EXPECT_TRUE(!ret);
69
70 enterpriseConnManagerTest->Clear();
71 EXPECT_TRUE(enterpriseConnManagerTest->abilityMgr_ == nullptr);
72 }
73
74 /**
75 * @tc.name: TestBundleConnectAbility
76 * @tc.desc: Test EnterpriseConnManager::CreateBundleConnection func.
77 * @tc.type: FUNC
78 */
79 HWTEST_F(EnterpriseConnManagerTest, TestBundleConnectAbility, TestSize.Level1)
80 {
81 std::string bundleName{"com.edm.test.demo"};
82 std::string abilityName{"com.edm.test.demo.Ability"};
83 AAFwk::Want connectWant;
84 connectWant.SetElementName(bundleName, abilityName);
85 std::shared_ptr<EnterpriseConnManager> manager = DelayedSingleton<EnterpriseConnManager>::GetInstance();
86 sptr<IEnterpriseConnection> connection = manager->CreateBundleConnection(connectWant,
87 static_cast<uint32_t>(ManagedEvent::BUNDLE_ADDED), DEFAULT_USERID, "com.edm.test.bundle");
88 bool ret = manager->ConnectAbility(connection);
89 EXPECT_TRUE(!ret);
90
91 ret = manager->ConnectAbility(nullptr);
92 EXPECT_TRUE(!ret);
93
94 enterpriseConnManagerTest->Clear();
95 EXPECT_TRUE(enterpriseConnManagerTest->abilityMgr_ == nullptr);
96 }
97 } // namespace TEST
98 } // namespace EDM
99 } // namespace OHOS