18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#include <bitset>
178bf80f4bSopenharmony_ci#include <limits>
188bf80f4bSopenharmony_ci#include <type_traits>
198bf80f4bSopenharmony_ci
208bf80f4bSopenharmony_ci#include <gmock/gmock.h>
218bf80f4bSopenharmony_ci#include <gtest/gtest.h>
228bf80f4bSopenharmony_ci
238bf80f4bSopenharmony_ci#include <base/util/uid_util.h>
248bf80f4bSopenharmony_ci
258bf80f4bSopenharmony_ci#include <meta/api/make_callback.h>
268bf80f4bSopenharmony_ci#include <meta/ext/object.h>
278bf80f4bSopenharmony_ci#include <meta/interface/animation/builtin_animations.h>
288bf80f4bSopenharmony_ci#include <meta/interface/animation/intf_animation.h>
298bf80f4bSopenharmony_ci#include <meta/interface/intf_class_registry.h>
308bf80f4bSopenharmony_ci#include <meta/interface/intf_object_registry.h>
318bf80f4bSopenharmony_ci
328bf80f4bSopenharmony_ci#include "src/test_runner.h"
338bf80f4bSopenharmony_ci
348bf80f4bSopenharmony_ciusing namespace testing;
358bf80f4bSopenharmony_ciusing namespace testing::ext;
368bf80f4bSopenharmony_ci
378bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
388bf80f4bSopenharmony_ci
398bf80f4bSopenharmony_ciclass IntfClassRegistryTest : public testing::Test {
408bf80f4bSopenharmony_ciprotected:
418bf80f4bSopenharmony_ci    IntfClassRegistryTest() : classRegistry_(META_NS::GetClassRegistry()) {}
428bf80f4bSopenharmony_ci
438bf80f4bSopenharmony_ci    void SetUp() override {}
448bf80f4bSopenharmony_ci    void TearDown() override {}
458bf80f4bSopenharmony_ci    static void SetUpTestSuite()
468bf80f4bSopenharmony_ci    {
478bf80f4bSopenharmony_ci        SetTest();
488bf80f4bSopenharmony_ci    }
498bf80f4bSopenharmony_ci    static void TearDownTestSuite()
508bf80f4bSopenharmony_ci    {
518bf80f4bSopenharmony_ci        ResetTest();
528bf80f4bSopenharmony_ci    }
538bf80f4bSopenharmony_ci
548bf80f4bSopenharmony_ciprotected:
558bf80f4bSopenharmony_ci    IClassRegistry& classRegistry_;
568bf80f4bSopenharmony_ci};
578bf80f4bSopenharmony_ci
588bf80f4bSopenharmony_ciMATCHER_P(ContainsClass, uid, "")
598bf80f4bSopenharmony_ci{
608bf80f4bSopenharmony_ci    for (const auto& info : arg) {
618bf80f4bSopenharmony_ci        if (info && info->GetClassInfo().Id() == uid) {
628bf80f4bSopenharmony_ci            return true;
638bf80f4bSopenharmony_ci        }
648bf80f4bSopenharmony_ci    }
658bf80f4bSopenharmony_ci    return false;
668bf80f4bSopenharmony_ci}
678bf80f4bSopenharmony_ci
688bf80f4bSopenharmony_ci/**
698bf80f4bSopenharmony_ci * @tc.name: IntfClassRegistryTest
708bf80f4bSopenharmony_ci * @tc.desc: test GetAllClasses function
718bf80f4bSopenharmony_ci * @tc.type: FUNC
728bf80f4bSopenharmony_ci * @tc.require: I7DMS1
738bf80f4bSopenharmony_ci */
748bf80f4bSopenharmony_ciHWTEST_F(IntfClassRegistryTest, GetAllClasses, TestSize.Level1)
758bf80f4bSopenharmony_ci{
768bf80f4bSopenharmony_ci    auto all = classRegistry_.GetAllTypes({});
778bf80f4bSopenharmony_ci    EXPECT_THAT(all, Not(IsEmpty()));
788bf80f4bSopenharmony_ci}
798bf80f4bSopenharmony_ci
808bf80f4bSopenharmony_ci/**
818bf80f4bSopenharmony_ci * @tc.name: IntfClassRegistryTest
828bf80f4bSopenharmony_ci * @tc.desc: test GetAllTypesUis function
838bf80f4bSopenharmony_ci * @tc.type: FUNC
848bf80f4bSopenharmony_ci * @tc.require: I7DMS1
858bf80f4bSopenharmony_ci */
868bf80f4bSopenharmony_ciHWTEST_F(IntfClassRegistryTest, GetAllTypesUis, TestSize.Level1)
878bf80f4bSopenharmony_ci{
888bf80f4bSopenharmony_ci    auto animations = classRegistry_.GetAllTypes({ IAnimation::UID });
898bf80f4bSopenharmony_ci    EXPECT_THAT(animations, Not(IsEmpty()));
908bf80f4bSopenharmony_ci
918bf80f4bSopenharmony_ci    EXPECT_THAT(animations, ContainsClass(ClassId::KeyframeAnimation));
928bf80f4bSopenharmony_ci
938bf80f4bSopenharmony_ci    auto staggered = classRegistry_.GetAllTypes({ IStaggeredAnimation::UID });
948bf80f4bSopenharmony_ci
958bf80f4bSopenharmony_ci    EXPECT_THAT(staggered, Not(ContainsClass(ClassId::KeyframeAnimation)));
968bf80f4bSopenharmony_ci}
978bf80f4bSopenharmony_ci
988bf80f4bSopenharmony_ciMETA_REGISTER_CLASS(MyObject1, "440d971e-afae-497a-95c3-97c3ba7f5d18", META_NS::ObjectCategoryBits::APPLICATION)
998bf80f4bSopenharmony_ciMETA_REGISTER_CLASS(MyObject2, "f8d418d7-99aa-4b19-92b7-319959cc98f1", META_NS::ObjectCategoryBits::APPLICATION)
1008bf80f4bSopenharmony_ci
1018bf80f4bSopenharmony_ciclass MyObject1 : public META_NS::ObjectFwd<MyObject1, ClassId::MyObject1, META_NS::ClassId::Object> {
1028bf80f4bSopenharmony_cipublic:
1038bf80f4bSopenharmony_ci};
1048bf80f4bSopenharmony_ciclass MyObject2 : public META_NS::ObjectFwd<MyObject2, ClassId::MyObject2, META_NS::ClassId::Object> {
1058bf80f4bSopenharmony_cipublic:
1068bf80f4bSopenharmony_ci};
1078bf80f4bSopenharmony_ci
1088bf80f4bSopenharmony_ci/**
1098bf80f4bSopenharmony_ci * @tc.name: IntfClassRegistryTest
1108bf80f4bSopenharmony_ci * @tc.desc: test Events function
1118bf80f4bSopenharmony_ci * @tc.type: FUNC
1128bf80f4bSopenharmony_ci * @tc.require: I7DMS1
1138bf80f4bSopenharmony_ci */
1148bf80f4bSopenharmony_ciHWTEST_F(IntfClassRegistryTest, Events, TestSize.Level1)
1158bf80f4bSopenharmony_ci{
1168bf80f4bSopenharmony_ci    auto& objReg = META_NS::GetObjectRegistry();
1178bf80f4bSopenharmony_ci    auto& classReg = objReg.GetClassRegistry();
1188bf80f4bSopenharmony_ci
1198bf80f4bSopenharmony_ci    int registered = 0;
1208bf80f4bSopenharmony_ci    int unregistered = 0;
1218bf80f4bSopenharmony_ci    ClassRegistrationInfo lastRegistered;
1228bf80f4bSopenharmony_ci    ClassRegistrationInfo lastUnregistered;
1238bf80f4bSopenharmony_ci
1248bf80f4bSopenharmony_ci    auto token1 = classReg.OnClassRegistered()->AddHandler(META_NS::MakeCallback<META_NS::IOnClassRegistrationChanged>(
1258bf80f4bSopenharmony_ci        [&registered, &lastRegistered](const ClassRegistrationInfo& info) {
1268bf80f4bSopenharmony_ci            registered++;
1278bf80f4bSopenharmony_ci            lastRegistered = info;
1288bf80f4bSopenharmony_ci        }));
1298bf80f4bSopenharmony_ci
1308bf80f4bSopenharmony_ci    auto token2 =
1318bf80f4bSopenharmony_ci        classReg.OnClassUnregistered()->AddHandler(META_NS::MakeCallback<META_NS::IOnClassRegistrationChanged>(
1328bf80f4bSopenharmony_ci            [&unregistered, &lastUnregistered](const ClassRegistrationInfo& info) {
1338bf80f4bSopenharmony_ci                unregistered++;
1348bf80f4bSopenharmony_ci                lastUnregistered = info;
1358bf80f4bSopenharmony_ci            }));
1368bf80f4bSopenharmony_ci
1378bf80f4bSopenharmony_ci    EXPECT_TRUE(objReg.RegisterObjectType(MyObject1::GetFactory()));
1388bf80f4bSopenharmony_ci    EXPECT_EQ(registered, 1);
1398bf80f4bSopenharmony_ci    EXPECT_EQ(unregistered, 0);
1408bf80f4bSopenharmony_ci    EXPECT_EQ(lastRegistered.classInfo, MyObject1::GetFactory());
1418bf80f4bSopenharmony_ci    EXPECT_EQ(lastUnregistered.classInfo, nullptr);
1428bf80f4bSopenharmony_ci    lastRegistered.classInfo.reset();
1438bf80f4bSopenharmony_ci
1448bf80f4bSopenharmony_ci    EXPECT_FALSE(objReg.RegisterObjectType(MyObject1::GetFactory()));
1458bf80f4bSopenharmony_ci    EXPECT_EQ(registered, 1);
1468bf80f4bSopenharmony_ci    EXPECT_EQ(unregistered, 0);
1478bf80f4bSopenharmony_ci    EXPECT_EQ(lastRegistered.classInfo, nullptr);
1488bf80f4bSopenharmony_ci    EXPECT_EQ(lastUnregistered.classInfo, nullptr);
1498bf80f4bSopenharmony_ci
1508bf80f4bSopenharmony_ci    EXPECT_TRUE(objReg.RegisterObjectType(MyObject2::GetFactory()));
1518bf80f4bSopenharmony_ci    EXPECT_EQ(registered, 2);
1528bf80f4bSopenharmony_ci    EXPECT_EQ(unregistered, 0);
1538bf80f4bSopenharmony_ci    EXPECT_EQ(lastRegistered.classInfo, MyObject2::GetFactory());
1548bf80f4bSopenharmony_ci    EXPECT_EQ(lastUnregistered.classInfo, nullptr);
1558bf80f4bSopenharmony_ci    lastRegistered.classInfo.reset();
1568bf80f4bSopenharmony_ci
1578bf80f4bSopenharmony_ci    EXPECT_TRUE(objReg.UnregisterObjectType(MyObject1::GetFactory()));
1588bf80f4bSopenharmony_ci    EXPECT_EQ(registered, 2);
1598bf80f4bSopenharmony_ci    EXPECT_EQ(unregistered, 1);
1608bf80f4bSopenharmony_ci    EXPECT_EQ(lastRegistered.classInfo, nullptr);
1618bf80f4bSopenharmony_ci    EXPECT_EQ(lastUnregistered.classInfo, MyObject1::GetFactory());
1628bf80f4bSopenharmony_ci    lastUnregistered.classInfo.reset();
1638bf80f4bSopenharmony_ci
1648bf80f4bSopenharmony_ci    EXPECT_FALSE(objReg.UnregisterObjectType(MyObject1::GetFactory()));
1658bf80f4bSopenharmony_ci    EXPECT_EQ(registered, 2);
1668bf80f4bSopenharmony_ci    EXPECT_EQ(unregistered, 1);
1678bf80f4bSopenharmony_ci    EXPECT_EQ(lastRegistered.classInfo, nullptr);
1688bf80f4bSopenharmony_ci    EXPECT_EQ(lastUnregistered.classInfo, nullptr);
1698bf80f4bSopenharmony_ci
1708bf80f4bSopenharmony_ci    EXPECT_TRUE(objReg.UnregisterObjectType(MyObject2::GetFactory()));
1718bf80f4bSopenharmony_ci    EXPECT_EQ(registered, 2);
1728bf80f4bSopenharmony_ci    EXPECT_EQ(unregistered, 2);
1738bf80f4bSopenharmony_ci    EXPECT_EQ(lastRegistered.classInfo, nullptr);
1748bf80f4bSopenharmony_ci    EXPECT_EQ(lastUnregistered.classInfo, MyObject2::GetFactory());
1758bf80f4bSopenharmony_ci
1768bf80f4bSopenharmony_ci    EXPECT_TRUE(classReg.OnClassRegistered()->RemoveHandler(token1));
1778bf80f4bSopenharmony_ci    EXPECT_TRUE(classReg.OnClassUnregistered()->RemoveHandler(token2));
1788bf80f4bSopenharmony_ci}
1798bf80f4bSopenharmony_ci
1808bf80f4bSopenharmony_ciMETA_END_NAMESPACE()