• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 "communication/connect_manager.h"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 using namespace OHOS::AppDistributedKv;
22 namespace OHOS::Test {
23 class ConnectManagerTest : public testing::Test {};
24 
25 /**
26 * @tc.name: RegisterInstance
27 * @tc.desc: Register Instance.
28 * @tc.type: FUNC
29 */
30 HWTEST_F(ConnectManagerTest, RegisterInstance, TestSize.Level1)
31 {
32     auto instance = ConnectManager::GetInstance();
33     ASSERT_NE(instance, nullptr);
34     auto result = ConnectManager::RegisterInstance(std::make_shared<ConnectManager>());
35     ASSERT_TRUE(result);
36     ASSERT_NE(ConnectManager::GetInstance(), nullptr);
37 }
38 
39 /**
40 * @tc.name: CloseSession
41 * @tc.desc: Close the session related to the networkid.
42 * @tc.type: FUNC
43 */
44 HWTEST_F(ConnectManagerTest, CloseSession, TestSize.Level1)
45 {
46     auto ret = ConnectManager::GetInstance()->CloseSession("");
47     ASSERT_FALSE(ret);
__anon195cce920102(const std::string &networkId) 48     ConnectManager::GetInstance()->closeSessionTask_ = [](const std::string &networkId) {
49         return true;
50     };
51     ret = ConnectManager::GetInstance()->CloseSession("");
52     ASSERT_TRUE(ret);
53 }
54 
55 /**
56 * @tc.name: RegisterCloseSessionTask
57 * @tc.desc: Only one session closing task can be registered.
58 * @tc.type: FUNC
59 */
60 HWTEST_F(ConnectManagerTest, RegisterCloseSessionTask, TestSize.Level1)
61 {
__anon195cce920202(const std::string &networkId) 62     ConnectManager::GetInstance()->closeSessionTask_ = [](const std::string &networkId) {
63         return true;
64     };
__anon195cce920302(const std::string &networkId) 65     auto ret = ConnectManager::GetInstance()->RegisterCloseSessionTask([](const std::string &networkId) {
66         return true;
67     });
68     ASSERT_FALSE(ret);
69 }
70 } // namespace OHOS::Test