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 };
46 
47 class OsAccountManager {
48 public:
SubscribeOsAccount(const std::shared_ptr<OsAccountSubscriber> & subscriber)49     static ErrCode SubscribeOsAccount(const std::shared_ptr<OsAccountSubscriber> &subscriber)
50     {
51         if (instance_ == nullptr) {
52             return -1;
53         }
54         return instance_->SubscribeOsAccount(subscriber);
55     };
56 
QueryActiveOsAccountIds(std::vector<int32_t> & ids)57     static ErrCode QueryActiveOsAccountIds(std::vector<int32_t>& ids)
58     {
59         if (instance_ == nullptr) {
60             return -1;
61         }
62         return instance_->QueryActiveOsAccountIds(ids);
63     };
64 
GetInterface()65     static std::shared_ptr<MockOsAccountManagerInterface> GetInterface()
66     {
67         if (instance_ == nullptr) {
68             std::lock_guard<std::mutex> lock(mutex_);
69             if (instance_ == nullptr) {
70                 instance_ = std::make_shared<MockOsAccountManagerInterface>();
71             }
72         }
73         return instance_;
74     };
75 
DelInterface()76     static void DelInterface()
77     {
78         if (instance_ != nullptr) {
79             instance_.reset();
80         }
81     };
82 
83 private:
84     static std::shared_ptr<MockOsAccountManagerInterface> instance_;
85     static std::mutex mutex_;
86 };
87 } // OHOS::AccountSA
88 #endif // SECURITY_GUARD_OS_ACCOUNT_MANAGER_MOCK_H