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_B_SECOND_ABILITY_H 17 #define AMS_KIT_SYSTEM_TEST_B_SECOND_ABILITY_H 18 #include "ability.h" 19 #include "ability_loader.h" 20 #include "common_event.h" 21 #include "common_event_manager.h" 22 #include "task_dispatcher_test_info.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 using namespace OHOS::EventFwk; 27 class SecondEventSubscriber; 28 class SecondAbility : public Ability { 29 public: 30 void SubscribeEvent(); 31 void TestDispatcher(int apiIndex, int caseIndex, int code); 32 33 void ExtraCase1(int code); 34 SecondAbility()35 SecondAbility() 36 { 37 mapCase_ = { 38 {(int)TestFunc::EXTRA, {}}, 39 }; 40 } 41 42 std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_; 43 int callingTime = 0; 44 ~SecondAbility(); 45 46 protected: 47 void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application, 48 std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 49 virtual void OnStart(const Want &want) override; 50 virtual void OnStop() override; 51 virtual void OnActive() override; 52 virtual void OnInactive() override; 53 virtual void OnBackground() override; 54 virtual void OnForeground(const Want &want) override; 55 std::shared_ptr<SecondEventSubscriber> subscriber_; 56 }; 57 class SecondEventSubscriber : public EventFwk::CommonEventSubscriber { 58 public: SecondEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)59 explicit SecondEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) 60 { 61 mapTestFunc_ = { 62 {"Dispatcher", 63 [this](int apiIndex, int caseIndex, int code) { TestDispatcher(apiIndex, caseIndex, code); }}, 64 }; 65 mainAbility = nullptr; 66 } 67 TestDispatcher(int apiIndex,int caseIndex,int code)68 void TestDispatcher(int apiIndex, int caseIndex, int code) 69 { 70 mainAbility->TestDispatcher(apiIndex, caseIndex, code); 71 } 72 73 virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); 74 75 SecondAbility *mainAbility; 76 std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_; 77 ~SecondEventSubscriber() = default; 78 }; 79 } // namespace AppExecFwk 80 } // namespace OHOS 81 #endif // AMS_KIT_SYSTEM_TEST_B_MAIN_ABILITY_H