• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)24 void MainAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
25     const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
26     const sptr<IRemoteObject> &token)
27 {
28     APP_LOGI("MainAbility::Init");
29     Ability::Init(abilityInfo, application, handler, token);
30 }
31 
OnStart(const Want & want)32 void MainAbility::OnStart(const Want &want)
33 {
34     APP_LOGI("MainAbility::onStart");
35     SubscribeEvent();
36     Ability::OnStart(want);
37     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, Ability::GetState(), "onStart");
38 }
39 
OnStop()40 void MainAbility::OnStop()
41 {
42     APP_LOGI("MainAbility::OnStop");
43     Ability::OnStop();
44     CommonEventManager::UnSubscribeCommonEvent(subscriber);
45     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, Ability::GetState(), "OnStop");
46 }
47 
OnActive()48 void MainAbility::OnActive()
49 {
50     APP_LOGI("MainAbility::OnActive");
51     Ability::OnActive();
52     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, Ability::GetState(), "OnActive");
53 }
54 
OnInactive()55 void MainAbility::OnInactive()
56 {
57     APP_LOGI("MainAbility::OnInactive");
58     Ability::OnInactive();
59     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, Ability::GetState(), "OnInactive");
60 }
61 
OnBackground()62 void MainAbility::OnBackground()
63 {
64     APP_LOGI("MainAbility::OnBackground");
65     Ability::OnBackground();
66     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, Ability::GetState(), "OnBackground");
67 }
68 
OnForeground(const Want & want)69 void MainAbility::OnForeground(const Want &want)
70 {
71     APP_LOGI("MainAbility::OnForeground");
72     Ability::OnForeground(want);
73     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, Ability::GetState(), "OnForeground");
74 }
75 
OnAbilityResult(int requestCode,int resultCode,const Want & resultData)76 void MainAbility::OnAbilityResult(int requestCode, int resultCode, const Want &resultData)
77 {
78     APP_LOGI("MainAbility::OnAbilityResult");
79     Ability::OnAbilityResult(requestCode, resultCode, resultData);
80     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_ON_ABILITY_RESULT, 0, resultData.ToUri());
81 }
82 
OnBackPressed()83 void MainAbility::OnBackPressed()
84 {
85     APP_LOGI("MainAbility::OnBackPressed");
86     Ability::OnBackPressed();
87     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_ON_BACK_PRESSED, 0, "");
88 }
89 
OnNewWant(const Want & want)90 void MainAbility::OnNewWant(const Want &want)
91 {
92     APP_LOGI("MainAbility::OnNewWant");
93     Ability::OnNewWant(want);
94     TestUtils::PublishEvent(g_EVENT_RESP_FIRST_ON_NEW_WANT, 0, want.ToUri());
95 }
96 
SubscribeEvent()97 void MainAbility::SubscribeEvent()
98 {
99     std::vector<std::string> eventList = {
100         g_EVENT_REQU_FIRST,
101     };
102     MatchingSkills matchingSkills;
103     for (const auto &e : eventList) {
104         matchingSkills.AddEvent(e);
105     }
106     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
107     subscribeInfo.SetPriority(1);
108     subscriber = std::make_shared<FirstEventSubscriber>(subscribeInfo, this);
109     CommonEventManager::SubscribeCommonEvent(subscriber);
110 }
111 
OnReceiveEvent(const CommonEventData & data)112 void FirstEventSubscriber::OnReceiveEvent(const CommonEventData &data)
113 {
114     APP_LOGI("FirstEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
115     APP_LOGI("FirstEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
116     APP_LOGI("FirstEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
117     auto eventName = data.GetWant().GetAction();
118     if (std::strcmp(eventName.c_str(), g_EVENT_REQU_FIRST.c_str()) == 0) {
119         auto target = data.GetData();
120         auto handle = 0;
121         auto api = 1;
122         auto code = 2;
123         auto caseInfo = TestUtils::split(target, "_");
124         auto paramMinSize = 3;
125         if (caseInfo.size() < static_cast<unsigned int>(paramMinSize)) {
126             return;
127         }
128         if (mapTestFunc_.find(caseInfo[handle]) != mapTestFunc_.end()) {
129             mapTestFunc_[caseInfo[handle]](std::stoi(caseInfo[api]), std::stoi(caseInfo[code]), data.GetCode());
130         } else {
131             APP_LOGI("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
132         }
133     }
134 }
135 
TestAbility(int apiIndex,int caseIndex,int code)136 void MainAbility::TestAbility(int apiIndex, int caseIndex, int code)
137 {
138     APP_LOGI("MainAbility::TestAbility");
139     if (mapCase_.find(apiIndex) != mapCase_.end()) {
140         if (caseIndex < (int)mapCase_[apiIndex].size()) {
141             mapCase_[apiIndex][caseIndex](code);
142         }
143     }
144 }
145 
146 // get current ability name
GetAbilityNameCase1(int code)147 void MainAbility::GetAbilityNameCase1(int code)
148 {
149     std::string abilityName = Ability::GetAbilityName();
150     bool result = abilityName == "MainAbility";
151     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
152 }
153 
154 // get AbilityPackage object
GetAbilityPackageCase1(int code)155 void MainAbility::GetAbilityPackageCase1(int code)
156 {
157     auto abilityPackage = Ability::GetAbilityPackage();
158     bool result = abilityPackage != nullptr;
159     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
160 }
161 
162 // get want from empty Want
GetWantCase1(int code)163 void MainAbility::GetWantCase1(int code)
164 {
165     auto getWant = Ability::GetWant();
166     bool result = getWant == nullptr;
167     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
168 }
169 
170 // set and get want
GetWantCase2(int code)171 void MainAbility::GetWantCase2(int code)
172 {
173     std::string action = "action";
174     Want setWant;
175     setWant.SetAction(action);
176     Ability::SetWant(setWant);
177     bool result = Ability::GetWant()->GetAction() == action;
178     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
179 }
180 
181 // get want repeatedly
GetWantCase3(int code)182 void MainAbility::GetWantCase3(int code)
183 {
184     std::string action = "action";
185     bool result = true;
186     std::string tmpAction;
187     for (int i = 0; i < pressureTimes; i++) {
188         Want setWant;
189         tmpAction = action + std::to_string(i);
190         setWant.SetAction(tmpAction);
191         Ability::SetWant(setWant);
192         result = result && Ability::GetWant()->GetAction() == tmpAction;
193     }
194     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
195 }
196 
GetWindowCase1(int code)197 void MainAbility::GetWindowCase1(int code)
198 {
199     APP_LOGI("MainAbility::GetWindowCase1");
200     bool result = true;
201     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
202 }
203 
DumpCase1(int code)204 void MainAbility::DumpCase1(int code)
205 {
206     APP_LOGI("MainAbility::DumpCase1");
207     std::string dumpInfo;
208     Ability::Dump(dumpInfo);
209     bool result = true;
210     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
211 }
212 
213 // set empty Want
SetWantCase1(int code)214 void MainAbility::SetWantCase1(int code)
215 {
216     std::string empty;
217     Want setWant;
218     Ability::SetWant(setWant);
219     bool result = Ability::GetWant()->GetAction() == empty;
220     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
221 }
222 
223 // set want repeatedly
SetWantCase2(int code)224 void MainAbility::SetWantCase2(int code)
225 {
226     std::string action = "action";
227     std::string tmpAction;
228     for (int i = 0; i < pressureTimes; i++) {
229         Want setWant;
230         tmpAction = action + std::to_string(i);
231         setWant.SetAction(tmpAction);
232         Ability::SetWant(setWant);
233     }
234     bool result = Ability::GetWant()->GetAction() == tmpAction;
235     TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
236 }
237 
238 REGISTER_AA(MainAbility)
239 }  // namespace AppExecFwk
240 }  // namespace OHOS
241