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
~MainAbility()38 MainAbility::~MainAbility()
39 {
40 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
41 }
42
OnStart(const Want & want)43 void MainAbility::OnStart(const Want &want)
44 {
45 APP_LOGI("MainAbility::onStart");
46 SubscribeEvent();
47 Ability::OnStart(want);
48 TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, MAIN_ABILITY_A_CODE, "onStart");
49 }
50
OnStop()51 void MainAbility::OnStop()
52 {
53 APP_LOGI("MainAbility::OnStop");
54 Ability::OnStop();
55 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
56 TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, MAIN_ABILITY_A_CODE, "OnStop");
57 }
58
OnActive()59 void MainAbility::OnActive()
60 {
61 APP_LOGI("MainAbility::OnActive");
62 Ability::OnActive();
63 TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, MAIN_ABILITY_A_CODE, "OnActive");
64 }
65
OnInactive()66 void MainAbility::OnInactive()
67 {
68 APP_LOGI("MainAbility::OnInactive");
69 Ability::OnInactive();
70 TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, MAIN_ABILITY_A_CODE, "OnInactive");
71 }
72
OnBackground()73 void MainAbility::OnBackground()
74 {
75 APP_LOGI("MainAbility::OnBackground");
76 Ability::OnBackground();
77 TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, MAIN_ABILITY_A_CODE, "OnBackground");
78 }
79
OnForeground(const Want & want)80 void MainAbility::OnForeground(const Want &want)
81 {
82 APP_LOGI("MainAbility::OnForeground");
83 Ability::OnForeground(want);
84 TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, MAIN_ABILITY_A_CODE, "OnForeground");
85 }
86
OnSetCaller()87 Uri MainAbility::OnSetCaller()
88 {
89 APP_LOGI("MainAbility::OnSetCaller");
90 TestUtils::PublishEvent(g_EVENT_RESP_FIRST_LIFECYCLE, MAIN_ABILITY_A_CODE, "OnSetCaller");
91 return Ability::OnSetCaller();
92 }
93
SubscribeEvent()94 void MainAbility::SubscribeEvent()
95 {
96 std::vector<std::string> eventList = {
97 g_EVENT_REQU_FIRST,
98 };
99 MatchingSkills matchingSkills;
100 for (const auto &e : eventList) {
101 matchingSkills.AddEvent(e);
102 }
103 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
104 subscribeInfo.SetPriority(1);
105 subscriber_ = std::make_shared<FirstEventSubscriber>(subscribeInfo);
106 subscriber_->mainAbility = this;
107 CommonEventManager::SubscribeCommonEvent(subscriber_);
108 }
109
OnReceiveEvent(const CommonEventData & data)110 void FirstEventSubscriber::OnReceiveEvent(const CommonEventData &data)
111 {
112 APP_LOGI("FirstEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
113 APP_LOGI("FirstEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
114 APP_LOGI("FirstEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
115 auto eventName = data.GetWant().GetAction();
116 if (std::strcmp(eventName.c_str(), g_EVENT_REQU_FIRST.c_str()) == 0) {
117 auto target = data.GetData();
118 auto caseInfo = TestUtils::split(target, "_");
119 if (caseInfo.size() < numThree) {
120 return;
121 }
122 if (mapTestFunc_.find(caseInfo[numZero]) != mapTestFunc_.end()) {
123 mapTestFunc_[caseInfo[numZero]](std::stoi(caseInfo[numOne]), std::stoi(caseInfo[numTwo]), data.GetCode());
124 } else {
125 APP_LOGI("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
126 }
127 }
128 }
129
TestAbility(int apiIndex,int caseIndex,int code)130 void MainAbility::TestAbility(int apiIndex, int caseIndex, int code)
131 {
132 APP_LOGI("MainAbility::TestAbility");
133 if (mapCase_.find(apiIndex) != mapCase_.end()) {
134 if (caseIndex < (int)mapCase_[apiIndex].size()) {
135 mapCase_[apiIndex][caseIndex](code);
136 }
137 }
138 }
139
OnSetCallerCase1(int code)140 void MainAbility::OnSetCallerCase1(int code)
141 {
142 bool result = true;
143 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
144 }
145
TerminateAndRemoveMissonCase1(int code)146 void MainAbility::TerminateAndRemoveMissonCase1(int code)
147 {
148 bool result = true;
149 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
150 TerminateAndRemoveMission();
151 }
152
TerminateAbilityResultCase1(int code)153 void MainAbility::TerminateAbilityResultCase1(int code)
154 {
155 bool result = TerminateAbilityResult(1);
156 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
157 }
158
GetDispalyOrientationCase1(int code)159 void MainAbility::GetDispalyOrientationCase1(int code)
160 {
161 bool result = true;
162 int orientation = GetDisplayOrientation();
163 result = orientation == static_cast<int>(DisplayOrientation::UNSPECIFIED);
164 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
165 }
166
GetPreferencesDirCase1(int code)167 void MainAbility::GetPreferencesDirCase1(int code)
168 {
169 bool result = true;
170 string preferencesDir = GetPreferencesDir();
171 result = !preferencesDir.empty();
172 result = result && (bool)preferencesDir.find(this->GetBundleName());
173 result = result && (bool)preferencesDir.find("com.ohos.amsst.AppAppendA/files/MainAbility/preferences");
174
175 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
176 }
177
StartAbilitiesCase1(int code)178 void MainAbility::StartAbilitiesCase1(int code)
179 {
180 bool result = true;
181 std::map<std::string, std::string> params;
182 Want want1 = TestUtils::MakeWant("", "SecondAbility", "com.ohos.amsst.AppAppendA", params);
183 Want want2 = TestUtils::MakeWant("", "MainAbility", "com.ohos.amsst.AppAppendB", params);
184 std::vector<Want> wants = {want1, want2};
185 AbilityContext::StartAbilities(wants);
186 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
187 }
188
GetColorModeCase1(int code)189 void MainAbility::GetColorModeCase1(int code)
190 {
191 int colormode = GetColorMode();
192 bool result = colormode == static_cast<int>(ModuleColorMode::AUTO);
193 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
194 }
195
SetColorModeCase1(int code)196 void MainAbility::SetColorModeCase1(int code)
197 {
198 SetColorMode((int)ModuleColorMode::DARK);
199 int colormode = GetColorMode();
200 bool result = colormode == static_cast<int>(ModuleColorMode::DARK);
201 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
202 }
203
IsFirstInMissionCase1(int code)204 void MainAbility::IsFirstInMissionCase1(int code)
205 {
206 bool result = IsFirstInMission();
207 TestUtils::PublishEvent(g_EVENT_RESP_FIRST, code, std::to_string(result));
208 }
209
210 REGISTER_AA(MainAbility)
211 } // namespace AppExecFwk
212 } // namespace OHOS
213