• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_A_SECOND_ABILITY_H
17 #define AMS_KIT_SYSTEM_TEST_A_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                     [this](int code) { ExtraCase1(code); },
41                 }},
42         };
43     }
44 
45     std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_;
46     int callingTime = 0;
47     ~SecondAbility();
48 
49 protected:
50     void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application,
51         std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
52     virtual void OnStart(const Want &want) override;
53     virtual void OnStop() override;
54     virtual void OnActive() override;
55     virtual void OnInactive() override;
56     virtual void OnBackground() override;
57     virtual void OnForeground(const Want &want) override;
58     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
59     std::shared_ptr<SecondEventSubscriber> subscriber_;
60 };
61 class SecondEventSubscriber : public EventFwk::CommonEventSubscriber {
62 public:
SecondEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)63     explicit SecondEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
64     {
65         mapTestFunc_ = {
66             {"Dispatcher",
67                 [this](int apiIndex, int caseIndex, int code) { TestDispatcher(apiIndex, caseIndex, code); }},
68         };
69         mainAbility = nullptr;
70     }
71 
TestDispatcher(int apiIndex,int caseIndex,int code)72     void TestDispatcher(int apiIndex, int caseIndex, int code)
73     {
74         mainAbility->TestDispatcher(apiIndex, caseIndex, code);
75     }
76 
77     virtual void OnReceiveEvent(const EventFwk::CommonEventData &data);
78 
79     SecondAbility *mainAbility;
80     std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_;
81     ~SecondEventSubscriber() = default;
82 };
83 }  // namespace AppExecFwk
84 }  // namespace OHOS
85 #endif  // AMS_KIT_SYSTEM_TEST_A_MAIN_ABILITY_H