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_FWK_MISSIONSTACK_TEST_MAIN_ABILITY_H 17 #define AMS_FWK_MISSIONSTACK_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 "ability_append_test_info.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 using namespace OHOS::EventFwk; 27 class MainAbilityEventSubscriber; 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 MainAbility()37 MainAbility() 38 { 39 mapCase_ = { 40 {(int)MissionStackApi::LockMission, 41 { 42 [this](int code) { MissionStackCase1(code); }, 43 [this](int code) { MissionStackCase2(code); }, 44 [this](int code) { MissionStackCase3(code); }, 45 }}, 46 }; 47 } 48 49 std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_; 50 ~MainAbility(); 51 52 protected: 53 void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application, 54 std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 55 virtual void OnStart(const Want &want) override; 56 virtual void OnStop() override; 57 virtual void OnActive() override; 58 virtual void OnInactive() override; 59 virtual void OnBackground() override; 60 virtual void OnForeground(const Want &want) override; 61 virtual void OnConfigurationUpdated(const Configuration &configuration) override; 62 63 std::shared_ptr<MainAbilityEventSubscriber> subscriber_; 64 std::string callbackSeq; 65 std::string callbackUpdated; 66 }; 67 class MainAbilityEventSubscriber : public EventFwk::CommonEventSubscriber { 68 public: MainAbilityEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)69 explicit MainAbilityEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) 70 { 71 mapTestFunc_ = { 72 {"MissionStack", [this](int apiIndex, int caseIndex, int code) { TestAbility(apiIndex, caseIndex, code); }}, 73 }; 74 mainAbility = nullptr; 75 } 76 TestAbility(int apiIndex,int caseIndex,int code)77 void TestAbility(int apiIndex, int caseIndex, int code) 78 { 79 mainAbility->TestAbility(apiIndex, caseIndex, code); 80 } 81 82 virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); 83 84 MainAbility *mainAbility; 85 std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_; 86 ~MainAbilityEventSubscriber() = default; 87 }; 88 } // namespace AppExecFwk 89 } // namespace OHOS 90 #endif // AMS_FWK_MISSIONSTACK_TEST_MAIN_ABILITY_H