1 /*
2  * Copyright (c) 2021-2022 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 #include "mock_serviceability_manager_service.h"
17 #include <gtest/gtest.h>
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <unistd.h>
23 
24 #include "session_info.h"
25 
26 using OHOS::AppExecFwk::ElementName;
27 
28 namespace OHOS {
29 namespace AAFwk {
MockServiceAbilityManagerService()30 MockServiceAbilityManagerService::MockServiceAbilityManagerService()
31 {}
32 
~MockServiceAbilityManagerService()33 MockServiceAbilityManagerService::~MockServiceAbilityManagerService()
34 {}
35 
StartAbility(const Want & want,int32_t userId,int requestCode)36 int MockServiceAbilityManagerService::StartAbility(const Want& want, int32_t userId, int requestCode)
37 {
38     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::StartAbility";
39     if (abilityScheduler_ != nullptr) {
40         startAbility = true;
41         LifeCycleStateInfo stateInfo;
42         stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INACTIVE;
43         want_.SetElementName("BundleName", "abilityName");
44         abilityScheduler_->ScheduleAbilityTransaction(want_, stateInfo);
45         return ERR_OK;
46     }
47     return 0;
48 }
49 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)50 int MockServiceAbilityManagerService::StartAbility(
51     const Want& want, const sptr<IRemoteObject>& callerToken, int32_t userId, int requestCode)
52 {
53     return 0;
54 }
55 
StartAbilityAsCaller(const Want & want,const sptr<IRemoteObject> & callerToken,sptr<IRemoteObject> asCallerSourceToken,int32_t userId,int requestCode)56 int MockServiceAbilityManagerService::StartAbilityAsCaller(
57     const Want& want, const sptr<IRemoteObject>& callerToken, sptr<IRemoteObject> asCallerSourceToken,
58     int32_t userId, int requestCode)
59 {
60     return 0;
61 }
62 
TerminateAbility(const sptr<IRemoteObject> & token,int resultCode,const Want * resultWant)63 int MockServiceAbilityManagerService::TerminateAbility(
64     const sptr<IRemoteObject>& token, int resultCode, const Want* resultWant)
65 {
66     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::TerminateAbility";
67     if (abilityScheduler_ != nullptr) {
68         startAbility = false;
69         LifeCycleStateInfo stateInfo;
70         stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INITIAL;
71         want_.SetElementName("BundleName", "abilityName");
72         abilityScheduler_->ScheduleAbilityTransaction(want_, stateInfo);
73         return ERR_OK;
74     }
75     return 0;
76 }
77 
ConnectAbility(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken,int32_t userId)78 int MockServiceAbilityManagerService::ConnectAbility(
79     const Want& want, const sptr<IAbilityConnection>& connect, const sptr<IRemoteObject>& callerToken, int32_t userId)
80 {
81     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::connectAbility";
82     if (abilityScheduler_ != nullptr) {
83         startAbility = false;
84         LifeCycleStateInfo stateInfo;
85         stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INACTIVE;
86         want_.SetElementName("BundleName", "abilityName");
87         abilityScheduler_->ScheduleAbilityTransaction(want_, stateInfo);
88         return ERR_OK;
89     }
90     return -1;
91 }
92 
DisconnectAbility(sptr<IAbilityConnection> connect)93 int MockServiceAbilityManagerService::DisconnectAbility(sptr<IAbilityConnection> connect)
94 {
95     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::DisconnectAbility";
96     if (abilityScheduler_ != nullptr) {
97         Want want;
98         want.SetElementName("BundleName", "abilityName");
99         abilityScheduler_->ScheduleDisconnectAbility(want_);
100         return ERR_OK;
101     }
102     return 0;
103 }
104 
AttachAbilityThread(const sptr<IAbilityScheduler> & scheduler,const sptr<IRemoteObject> & token)105 int MockServiceAbilityManagerService::AttachAbilityThread(
106     const sptr<IAbilityScheduler>& scheduler, const sptr<IRemoteObject>& token)
107 {
108     abilityScheduler_ = scheduler;
109     EXPECT_NE(nullptr, token);
110     return 0;
111 }
112 
DumpState(const std::string & args,std::vector<std::string> & info)113 void MockServiceAbilityManagerService::DumpState(const std::string& args, std::vector<std::string>& info)
114 {}
115 
DumpSysState(const std::string & args,std::vector<std::string> & state,bool isClient,bool isUserID,int UserID)116 void MockServiceAbilityManagerService::DumpSysState(
117     const std::string& args, std::vector<std::string>& state, bool isClient, bool isUserID, int UserID)
118 {}
119 
AbilityTransitionDone(const sptr<IRemoteObject> & token,int state,const PacMap & saveData)120 int MockServiceAbilityManagerService::AbilityTransitionDone(
121     const sptr<IRemoteObject>& token, int state, const PacMap& saveData)
122 {
123     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::AbilityTransitionDone startAbility is " << startAbility;
124     want_.SetElementName("BundleName", "abilityName");
125     if (abilityScheduler_ != nullptr && state == AAFwk::ABILITY_STATE_INACTIVE) {
126         want_.SetElementName("BundleName", "abilityName");
127         if (startAbility) {
128             abilityScheduler_->ScheduleCommandAbility(want_, false, -1);
129         } else {
130             abilityScheduler_->ScheduleConnectAbility(want_);
131         }
132     }
133     return 0;
134 }
135 
AbilityWindowConfigTransitionDone(const sptr<IRemoteObject> & token,const WindowConfig & windowConfig)136 int MockServiceAbilityManagerService::AbilityWindowConfigTransitionDone(
137     const sptr<IRemoteObject>& token, const WindowConfig& windowConfig)
138 {
139     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::AbilityWindowConfigTransitionDone startAbility is " <<
140         startAbility;
141     want_.SetElementName("BundleName", "abilityName");
142     if (abilityScheduler_ != nullptr) {
143         want_.SetElementName("BundleName", "abilityName");
144         if (startAbility) {
145             abilityScheduler_->ScheduleCommandAbility(want_, false, -1);
146         } else {
147             abilityScheduler_->ScheduleConnectAbility(want_);
148         }
149     }
150     return 0;
151 }
152 
ScheduleConnectAbilityDone(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & remoteObject)153 int MockServiceAbilityManagerService::ScheduleConnectAbilityDone(
154     const sptr<IRemoteObject>& token, const sptr<IRemoteObject>& remoteObject)
155 {
156     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::ScheduleConnectAbilityDone";
157     return 0;
158 }
159 
ScheduleDisconnectAbilityDone(const sptr<IRemoteObject> & token)160 int MockServiceAbilityManagerService::ScheduleDisconnectAbilityDone(const sptr<IRemoteObject>& token)
161 {
162     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::ScheduleDisconnectAbilityDone";
163 
164     if (abilityScheduler_ != nullptr) {
165         LifeCycleStateInfo stateInfo;
166         stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INITIAL;
167         abilityScheduler_->ScheduleAbilityTransaction(want_, stateInfo);
168         return ERR_OK;
169     }
170     return 0;
171 }
172 
ScheduleCommandAbilityDone(const sptr<IRemoteObject> & token)173 int MockServiceAbilityManagerService::ScheduleCommandAbilityDone(const sptr<IRemoteObject>& token)
174 {
175     return 0;
176 }
177 
ScheduleCommandAbilityWindowDone(const sptr<IRemoteObject> & token,const sptr<SessionInfo> & sessionInfo,WindowCommand winCmd,AbilityCommand abilityCmd)178 int MockServiceAbilityManagerService::ScheduleCommandAbilityWindowDone(
179     const sptr<IRemoteObject> &token,
180     const sptr<SessionInfo> &sessionInfo,
181     WindowCommand winCmd,
182     AbilityCommand abilityCmd)
183 {
184     return 0;
185 }
186 
StopServiceAbility(const Want & want,int32_t userId,const sptr<IRemoteObject> & token)187 int MockServiceAbilityManagerService::StopServiceAbility(const Want& want, int32_t userId,
188     const sptr<IRemoteObject> &token)
189 {
190     GTEST_LOG_(INFO) << "MockServiceAbilityManagerService::StopServiceAbility";
191     if (abilityScheduler_ != nullptr) {
192         startAbility = false;
193         LifeCycleStateInfo stateInfo;
194         stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INITIAL;
195         want_.SetElementName("BundleName", "abilityName");
196         abilityScheduler_->ScheduleAbilityTransaction(want_, stateInfo);
197         return ERR_OK;
198     }
199     return -1;
200 }
201 }  // namespace AAFwk
202 }  // namespace OHOS
203