16ea96550Sopenharmony_ci/* 26ea96550Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 36ea96550Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46ea96550Sopenharmony_ci * you may not use this file except in compliance with the License. 56ea96550Sopenharmony_ci * You may obtain a copy of the License at 66ea96550Sopenharmony_ci * 76ea96550Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86ea96550Sopenharmony_ci * 96ea96550Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106ea96550Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116ea96550Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126ea96550Sopenharmony_ci * See the License for the specific language governing permissions and 136ea96550Sopenharmony_ci * limitations under the License. 146ea96550Sopenharmony_ci */ 156ea96550Sopenharmony_ci 166ea96550Sopenharmony_ci#ifndef SECURITY_GUARD_RDB_HELPER_MOCK_H 176ea96550Sopenharmony_ci#define SECURITY_GUARD_RDB_HELPER_MOCK_H 186ea96550Sopenharmony_ci 196ea96550Sopenharmony_ci#include <memory> 206ea96550Sopenharmony_ci#include <mutex> 216ea96550Sopenharmony_ci#include <string> 226ea96550Sopenharmony_ci 236ea96550Sopenharmony_ci#include "gmock/gmock.h" 246ea96550Sopenharmony_ci#include "singleton.h" 256ea96550Sopenharmony_ci 266ea96550Sopenharmony_ci#include "rdb_open_callback.h" 276ea96550Sopenharmony_ci#include "rdb_store.h" 286ea96550Sopenharmony_ci#include "rdb_store_config.h" 296ea96550Sopenharmony_ci 306ea96550Sopenharmony_cinamespace OHOS::NativeRdb { 316ea96550Sopenharmony_ciclass RdbHelperInterface { 326ea96550Sopenharmony_cipublic: 336ea96550Sopenharmony_ci virtual ~RdbHelperInterface() = default; 346ea96550Sopenharmony_ci virtual std::shared_ptr<RdbStore> GetRdbStore( 356ea96550Sopenharmony_ci const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode) = 0; 366ea96550Sopenharmony_ci}; 376ea96550Sopenharmony_ci 386ea96550Sopenharmony_ciclass MockRdbHelperInterface : public RdbHelperInterface { 396ea96550Sopenharmony_cipublic: 406ea96550Sopenharmony_ci MockRdbHelperInterface() = default; 416ea96550Sopenharmony_ci ~MockRdbHelperInterface() override = default; 426ea96550Sopenharmony_ci MOCK_METHOD4(GetRdbStore, std::shared_ptr<RdbStore>(const RdbStoreConfig &config, int version, 436ea96550Sopenharmony_ci RdbOpenCallback &openCallback, int &errCode)); 446ea96550Sopenharmony_ci}; 456ea96550Sopenharmony_ci 466ea96550Sopenharmony_ciclass RdbHelper { 476ea96550Sopenharmony_cipublic: 486ea96550Sopenharmony_ci static std::shared_ptr<RdbStore> GetRdbStore( 496ea96550Sopenharmony_ci const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode) 506ea96550Sopenharmony_ci { 516ea96550Sopenharmony_ci if (instance_ == nullptr) { 526ea96550Sopenharmony_ci return nullptr; 536ea96550Sopenharmony_ci } 546ea96550Sopenharmony_ci return instance_->GetRdbStore(config, version, openCallback, errCode); 556ea96550Sopenharmony_ci }; 566ea96550Sopenharmony_ci 576ea96550Sopenharmony_ci static std::shared_ptr<MockRdbHelperInterface> GetInterface() 586ea96550Sopenharmony_ci { 596ea96550Sopenharmony_ci if (instance_ == nullptr) { 606ea96550Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 616ea96550Sopenharmony_ci if (instance_ == nullptr) { 626ea96550Sopenharmony_ci instance_ = std::make_shared<MockRdbHelperInterface>(); 636ea96550Sopenharmony_ci } 646ea96550Sopenharmony_ci } 656ea96550Sopenharmony_ci return instance_; 666ea96550Sopenharmony_ci }; 676ea96550Sopenharmony_ci 686ea96550Sopenharmony_ci static void DelInterface() 696ea96550Sopenharmony_ci { 706ea96550Sopenharmony_ci if (instance_ != nullptr) { 716ea96550Sopenharmony_ci instance_.reset(); 726ea96550Sopenharmony_ci } 736ea96550Sopenharmony_ci }; 746ea96550Sopenharmony_ci 756ea96550Sopenharmony_ciprivate: 766ea96550Sopenharmony_ci static std::shared_ptr<MockRdbHelperInterface> instance_; 776ea96550Sopenharmony_ci static std::mutex mutex_; 786ea96550Sopenharmony_ci}; 796ea96550Sopenharmony_ci} // namespace OHOS::NativeRdb 806ea96550Sopenharmony_ci#endif 81