• 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 <cstdint>
17 #include <cstring>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <map>
21 #include <mutex>
22 
23 #define private public
24 #include "ability_manager_client.h"
25 #include "iservice_registry.h"
26 #include "migration_manager_adapter_impl.h"
27 #include "system_ability.h"
28 #include "system_ability_definition.h"
29 
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace NWeb {
35 
36 class MigrationListenerAdapterTest : public MigrationListenerAdapter {
37 public:
38     MigrationListenerAdapterTest() = default;
39 
40     virtual ~MigrationListenerAdapterTest() = default;
41 
OnMigrationReply(int32_t errorCode,int32_t succussCount,const std::vector<int32_t> & errorIndex,const std::vector<int32_t> & codeList)42     void OnMigrationReply(int32_t errorCode, int32_t succussCount, const std::vector<int32_t>& errorIndex,
43         const std::vector<int32_t>& codeList) override
44     {}
45 };
46 
47 class MigrationManagerAdapterlmplTest : public testing::Test {
48 public:
49     static void SetUpTestCase(void);
50     static void TearDownTestCase(void);
51     void SetUp();
52     void TearDown();
53 };
54 
SetUpTestCase(void)55 void MigrationManagerAdapterlmplTest::SetUpTestCase(void) {}
56 
TearDownTestCase(void)57 void MigrationManagerAdapterlmplTest::TearDownTestCase(void) {}
58 
SetUp(void)59 void MigrationManagerAdapterlmplTest::SetUp(void) {}
60 
TearDown(void)61 void MigrationManagerAdapterlmplTest::TearDown(void) {}
62 
63 /**
64  * @tc.name: MigrationManagerAdapterlmplTest_001
65  * @tc.desc: MigrationListenAdapter Func
66  * @tc.type: FUNC
67  * @tc.require: AR000I7I57
68  */
69 HWTEST_F(MigrationManagerAdapterlmplTest, MigrationManagerAdapterlmplTest_001, TestSize.Level1)
70 {
71     std::shared_ptr<MigrationListenerAdapter> listener = std::make_shared<MigrationListenerAdapterTest>();
72     std::unique_ptr<MigrationManagerAdapterImpl::MigrationListenerAdapterImpl> migrationListener =
73         std::make_unique<MigrationManagerAdapterImpl::MigrationListenerAdapterImpl>(listener);
74     std::vector<int32_t> vec(0);
75     migrationListener->SetJsonData("JsonTest");
76     migrationListener->SetInterfaceToken("InterfaceTokenTest");
77     migrationListener->OnMigrationReply(0, 0, vec, vec);
78     EXPECT_NE(migrationListener, nullptr);
79 }
80 
81 /**
82  * @tc.name: MigrationManagerAdapterlmplTest_002
83  * @tc.desc: MigrationManagerAdapterlmpl SendMigrationRequest.
84  * @tc.type: FUNC
85  * @tc.require:
86  */
87 HWTEST_F(MigrationManagerAdapterlmplTest, MigrationManagerAdapterlmplTest_002, TestSize.Level1)
88 {
89     std::shared_ptr<MigrationManagerAdapterImpl> migrationManagermpl = std::make_shared<MigrationManagerAdapterImpl>();
90     std::shared_ptr<MigrationListenerAdapter> listener = std::make_shared<MigrationListenerAdapterTest>();
91     EXPECT_NE(migrationManagermpl, nullptr);
92     EXPECT_NE(listener, nullptr);
93     EXPECT_EQ(migrationManagermpl->RegisterMigrationListener(listener), 0);
94     migrationManagermpl->SetMigrationParam("", "", "");
95     EXPECT_FALSE(migrationManagermpl->SendMigrationRequest(std::make_shared<std::string>("")));
96 }
97 
98 /**
99  *
100  * @tc.name: MigrationManagerAdapterlmplTest_003
101  * @tc.desc: MigrationManagerAdapterlmpl OnAbilityConnectDone
102  * @tc.type: FUNC
103  * @tc.require:
104  */
105 HWTEST_F(MigrationManagerAdapterlmplTest, MigrationManagerAdapterlmplTest_003, TestSize.Level1)
106 {
107     std::shared_ptr<MigrationManagerAdapterImpl> migrationManagermpl = std::make_shared<MigrationManagerAdapterImpl>();
108     std::shared_ptr<MigrationListenerAdapter> listener = std::make_shared<MigrationListenerAdapterTest>();
109     std::unique_ptr<MigrationManagerAdapterImpl::MigrationListenerAdapterImpl> migrationListener =
110         std::make_unique<MigrationManagerAdapterImpl::MigrationListenerAdapterImpl>(listener);
111     EXPECT_EQ(migrationManagermpl->RegisterMigrationListener(listener), 0);
112 
113     const AppExecFwk::ElementName elementName("aaa", "bbb", "ccc");
114     OHOS::sptr<OHOS::IRemoteObject> sp(nullptr);
115     migrationListener->OnAbilityConnectDone(elementName, sp, 0);
116 
117     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
118     sp = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
119     EXPECT_NE(sp, nullptr);
120     migrationListener->OnAbilityConnectDone(elementName, sp, 0);
121     EXPECT_TRUE(migrationManagermpl->SendMigrationRequest(std::make_shared<std::string>("")));
122 
123     migrationListener->OnAbilityDisconnectDone(elementName, 0);
124     EXPECT_FALSE(migrationManagermpl->SendMigrationRequest(std::make_shared<std::string>("")));
125 }
126 
127 } // namespace NWeb
128 } // namespace OHOS