1 /*
2  * Copyright (c) 2022 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 "ability_delegator_registry.h"
19 #include "ability_runtime/context/context_impl.h"
20 #include "app_loader.h"
21 #include "hilog_tag_wrapper.h"
22 #include "mock_ability_delegator_stub.h"
23 #include "ohos_application.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::AppExecFwk;
28 using namespace OHOS::AAFwk;
29 
30 namespace {
31 const std::string KEY_TEST_BUNDLE_NAME = "-p";
32 const std::string VALUE_TEST_BUNDLE_NAME = "com.example.myapplicationmodule";
33 const std::string KEY_TEST_RUNNER_CLASS = "-s unittest";
34 const std::string VALUE_TEST_RUNNER_CLASS = "JSUserTestRunnermodule";
35 const std::string KEY_TEST_CASE = "-s class";
36 const std::string VALUE_TEST_CASE =
37     "ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction0010_module";
38 const std::string KEY_TEST_WAIT_TIMEOUT = "-w";
39 const std::string VALUE_TEST_WAIT_TIMEOUT = "160";
40 }
41 
42 class AbilityDelegatorRegistryModuleTest : public ::testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46     void SetUp() override;
47     void TearDown() override;
48 };
49 
SetUpTestCase()50 void AbilityDelegatorRegistryModuleTest::SetUpTestCase()
51 {}
52 
TearDownTestCase()53 void AbilityDelegatorRegistryModuleTest::TearDownTestCase()
54 {}
55 
SetUp()56 void AbilityDelegatorRegistryModuleTest::SetUp()
57 {}
58 
TearDown()59 void AbilityDelegatorRegistryModuleTest::TearDown()
60 {}
61 
62 /**
63  * @tc.number: Ability_Delegator_Registry_Module_Test_0100
64  * @tc.name: RegisterInstance and GetAbilityDelegator and GetArguments
65  * @tc.desc: Verify the RegisterInstance and GetAbilityDelegator and GetArguments.
66  */
67 HWTEST_F(AbilityDelegatorRegistryModuleTest,
68     Ability_Delegator_Registry_Module_Test_0100, Function | MediumTest | Level1)
69 {
70     TAG_LOGI(AAFwkTag::TEST, "Ability_Delegator_Registry_Module_Test_0100 is called");
71 
72     std::map<std::string, std::string> paras;
73     paras.emplace(KEY_TEST_BUNDLE_NAME, VALUE_TEST_BUNDLE_NAME);
74     paras.emplace(KEY_TEST_RUNNER_CLASS, VALUE_TEST_RUNNER_CLASS);
75     paras.emplace(KEY_TEST_CASE, VALUE_TEST_CASE);
76     paras.emplace(KEY_TEST_WAIT_TIMEOUT, VALUE_TEST_WAIT_TIMEOUT);
77 
78     Want want;
79     for (auto para : paras) {
80         want.SetParam(para.first, para.second);
81     }
82     std::shared_ptr<AbilityDelegatorArgs> abilityArgs = std::make_shared<AbilityDelegatorArgs>(want);
83     std::unique_ptr<TestRunner> testRunner = TestRunner::Create(
84         std::shared_ptr<OHOSApplication>(ApplicationLoader::GetInstance().GetApplicationByName())->GetRuntime(),
85         abilityArgs,
86         true);
87     std::shared_ptr<AbilityDelegator> abilityDelegator =
88         std::make_shared<AbilityDelegator>(nullptr, std::move(testRunner), nullptr);
89     AbilityDelegatorRegistry::RegisterInstance(abilityDelegator, abilityArgs);
90 
91     EXPECT_EQ(AbilityDelegatorRegistry::GetAbilityDelegator(), abilityDelegator);
92     EXPECT_EQ(AbilityDelegatorRegistry::GetArguments(), abilityArgs);
93 }
94