• 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_B_MAIN_ABILITY_H
17 #define AMS_KIT_SYSTEM_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 "task_dispatcher_tools.h"
23 #include "task_dispatcher_test_info.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 using namespace OHOS::EventFwk;
28 class FirstEventSubscriber;
29 class MainAbility : public Ability {
30 public:
MainAbility()31     MainAbility()
32     {
33         initGlobal();
34         initParallel();
35         initSerial();
36         initSpec();
37         initHybrid();
38         initMultiApp();
39         initPriority();
40         initCancelTask();
41         initExtra();
42     }
initGlobal()43     void initGlobal()
44     {
45         mapCase_[(int)TestFunc::GLOBAL] = {};
46     }
initParallel()47     void initParallel()
48     {
49         mapCase_[(int)TestFunc::PARALLEL] = {};
50     }
initSerial()51     void initSerial()
52     {
53         mapCase_[(int)TestFunc::SERIAL] = {};
54     }
initSpec()55     void initSpec()
56     {
57         mapCase_[(int)TestFunc::SPEC] = {};
58     }
initHybrid()59     void initHybrid()
60     {
61         mapCase_[(int)TestFunc::HYBRID] = {};
62     }
initMultiApp()63     void initMultiApp()
64     {
65         mapCase_[(int)TestFunc::MULTI_APP] = {
66             [this](int code) { MultiAppCase1(code); },
67             [this](int code) { MultiAppCase2(code); },
68             [this](int code) { MultiAppCase3(code); },
69             [this](int code) { MultiAppCase4(code); },
70         };
71     }
initPriority()72     void initPriority()
73     {
74         mapCase_[(int)TestFunc::PRIORITY] = {};
75     }
initCancelTask()76     void initCancelTask()
77     {
78         mapCase_[(int)TestFunc::CANCEL_TASK] = {};
79     }
initExtra()80     void initExtra()
81     {
82         mapCase_[(int)TestFunc::EXTRA] = {};
83     }
84 
85     void SubscribeEvent();
86     void TestDispatcher(int apiIndex, int caseIndex, int code);
87     int Dispatch(STtools::TestSetting outer, STtools::TestSetting inner);
88 
89     void MultiAppCase1(int code);
90     void MultiAppCase2(int code);
91     void MultiAppCase3(int code);
92     void MultiAppCase4(int code);
93 
94     std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_;
95     ~MainAbility();
96 
97 protected:
98     void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application,
99         std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
100     virtual void OnStart(const Want &want) override;
101     virtual void OnStop() override;
102     virtual void OnActive() override;
103     virtual void OnInactive() override;
104     virtual void OnBackground() override;
105     virtual void OnForeground(const Want &want) override;
106     std::shared_ptr<FirstEventSubscriber> subscriber_;
107 };
108 class FirstEventSubscriber : public EventFwk::CommonEventSubscriber {
109 public:
FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)110     explicit FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
111     {
112         mapTestFunc_ = {
113             {"Dispatcher",
114                 [this](int apiIndex, int caseIndex, int code) { TestDispatcher(apiIndex, caseIndex, code); }},
115         };
116         mainAbility = nullptr;
117     }
118 
TestDispatcher(int apiIndex,int caseIndex,int code)119     void TestDispatcher(int apiIndex, int caseIndex, int code)
120     {
121         mainAbility->TestDispatcher(apiIndex, caseIndex, code);
122     }
123 
124     virtual void OnReceiveEvent(const EventFwk::CommonEventData &data);
125 
126     MainAbility *mainAbility;
127     std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_;
128     ~FirstEventSubscriber() = default;
129 };
130 }  // namespace AppExecFwk
131 }  // namespace OHOS
132 #endif  // AMS_KIT_SYSTEM_TEST_B_MAIN_ABILITY_H