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 #ifndef _AMS_KIT_SYSTEM_TEST_MAIN_ABILITY_H_ 17 #define _AMS_KIT_SYSTEM_TEST_MAIN_ABILITY_H_ 18 #include "ability.h" 19 #include "ability_loader.h" 20 #include "common_event.h" 21 #include "common_event_manager.h" 22 #include "kit_test_common_info.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 class FirstEventSubscriber; 27 class MainAbility : public Ability { 28 public: MainAbility()29 MainAbility() 30 { 31 mapCase_ = { 32 {(int)AbilityApi::GetAbilityName, 33 { 34 [this](int code) { GetAbilityNameCase1(code); }, 35 }}, 36 {(int)AbilityApi::GetAbilityPackage, 37 { 38 [this](int code) { GetAbilityPackageCase1(code); }, 39 }}, 40 {(int)AbilityApi::GetWant, 41 { 42 [this](int code) { GetWantCase1(code); }, 43 [this](int code) { GetWantCase2(code); }, 44 [this](int code) { GetWantCase3(code); }, 45 }}, 46 {(int)AbilityApi::GetWindow, 47 { 48 [this](int code) { GetWindowCase1(code); }, 49 }}, 50 {(int)AbilityApi::Dump, 51 { 52 [this](int code) { DumpCase1(code); }, 53 }}, 54 {(int)AbilityApi::SetWant, 55 { 56 [this](int code) { SetWantCase1(code); }, 57 [this](int code) { SetWantCase2(code); }, 58 }}, 59 }; 60 } 61 ~MainAbility() = default; 62 63 void SubscribeEvent(); 64 void TestAbility(int apiIndex, int caseIndex, int code); 65 void GetAbilityNameCase1(int code); 66 void GetAbilityPackageCase1(int code); 67 void GetWantCase1(int code); 68 void GetWantCase2(int code); 69 void GetWantCase3(int code); 70 void GetWindowCase1(int code); 71 void DumpCase1(int code); 72 void SetWantCase1(int code); 73 void SetWantCase2(int code); 74 75 std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_; 76 std::shared_ptr<FirstEventSubscriber> subscriber; 77 78 protected: 79 void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application, 80 std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 81 virtual void OnStart(const Want &want) override; 82 virtual void OnStop() override; 83 virtual void OnActive() override; 84 virtual void OnInactive() override; 85 virtual void OnBackground() override; 86 virtual void OnForeground(const Want &want) override; 87 virtual void OnAbilityResult(int requestCode, int resultCode, const Want &resultData) override; 88 virtual void OnBackPressed() override; 89 virtual void OnNewWant(const Want &want) override; 90 }; 91 class FirstEventSubscriber : public EventFwk::CommonEventSubscriber { 92 public: FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp,MainAbility * ability)93 FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp, MainAbility *ability) : CommonEventSubscriber(sp) 94 { 95 mapTestFunc_ = { 96 {"Ability", [this](int apiIndex, int caseIndex, int code) { TestAbility(apiIndex, caseIndex, code); }}, 97 }; 98 mainAbility = ability; 99 } 100 ~FirstEventSubscriber() = default; 101 TestAbility(int apiIndex,int caseIndex,int code)102 void TestAbility(int apiIndex, int caseIndex, int code) 103 { 104 mainAbility->TestAbility(apiIndex, caseIndex, code); 105 } 106 107 virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); 108 109 MainAbility *mainAbility; 110 std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_; 111 }; 112 } // namespace AppExecFwk 113 } // namespace OHOS 114 #endif // _AMS_KIT_SYSTEM_TEST_MAIN_ABILITY_H_