1 /*
2 * Copyright (c) 2021 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 "main_ability.h"
17 #include "app_log_wrapper.h"
18 #include "test_utils.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 using namespace OHOS::EventFwk;
23 namespace {
24 constexpr int numZero = 0;
25 constexpr int numOne = 0;
26 constexpr int numTwo = 0;
27 constexpr int numThree = 0;
28 }
29
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)30 void MainAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
31 const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
32 const sptr<IRemoteObject> &token)
33 {
34 APP_LOGI("MainAbility::Init");
35 Ability::Init(abilityInfo, application, handler, token);
36 }
37
OnStart(const Want & want)38 void MainAbility::OnStart(const Want &want)
39 {
40 APP_LOGI("MainAbility::onStart");
41 SubscribeEvent();
42 Ability::OnStart(want);
43 TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MAIN_ABILITY_B_CODE, "onStart");
44 }
45
OnStop()46 void MainAbility::OnStop()
47 {
48 APP_LOGI("MainAbility::OnStop");
49 Ability::OnStop();
50 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
51 TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MAIN_ABILITY_B_CODE, "OnStop");
52 }
53
OnActive()54 void MainAbility::OnActive()
55 {
56 APP_LOGI("MainAbility::OnActive");
57 Ability::OnActive();
58 TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MAIN_ABILITY_B_CODE, "OnActive");
59 }
60
OnInactive()61 void MainAbility::OnInactive()
62 {
63 APP_LOGI("MainAbility::OnInactive");
64 Ability::OnInactive();
65 TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MAIN_ABILITY_B_CODE, "OnInactive");
66 }
67
OnBackground()68 void MainAbility::OnBackground()
69 {
70 APP_LOGI("MainAbility::OnBackground");
71 Ability::OnBackground();
72 TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MAIN_ABILITY_B_CODE, "OnBackground");
73 }
74
OnForeground(const Want & want)75 void MainAbility::OnForeground(const Want &want)
76 {
77 APP_LOGI("MainAbility::OnForeground");
78 Ability::OnForeground(want);
79 TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MAIN_ABILITY_B_CODE, "OnForeground");
80 }
81
~MainAbility()82 MainAbility::~MainAbility()
83 {
84 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
85 }
86
SubscribeEvent()87 void MainAbility::SubscribeEvent()
88 {
89 std::vector<std::string> eventList = {
90 g_EVENT_REQU_FIRSTB,
91 };
92 MatchingSkills matchingSkills;
93 for (const auto &e : eventList) {
94 matchingSkills.AddEvent(e);
95 }
96 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
97 subscribeInfo.SetPriority(1);
98 subscriber_ = std::make_shared<FirstEventSubscriber>(subscribeInfo);
99 subscriber_->mainAbility = this;
100 CommonEventManager::SubscribeCommonEvent(subscriber_);
101 }
102
OnReceiveEvent(const CommonEventData & data)103 void FirstEventSubscriber::OnReceiveEvent(const CommonEventData &data)
104 {
105 APP_LOGI("FirstEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
106 APP_LOGI("FirstEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
107 APP_LOGI("FirstEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
108 auto eventName = data.GetWant().GetAction();
109 if (std::strcmp(eventName.c_str(), g_EVENT_REQU_FIRSTB.c_str()) == 0) {
110 auto target = data.GetData();
111 auto caseInfo = TestUtils::split(target, "_");
112 if (caseInfo.size() < numThree) {
113 return;
114 }
115 if (mapTestFunc_.find(caseInfo[numZero]) != mapTestFunc_.end()) {
116 mapTestFunc_[caseInfo[numZero]](std::stoi(caseInfo[numOne]), std::stoi(caseInfo[numTwo]), data.GetCode());
117 } else {
118 APP_LOGI("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
119 }
120 }
121 }
122
TestAbility(int apiIndex,int caseIndex,int code)123 void MainAbility::TestAbility(int apiIndex, int caseIndex, int code)
124 {
125 APP_LOGI("MainAbility::TestAbility");
126 if (mapCase_.find(apiIndex) != mapCase_.end()) {
127 if (caseIndex < (int)mapCase_[apiIndex].size()) {
128 mapCase_[apiIndex][caseIndex](code);
129 }
130 }
131 }
132
133 REGISTER_AA(MainAbility)
134 } // namespace AppExecFwk
135 } // namespace OHOS
136