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 #include <map>
18 #include <string>
19 #include <thread>
20 #include <iostream>
21
22 #include "ability_lifecycle_executor.h"
23
24 #define private public
25 #include "iability_monitor.h"
26 #include "iability_stage_monitor.h"
27 #include "ability_manager_client.h"
28 #undef private
29
30 #include "hilog_wrapper.h"
31 #include "mock_ability_delegator_stub.h"
32
33 using namespace testing::ext;
34 using namespace OHOS;
35 using namespace OHOS::AppExecFwk;
36 using namespace OHOS::AAFwk;
37
38 namespace {
39 const std::string ABILITY_NAME = "com.example.myapplication.MainAbilitymodule";
40 const std::string PROPERTY_ABILITY_NAME = "com.example.myapplication.MainAbilitymodule";
41 const std::string PROPERTY_ABILITY_NAME1 = "com.example.myapplication.MainAbility1module";
42 const std::string ABILITY_STAGE_MODULE_NAME = "com.example.entry_test";
43 const std::string ABILITY_STAGE_SOURCE_ENTRANCE = "./ets/Application/TestAbilityStage.ts";
44 const std::string PROPERTY_ABILITY_STAGE_MODULE_NAME = "com.example.entry_test";
45 const std::string PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE = "./ets/Application/TestAbilityStage.ts";
46 const std::string PROPERTY_ABILITY_STAGE_MODULE_NAME2 = "com.example.entry_test2";
47 const std::string PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE2 = "./ets/Application/TestAbilityStage2.ts";
48 }
49
50 class IabilityMonitorModuleTest : public ::testing::Test {
51 public:
52 static void SetUpTestCase();
53 static void TearDownTestCase();
54 void SetUp() override;
55 void TearDown() override;
56
57 void MakeMockObjects() const;
58 };
59
SetUpTestCase()60 void IabilityMonitorModuleTest::SetUpTestCase()
61 {}
62
TearDownTestCase()63 void IabilityMonitorModuleTest::TearDownTestCase()
64 {}
65
SetUp()66 void IabilityMonitorModuleTest::SetUp()
67 {
68 // reset optind to 0
69 optind = 0;
70
71 // make mock objects
72 MakeMockObjects();
73 }
74
TearDown()75 void IabilityMonitorModuleTest::TearDown()
76 {}
77
MakeMockObjects() const78 void IabilityMonitorModuleTest::MakeMockObjects() const
79 {
80 // mock a stub
81 auto managerStubPtr = sptr<OHOS::AAFwk::IAbilityManager>(new MockAbilityDelegatorStub());
82
83 // set the mock stub
84 auto managerClientPtr = AbilityManagerClient::GetInstance();
85 managerClientPtr->proxy_ = managerStubPtr;
86 }
87
88 /**
89 * @tc.number: Iability_Monitor_Test_0100
90 * @tc.name: Match
91 * @tc.desc: Verify the Match.
92 */
93 HWTEST_F(IabilityMonitorModuleTest, Iability_Monitor_Test_0100, Function | MediumTest | Level1)
94 {
95 HILOG_INFO("Iability_Monitor_Test_0100 is called");
96
97 IAbilityMonitor iabilityMonitor(ABILITY_NAME);
98 std::shared_ptr<ADelegatorAbilityProperty> property = std::make_shared<ADelegatorAbilityProperty>();
99 property->token_ = new MockAbilityDelegatorStub;
100 property->name_ = PROPERTY_ABILITY_NAME;
101 EXPECT_TRUE(iabilityMonitor.Match(property));
102 }
103
104 /**
105 * @tc.number: Iability_Monitor_Test_0200
106 * @tc.name: Match AbilityStage
107 * @tc.desc: Verify the AbilityStage Match.
108 * @tc.require: issueI5801E
109 */
110 HWTEST_F(IabilityMonitorModuleTest, Iability_Monitor_Test_0200, Function | MediumTest | Level1)
111 {
112 HILOG_INFO("Iability_Monitor_Test_0200 is called");
113
114 IAbilityStageMonitor stageMonitor(ABILITY_STAGE_MODULE_NAME, ABILITY_STAGE_SOURCE_ENTRANCE);
115 std::shared_ptr<DelegatorAbilityStageProperty> property = std::make_shared<DelegatorAbilityStageProperty>();
116 property->moduleName_ = PROPERTY_ABILITY_STAGE_MODULE_NAME;
117 property->srcEntrance_ = PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE;
118 EXPECT_TRUE(stageMonitor.Match(property));
119
120 std::shared_ptr<DelegatorAbilityStageProperty> property2 = std::make_shared<DelegatorAbilityStageProperty>();
121 property->moduleName_ = PROPERTY_ABILITY_STAGE_MODULE_NAME2;
122 property->srcEntrance_ = PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE;
123 EXPECT_FALSE(stageMonitor.Match(property2));
124
125 std::shared_ptr<DelegatorAbilityStageProperty> property3 = std::make_shared<DelegatorAbilityStageProperty>();
126 property->moduleName_ = PROPERTY_ABILITY_STAGE_MODULE_NAME;
127 property->srcEntrance_ = PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE2;
128 EXPECT_FALSE(stageMonitor.Match(property3));
129 }