1 2 /* 3 * Copyright (c) 2021 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef UNITTEST_OHOS_ABILITY_RUNTIME_MOCK_DATAOBS_MGR_SERVICE_H 18 #define UNITTEST_OHOS_ABILITY_RUNTIME_MOCK_DATAOBS_MGR_SERVICE_H 19 20 #include <gmock/gmock.h> 21 #define protected public 22 #define private public 23 #include "dataobs_mgr_stub.h" 24 25 namespace OHOS { 26 namespace AAFwk { 27 class MockDataObsMgrService : public DataObsManagerStub { 28 public: 29 MockDataObsMgrService() = default; 30 virtual ~MockDataObsMgrService() = default; 31 32 MOCK_METHOD2(RegisterObserverCall, int(const Uri &, const sptr<IDataAbilityObserver> &)); 33 MOCK_METHOD2(UnregisterObserverCall, int(const Uri &, const sptr<IDataAbilityObserver> &)); 34 MOCK_METHOD1(NotifyChangeCall, int(const Uri &)); 35 RegisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)36 int RegisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver) 37 { 38 RegisterObserverCall(uri, dataObserver); 39 return 1; 40 } UnregisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)41 int UnregisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver) 42 { 43 UnregisterObserverCall(uri, dataObserver); 44 return 1; 45 } NotifyChange(const Uri & uri)46 int NotifyChange(const Uri &uri) 47 { 48 NotifyChangeCall(uri); 49 return 1; 50 } 51 OnStart()52 void OnStart() 53 { 54 if (state_ == DataObsServiceRunningState::STATE_RUNNING) { 55 HILOG_INFO("Dataobs Manager Service has already started."); 56 return; 57 } 58 HILOG_INFO("Dataobs Manager Service started."); 59 if (!Init()) { 60 HILOG_ERROR("failed to init service."); 61 return; 62 } 63 state_ = DataObsServiceRunningState::STATE_RUNNING; 64 eventLoop_->Run(); 65 66 HILOG_INFO("Ability Manager Service start success."); 67 } OnStop()68 void OnStop() 69 { 70 HILOG_INFO("stop service"); 71 eventLoop_.reset(); 72 handler_.reset(); 73 state_ = DataObsServiceRunningState::STATE_NOT_START; 74 } 75 /** 76 * GetEventHandler, get the dataobs manager service's handler. 77 * @return Returns EventHandler ptr. 78 */ GetEventHandler()79 std::shared_ptr<EventHandler> GetEventHandler() 80 { 81 return handler_; 82 } 83 84 private: Init()85 bool Init() 86 { 87 eventLoop_ = AppExecFwk::EventRunner::Create("DataObsMgrService"); 88 if (eventLoop_ == nullptr) { 89 return false; 90 } 91 92 handler_ = std::make_shared<AppExecFwk::EventHandler>(eventLoop_); 93 if (handler_ == nullptr) { 94 return false; 95 } 96 97 HILOG_INFO("init success"); 98 return true; 99 } 100 101 std::shared_ptr<EventRunner> eventLoop_; 102 std::shared_ptr<EventHandler> handler_; 103 DataObsServiceRunningState state_; 104 std::shared_ptr<DataObsMgrInner> dataObsMgrInner_; 105 const int taskMax_ = 50; 106 }; 107 } // namespace AAFwk 108 } // namespace OHOS 109 #endif // UNITTEST_OHOS_ABILITY_RUNTIME_MOCK_DATAOBS_MGR_SERVICE_H 110