13f4cbf05Sopenharmony_ci/*
23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License.
53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at
63f4cbf05Sopenharmony_ci *
73f4cbf05Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f4cbf05Sopenharmony_ci *
93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and
133f4cbf05Sopenharmony_ci * limitations under the License.
143f4cbf05Sopenharmony_ci */
153f4cbf05Sopenharmony_ci#include <gtest/gtest.h>
163f4cbf05Sopenharmony_ci#include <algorithm>
173f4cbf05Sopenharmony_ci#include <fstream>
183f4cbf05Sopenharmony_ci#include <iostream>
193f4cbf05Sopenharmony_ci#include <vector>
203f4cbf05Sopenharmony_ci#include <thread>
213f4cbf05Sopenharmony_ci#include <map>
223f4cbf05Sopenharmony_ci#include <mutex>
233f4cbf05Sopenharmony_ci#include "refbase.h"
243f4cbf05Sopenharmony_ci#include "singleton.h"
253f4cbf05Sopenharmony_ci
263f4cbf05Sopenharmony_ci#include <future>
273f4cbf05Sopenharmony_ciusing namespace testing::ext;
283f4cbf05Sopenharmony_ciusing namespace std;
293f4cbf05Sopenharmony_ci
303f4cbf05Sopenharmony_cinamespace OHOS {
313f4cbf05Sopenharmony_cinamespace {
323f4cbf05Sopenharmony_cistatic constexpr int FLAG_OF_CONS = 1;
333f4cbf05Sopenharmony_cistatic constexpr int FLAG_OF_DEST = 2;
343f4cbf05Sopenharmony_cistatic int g_sptrCount = 0;
353f4cbf05Sopenharmony_cistatic int g_wptrCount = 0;
363f4cbf05Sopenharmony_cistatic int g_refbaseflag = 0;
373f4cbf05Sopenharmony_cistatic int g_freeFlag = 0;
383f4cbf05Sopenharmony_ci
393f4cbf05Sopenharmony_ciclass UtilsRefbaseTest : public testing::Test {
403f4cbf05Sopenharmony_cipublic:
413f4cbf05Sopenharmony_ci    static void SetUpTestCase(void);
423f4cbf05Sopenharmony_ci};
433f4cbf05Sopenharmony_ci
443f4cbf05Sopenharmony_civoid UtilsRefbaseTest::SetUpTestCase(void)
453f4cbf05Sopenharmony_ci{
463f4cbf05Sopenharmony_ci    g_sptrCount = 0;
473f4cbf05Sopenharmony_ci    g_wptrCount = 0;
483f4cbf05Sopenharmony_ci    g_refbaseflag = 0;
493f4cbf05Sopenharmony_ci}
503f4cbf05Sopenharmony_ci
513f4cbf05Sopenharmony_ciclass RefBaseTest : public RefBase {
523f4cbf05Sopenharmony_cipublic:
533f4cbf05Sopenharmony_ci    RefBaseTest()
543f4cbf05Sopenharmony_ci    {
553f4cbf05Sopenharmony_ci        g_refbaseflag = FLAG_OF_CONS;
563f4cbf05Sopenharmony_ci        isgetrefptr_ = false;
573f4cbf05Sopenharmony_ci    }
583f4cbf05Sopenharmony_ci    ~RefBaseTest()
593f4cbf05Sopenharmony_ci    {
603f4cbf05Sopenharmony_ci        g_refbaseflag = FLAG_OF_DEST;
613f4cbf05Sopenharmony_ci    }
623f4cbf05Sopenharmony_ci
633f4cbf05Sopenharmony_ci    void OnLastStrongRef(const void *objectId) override
643f4cbf05Sopenharmony_ci    {
653f4cbf05Sopenharmony_ci        g_freeFlag = 1;
663f4cbf05Sopenharmony_ci    }
673f4cbf05Sopenharmony_ci
683f4cbf05Sopenharmony_ci    void OnLastWeakRef(const void *objectIda) override
693f4cbf05Sopenharmony_ci    {
703f4cbf05Sopenharmony_ci    }
713f4cbf05Sopenharmony_ci
723f4cbf05Sopenharmony_ci    void SetRefPtr()
733f4cbf05Sopenharmony_ci    {
743f4cbf05Sopenharmony_ci        isgetrefptr_ = true;
753f4cbf05Sopenharmony_ci    }
763f4cbf05Sopenharmony_ci    bool GetTestRefPtrFlag()
773f4cbf05Sopenharmony_ci    {
783f4cbf05Sopenharmony_ci        return isgetrefptr_;
793f4cbf05Sopenharmony_ci    }
803f4cbf05Sopenharmony_ci
813f4cbf05Sopenharmony_ciprivate:
823f4cbf05Sopenharmony_ci    bool isgetrefptr_;
833f4cbf05Sopenharmony_ci};
843f4cbf05Sopenharmony_ci
853f4cbf05Sopenharmony_ciclass IRemoteObject : public virtual RefBase {
863f4cbf05Sopenharmony_cipublic:
873f4cbf05Sopenharmony_ci    IRemoteObject() { ExtendObjectLifetime(); }
883f4cbf05Sopenharmony_ci    virtual bool IsProxyObject() = 0;
893f4cbf05Sopenharmony_ci    ~IRemoteObject() {}
903f4cbf05Sopenharmony_ci};
913f4cbf05Sopenharmony_ci
923f4cbf05Sopenharmony_ciclass RefBaseTestTracker : public RefBase {
933f4cbf05Sopenharmony_cipublic:
943f4cbf05Sopenharmony_ci    explicit RefBaseTestTracker(int value) : value_(value)
953f4cbf05Sopenharmony_ci    {
963f4cbf05Sopenharmony_ci        checkCount_++;
973f4cbf05Sopenharmony_ci    }
983f4cbf05Sopenharmony_ci    RefBaseTestTracker() = default;
993f4cbf05Sopenharmony_ci    ~RefBaseTestTracker()
1003f4cbf05Sopenharmony_ci    {
1013f4cbf05Sopenharmony_ci        checkCount_--;
1023f4cbf05Sopenharmony_ci    }
1033f4cbf05Sopenharmony_ci
1043f4cbf05Sopenharmony_ci    RefBaseTestTracker(const RefBaseTestTracker &testTracker)
1053f4cbf05Sopenharmony_ci    {
1063f4cbf05Sopenharmony_ci        checkCount_++;
1073f4cbf05Sopenharmony_ci        value_ = testTracker.value_;
1083f4cbf05Sopenharmony_ci    }
1093f4cbf05Sopenharmony_ci
1103f4cbf05Sopenharmony_ci    RefBaseTestTracker &operator=(const RefBaseTestTracker &testTracker)
1113f4cbf05Sopenharmony_ci    {
1123f4cbf05Sopenharmony_ci        checkCount_++;
1133f4cbf05Sopenharmony_ci        value_ = testTracker.value_;
1143f4cbf05Sopenharmony_ci        return *this;
1153f4cbf05Sopenharmony_ci    }
1163f4cbf05Sopenharmony_ci
1173f4cbf05Sopenharmony_ci    RefBaseTestTracker(RefBaseTestTracker &&testTracker)
1183f4cbf05Sopenharmony_ci    {
1193f4cbf05Sopenharmony_ci        checkCount_++;
1203f4cbf05Sopenharmony_ci        value_ = testTracker.value_;
1213f4cbf05Sopenharmony_ci    }
1223f4cbf05Sopenharmony_ci
1233f4cbf05Sopenharmony_ci    RefBaseTestTracker &operator=(RefBaseTestTracker &&testTracker)
1243f4cbf05Sopenharmony_ci    {
1253f4cbf05Sopenharmony_ci        checkCount_++;
1263f4cbf05Sopenharmony_ci        value_ = testTracker.value_;
1273f4cbf05Sopenharmony_ci        return *this;
1283f4cbf05Sopenharmony_ci    }
1293f4cbf05Sopenharmony_ci
1303f4cbf05Sopenharmony_ci    static RefBaseTestTracker *GetInstance()
1313f4cbf05Sopenharmony_ci    {
1323f4cbf05Sopenharmony_ci        static RefBaseTestTracker instance;
1333f4cbf05Sopenharmony_ci        return &instance;
1343f4cbf05Sopenharmony_ci    }
1353f4cbf05Sopenharmony_ci
1363f4cbf05Sopenharmony_ci    void InitTracker()
1373f4cbf05Sopenharmony_ci    {
1383f4cbf05Sopenharmony_ci        checkCount_ = 0;
1393f4cbf05Sopenharmony_ci        freeCount_ = 0;
1403f4cbf05Sopenharmony_ci        firstRefCount_ = 0;
1413f4cbf05Sopenharmony_ci        lastRefCount_ = 0;
1423f4cbf05Sopenharmony_ci    }
1433f4cbf05Sopenharmony_ci
1443f4cbf05Sopenharmony_ci    void TrackObject(IRemoteObject *object)
1453f4cbf05Sopenharmony_ci    {
1463f4cbf05Sopenharmony_ci        std::lock_guard<std::mutex> lockGuard(objectMutex_);
1473f4cbf05Sopenharmony_ci        trackObjects_.emplace_back(object);
1483f4cbf05Sopenharmony_ci    }
1493f4cbf05Sopenharmony_ci
1503f4cbf05Sopenharmony_ci    void TrackNewObject(IRemoteObject *object)
1513f4cbf05Sopenharmony_ci    {
1523f4cbf05Sopenharmony_ci        std::lock_guard<std::mutex> lockGuard(objectOnfirstMutex_);
1533f4cbf05Sopenharmony_ci        RefBaseTestTracker::firstRefCount_++;
1543f4cbf05Sopenharmony_ci    }
1553f4cbf05Sopenharmony_ci
1563f4cbf05Sopenharmony_ci    void UntrackObject(IRemoteObject *object)
1573f4cbf05Sopenharmony_ci    {
1583f4cbf05Sopenharmony_ci        std::lock_guard<std::mutex> lockGuard(objectMutex_);
1593f4cbf05Sopenharmony_ci        auto iter = find(trackObjects_.begin(), trackObjects_.end(), object);
1603f4cbf05Sopenharmony_ci        if (iter != trackObjects_.end()) {
1613f4cbf05Sopenharmony_ci            trackObjects_.erase(iter);
1623f4cbf05Sopenharmony_ci        }
1633f4cbf05Sopenharmony_ci    }
1643f4cbf05Sopenharmony_ci
1653f4cbf05Sopenharmony_ci    void TrackFreeObject(IRemoteObject *object)
1663f4cbf05Sopenharmony_ci    {
1673f4cbf05Sopenharmony_ci        std::lock_guard<std::mutex> lockGuard(objectOnfreeMutex_);
1683f4cbf05Sopenharmony_ci        RefBaseTestTracker::freeCount_++;
1693f4cbf05Sopenharmony_ci    }
1703f4cbf05Sopenharmony_ci
1713f4cbf05Sopenharmony_ci    void PrintTrackResults()
1723f4cbf05Sopenharmony_ci    {
1733f4cbf05Sopenharmony_ci        std::lock_guard<std::mutex> lockGuard(objectMutex_);
1743f4cbf05Sopenharmony_ci        if (!trackObjects_.empty()) {
1753f4cbf05Sopenharmony_ci            for (auto o : trackObjects_) {
1763f4cbf05Sopenharmony_ci                std::cout << "object: " << o <<"strong: " << o->GetSptrRefCount() << ", weak:" << o->GetWptrRefCount() << std::endl;
1773f4cbf05Sopenharmony_ci            }
1783f4cbf05Sopenharmony_ci        }
1793f4cbf05Sopenharmony_ci        std::cout << "firstRefCount_: " << RefBaseTestTracker::firstRefCount_ << std::endl;
1803f4cbf05Sopenharmony_ci        std::cout << "lastRefCount_: " << RefBaseTestTracker::lastRefCount_ << std::endl;
1813f4cbf05Sopenharmony_ci        std::cout << "freeCount_: " << RefBaseTestTracker::freeCount_ << std::endl;
1823f4cbf05Sopenharmony_ci    }
1833f4cbf05Sopenharmony_ci
1843f4cbf05Sopenharmony_cipublic:
1853f4cbf05Sopenharmony_ci    int checkCount_ = 0;
1863f4cbf05Sopenharmony_ci    int freeCount_ = 0;
1873f4cbf05Sopenharmony_ci    int firstRefCount_ = 0;
1883f4cbf05Sopenharmony_ci    int lastRefCount_ = 0;
1893f4cbf05Sopenharmony_ci
1903f4cbf05Sopenharmony_ciprivate:
1913f4cbf05Sopenharmony_ci
1923f4cbf05Sopenharmony_ci    std::vector<IRemoteObject *> trackObjects_;
1933f4cbf05Sopenharmony_ci    std::mutex objectMutex_;
1943f4cbf05Sopenharmony_ci    std::mutex objectOnfirstMutex_;
1953f4cbf05Sopenharmony_ci    std::mutex objectOnfreeMutex_;
1963f4cbf05Sopenharmony_ci    int value_;
1973f4cbf05Sopenharmony_ci};
1983f4cbf05Sopenharmony_ci
1993f4cbf05Sopenharmony_ci
2003f4cbf05Sopenharmony_ciclass IPCObjectProxy : public IRemoteObject
2013f4cbf05Sopenharmony_ci{
2023f4cbf05Sopenharmony_cipublic:
2033f4cbf05Sopenharmony_ci    bool IsProxyObject() override { return 0; }
2043f4cbf05Sopenharmony_ci    string descriptor_;
2053f4cbf05Sopenharmony_ci    IPCObjectProxy(const string &descriptor)
2063f4cbf05Sopenharmony_ci    {
2073f4cbf05Sopenharmony_ci        descriptor_ = descriptor;
2083f4cbf05Sopenharmony_ci        RefBaseTestTracker *tracker = RefBaseTestTracker::GetInstance();
2093f4cbf05Sopenharmony_ci        tracker->TrackObject(this);
2103f4cbf05Sopenharmony_ci        tracker->TrackNewObject(this);
2113f4cbf05Sopenharmony_ci    };
2123f4cbf05Sopenharmony_ci    ~IPCObjectProxy() {}
2133f4cbf05Sopenharmony_ci    void RefPtrCallback() override;
2143f4cbf05Sopenharmony_ci    void OnLastStrongRef(const void *objectId) override;
2153f4cbf05Sopenharmony_ci    void OnFirstStrongRef(const void *objectId) override;
2163f4cbf05Sopenharmony_ci    std::mutex mutexLast_;
2173f4cbf05Sopenharmony_ci};
2183f4cbf05Sopenharmony_ci
2193f4cbf05Sopenharmony_ciclass IPCProcessSkeleton : public virtual RefBase, public Singleton<IPCProcessSkeleton>
2203f4cbf05Sopenharmony_ci{
2213f4cbf05Sopenharmony_ci    friend class Singleton<IPCProcessSkeleton>;
2223f4cbf05Sopenharmony_ci
2233f4cbf05Sopenharmony_ciprivate:
2243f4cbf05Sopenharmony_ci    IPCProcessSkeleton() = default;
2253f4cbf05Sopenharmony_ci
2263f4cbf05Sopenharmony_cipublic:
2273f4cbf05Sopenharmony_ci    ~IPCProcessSkeleton() override = default;
2283f4cbf05Sopenharmony_ci
2293f4cbf05Sopenharmony_ci    std::mutex mutex_;
2303f4cbf05Sopenharmony_ci    std::map<string, wptr<IRemoteObject>> objects1_;
2313f4cbf05Sopenharmony_ci
2323f4cbf05Sopenharmony_ci    void DumpMapObjects()
2333f4cbf05Sopenharmony_ci    {
2343f4cbf05Sopenharmony_ci        if (!objects1_.empty()) {
2353f4cbf05Sopenharmony_ci            for (auto &o : objects1_) {
2363f4cbf05Sopenharmony_ci                std::cout << "strong: " << o.second->GetSptrRefCount() << "weak:" << o.second->GetWptrRefCount() << std::endl;
2373f4cbf05Sopenharmony_ci            }
2383f4cbf05Sopenharmony_ci        }
2393f4cbf05Sopenharmony_ci    }
2403f4cbf05Sopenharmony_ci    IRemoteObject *QueryObjectInner(const string &descriptor)
2413f4cbf05Sopenharmony_ci    {
2423f4cbf05Sopenharmony_ci        auto it = objects1_.find(descriptor);
2433f4cbf05Sopenharmony_ci        if (it != objects1_.end())
2443f4cbf05Sopenharmony_ci        {
2453f4cbf05Sopenharmony_ci            it->second->AttemptAcquire(this);
2463f4cbf05Sopenharmony_ci            std::this_thread::sleep_for(std::chrono::milliseconds(1));
2473f4cbf05Sopenharmony_ci            return it->second.GetRefPtr();
2483f4cbf05Sopenharmony_ci        }
2493f4cbf05Sopenharmony_ci
2503f4cbf05Sopenharmony_ci        return nullptr;
2513f4cbf05Sopenharmony_ci    }
2523f4cbf05Sopenharmony_ci
2533f4cbf05Sopenharmony_ci    IRemoteObject *FindOrNewObject(int handle)
2543f4cbf05Sopenharmony_ci    {
2553f4cbf05Sopenharmony_ci        std::lock_guard<std::mutex> lockGuard(mutex_);
2563f4cbf05Sopenharmony_ci        IRemoteObject *remoteObject = QueryObjectInner(to_string(handle));
2573f4cbf05Sopenharmony_ci        if (remoteObject != nullptr)
2583f4cbf05Sopenharmony_ci        {
2593f4cbf05Sopenharmony_ci            std::this_thread::sleep_for(std::chrono::milliseconds(1));
2603f4cbf05Sopenharmony_ci            return remoteObject;
2613f4cbf05Sopenharmony_ci        }
2623f4cbf05Sopenharmony_ci
2633f4cbf05Sopenharmony_ci        remoteObject = new IPCObjectProxy(to_string(handle));
2643f4cbf05Sopenharmony_ci        remoteObject->AttemptAcquire(this);
2653f4cbf05Sopenharmony_ci        objects1_.insert(std::pair<string, wptr<IRemoteObject>>(to_string(handle), remoteObject));
2663f4cbf05Sopenharmony_ci        return remoteObject;
2673f4cbf05Sopenharmony_ci    }
2683f4cbf05Sopenharmony_ci
2693f4cbf05Sopenharmony_ci    bool DetachObject(IRemoteObject *object, string descriptor)
2703f4cbf05Sopenharmony_ci    {
2713f4cbf05Sopenharmony_ci        std::lock_guard<std::mutex> lockGuard(mutex_);
2723f4cbf05Sopenharmony_ci        if (object->GetSptrRefCount())
2733f4cbf05Sopenharmony_ci        {
2743f4cbf05Sopenharmony_ci            return false;
2753f4cbf05Sopenharmony_ci        }
2763f4cbf05Sopenharmony_ci        return (objects1_.erase(descriptor) > 0);
2773f4cbf05Sopenharmony_ci    }
2783f4cbf05Sopenharmony_ci};
2793f4cbf05Sopenharmony_ci
2803f4cbf05Sopenharmony_civoid IPCObjectProxy::OnLastStrongRef(const void *objectId)
2813f4cbf05Sopenharmony_ci{
2823f4cbf05Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutexLast_);
2833f4cbf05Sopenharmony_ci    (void)IPCProcessSkeleton::GetInstance().DetachObject(this, descriptor_);
2843f4cbf05Sopenharmony_ci    RefBaseTestTracker *tracker = RefBaseTestTracker::GetInstance();
2853f4cbf05Sopenharmony_ci    tracker->lastRefCount_++;
2863f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::nanoseconds(10));
2873f4cbf05Sopenharmony_ci}
2883f4cbf05Sopenharmony_ci
2893f4cbf05Sopenharmony_civoid IPCObjectProxy::OnFirstStrongRef(const void *objectId)
2903f4cbf05Sopenharmony_ci{
2913f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::nanoseconds(10));
2923f4cbf05Sopenharmony_ci}
2933f4cbf05Sopenharmony_ci
2943f4cbf05Sopenharmony_civoid IPCObjectProxy::RefPtrCallback()
2953f4cbf05Sopenharmony_ci{
2963f4cbf05Sopenharmony_ci    RefBaseTestTracker *tracker = RefBaseTestTracker::GetInstance();
2973f4cbf05Sopenharmony_ci    tracker->UntrackObject(this);
2983f4cbf05Sopenharmony_ci    tracker->TrackFreeObject(this);
2993f4cbf05Sopenharmony_ci    RefBase::RefPtrCallback();
3003f4cbf05Sopenharmony_ci}
3013f4cbf05Sopenharmony_ci
3023f4cbf05Sopenharmony_ciconstexpr int CYCLE_NUM1 = 100;
3033f4cbf05Sopenharmony_ciconstexpr int CYCLE_NUM2 = 100;
3043f4cbf05Sopenharmony_ci
3053f4cbf05Sopenharmony_ciint RegisterEventThread()
3063f4cbf05Sopenharmony_ci{
3073f4cbf05Sopenharmony_ci    auto &ipc = IPCProcessSkeleton::GetInstance();
3083f4cbf05Sopenharmony_ci    int handle = 10;
3093f4cbf05Sopenharmony_ci    for (int i = 0; i < CYCLE_NUM2; i++) {
3103f4cbf05Sopenharmony_ci        sptr<IRemoteObject> remote = ipc.FindOrNewObject(handle);
3113f4cbf05Sopenharmony_ci        remote->CheckIsAttemptAcquireSet(remote);
3123f4cbf05Sopenharmony_ci        if (remote) {
3133f4cbf05Sopenharmony_ci            remote->IsProxyObject();
3143f4cbf05Sopenharmony_ci        }
3153f4cbf05Sopenharmony_ci    }
3163f4cbf05Sopenharmony_ci    return 0;
3173f4cbf05Sopenharmony_ci}
3183f4cbf05Sopenharmony_ci
3193f4cbf05Sopenharmony_ci/*
3203f4cbf05Sopenharmony_ci * @tc.name: testRefbaseOperateThreads001
3213f4cbf05Sopenharmony_ci * @tc.desc: Refbase for threads
3223f4cbf05Sopenharmony_ci */
3233f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseOperateThreads001, TestSize.Level0)
3243f4cbf05Sopenharmony_ci{
3253f4cbf05Sopenharmony_ci    RefBaseTestTracker *tracker = RefBaseTestTracker::GetInstance();
3263f4cbf05Sopenharmony_ci    tracker->InitTracker();
3273f4cbf05Sopenharmony_ci    for (int n = 0; n < 1; n++) {
3283f4cbf05Sopenharmony_ci        std::vector<std::future<int>> threads;
3293f4cbf05Sopenharmony_ci        for (int i = 0; i < CYCLE_NUM1; i++) {
3303f4cbf05Sopenharmony_ci            threads.emplace_back(std::async(RegisterEventThread));
3313f4cbf05Sopenharmony_ci        }
3323f4cbf05Sopenharmony_ci
3333f4cbf05Sopenharmony_ci        for (auto &f : threads) {
3343f4cbf05Sopenharmony_ci            f.get();
3353f4cbf05Sopenharmony_ci        }
3363f4cbf05Sopenharmony_ci    }
3373f4cbf05Sopenharmony_ci    auto &ipc = IPCProcessSkeleton::GetInstance();
3383f4cbf05Sopenharmony_ci    ipc.DumpMapObjects();
3393f4cbf05Sopenharmony_ci    EXPECT_EQ(tracker->firstRefCount_, tracker->freeCount_);
3403f4cbf05Sopenharmony_ci}
3413f4cbf05Sopenharmony_ci
3423f4cbf05Sopenharmony_ci/*
3433f4cbf05Sopenharmony_ci * @tc.name: testRefbaseOperateNull001
3443f4cbf05Sopenharmony_ci * @tc.desc: Refbase for null
3453f4cbf05Sopenharmony_ci */
3463f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseOperateNull001, TestSize.Level0)
3473f4cbf05Sopenharmony_ci{
3483f4cbf05Sopenharmony_ci    RefBaseTestTracker *tracker = RefBaseTestTracker::GetInstance();
3493f4cbf05Sopenharmony_ci    tracker->InitTracker();
3503f4cbf05Sopenharmony_ci
3513f4cbf05Sopenharmony_ci    auto remoteObject = new IPCObjectProxy("ss");
3523f4cbf05Sopenharmony_ci    remoteObject->AttemptAcquire(this);
3533f4cbf05Sopenharmony_ci
3543f4cbf05Sopenharmony_ci    remoteObject->IncWeakRef(nullptr);
3553f4cbf05Sopenharmony_ci    remoteObject->IncStrongRef(nullptr);
3563f4cbf05Sopenharmony_ci    remoteObject->CheckIsAttemptAcquireSet(nullptr);
3573f4cbf05Sopenharmony_ci    remoteObject->DecStrongRef(nullptr);
3583f4cbf05Sopenharmony_ci    remoteObject->AttemptAcquire(this);
3593f4cbf05Sopenharmony_ci
3603f4cbf05Sopenharmony_ci    remoteObject->IncStrongRef(nullptr);
3613f4cbf05Sopenharmony_ci    remoteObject->CheckIsAttemptAcquireSet(nullptr);
3623f4cbf05Sopenharmony_ci    remoteObject->DecStrongRef(nullptr);
3633f4cbf05Sopenharmony_ci
3643f4cbf05Sopenharmony_ci    remoteObject->DecWeakRef(nullptr);
3653f4cbf05Sopenharmony_ci    EXPECT_EQ(tracker->firstRefCount_, tracker->freeCount_);
3663f4cbf05Sopenharmony_ci}
3673f4cbf05Sopenharmony_ci
3683f4cbf05Sopenharmony_ciclass RefBaseMemTest : public RefBase {
3693f4cbf05Sopenharmony_cipublic:
3703f4cbf05Sopenharmony_ci    explicit RefBaseMemTest(int value): value_(value)
3713f4cbf05Sopenharmony_ci    {
3723f4cbf05Sopenharmony_ci        g_checkCount++;
3733f4cbf05Sopenharmony_ci    }
3743f4cbf05Sopenharmony_ci
3753f4cbf05Sopenharmony_ci    ~RefBaseMemTest()
3763f4cbf05Sopenharmony_ci    {
3773f4cbf05Sopenharmony_ci        g_checkCount--;
3783f4cbf05Sopenharmony_ci    }
3793f4cbf05Sopenharmony_ci
3803f4cbf05Sopenharmony_ci    RefBaseMemTest(const RefBaseMemTest &testRefbaseMem)
3813f4cbf05Sopenharmony_ci    {
3823f4cbf05Sopenharmony_ci        g_checkCount++;
3833f4cbf05Sopenharmony_ci        value_ = testRefbaseMem.value_;
3843f4cbf05Sopenharmony_ci    }
3853f4cbf05Sopenharmony_ci
3863f4cbf05Sopenharmony_ci    RefBaseMemTest &operator=(const RefBaseMemTest &testRefbaseMem)
3873f4cbf05Sopenharmony_ci    {
3883f4cbf05Sopenharmony_ci        g_checkCount++;
3893f4cbf05Sopenharmony_ci        value_ = testRefbaseMem.value_;
3903f4cbf05Sopenharmony_ci        return *this;
3913f4cbf05Sopenharmony_ci    }
3923f4cbf05Sopenharmony_ci
3933f4cbf05Sopenharmony_ci    RefBaseMemTest(RefBaseMemTest &&testRefbaseMem)
3943f4cbf05Sopenharmony_ci    {
3953f4cbf05Sopenharmony_ci        g_checkCount++;
3963f4cbf05Sopenharmony_ci        value_ = testRefbaseMem.value_;
3973f4cbf05Sopenharmony_ci    }
3983f4cbf05Sopenharmony_ci
3993f4cbf05Sopenharmony_ci    RefBaseMemTest &operator=(RefBaseMemTest &&testRefbaseMem)
4003f4cbf05Sopenharmony_ci    {
4013f4cbf05Sopenharmony_ci        g_checkCount++;
4023f4cbf05Sopenharmony_ci        value_ = testRefbaseMem.value_;
4033f4cbf05Sopenharmony_ci        return *this;
4043f4cbf05Sopenharmony_ci    }
4053f4cbf05Sopenharmony_ci
4063f4cbf05Sopenharmony_cipublic:
4073f4cbf05Sopenharmony_ci    static inline int g_checkCount = 0;
4083f4cbf05Sopenharmony_ci
4093f4cbf05Sopenharmony_ciprivate:
4103f4cbf05Sopenharmony_ci    int value_;
4113f4cbf05Sopenharmony_ci};
4123f4cbf05Sopenharmony_ci
4133f4cbf05Sopenharmony_ci/*
4143f4cbf05Sopenharmony_ci * @tc.name: testRefbaseOperateLeftValue001
4153f4cbf05Sopenharmony_ci * @tc.desc: Refbase
4163f4cbf05Sopenharmony_ci */
4173f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseOperateLeftValue001, TestSize.Level0)
4183f4cbf05Sopenharmony_ci{
4193f4cbf05Sopenharmony_ci    RefBaseMemTest::g_checkCount = 0;
4203f4cbf05Sopenharmony_ci    {
4213f4cbf05Sopenharmony_ci        vector<RefBaseMemTest> refMemTestArray;
4223f4cbf05Sopenharmony_ci        sptr<RefBaseMemTest>refMemTestObj1 = new RefBaseMemTest(1);
4233f4cbf05Sopenharmony_ci        sptr<RefBaseMemTest>refMemTestObj2 = new RefBaseMemTest(2);
4243f4cbf05Sopenharmony_ci        refMemTestArray.push_back(*refMemTestObj1);
4253f4cbf05Sopenharmony_ci        refMemTestArray.push_back(*refMemTestObj2);
4263f4cbf05Sopenharmony_ci    }
4273f4cbf05Sopenharmony_ci    EXPECT_EQ(RefBaseMemTest::g_checkCount, 0);
4283f4cbf05Sopenharmony_ci
4293f4cbf05Sopenharmony_ci    {
4303f4cbf05Sopenharmony_ci        vector<RefBaseMemTest> refMemTestArray;
4313f4cbf05Sopenharmony_ci        RefBaseMemTest refMemTestObj1(1);
4323f4cbf05Sopenharmony_ci        RefBaseMemTest refMemTestObj2(2);
4333f4cbf05Sopenharmony_ci        refMemTestArray.push_back(refMemTestObj1);
4343f4cbf05Sopenharmony_ci        refMemTestArray.push_back(refMemTestObj2);
4353f4cbf05Sopenharmony_ci    }
4363f4cbf05Sopenharmony_ci    EXPECT_EQ(RefBaseMemTest::g_checkCount, 0);
4373f4cbf05Sopenharmony_ci}
4383f4cbf05Sopenharmony_ci
4393f4cbf05Sopenharmony_ci/*
4403f4cbf05Sopenharmony_ci * @tc.name: testRefbaseOperateRightValue001
4413f4cbf05Sopenharmony_ci * @tc.desc: Refbase
4423f4cbf05Sopenharmony_ci */
4433f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseOperateRightValue001, TestSize.Level0)
4443f4cbf05Sopenharmony_ci{
4453f4cbf05Sopenharmony_ci    RefBaseMemTest::g_checkCount = 0;
4463f4cbf05Sopenharmony_ci    {
4473f4cbf05Sopenharmony_ci        vector<RefBaseMemTest> refMemTestArray;
4483f4cbf05Sopenharmony_ci        sptr<RefBaseMemTest>refMemTestObj1 = new RefBaseMemTest(1);
4493f4cbf05Sopenharmony_ci        sptr<RefBaseMemTest>refMemTestObj2 = new RefBaseMemTest(2);
4503f4cbf05Sopenharmony_ci        refMemTestArray.emplace_back(*refMemTestObj1);
4513f4cbf05Sopenharmony_ci        refMemTestArray.emplace_back(*refMemTestObj2);
4523f4cbf05Sopenharmony_ci    }
4533f4cbf05Sopenharmony_ci    EXPECT_EQ(RefBaseMemTest::g_checkCount, 0);
4543f4cbf05Sopenharmony_ci
4553f4cbf05Sopenharmony_ci    {
4563f4cbf05Sopenharmony_ci        vector<RefBaseMemTest> refMemTestArray;
4573f4cbf05Sopenharmony_ci        RefBaseMemTest refMemTestObj1(1);
4583f4cbf05Sopenharmony_ci        RefBaseMemTest refMemTestObj2(2);
4593f4cbf05Sopenharmony_ci        refMemTestArray.emplace_back(refMemTestObj1);
4603f4cbf05Sopenharmony_ci        refMemTestArray.emplace_back(refMemTestObj2);
4613f4cbf05Sopenharmony_ci    }
4623f4cbf05Sopenharmony_ci    EXPECT_EQ(RefBaseMemTest::g_checkCount, 0);
4633f4cbf05Sopenharmony_ci}
4643f4cbf05Sopenharmony_ci
4653f4cbf05Sopenharmony_ci/*
4663f4cbf05Sopenharmony_ci * @tc.name: testRefbaseAcquire001
4673f4cbf05Sopenharmony_ci * @tc.desc: Refbase
4683f4cbf05Sopenharmony_ci */
4693f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseAcquire001, TestSize.Level0)
4703f4cbf05Sopenharmony_ci{
4713f4cbf05Sopenharmony_ci    RefBaseTest* testobject = new RefBaseTest();
4723f4cbf05Sopenharmony_ci    testobject->AttemptAcquire(this);
4733f4cbf05Sopenharmony_ci
4743f4cbf05Sopenharmony_ci    g_freeFlag = 0;
4753f4cbf05Sopenharmony_ci    EXPECT_EQ(testobject->GetSptrRefCount(), 1);
4763f4cbf05Sopenharmony_ci    {
4773f4cbf05Sopenharmony_ci        EXPECT_TRUE(testobject->IsAttemptAcquireSet());
4783f4cbf05Sopenharmony_ci        testobject->CheckIsAttemptAcquireSet(this);
4793f4cbf05Sopenharmony_ci        sptr<RefBaseTest> sptrRef = testobject;
4803f4cbf05Sopenharmony_ci        EXPECT_EQ(sptrRef->GetSptrRefCount(), 1);
4813f4cbf05Sopenharmony_ci        EXPECT_FALSE(testobject->IsAttemptAcquireSet());
4823f4cbf05Sopenharmony_ci    }
4833f4cbf05Sopenharmony_ci
4843f4cbf05Sopenharmony_ci    EXPECT_EQ(g_freeFlag, 1);
4853f4cbf05Sopenharmony_ci}
4863f4cbf05Sopenharmony_ci
4873f4cbf05Sopenharmony_ci/*
4883f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase001
4893f4cbf05Sopenharmony_ci * @tc.desc: Refbase
4903f4cbf05Sopenharmony_ci */
4913f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase001, TestSize.Level0)
4923f4cbf05Sopenharmony_ci{
4933f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testobject = new RefBaseTest();
4943f4cbf05Sopenharmony_ci    testobject->ExtendObjectLifetime();
4953f4cbf05Sopenharmony_ci    EXPECT_TRUE(testobject->IsExtendLifeTimeSet());
4963f4cbf05Sopenharmony_ci    EXPECT_EQ(g_refbaseflag, 1);
4973f4cbf05Sopenharmony_ci    wptr<RefBaseTest> weakObject(testobject);
4983f4cbf05Sopenharmony_ci    int count = testobject->GetWptrRefCount();
4993f4cbf05Sopenharmony_ci    EXPECT_EQ(count, 2);
5003f4cbf05Sopenharmony_ci    testobject = nullptr;
5013f4cbf05Sopenharmony_ci
5023f4cbf05Sopenharmony_ci    sptr<RefBaseTest> strongObject = weakObject.promote();
5033f4cbf05Sopenharmony_ci    EXPECT_EQ(strongObject->GetSptrRefCount(), 1);
5043f4cbf05Sopenharmony_ci}
5053f4cbf05Sopenharmony_ci
5063f4cbf05Sopenharmony_ci/*
5073f4cbf05Sopenharmony_ci * @tc.name: testSptrefbaseRealease001
5083f4cbf05Sopenharmony_ci * @tc.desc: Refbase
5093f4cbf05Sopenharmony_ci */
5103f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbaseRealease001, TestSize.Level0)
5113f4cbf05Sopenharmony_ci{
5123f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject = new RefBaseTest();
5133f4cbf05Sopenharmony_ci    EXPECT_EQ(g_refbaseflag, 1);
5143f4cbf05Sopenharmony_ci    wptr<RefBaseTest> weakObject(testObject);
5153f4cbf05Sopenharmony_ci    testObject = nullptr;
5163f4cbf05Sopenharmony_ci    EXPECT_EQ(g_refbaseflag, FLAG_OF_DEST);
5173f4cbf05Sopenharmony_ci}
5183f4cbf05Sopenharmony_ci
5193f4cbf05Sopenharmony_ci/*
5203f4cbf05Sopenharmony_ci * @tc.name: testSptrefbaseRealease002
5213f4cbf05Sopenharmony_ci * @tc.desc: Refbase
5223f4cbf05Sopenharmony_ci */
5233f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbaseRealease002, TestSize.Level0)
5243f4cbf05Sopenharmony_ci{
5253f4cbf05Sopenharmony_ci    wptr<RefBaseTest> testObject = new RefBaseTest();
5263f4cbf05Sopenharmony_ci    EXPECT_EQ(g_refbaseflag, 1);
5273f4cbf05Sopenharmony_ci    testObject = nullptr;
5283f4cbf05Sopenharmony_ci    EXPECT_EQ(g_refbaseflag, FLAG_OF_DEST);
5293f4cbf05Sopenharmony_ci}
5303f4cbf05Sopenharmony_ci
5313f4cbf05Sopenharmony_ci/*
5323f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase002
5333f4cbf05Sopenharmony_ci * @tc.desc: Refbase
5343f4cbf05Sopenharmony_ci */
5353f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase002, TestSize.Level0)
5363f4cbf05Sopenharmony_ci{
5373f4cbf05Sopenharmony_ci    {
5383f4cbf05Sopenharmony_ci        sptr<RefBaseTest> testObject(new RefBaseTest());
5393f4cbf05Sopenharmony_ci        EXPECT_EQ(g_refbaseflag, 1);
5403f4cbf05Sopenharmony_ci    }
5413f4cbf05Sopenharmony_ci    EXPECT_EQ(g_refbaseflag, 2);
5423f4cbf05Sopenharmony_ci}
5433f4cbf05Sopenharmony_ci
5443f4cbf05Sopenharmony_ci/*
5453f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase003
5463f4cbf05Sopenharmony_ci * @tc.desc: Refbase
5473f4cbf05Sopenharmony_ci */
5483f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase003, TestSize.Level0)
5493f4cbf05Sopenharmony_ci{
5503f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1(new RefBaseTest());
5513f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject2 = testObject1.GetRefPtr();
5523f4cbf05Sopenharmony_ci    testObject2->SetRefPtr();
5533f4cbf05Sopenharmony_ci    EXPECT_TRUE(testObject1->GetTestRefPtrFlag());
5543f4cbf05Sopenharmony_ci
5553f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject3(testObject1);
5563f4cbf05Sopenharmony_ci    EXPECT_TRUE(testObject3->GetTestRefPtrFlag());
5573f4cbf05Sopenharmony_ci
5583f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject4 = testObject1;
5593f4cbf05Sopenharmony_ci    EXPECT_TRUE(testObject3->GetTestRefPtrFlag());
5603f4cbf05Sopenharmony_ci
5613f4cbf05Sopenharmony_ci    bool ret = (testObject3 == testObject4);
5623f4cbf05Sopenharmony_ci    EXPECT_TRUE(ret);
5633f4cbf05Sopenharmony_ci
5643f4cbf05Sopenharmony_ci    int refcount = testObject1->GetSptrRefCount();
5653f4cbf05Sopenharmony_ci    EXPECT_EQ(refcount, 4);
5663f4cbf05Sopenharmony_ci
5673f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject5(new RefBaseTest());
5683f4cbf05Sopenharmony_ci    ret = (testObject5 != testObject1);
5693f4cbf05Sopenharmony_ci    EXPECT_TRUE(ret);
5703f4cbf05Sopenharmony_ci}
5713f4cbf05Sopenharmony_ci
5723f4cbf05Sopenharmony_ci/*
5733f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase004
5743f4cbf05Sopenharmony_ci * @tc.desc: Refbase
5753f4cbf05Sopenharmony_ci */
5763f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase004, TestSize.Level0)
5773f4cbf05Sopenharmony_ci{
5783f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1(new RefBaseTest());
5793f4cbf05Sopenharmony_ci    testObject1->SetRefPtr();
5803f4cbf05Sopenharmony_ci    RefBaseTest testObject2 = *testObject1;
5813f4cbf05Sopenharmony_ci    EXPECT_TRUE(testObject2.GetTestRefPtrFlag());
5823f4cbf05Sopenharmony_ci
5833f4cbf05Sopenharmony_ci    auto testObject3 = testObject1;
5843f4cbf05Sopenharmony_ci    testObject1 = nullptr;
5853f4cbf05Sopenharmony_ci    testObject3 = nullptr;
5863f4cbf05Sopenharmony_ci    EXPECT_EQ(g_refbaseflag, 2);
5873f4cbf05Sopenharmony_ci}
5883f4cbf05Sopenharmony_ci
5893f4cbf05Sopenharmony_ci/*
5903f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase005
5913f4cbf05Sopenharmony_ci * @tc.desc: Refbase
5923f4cbf05Sopenharmony_ci */
5933f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase005, TestSize.Level0)
5943f4cbf05Sopenharmony_ci{
5953f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1(new RefBaseTest());
5963f4cbf05Sopenharmony_ci    wptr<RefBaseTest> testObject2 = testObject1;
5973f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetSptrRefCount(), 1);
5983f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetWptrRefCount(), 2);
5993f4cbf05Sopenharmony_ci}
6003f4cbf05Sopenharmony_ci
6013f4cbf05Sopenharmony_ci/*
6023f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase006
6033f4cbf05Sopenharmony_ci * @tc.desc: Refbase
6043f4cbf05Sopenharmony_ci */
6053f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase006, TestSize.Level0)
6063f4cbf05Sopenharmony_ci{
6073f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1;
6083f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1.GetRefPtr(), nullptr);
6093f4cbf05Sopenharmony_ci    testObject1 = new RefBaseTest();
6103f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject2(testObject1);
6113f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetSptrRefCount(), 2);
6123f4cbf05Sopenharmony_ci}
6133f4cbf05Sopenharmony_ci
6143f4cbf05Sopenharmony_ci/*
6153f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase007
6163f4cbf05Sopenharmony_ci * @tc.desc: Refbase
6173f4cbf05Sopenharmony_ci */
6183f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase007, TestSize.Level0)
6193f4cbf05Sopenharmony_ci{
6203f4cbf05Sopenharmony_ci    const sptr<RefBaseTest> &testObject1 = new RefBaseTest();
6213f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject2(testObject1);
6223f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetSptrRefCount(), 2);
6233f4cbf05Sopenharmony_ci}
6243f4cbf05Sopenharmony_ci
6253f4cbf05Sopenharmony_ci/*
6263f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase008
6273f4cbf05Sopenharmony_ci * @tc.desc: Refbase
6283f4cbf05Sopenharmony_ci */
6293f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase008, TestSize.Level0)
6303f4cbf05Sopenharmony_ci{
6313f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1;
6323f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject2(testObject1);
6333f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2, nullptr);
6343f4cbf05Sopenharmony_ci}
6353f4cbf05Sopenharmony_ci
6363f4cbf05Sopenharmony_ci/*
6373f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase009
6383f4cbf05Sopenharmony_ci * @tc.desc: Refbase
6393f4cbf05Sopenharmony_ci */
6403f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase009, TestSize.Level0)
6413f4cbf05Sopenharmony_ci{
6423f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject0 = new RefBaseTest();
6433f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1 = move(testObject0);
6443f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject2(testObject1);
6453f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject0.GetRefPtr(), nullptr);
6463f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2.GetRefPtr(), testObject1.GetRefPtr());
6473f4cbf05Sopenharmony_ci}
6483f4cbf05Sopenharmony_ci
6493f4cbf05Sopenharmony_ci/*
6503f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase010
6513f4cbf05Sopenharmony_ci * @tc.desc: Refbase
6523f4cbf05Sopenharmony_ci */
6533f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase010, TestSize.Level0)
6543f4cbf05Sopenharmony_ci{
6553f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1 = new RefBaseTest();
6563f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject3(new RefBaseTest());
6573f4cbf05Sopenharmony_ci    sptr<RefBaseTest> &testObject2 = testObject3;
6583f4cbf05Sopenharmony_ci    testObject2 = testObject1;
6593f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2.GetRefPtr(), testObject1.GetRefPtr());
6603f4cbf05Sopenharmony_ci
6613f4cbf05Sopenharmony_ci    const sptr<RefBaseTest> &testObject4 = new RefBaseTest();
6623f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetSptrRefCount(), 2);
6633f4cbf05Sopenharmony_ci    testObject2 = testObject4;
6643f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2.GetRefPtr(), testObject4.GetRefPtr());
6653f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject4->GetSptrRefCount(), 2);
6663f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetSptrRefCount(), 1);
6673f4cbf05Sopenharmony_ci}
6683f4cbf05Sopenharmony_ci
6693f4cbf05Sopenharmony_ci/*
6703f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase011
6713f4cbf05Sopenharmony_ci * @tc.desc: Refbase
6723f4cbf05Sopenharmony_ci */
6733f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase011, TestSize.Level0)
6743f4cbf05Sopenharmony_ci{
6753f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testobject = sptr<RefBaseTest>::MakeSptr();
6763f4cbf05Sopenharmony_ci    testobject->ExtendObjectLifetime();
6773f4cbf05Sopenharmony_ci    EXPECT_TRUE(testobject->IsExtendLifeTimeSet());
6783f4cbf05Sopenharmony_ci    EXPECT_EQ(g_refbaseflag, 1);
6793f4cbf05Sopenharmony_ci    wptr<RefBaseTest> weakObject(testobject);
6803f4cbf05Sopenharmony_ci    int count = testobject->GetWptrRefCount();
6813f4cbf05Sopenharmony_ci    EXPECT_EQ(count, 2);
6823f4cbf05Sopenharmony_ci    testobject = nullptr;
6833f4cbf05Sopenharmony_ci
6843f4cbf05Sopenharmony_ci    sptr<RefBaseTest> strongObject = weakObject.promote();
6853f4cbf05Sopenharmony_ci    EXPECT_EQ(strongObject->GetSptrRefCount(), 1);
6863f4cbf05Sopenharmony_ci}
6873f4cbf05Sopenharmony_ci
6883f4cbf05Sopenharmony_ci/*
6893f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase012
6903f4cbf05Sopenharmony_ci * @tc.desc: Refbase
6913f4cbf05Sopenharmony_ci */
6923f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase012, TestSize.Level0)
6933f4cbf05Sopenharmony_ci{
6943f4cbf05Sopenharmony_ci    // test clear
6953f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1 = new RefBaseTest();
6963f4cbf05Sopenharmony_ci    testObject1.clear();
6973f4cbf05Sopenharmony_ci    ASSERT_EQ(testObject1.GetRefPtr(), nullptr);
6983f4cbf05Sopenharmony_ci}
6993f4cbf05Sopenharmony_ci
7003f4cbf05Sopenharmony_ci/*
7013f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase013
7023f4cbf05Sopenharmony_ci * @tc.desc: Refbase
7033f4cbf05Sopenharmony_ci */
7043f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase013, TestSize.Level0)
7053f4cbf05Sopenharmony_ci{
7063f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1;
7073f4cbf05Sopenharmony_ci    wptr<RefBaseTest> testObject2 = new RefBaseTest();
7083f4cbf05Sopenharmony_ci    testObject1 = testObject2;
7093f4cbf05Sopenharmony_ci    ASSERT_EQ(testObject2->GetWptrRefCount(), 2);
7103f4cbf05Sopenharmony_ci    ASSERT_EQ(testObject1->GetSptrRefCount(), 1);
7113f4cbf05Sopenharmony_ci}
7123f4cbf05Sopenharmony_ci
7133f4cbf05Sopenharmony_ci/*
7143f4cbf05Sopenharmony_ci * @tc.name: testSptrefbase014
7153f4cbf05Sopenharmony_ci * @tc.desc: Refbase
7163f4cbf05Sopenharmony_ci */
7173f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrefbase014, TestSize.Level0)
7183f4cbf05Sopenharmony_ci{
7193f4cbf05Sopenharmony_ci    sptr<RefBaseTest> testObject1(new RefBaseTest());
7203f4cbf05Sopenharmony_ci    const RefBaseTest *rawPointer = testObject1.GetRefPtr();
7213f4cbf05Sopenharmony_ci    ASSERT_TRUE(testObject1 == rawPointer);
7223f4cbf05Sopenharmony_ci
7233f4cbf05Sopenharmony_ci    wptr<RefBaseTest> testObject2(new RefBaseTest());
7243f4cbf05Sopenharmony_ci    ASSERT_FALSE(testObject1 == testObject2);
7253f4cbf05Sopenharmony_ci}
7263f4cbf05Sopenharmony_ci
7273f4cbf05Sopenharmony_ciclass SptrTest : public RefBase {
7283f4cbf05Sopenharmony_cipublic:
7293f4cbf05Sopenharmony_ci    SptrTest()
7303f4cbf05Sopenharmony_ci    {
7313f4cbf05Sopenharmony_ci        g_sptrCount++;
7323f4cbf05Sopenharmony_ci    }
7333f4cbf05Sopenharmony_ci    ~SptrTest()
7343f4cbf05Sopenharmony_ci    {
7353f4cbf05Sopenharmony_ci        g_sptrCount--;
7363f4cbf05Sopenharmony_ci    }
7373f4cbf05Sopenharmony_ci    void CreateSptr()
7383f4cbf05Sopenharmony_ci    {
7393f4cbf05Sopenharmony_ci        test1 = new SptrTest();
7403f4cbf05Sopenharmony_ci    }
7413f4cbf05Sopenharmony_ci
7423f4cbf05Sopenharmony_ciprivate:
7433f4cbf05Sopenharmony_ci    sptr<SptrTest> test1;
7443f4cbf05Sopenharmony_ci};
7453f4cbf05Sopenharmony_ci
7463f4cbf05Sopenharmony_ci/*
7473f4cbf05Sopenharmony_ci * @tc.name: testRefbase005
7483f4cbf05Sopenharmony_ci * @tc.desc: Refbase
7493f4cbf05Sopenharmony_ci */
7503f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbase005, TestSize.Level0)
7513f4cbf05Sopenharmony_ci{
7523f4cbf05Sopenharmony_ci    {
7533f4cbf05Sopenharmony_ci        sptr<SptrTest> testObject1(new SptrTest());
7543f4cbf05Sopenharmony_ci        testObject1->CreateSptr();
7553f4cbf05Sopenharmony_ci    }
7563f4cbf05Sopenharmony_ci    EXPECT_EQ(g_sptrCount, 0);
7573f4cbf05Sopenharmony_ci}
7583f4cbf05Sopenharmony_ci
7593f4cbf05Sopenharmony_ciclass SptrTest1;
7603f4cbf05Sopenharmony_ciclass SptrTest2;
7613f4cbf05Sopenharmony_ciclass SptrTest2 : public RefBase {
7623f4cbf05Sopenharmony_cipublic:
7633f4cbf05Sopenharmony_ci    SptrTest2()
7643f4cbf05Sopenharmony_ci    {
7653f4cbf05Sopenharmony_ci        g_sptrCount++;
7663f4cbf05Sopenharmony_ci    }
7673f4cbf05Sopenharmony_ci    ~SptrTest2()
7683f4cbf05Sopenharmony_ci    {
7693f4cbf05Sopenharmony_ci        g_sptrCount--;
7703f4cbf05Sopenharmony_ci    }
7713f4cbf05Sopenharmony_ci
7723f4cbf05Sopenharmony_ciprivate:
7733f4cbf05Sopenharmony_ci    sptr<SptrTest1> test;
7743f4cbf05Sopenharmony_ci};
7753f4cbf05Sopenharmony_ci
7763f4cbf05Sopenharmony_ciclass SptrTest1 : public RefBase {
7773f4cbf05Sopenharmony_cipublic:
7783f4cbf05Sopenharmony_ci    SptrTest1()
7793f4cbf05Sopenharmony_ci    {
7803f4cbf05Sopenharmony_ci        g_sptrCount++;
7813f4cbf05Sopenharmony_ci    }
7823f4cbf05Sopenharmony_ci    ~SptrTest1()
7833f4cbf05Sopenharmony_ci    {
7843f4cbf05Sopenharmony_ci        g_sptrCount--;
7853f4cbf05Sopenharmony_ci    }
7863f4cbf05Sopenharmony_ci
7873f4cbf05Sopenharmony_ciprivate:
7883f4cbf05Sopenharmony_ci    sptr<SptrTest2> test;
7893f4cbf05Sopenharmony_ci};
7903f4cbf05Sopenharmony_ci
7913f4cbf05Sopenharmony_ci/*
7923f4cbf05Sopenharmony_ci * @tc.name: testRefbase006
7933f4cbf05Sopenharmony_ci * @tc.desc: Refbase
7943f4cbf05Sopenharmony_ci */
7953f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbase006, TestSize.Level0)
7963f4cbf05Sopenharmony_ci{
7973f4cbf05Sopenharmony_ci    {
7983f4cbf05Sopenharmony_ci        sptr<SptrTest1> testObject1(new SptrTest1());
7993f4cbf05Sopenharmony_ci        sptr<SptrTest2> testObject2(new SptrTest2());
8003f4cbf05Sopenharmony_ci        EXPECT_EQ(g_sptrCount, 2);
8013f4cbf05Sopenharmony_ci    }
8023f4cbf05Sopenharmony_ci    EXPECT_EQ(g_sptrCount, 0);
8033f4cbf05Sopenharmony_ci}
8043f4cbf05Sopenharmony_ci
8053f4cbf05Sopenharmony_ci/*
8063f4cbf05Sopenharmony_ci * @tc.name: testRefbase007
8073f4cbf05Sopenharmony_ci * @tc.desc: test count of refcounter.
8083f4cbf05Sopenharmony_ci */
8093f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbase007, TestSize.Level0)
8103f4cbf05Sopenharmony_ci{
8113f4cbf05Sopenharmony_ci    sptr<RefBase> testObject1(new RefBase());
8123f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetRefCounter()->GetRefCount(), 1);
8133f4cbf05Sopenharmony_ci    wptr<RefBase> testObject2(testObject1);
8143f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetRefCounter()->GetRefCount(), 2); // 2: Refbase and WeakRefCounter
8153f4cbf05Sopenharmony_ci}
8163f4cbf05Sopenharmony_ci
8173f4cbf05Sopenharmony_ci/*
8183f4cbf05Sopenharmony_ci * @tc.name: testRefbase008
8193f4cbf05Sopenharmony_ci * @tc.desc: test move constructor.
8203f4cbf05Sopenharmony_ci */
8213f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbase008, TestSize.Level0)
8223f4cbf05Sopenharmony_ci{
8233f4cbf05Sopenharmony_ci    RefBase baseObject1{};
8243f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject1.GetRefCounter()->GetRefCount(), 1);
8253f4cbf05Sopenharmony_ci
8263f4cbf05Sopenharmony_ci    RefBase baseObject2{};
8273f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject2.GetRefCounter()->GetRefCount(), 1);
8283f4cbf05Sopenharmony_ci    baseObject2 = std::move(baseObject1);
8293f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject2.GetRefCounter()->GetRefCount(), 1);
8303f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject1.GetRefCounter(), nullptr);
8313f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject1.GetSptrRefCount(), 0);
8323f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject1.GetWptrRefCount(), 0);
8333f4cbf05Sopenharmony_ci
8343f4cbf05Sopenharmony_ci    RefBase baseObject3{};
8353f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject3.GetRefCounter()->GetRefCount(), 1);
8363f4cbf05Sopenharmony_ci    baseObject3 = std::move(baseObject2);
8373f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject3.GetRefCounter()->GetRefCount(), 1);
8383f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject2.GetRefCounter(), nullptr);
8393f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject2.GetSptrRefCount(), 0);
8403f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject2.GetWptrRefCount(), 0);
8413f4cbf05Sopenharmony_ci
8423f4cbf05Sopenharmony_ci    baseObject2 = std::move(baseObject1);
8433f4cbf05Sopenharmony_ci    EXPECT_EQ(baseObject1.GetRefCounter(), baseObject2.GetRefCounter());
8443f4cbf05Sopenharmony_ci}
8453f4cbf05Sopenharmony_ci
8463f4cbf05Sopenharmony_ciclass WptrTest : public RefBase {
8473f4cbf05Sopenharmony_cipublic:
8483f4cbf05Sopenharmony_ci    WptrTest()
8493f4cbf05Sopenharmony_ci    {
8503f4cbf05Sopenharmony_ci        g_sptrCount++;
8513f4cbf05Sopenharmony_ci    }
8523f4cbf05Sopenharmony_ci    ~WptrTest()
8533f4cbf05Sopenharmony_ci    {
8543f4cbf05Sopenharmony_ci        g_sptrCount--;
8553f4cbf05Sopenharmony_ci    }
8563f4cbf05Sopenharmony_ci};
8573f4cbf05Sopenharmony_ci
8583f4cbf05Sopenharmony_ciclass WptrTest2 : public RefBase {
8593f4cbf05Sopenharmony_cipublic:
8603f4cbf05Sopenharmony_ci    WptrTest2()
8613f4cbf05Sopenharmony_ci    {
8623f4cbf05Sopenharmony_ci        g_sptrCount++;
8633f4cbf05Sopenharmony_ci        flag_ = 0;
8643f4cbf05Sopenharmony_ci    }
8653f4cbf05Sopenharmony_ci    ~WptrTest2()
8663f4cbf05Sopenharmony_ci    {
8673f4cbf05Sopenharmony_ci        g_sptrCount--;
8683f4cbf05Sopenharmony_ci    }
8693f4cbf05Sopenharmony_ci
8703f4cbf05Sopenharmony_ciprivate:
8713f4cbf05Sopenharmony_ci    int flag_;
8723f4cbf05Sopenharmony_ci};
8733f4cbf05Sopenharmony_ci
8743f4cbf05Sopenharmony_ci/*
8753f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase001
8763f4cbf05Sopenharmony_ci * @tc.desc: Copy constructor with same managed class type.
8773f4cbf05Sopenharmony_ci */
8783f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase001, TestSize.Level0)
8793f4cbf05Sopenharmony_ci{
8803f4cbf05Sopenharmony_ci    // test wptr<T>::wptr(const wptr<T>&)
8813f4cbf05Sopenharmony_ci    wptr<WptrTest> testOrigWptrObject(new WptrTest());
8823f4cbf05Sopenharmony_ci    EXPECT_EQ(testOrigWptrObject->GetWptrRefCount(), 1);
8833f4cbf05Sopenharmony_ci
8843f4cbf05Sopenharmony_ci    wptr<WptrTest> testTargetWptrObject1(testOrigWptrObject);
8853f4cbf05Sopenharmony_ci
8863f4cbf05Sopenharmony_ci    EXPECT_EQ(testOrigWptrObject.GetRefPtr(), testTargetWptrObject1.GetRefPtr());
8873f4cbf05Sopenharmony_ci    EXPECT_EQ(&(*testOrigWptrObject), &(*testTargetWptrObject1));
8883f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject1->GetWptrRefCount(), testOrigWptrObject->GetWptrRefCount());
8893f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject1.GetWeakRefCount(), testOrigWptrObject.GetWeakRefCount());
8903f4cbf05Sopenharmony_ci
8913f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject1->GetWptrRefCount(), 1);
8923f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject1.GetWeakRefCount(), 2);
8933f4cbf05Sopenharmony_ci
8943f4cbf05Sopenharmony_ci    // test wptr<T>::operator=(const wptr<T>&)
8953f4cbf05Sopenharmony_ci    wptr<WptrTest> testTargetWptrObject2(new WptrTest());
8963f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2->GetWptrRefCount(), 1);
8973f4cbf05Sopenharmony_ci
8983f4cbf05Sopenharmony_ci    testTargetWptrObject2 = testOrigWptrObject;
8993f4cbf05Sopenharmony_ci
9003f4cbf05Sopenharmony_ci    EXPECT_EQ(testOrigWptrObject.GetRefPtr(), testTargetWptrObject2.GetRefPtr());
9013f4cbf05Sopenharmony_ci    EXPECT_EQ(&(*testOrigWptrObject), &(*testTargetWptrObject2));
9023f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2->GetWptrRefCount(), testOrigWptrObject->GetWptrRefCount());
9033f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2.GetWeakRefCount(), testOrigWptrObject.GetWeakRefCount());
9043f4cbf05Sopenharmony_ci
9053f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2->GetWptrRefCount(), 1);
9063f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2.GetWeakRefCount(), 3);
9073f4cbf05Sopenharmony_ci
9083f4cbf05Sopenharmony_ci    // test wptr<T>::wptr(const sptr<T>&)
9093f4cbf05Sopenharmony_ci    sptr<WptrTest> testOrigSptrObject(new WptrTest());
9103f4cbf05Sopenharmony_ci    EXPECT_EQ(testOrigSptrObject->GetSptrRefCount(), 1);
9113f4cbf05Sopenharmony_ci
9123f4cbf05Sopenharmony_ci    wptr<WptrTest> testTargetWptrObject3(testOrigSptrObject);
9133f4cbf05Sopenharmony_ci
9143f4cbf05Sopenharmony_ci    EXPECT_EQ(testOrigSptrObject.GetRefPtr(), testTargetWptrObject3.GetRefPtr());
9153f4cbf05Sopenharmony_ci    EXPECT_EQ(&(*testOrigSptrObject), &(*testTargetWptrObject3));
9163f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3->GetSptrRefCount(), testOrigSptrObject->GetSptrRefCount());
9173f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3->GetWptrRefCount(), testOrigSptrObject->GetWptrRefCount());
9183f4cbf05Sopenharmony_ci
9193f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3->GetSptrRefCount(), 1);
9203f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3->GetWptrRefCount(), 2);
9213f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3.GetWeakRefCount(), 1);
9223f4cbf05Sopenharmony_ci
9233f4cbf05Sopenharmony_ci    // test wptr<T>::operator=(const sptr<T>&)
9243f4cbf05Sopenharmony_ci    wptr<WptrTest> testTargetWptrObject4(new WptrTest());
9253f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetWptrRefCount(), 1);
9263f4cbf05Sopenharmony_ci
9273f4cbf05Sopenharmony_ci    testTargetWptrObject4 = testOrigSptrObject;
9283f4cbf05Sopenharmony_ci
9293f4cbf05Sopenharmony_ci    EXPECT_EQ(testOrigSptrObject.GetRefPtr(), testTargetWptrObject4.GetRefPtr());
9303f4cbf05Sopenharmony_ci    EXPECT_EQ(&(*testOrigSptrObject), &(*testTargetWptrObject4));
9313f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetSptrRefCount(), testOrigSptrObject->GetSptrRefCount());
9323f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetWptrRefCount(), testOrigSptrObject->GetWptrRefCount());
9333f4cbf05Sopenharmony_ci
9343f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetSptrRefCount(), 1);
9353f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetWptrRefCount(), 3);
9363f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4.GetWeakRefCount(), 1);
9373f4cbf05Sopenharmony_ci}
9383f4cbf05Sopenharmony_ci
9393f4cbf05Sopenharmony_ci/*
9403f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase002
9413f4cbf05Sopenharmony_ci * @tc.desc: Copy constructor with different managed class type.
9423f4cbf05Sopenharmony_ci */
9433f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase002, TestSize.Level0)
9443f4cbf05Sopenharmony_ci{
9453f4cbf05Sopenharmony_ci    // test wptr<T>::wptr(const wptr<O>&)
9463f4cbf05Sopenharmony_ci    wptr<WptrTest2> testOrigWptrObject(new WptrTest2());
9473f4cbf05Sopenharmony_ci    EXPECT_EQ(testOrigWptrObject->GetWptrRefCount(), 1);
9483f4cbf05Sopenharmony_ci
9493f4cbf05Sopenharmony_ci    wptr<WptrTest> testTargetWptrObject1(testOrigWptrObject);
9503f4cbf05Sopenharmony_ci
9513f4cbf05Sopenharmony_ci    EXPECT_EQ(static_cast<void *>(testOrigWptrObject.GetRefPtr()),
9523f4cbf05Sopenharmony_ci              static_cast<void *>(testTargetWptrObject1.GetRefPtr()));
9533f4cbf05Sopenharmony_ci    EXPECT_EQ(static_cast<void *>(&(*testOrigWptrObject)),
9543f4cbf05Sopenharmony_ci              static_cast<void *>(&(*testTargetWptrObject1)));
9553f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject1->GetWptrRefCount(), testOrigWptrObject->GetWptrRefCount());
9563f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject1.GetWeakRefCount(), testOrigWptrObject.GetWeakRefCount());
9573f4cbf05Sopenharmony_ci
9583f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject1->GetWptrRefCount(), 1);
9593f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject1.GetWeakRefCount(), 2);
9603f4cbf05Sopenharmony_ci
9613f4cbf05Sopenharmony_ci    // test wptr<T>::operator=(const wptr<O>&)
9623f4cbf05Sopenharmony_ci    wptr<WptrTest> testTargetWptrObject2(new WptrTest());
9633f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2->GetWptrRefCount(), 1);
9643f4cbf05Sopenharmony_ci
9653f4cbf05Sopenharmony_ci    testTargetWptrObject2 = testOrigWptrObject;
9663f4cbf05Sopenharmony_ci
9673f4cbf05Sopenharmony_ci    EXPECT_EQ(static_cast<void *>(testOrigWptrObject.GetRefPtr()),
9683f4cbf05Sopenharmony_ci              static_cast<void *>(testTargetWptrObject2.GetRefPtr()));
9693f4cbf05Sopenharmony_ci    EXPECT_EQ(static_cast<void *>(&(*testOrigWptrObject)),
9703f4cbf05Sopenharmony_ci              static_cast<void *>(&(*testTargetWptrObject2)));
9713f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2->GetWptrRefCount(), testOrigWptrObject->GetWptrRefCount());
9723f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2.GetWeakRefCount(), testOrigWptrObject.GetWeakRefCount());
9733f4cbf05Sopenharmony_ci
9743f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2->GetWptrRefCount(), 1);
9753f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject2.GetWeakRefCount(), 3);
9763f4cbf05Sopenharmony_ci
9773f4cbf05Sopenharmony_ci    // test wptr<T>::wptr(const sptr<O>&)
9783f4cbf05Sopenharmony_ci    sptr<WptrTest2> testOrigSptrObject(new WptrTest2());
9793f4cbf05Sopenharmony_ci    EXPECT_EQ(testOrigSptrObject->GetSptrRefCount(), 1);
9803f4cbf05Sopenharmony_ci
9813f4cbf05Sopenharmony_ci    wptr<WptrTest> testTargetWptrObject3(testOrigSptrObject);
9823f4cbf05Sopenharmony_ci
9833f4cbf05Sopenharmony_ci    EXPECT_EQ(static_cast<void *>(testOrigSptrObject.GetRefPtr()),
9843f4cbf05Sopenharmony_ci              static_cast<void *>(testTargetWptrObject3.GetRefPtr()));
9853f4cbf05Sopenharmony_ci    EXPECT_EQ(static_cast<void *>(&(*testOrigSptrObject)), static_cast<void *>(&(*testTargetWptrObject3)));
9863f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3->GetSptrRefCount(), testOrigSptrObject->GetSptrRefCount());
9873f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3->GetWptrRefCount(), testOrigSptrObject->GetWptrRefCount());
9883f4cbf05Sopenharmony_ci
9893f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3->GetSptrRefCount(), 1);
9903f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3->GetWptrRefCount(), 2);
9913f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject3.GetWeakRefCount(), 1);
9923f4cbf05Sopenharmony_ci
9933f4cbf05Sopenharmony_ci    // test wptr<T>::operator=(const sptr<O>&)
9943f4cbf05Sopenharmony_ci    wptr<WptrTest> testTargetWptrObject4(new WptrTest());
9953f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetWptrRefCount(), 1);
9963f4cbf05Sopenharmony_ci
9973f4cbf05Sopenharmony_ci    testTargetWptrObject4 = testOrigSptrObject;
9983f4cbf05Sopenharmony_ci
9993f4cbf05Sopenharmony_ci    EXPECT_EQ(static_cast<void *>(testOrigSptrObject.GetRefPtr()),
10003f4cbf05Sopenharmony_ci              static_cast<void *>(testTargetWptrObject4.GetRefPtr()));
10013f4cbf05Sopenharmony_ci    EXPECT_EQ(static_cast<void *>(&(*testOrigSptrObject)), static_cast<void *>(&(*testTargetWptrObject4)));
10023f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetSptrRefCount(), testOrigSptrObject->GetSptrRefCount());
10033f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetWptrRefCount(), testOrigSptrObject->GetWptrRefCount());
10043f4cbf05Sopenharmony_ci
10053f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetSptrRefCount(), 1);
10063f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4->GetWptrRefCount(), 3);
10073f4cbf05Sopenharmony_ci    EXPECT_EQ(testTargetWptrObject4.GetWeakRefCount(), 1);
10083f4cbf05Sopenharmony_ci}
10093f4cbf05Sopenharmony_ci
10103f4cbf05Sopenharmony_ci/*
10113f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase003
10123f4cbf05Sopenharmony_ci * @tc.desc: Refbase
10133f4cbf05Sopenharmony_ci */
10143f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase003, TestSize.Level0)
10153f4cbf05Sopenharmony_ci{
10163f4cbf05Sopenharmony_ci    const wptr<WptrTest> &testObject1(new WptrTest());
10173f4cbf05Sopenharmony_ci    wptr<WptrTest> testObject2(testObject1);
10183f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1.GetRefPtr(), testObject2.GetRefPtr());
10193f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetWptrRefCount(), 1);
10203f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2->GetWptrRefCount(), 1);
10213f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1.GetRefPtr(), testObject2.GetRefPtr());
10223f4cbf05Sopenharmony_ci}
10233f4cbf05Sopenharmony_ci
10243f4cbf05Sopenharmony_ci/*
10253f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase004
10263f4cbf05Sopenharmony_ci * @tc.desc: Refbase
10273f4cbf05Sopenharmony_ci */
10283f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase004, TestSize.Level0)
10293f4cbf05Sopenharmony_ci{
10303f4cbf05Sopenharmony_ci    const sptr<WptrTest2> &testObject1(new WptrTest2());
10313f4cbf05Sopenharmony_ci    EXPECT_NE(testObject1, nullptr);
10323f4cbf05Sopenharmony_ci    wptr<WptrTest> testObject2 = testObject1;
10333f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetWptrRefCount(), 2);
10343f4cbf05Sopenharmony_ci}
10353f4cbf05Sopenharmony_ci
10363f4cbf05Sopenharmony_ci/*
10373f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase005
10383f4cbf05Sopenharmony_ci * @tc.desc: wptr without managed object
10393f4cbf05Sopenharmony_ci */
10403f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase005, TestSize.Level0)
10413f4cbf05Sopenharmony_ci{
10423f4cbf05Sopenharmony_ci    wptr<WptrTest> testObject3;
10433f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject3.GetRefPtr(), nullptr);
10443f4cbf05Sopenharmony_ci}
10453f4cbf05Sopenharmony_ci
10463f4cbf05Sopenharmony_ci/*
10473f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase006
10483f4cbf05Sopenharmony_ci * @tc.desc: Refbase
10493f4cbf05Sopenharmony_ci */
10503f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase006, TestSize.Level0)
10513f4cbf05Sopenharmony_ci{
10523f4cbf05Sopenharmony_ci    wptr<WptrTest> testObject1 = new WptrTest();
10533f4cbf05Sopenharmony_ci    wptr<WptrTest> &testObject2 = testObject1;
10543f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2->GetWptrRefCount(), 1);
10553f4cbf05Sopenharmony_ci}
10563f4cbf05Sopenharmony_ci
10573f4cbf05Sopenharmony_ci/*
10583f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase007
10593f4cbf05Sopenharmony_ci * @tc.desc: Refbase
10603f4cbf05Sopenharmony_ci */
10613f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase007, TestSize.Level0)
10623f4cbf05Sopenharmony_ci{
10633f4cbf05Sopenharmony_ci    wptr<WptrTest2> testObject1 = new WptrTest2();
10643f4cbf05Sopenharmony_ci    wptr<WptrTest2> testObject2 = testObject1.GetRefPtr();
10653f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetWptrRefCount(), 2);
10663f4cbf05Sopenharmony_ci}
10673f4cbf05Sopenharmony_ci
10683f4cbf05Sopenharmony_ci/*
10693f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase008
10703f4cbf05Sopenharmony_ci * @tc.desc: Refbase
10713f4cbf05Sopenharmony_ci */
10723f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase008, TestSize.Level0)
10733f4cbf05Sopenharmony_ci{
10743f4cbf05Sopenharmony_ci    wptr<WptrTest> testObject1 = new WptrTest();
10753f4cbf05Sopenharmony_ci    wptr<WptrTest2> testObject2;
10763f4cbf05Sopenharmony_ci    testObject2 = testObject1.GetRefPtr();
10773f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetWptrRefCount(), 2);
10783f4cbf05Sopenharmony_ci}
10793f4cbf05Sopenharmony_ci
10803f4cbf05Sopenharmony_ci/*
10813f4cbf05Sopenharmony_ci * @tc.name: testWptrefbase008
10823f4cbf05Sopenharmony_ci * @tc.desc: Refbase
10833f4cbf05Sopenharmony_ci */
10843f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testWptrefbase009, TestSize.Level0)
10853f4cbf05Sopenharmony_ci{
10863f4cbf05Sopenharmony_ci    // test bool operator==(const T *)
10873f4cbf05Sopenharmony_ci    wptr<WptrTest> testObject1 = new WptrTest();
10883f4cbf05Sopenharmony_ci    const WptrTest *rawPointer = testObject1.GetRefPtr();
10893f4cbf05Sopenharmony_ci    ASSERT_TRUE(testObject1 == rawPointer);
10903f4cbf05Sopenharmony_ci
10913f4cbf05Sopenharmony_ci    // test bool operator==(const wptr &)
10923f4cbf05Sopenharmony_ci    wptr<WptrTest> testObject2 = testObject1;
10933f4cbf05Sopenharmony_ci    ASSERT_TRUE(testObject2 == testObject1);
10943f4cbf05Sopenharmony_ci
10953f4cbf05Sopenharmony_ci    // test operator==(const sptr &)
10963f4cbf05Sopenharmony_ci    sptr<WptrTest> testObject3 = new WptrTest();
10973f4cbf05Sopenharmony_ci    ASSERT_FALSE(testObject2 == testObject3);
10983f4cbf05Sopenharmony_ci}
10993f4cbf05Sopenharmony_ci
11003f4cbf05Sopenharmony_ci/*
11013f4cbf05Sopenharmony_ci * @tc.name: testSptrWptrefbase001
11023f4cbf05Sopenharmony_ci * @tc.desc: test interaction between sptr and wptr.
11033f4cbf05Sopenharmony_ci */
11043f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testSptrWptrefbase001, TestSize.Level0)
11053f4cbf05Sopenharmony_ci{
11063f4cbf05Sopenharmony_ci    wptr<RefBase> testObject1(new RefBase());
11073f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetWptrRefCount(), 1);
11083f4cbf05Sopenharmony_ci    {
11093f4cbf05Sopenharmony_ci        sptr<RefBase> testObject2{};
11103f4cbf05Sopenharmony_ci        testObject2 = testObject1;
11113f4cbf05Sopenharmony_ci        EXPECT_EQ(testObject2->GetSptrRefCount(), 1);
11123f4cbf05Sopenharmony_ci        EXPECT_EQ(testObject2->GetWptrRefCount(), 2); // 2: sptr and WeakRefCounter
11133f4cbf05Sopenharmony_ci
11143f4cbf05Sopenharmony_ci        sptr<RefBase> testObject3 = testObject1.promote();
11153f4cbf05Sopenharmony_ci        EXPECT_EQ(testObject2->GetSptrRefCount(), 2); // 2: 2 sptrs
11163f4cbf05Sopenharmony_ci        EXPECT_EQ(testObject2->GetWptrRefCount(), 3); // 3: 2 sptrs and WeakRefCounter
11173f4cbf05Sopenharmony_ci        testObject2->ExtendObjectLifetime();
11183f4cbf05Sopenharmony_ci    }
11193f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject1->GetWptrRefCount(), 1);
11203f4cbf05Sopenharmony_ci}
11213f4cbf05Sopenharmony_ci
11223f4cbf05Sopenharmony_ci/*
11233f4cbf05Sopenharmony_ci * @tc.name: testRefbaseDebug001
11243f4cbf05Sopenharmony_ci * @tc.desc: Test for single thread. Tracker can be enabled after construction
11253f4cbf05Sopenharmony_ci *           of sptr.
11263f4cbf05Sopenharmony_ci */
11273f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseDebug001, TestSize.Level1)
11283f4cbf05Sopenharmony_ci{
11293f4cbf05Sopenharmony_ci    sptr<RefBase> testObject1(new RefBase());
11303f4cbf05Sopenharmony_ci    testObject1->EnableTracker();
11313f4cbf05Sopenharmony_ci    sptr<RefBase> testObject2(testObject1);
11323f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2->GetSptrRefCount(), 2);
11333f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11343f4cbf05Sopenharmony_ci    wptr<RefBase> testObject3(testObject2);
11353f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11363f4cbf05Sopenharmony_ci    wptr<RefBase> testObject4(testObject3);
11373f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject4->GetWptrRefCount(), 3);
11383f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11393f4cbf05Sopenharmony_ci}
11403f4cbf05Sopenharmony_ci
11413f4cbf05Sopenharmony_ci/*
11423f4cbf05Sopenharmony_ci * @tc.name: testRefbaseDebug002
11433f4cbf05Sopenharmony_ci * @tc.desc: Test for single thread. Tracker can be enabled after construction
11443f4cbf05Sopenharmony_ci *           of wptr.
11453f4cbf05Sopenharmony_ci */
11463f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseDebug002, TestSize.Level1)
11473f4cbf05Sopenharmony_ci{
11483f4cbf05Sopenharmony_ci    wptr<RefBase> testObject1(new RefBase());
11493f4cbf05Sopenharmony_ci    testObject1->EnableTracker();
11503f4cbf05Sopenharmony_ci    sptr<RefBase> testObject2 = testObject1.promote();
11513f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2->GetSptrRefCount(), 1);
11523f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11533f4cbf05Sopenharmony_ci    wptr<RefBase> testObject3(testObject2);
11543f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11553f4cbf05Sopenharmony_ci    wptr<RefBase> testObject4(testObject3);
11563f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject4->GetWptrRefCount(), 3);
11573f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11583f4cbf05Sopenharmony_ci}
11593f4cbf05Sopenharmony_ci
11603f4cbf05Sopenharmony_ci// This is a class which can be tracked when implemented.
11613f4cbf05Sopenharmony_ciclass TestDebug : public RefBase {
11623f4cbf05Sopenharmony_cipublic:
11633f4cbf05Sopenharmony_ci    TestDebug()
11643f4cbf05Sopenharmony_ci    {
11653f4cbf05Sopenharmony_ci        EnableTracker();
11663f4cbf05Sopenharmony_ci    }
11673f4cbf05Sopenharmony_ci};
11683f4cbf05Sopenharmony_ci
11693f4cbf05Sopenharmony_ci/*
11703f4cbf05Sopenharmony_ci * @tc.name: testRefbaseDebug003
11713f4cbf05Sopenharmony_ci * @tc.desc: Test for single thread. Tracker can be enabled with construction
11723f4cbf05Sopenharmony_ci *           of sptr.
11733f4cbf05Sopenharmony_ci */
11743f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseDebug003, TestSize.Level1)
11753f4cbf05Sopenharmony_ci{
11763f4cbf05Sopenharmony_ci    sptr<TestDebug> testObject1(new TestDebug());
11773f4cbf05Sopenharmony_ci    sptr<TestDebug> testObject2(testObject1);
11783f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11793f4cbf05Sopenharmony_ci    sptr<TestDebug> testObject3;
11803f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject2->GetSptrRefCount(), 2);
11813f4cbf05Sopenharmony_ci    testObject3 = testObject2;
11823f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11833f4cbf05Sopenharmony_ci    wptr<TestDebug> testObject4(testObject3);
11843f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject4->GetWptrRefCount(), 4);
11853f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11863f4cbf05Sopenharmony_ci}
11873f4cbf05Sopenharmony_ci
11883f4cbf05Sopenharmony_ci/*
11893f4cbf05Sopenharmony_ci * @tc.name: testRefbaseDebug004
11903f4cbf05Sopenharmony_ci * @tc.desc: Test for mult-thread.
11913f4cbf05Sopenharmony_ci */
11923f4cbf05Sopenharmony_ciHWTEST_F(UtilsRefbaseTest, testRefbaseDebug004, TestSize.Level1)
11933f4cbf05Sopenharmony_ci{
11943f4cbf05Sopenharmony_ci    sptr<TestDebug> testObject1(new TestDebug());
11953f4cbf05Sopenharmony_ci    std::thread subThread {[&testObject1]() {
11963f4cbf05Sopenharmony_ci        sptr<TestDebug> subTestObject1(testObject1);
11973f4cbf05Sopenharmony_ci        EXPECT_EQ(testObject1->GetSptrRefCount(), 2);
11983f4cbf05Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
11993f4cbf05Sopenharmony_ci        wptr<TestDebug> subTestObject2(subTestObject1);
12003f4cbf05Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
12013f4cbf05Sopenharmony_ci        wptr<TestDebug> subTestObject3(subTestObject2);
12023f4cbf05Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
12033f4cbf05Sopenharmony_ci    }};
12043f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
12053f4cbf05Sopenharmony_ci    wptr<TestDebug> testObject2(testObject1);
12063f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
12073f4cbf05Sopenharmony_ci    wptr<TestDebug> testObject3(testObject2);
12083f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
12093f4cbf05Sopenharmony_ci    subThread.join();
12103f4cbf05Sopenharmony_ci    EXPECT_EQ(testObject3->GetWptrRefCount(), 2);
12113f4cbf05Sopenharmony_ci}
12123f4cbf05Sopenharmony_ci}  // namespace
12133f4cbf05Sopenharmony_ci}  // namespace OHOS
1214