• 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_ABILITY_APPEND_TEST_A_MAIN_ABILITY_H
17 #define AMS_ABILITY_APPEND_TEST_A_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:
30     void SubscribeEvent();
31     void TestAbility(int apiIndex, int caseIndex, int code);
32 
33     void OnSetCallerCase1(int code);
34     void TerminateAndRemoveMissonCase1(int code);
35     void TerminateAbilityResultCase1(int code);
36     void GetDispalyOrientationCase1(int code);
37     void GetPreferencesDirCase1(int code);
38     void StartAbilitiesCase1(int code);
39     void GetColorModeCase1(int code);
40     void SetColorModeCase1(int code);
41     void IsFirstInMissionCase1(int code);
42 
MainAbility()43     MainAbility()
44     {
45         mapCase_ = {
46             {(int)AppendApi::OnSetCaller,
47                 {
48                     [this](int code) { OnSetCallerCase1(code); },
49                 }},
50                 {(int)AppendApi::TerminateAndRemoveMisson,
51                 {
52                     [this](int code) { TerminateAndRemoveMissonCase1(code); },
53                 }},
54                 {(int)AppendApi::TerminateAbilityResult,
55                 {
56                     [this](int code) { TerminateAbilityResultCase1(code); },
57                 }},
58                 {(int)AppendApi::GetDispalyOrientation,
59                 {
60                     [this](int code) { GetDispalyOrientationCase1(code); },
61                 }},
62                 {(int)AppendApi::GetPreferencesDir,
63                 {
64                     [this](int code) { GetPreferencesDirCase1(code); },
65                 }},
66                 {(int)AppendApi::StartAbilities,
67                 {
68                     [this](int code) { StartAbilitiesCase1(code); },
69                 }},
70                 {(int)AppendApi::GetColorMode,
71                 {
72                     [this](int code) { GetColorModeCase1(code); },
73                 }},
74                 {(int)AppendApi::SetColorMode,
75                 {
76                     [this](int code) { SetColorModeCase1(code); },
77                 }},
78                 {(int)AppendApi::IsFirstInMission,
79                 {
80                     [this](int code) { IsFirstInMissionCase1(code); },
81                 }},
82         };
83     }
84 
85     std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_;
86     ~MainAbility();
87 
88 protected:
89     void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application,
90         std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
91     virtual void OnStart(const Want &want) override;
92     virtual void OnStop() override;
93     virtual void OnActive() override;
94     virtual void OnInactive() override;
95     virtual void OnBackground() override;
96     virtual void OnForeground(const Want &want) override;
97     virtual Uri OnSetCaller() override;
98     std::shared_ptr<FirstEventSubscriber> subscriber_;
99 };
100 class FirstEventSubscriber : public EventFwk::CommonEventSubscriber {
101 public:
FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)102     explicit FirstEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
103     {
104         mapTestFunc_ = {
105             {"Ability", [this](int apiIndex, int caseIndex, int code) { TestAbility(apiIndex, caseIndex, code); }},
106         };
107         mainAbility = nullptr;
108     }
109 
TestAbility(int apiIndex,int caseIndex,int code)110     void TestAbility(int apiIndex, int caseIndex, int code)
111     {
112         mainAbility->TestAbility(apiIndex, caseIndex, code);
113     }
114 
115     virtual void OnReceiveEvent(const EventFwk::CommonEventData &data);
116 
117     MainAbility *mainAbility;
118     std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_;
119     ~FirstEventSubscriber() = default;
120 };
121 }  // namespace AppExecFwk
122 }  // namespace OHOS
123 #endif  // AMS_ABILITY_APPEND_TEST_A_MAIN_ABILITY_H