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_B_MAIN_ABILITY_H 17 #define AMS_ABILITY_APPEND_TEST_B_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: MainAbility()30 MainAbility() 31 { 32 mapCase_ = {}; 33 } 34 void SubscribeEvent(); 35 void TestAbility(int apiIndex, int caseIndex, int code); 36 ~MainAbility(); 37 std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_; 38 39 protected: 40 void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application, 41 std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 42 virtual void OnStart(const Want &want) override; 43 virtual void OnStop() override; 44 virtual void OnActive() override; 45 virtual void OnInactive() override; 46 virtual void OnBackground() override; 47 virtual void OnForeground(const Want &want) override; 48 std::shared_ptr<FirstEventSubscriber> subscriber_; 49 }; 50 class FirstEventSubscriber : public EventFwk::CommonEventSubscriber { 51 public: FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)52 explicit FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) 53 { 54 mapTestFunc_ = { 55 {"Ability", [this](int apiIndex, int caseIndex, int code) { TestAbility(apiIndex, caseIndex, code); }}, 56 }; 57 mainAbility = nullptr; 58 } 59 TestAbility(int apiIndex,int caseIndex,int code)60 void TestAbility(int apiIndex, int caseIndex, int code) 61 { 62 mainAbility->TestAbility(apiIndex, caseIndex, code); 63 } 64 65 virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); 66 67 MainAbility *mainAbility; 68 std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_; ~FirstEventSubscriber()69 ~FirstEventSubscriber(){}; 70 }; 71 } // namespace AppExecFwk 72 } // namespace OHOS 73 #endif // AMS_ABILITY_APPEND_TEST_B_MAIN_ABILITY_H