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_ABILITY_APPEND_TEST_A_MAIN_ABILITY_H 17 #define AMS_ABILITY_APPEND_TEST_A_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 "ability_append_test_info.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 using namespace OHOS::EventFwk; 27 class FirstEventSubscriber; 28 class MainAbility : public Ability { 29 public: 30 void SubscribeEvent(); 31 void TestAbility(int apiIndex, int caseIndex, int code); 32 33 void MissionStackCase1(int code); 34 void MissionStackCase2(int code); 35 void MissionStackCase3(int code); 36 void MissionStackCase4(int code); 37 void MissionStackCase5(int code); 38 void MissionStackCase6(int code); 39 void MissionStackCase7(int code); 40 void MissionStackCase8(int code); 41 void MissionStackCase9(int code); 42 void MissionStackCase10(int code); 43 void MissionStackCase11(int code); 44 void MissionStackCase12(int code); 45 void MissionStackCase13(int code); 46 void MissionStackCase14(int code); 47 void MissionStackCase15(int code); 48 void MissionStackCase16(int code); 49 void MissionStackCase17(int code); 50 void MissionStackCase18(int code); 51 52 void SaveAbilityStateCase1(int code); 53 void SaveAbilityStateCase2(int code); 54 void SaveAbilityStateCase3(int code); 55 56 void RestoreAbilityStateCase1(int code); 57 void RestoreAbilityStateCase2(int code); 58 void RestoreAbilityStateCase3(int code); 59 MainAbility()60 MainAbility() 61 { 62 mapCase_ = { 63 {(int)MissionStackApi::LockMission, 64 { 65 [this](int code) { MissionStackCase1(code); }, 66 [this](int code) { MissionStackCase2(code); }, 67 [this](int code) { MissionStackCase3(code); }, 68 [this](int code) { MissionStackCase4(code); }, 69 [this](int code) { MissionStackCase5(code); }, 70 [this](int code) { MissionStackCase6(code); }, 71 [this](int code) { MissionStackCase7(code); }, 72 [this](int code) { MissionStackCase8(code); }, 73 [this](int code) { MissionStackCase9(code); }, 74 [this](int code) { MissionStackCase10(code); }, 75 [this](int code) { MissionStackCase11(code); }, 76 [this](int code) { MissionStackCase12(code); }, 77 [this](int code) { MissionStackCase13(code); }, 78 [this](int code) { MissionStackCase14(code); }, 79 [this](int code) { MissionStackCase15(code); }, 80 [this](int code) { MissionStackCase16(code); }, 81 [this](int code) { MissionStackCase17(code); }, 82 [this](int code) { MissionStackCase18(code); }, 83 84 }}, 85 {(int)TestAbilityState::OnSaveAbilityState, 86 { 87 [this](int code) { SaveAbilityStateCase1(code); }, 88 [this](int code) { SaveAbilityStateCase2(code); }, 89 [this](int code) { SaveAbilityStateCase3(code); }, 90 }}, 91 {(int)TestAbilityState::OnRestoreAbilityState, 92 { 93 [this](int code) { RestoreAbilityStateCase1(code); }, 94 [this](int code) { RestoreAbilityStateCase2(code); }, 95 [this](int code) { RestoreAbilityStateCase3(code); }, 96 }}, 97 }; 98 } 99 100 std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_; 101 ~MainAbility(); 102 103 protected: 104 void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application, 105 std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 106 virtual void OnStart(const Want &want) override; 107 virtual void OnStop() override; 108 virtual void OnActive() override; 109 virtual void OnInactive() override; 110 virtual void OnBackground() override; 111 virtual void OnForeground(const Want &want) override; 112 virtual void OnRestoreAbilityState(const PacMap &inState) override; 113 virtual void OnSaveAbilityState(PacMap &outState) override; 114 std::shared_ptr<FirstEventSubscriber> subscriber_; 115 }; 116 class FirstEventSubscriber : public EventFwk::CommonEventSubscriber { 117 public: FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)118 explicit FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) 119 { 120 mapTestFunc_ = { 121 {"MissionStack", [this](int apiIndex, int caseIndex, int code) { TestAbility(apiIndex, caseIndex, code); }}, 122 {"TestAbilityState", [this](int apiIndex, int caseIndex, int code) { TestAbility(apiIndex, caseIndex, code); }}, 123 }; 124 mainAbility = nullptr; 125 } 126 TestAbility(int apiIndex,int caseIndex,int code)127 void TestAbility(int apiIndex, int caseIndex, int code) 128 { 129 mainAbility->TestAbility(apiIndex, caseIndex, code); 130 } 131 132 virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); 133 134 MainAbility *mainAbility; 135 std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_; 136 ~FirstEventSubscriber() = default; 137 }; 138 } // namespace AppExecFwk 139 } // namespace OHOS 140 #endif // AMS_ABILITY_APPEND_TEST_A_MAIN_ABILITY_H