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 "second_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 using namespace OHOS::AAFwk;
24 constexpr int index_f = 0;
25 constexpr int index_s = 1;
26 constexpr int index_t = 2;
27 constexpr int paramCnt = 3;
28
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)29 void SecondAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
30 const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
31 const sptr<IRemoteObject> &token)
32 {
33 APP_LOGI("SecondAbility::Init");
34 Ability::Init(abilityInfo, application, handler, token);
35 }
36
~SecondAbility()37 SecondAbility::~SecondAbility()
38 {
39 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
40 }
41
OnStart(const Want & want)42 void SecondAbility::OnStart(const Want &want)
43 {
44 APP_LOGI("SecondAbility::OnStart");
45 SubscribeEvent();
46 Ability::OnStart(want);
47 callbackSeq += "OnStart";
48 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, "OnStart");
49 }
50
OnStop()51 void SecondAbility::OnStop()
52 {
53 APP_LOGI("SecondAbility::OnStop");
54 Ability::OnStop();
55 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
56 callbackSeq = "OnStop";
57 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, callbackSeq);
58 callbackSeq = "";
59 }
60
OnActive()61 void SecondAbility::OnActive()
62 {
63 APP_LOGI("SecondAbility::OnActive====<");
64 Ability::OnActive();
65 callbackSeq = "OnActive";
66 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, callbackSeq);
67 callbackSeq = "";
68 }
69
OnConfigurationUpdated(const Configuration & configuration)70 void SecondAbility::OnConfigurationUpdated(const Configuration &configuration)
71 {
72 APP_LOGI("SecondAbility::OnConfigurationUpdated====<");
73 Ability::OnConfigurationUpdated(configuration);
74 callbackUpdated += "Updated"; // UpdatedUpdated
75 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, callbackUpdated);
76 }
77
OnInactive()78 void SecondAbility::OnInactive()
79 {
80 APP_LOGI("SecondAbility::OnInactive");
81 Ability::OnInactive();
82 callbackSeq += "OnInactive";
83 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, "OnInactive");
84 }
85
OnBackground()86 void SecondAbility::OnBackground()
87 {
88 APP_LOGI("SecondAbility::OnBackground");
89 Ability::OnBackground();
90 callbackSeq += "OnBackground";
91 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, "OnBackground");
92 }
93
OnForeground(const Want & want)94 void SecondAbility::OnForeground(const Want &want)
95 {
96 APP_LOGI("SecondAbility::OnForeground");
97 Ability::OnForeground(want);
98 callbackSeq += "OnForeground";
99 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, "OnForeground");
100 }
101
OnRestoreAbilityState(const PacMap & inState)102 void SecondAbility::OnRestoreAbilityState(const PacMap &inState)
103 {
104 APP_LOGI("SecondAbility::OnRestoreAbilityState");
105 Ability::OnRestoreAbilityState(inState);
106 TestUtils::PublishEvent(
107 g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, "OnRestoreAbilityState");
108 }
OnSaveAbilityState(PacMap & outState)109 void SecondAbility::OnSaveAbilityState(PacMap &outState)
110 {
111 APP_LOGI("SecondAbility::OnSaveAbilityState");
112 Ability::OnSaveAbilityState(outState);
113 TestUtils::PublishEvent(
114 g_EVENT_RESP_SECOND_LIFECYCLE_SUBSIDIARY, SECOND_ABILITY_CODE_SUBSIDIARY, "OnSaveAbilityState");
115 }
116
SubscribeEvent()117 void SecondAbility::SubscribeEvent()
118 {
119 std::vector<std::string> eventList = {
120 g_EVENT_REQU_SECOND_SUBSIDIARY,
121 };
122 MatchingSkills matchingSkills;
123 for (const auto &e : eventList) {
124 matchingSkills.AddEvent(e);
125 }
126 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
127 subscribeInfo.SetPriority(1);
128 subscriber_ = std::make_shared<SecondAbilityEventSubscriber>(subscribeInfo);
129 subscriber_->secondAbility = this;
130 CommonEventManager::SubscribeCommonEvent(subscriber_);
131 }
132
OnReceiveEvent(const CommonEventData & data)133 void SecondAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
134 {
135 APP_LOGI("SecondAbilityEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
136 APP_LOGI("SecondAbilityEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
137 APP_LOGI("SecondAbilityEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
138 auto eventName = data.GetWant().GetAction();
139 if (std::strcmp(eventName.c_str(), g_EVENT_REQU_SECOND.c_str()) == 0) {
140 auto target = data.GetData();
141 auto caseInfo = TestUtils::split(target, "_");
142 if (caseInfo.size() < paramCnt) {
143 return;
144 }
145 if (mapTestFunc_.find(caseInfo[index_f]) != mapTestFunc_.end()) {
146 mapTestFunc_[caseInfo[index_f]](std::stoi(caseInfo[index_s]), std::stoi(caseInfo[index_t]), data.GetCode());
147 } else {
148 APP_LOGI("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
149 }
150 }
151 }
152
TestAbility(int apiIndex,int caseIndex,int code)153 void SecondAbility::TestAbility(int apiIndex, int caseIndex, int code)
154 {
155 APP_LOGI("SecondAbility::TestAbility");
156 if (mapCase_.find(apiIndex) != mapCase_.end()) {
157 if (caseIndex < (int)mapCase_[apiIndex].size()) {
158 mapCase_[apiIndex][caseIndex](code);
159 }
160 }
161 }
162
MissionStackCase1(int code)163 void SecondAbility::MissionStackCase1(int code)
164 {
165 APP_LOGI("SecondAbility::MissionStackCase1====<");
166 bool result = true;
167 result = true;
168
169 TestUtils::PublishEvent(g_EVENT_RESP_SECOND, code, std::to_string(result));
170 }
171
MissionStackCase2(int code)172 void SecondAbility::MissionStackCase2(int code)
173 {
174 bool result = true;
175 result = true;
176 TestUtils::PublishEvent(g_EVENT_RESP_SECOND, code, std::to_string(result));
177 }
178
179 REGISTER_AA(SecondAbility)
180 } // namespace AppExecFwk
181 } // namespace OHOS
182