1 /* 2 * Copyright (c) 2021-2023 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 SECURITY_GUARD_OS_ACCOUNT_MANAGER_MOCK_H 17 #define SECURITY_GUARD_OS_ACCOUNT_MANAGER_MOCK_H 18 19 #include <memory> 20 #include <mutex> 21 22 #include "errors.h" 23 24 #include "gmock/gmock.h" 25 26 namespace OHOS::AccountSA { 27 class OsAccountSubscriber { 28 public: 29 virtual ~OsAccountSubscriber() = default; 30 virtual void OnAccountsChanged(const int &id) = 0; 31 }; 32 33 class OsAccountManagerInterface { 34 public: 35 virtual ~OsAccountManagerInterface() = default; 36 virtual ErrCode QueryActiveOsAccountIds(std::vector<int32_t>& ids) = 0; 37 }; 38 39 class MockOsAccountManagerInterface : public OsAccountManagerInterface { 40 public: 41 MockOsAccountManagerInterface() = default; 42 ~MockOsAccountManagerInterface() override = default; 43 MOCK_METHOD1(QueryActiveOsAccountIds, ErrCode(std::vector<int32_t>& ids)); 44 MOCK_METHOD1(SubscribeOsAccount, ErrCode(const std::shared_ptr<OsAccountSubscriber> &subscriber)); 45 MOCK_METHOD1(GetForegroundOsAccountLocalId, ErrCode(int32_t &id)); 46 }; 47 48 class OsAccountManager { 49 public: SubscribeOsAccount(const std::shared_ptr<OsAccountSubscriber> & subscriber)50 static ErrCode SubscribeOsAccount(const std::shared_ptr<OsAccountSubscriber> &subscriber) 51 { 52 if (instance_ == nullptr) { 53 return -1; 54 } 55 return instance_->SubscribeOsAccount(subscriber); 56 }; 57 GetForegroundOsAccountLocalId(int32_t & id)58 static ErrCode GetForegroundOsAccountLocalId(int32_t &id) 59 { 60 if (instance_ == nullptr) { 61 return -1; 62 } 63 return instance_->GetForegroundOsAccountLocalId(id); 64 }; 65 QueryActiveOsAccountIds(std::vector<int32_t> & ids)66 static ErrCode QueryActiveOsAccountIds(std::vector<int32_t>& ids) 67 { 68 if (instance_ == nullptr) { 69 return -1; 70 } 71 return instance_->QueryActiveOsAccountIds(ids); 72 }; 73 GetInterface()74 static std::shared_ptr<MockOsAccountManagerInterface> GetInterface() 75 { 76 if (instance_ == nullptr) { 77 std::lock_guard<std::mutex> lock(mutex_); 78 if (instance_ == nullptr) { 79 instance_ = std::make_shared<MockOsAccountManagerInterface>(); 80 } 81 } 82 return instance_; 83 }; 84 DelInterface()85 static void DelInterface() 86 { 87 if (instance_ != nullptr) { 88 instance_.reset(); 89 } 90 }; 91 92 private: 93 static std::shared_ptr<MockOsAccountManagerInterface> instance_; 94 static std::mutex mutex_; 95 }; 96 } // OHOS::AccountSA 97 #endif // SECURITY_GUARD_OS_ACCOUNT_MANAGER_MOCK_H