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 <gtest/gtest.h>
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <meta/api/call_context.h>
198bf80f4bSopenharmony_ci#include <meta/api/function.h>
208bf80f4bSopenharmony_ci
218bf80f4bSopenharmony_ci#include "src/test_runner.h"
228bf80f4bSopenharmony_ci#include "src/testing_objects.h"
238bf80f4bSopenharmony_ci#include "src/util.h"
248bf80f4bSopenharmony_ci
258bf80f4bSopenharmony_ciusing namespace testing::ext;
268bf80f4bSopenharmony_ci
278bf80f4bSopenharmony_cinamespace {
288bf80f4bSopenharmony_ci    const int TEN = 10; // test value for test functions
298bf80f4bSopenharmony_ci}
308bf80f4bSopenharmony_ci
318bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
328bf80f4bSopenharmony_ci
338bf80f4bSopenharmony_ciusing namespace testing;
348bf80f4bSopenharmony_ci
358bf80f4bSopenharmony_ciclass IntfCallContextTest : public testing::Test {
368bf80f4bSopenharmony_cipublic:
378bf80f4bSopenharmony_ci    static void SetUpTestSuite()
388bf80f4bSopenharmony_ci    {
398bf80f4bSopenharmony_ci        SetTest();
408bf80f4bSopenharmony_ci    }
418bf80f4bSopenharmony_ci    static void TearDownTestSuite()
428bf80f4bSopenharmony_ci    {
438bf80f4bSopenharmony_ci        ResetTest();
448bf80f4bSopenharmony_ci    }
458bf80f4bSopenharmony_ci    void SetUp() override {}
468bf80f4bSopenharmony_ci    void TearDown() override {}
478bf80f4bSopenharmony_ci};
488bf80f4bSopenharmony_ci
498bf80f4bSopenharmony_ci/**
508bf80f4bSopenharmony_ci * @tc.name: IntfCallContextTest
518bf80f4bSopenharmony_ci * @tc.desc: test IntroduceInterfaces function
528bf80f4bSopenharmony_ci * @tc.type: FUNC
538bf80f4bSopenharmony_ci * @tc.require: I7DMS1
548bf80f4bSopenharmony_ci */
558bf80f4bSopenharmony_ciHWTEST_F(IntfCallContextTest, Empty, TestSize.Level1)
568bf80f4bSopenharmony_ci{
578bf80f4bSopenharmony_ci    auto context = GetObjectRegistry().ConstructDefaultCallContext();
588bf80f4bSopenharmony_ci    EXPECT_FALSE(Get<int>(context, "some"));
598bf80f4bSopenharmony_ci    EXPECT_TRUE(context->GetParameters().empty());
608bf80f4bSopenharmony_ci    EXPECT_FALSE(context->Succeeded());
618bf80f4bSopenharmony_ci    EXPECT_FALSE(GetResult<int>(context));
628bf80f4bSopenharmony_ci
638bf80f4bSopenharmony_ci    EXPECT_TRUE(DefineResult<void>(context));
648bf80f4bSopenharmony_ci
658bf80f4bSopenharmony_ci    EXPECT_FALSE(context->Succeeded());
668bf80f4bSopenharmony_ci    EXPECT_TRUE(SetResult(context));
678bf80f4bSopenharmony_ci    EXPECT_TRUE(context->Succeeded());
688bf80f4bSopenharmony_ci}
698bf80f4bSopenharmony_ci
708bf80f4bSopenharmony_ci/**
718bf80f4bSopenharmony_ci * @tc.name: IntfCallContextTest
728bf80f4bSopenharmony_ci * @tc.desc: test ReturnType function
738bf80f4bSopenharmony_ci * @tc.type: FUNC
748bf80f4bSopenharmony_ci * @tc.require: I7DMS1
758bf80f4bSopenharmony_ci */
768bf80f4bSopenharmony_ciHWTEST_F(IntfCallContextTest, ReturnType, TestSize.Level1)
778bf80f4bSopenharmony_ci{
788bf80f4bSopenharmony_ci    auto context = GetObjectRegistry().ConstructDefaultCallContext();
798bf80f4bSopenharmony_ci    EXPECT_TRUE(DefineResult<int>(context));
808bf80f4bSopenharmony_ci
818bf80f4bSopenharmony_ci    EXPECT_FALSE(context->Succeeded());
828bf80f4bSopenharmony_ci    EXPECT_TRUE(SetResult<int>(context, 2));
838bf80f4bSopenharmony_ci    EXPECT_TRUE(context->Succeeded());
848bf80f4bSopenharmony_ci    auto res = GetResult<int>(context);
858bf80f4bSopenharmony_ci    ASSERT_TRUE(res);
868bf80f4bSopenharmony_ci    EXPECT_EQ(*res, 2);
878bf80f4bSopenharmony_ci}
888bf80f4bSopenharmony_ci
898bf80f4bSopenharmony_ci/**
908bf80f4bSopenharmony_ci * @tc.name: IntfCallContextTest
918bf80f4bSopenharmony_ci * @tc.desc: test Parameters function
928bf80f4bSopenharmony_ci * @tc.type: FUNC
938bf80f4bSopenharmony_ci * @tc.require: I7DMS1
948bf80f4bSopenharmony_ci */
958bf80f4bSopenharmony_ciHWTEST_F(IntfCallContextTest, Parameters, TestSize.Level1)
968bf80f4bSopenharmony_ci{
978bf80f4bSopenharmony_ci    auto context = GetObjectRegistry().ConstructDefaultCallContext();
988bf80f4bSopenharmony_ci    DefineParameter<int>(context, "test");
998bf80f4bSopenharmony_ci    DefineParameter<BASE_NS::string>(context, "some", "hips");
1008bf80f4bSopenharmony_ci    EXPECT_FALSE(context->Succeeded());
1018bf80f4bSopenharmony_ci    auto p1 = Get<int>(context, "test");
1028bf80f4bSopenharmony_ci    ASSERT_TRUE(p1);
1038bf80f4bSopenharmony_ci    EXPECT_EQ(*p1, 0);
1048bf80f4bSopenharmony_ci    auto p2 = Get<BASE_NS::string>(context, "some");
1058bf80f4bSopenharmony_ci    ASSERT_TRUE(p2);
1068bf80f4bSopenharmony_ci    EXPECT_EQ(*p2, "hips");
1078bf80f4bSopenharmony_ci    EXPECT_FALSE(Get<float>(context, "test"));
1088bf80f4bSopenharmony_ci    EXPECT_FALSE(Set<float>(context, "test", 3.0));
1098bf80f4bSopenharmony_ci    EXPECT_TRUE(Set<int>(context, "test", 2));
1108bf80f4bSopenharmony_ci    auto ap1 = Get<int>(context, "test");
1118bf80f4bSopenharmony_ci    ASSERT_TRUE(ap1);
1128bf80f4bSopenharmony_ci    EXPECT_EQ(*ap1, 2);
1138bf80f4bSopenharmony_ci    SetResult(context);
1148bf80f4bSopenharmony_ci    EXPECT_TRUE(context->Succeeded());
1158bf80f4bSopenharmony_ci}
1168bf80f4bSopenharmony_ci
1178bf80f4bSopenharmony_cinamespace {
1188bf80f4bSopenharmony_cistruct TestClass : IObjectInstance {
1198bf80f4bSopenharmony_ci    int TestFunc(int a, int b, BASE_NS::string c)
1208bf80f4bSopenharmony_ci    {
1218bf80f4bSopenharmony_ci        return a + b + c.size();
1228bf80f4bSopenharmony_ci    }
1238bf80f4bSopenharmony_ci    void TestFuncMeta(const ICallContext::Ptr& c)
1248bf80f4bSopenharmony_ci    {
1258bf80f4bSopenharmony_ci        CallFunction(c, this, &TestClass::TestFunc);
1268bf80f4bSopenharmony_ci    }
1278bf80f4bSopenharmony_ci    int TestFunc2(int a, int b)
1288bf80f4bSopenharmony_ci    {
1298bf80f4bSopenharmony_ci        return a + TEN * b;
1308bf80f4bSopenharmony_ci    }
1318bf80f4bSopenharmony_ci    void TestFunc3(int a, int b, float c) {}
1328bf80f4bSopenharmony_ci    void ConstFunc() const {}
1338bf80f4bSopenharmony_ci    void ConstFuncMeta(const ICallContext::Ptr& c) const
1348bf80f4bSopenharmony_ci    {
1358bf80f4bSopenharmony_ci        CallFunction(c, this, &TestClass::ConstFunc);
1368bf80f4bSopenharmony_ci    }
1378bf80f4bSopenharmony_ci    void RefFunc(const int& in, int& out)
1388bf80f4bSopenharmony_ci    {
1398bf80f4bSopenharmony_ci        out = in + 1;
1408bf80f4bSopenharmony_ci    }
1418bf80f4bSopenharmony_ci    void RefFuncMeta(const ICallContext::Ptr& c)
1428bf80f4bSopenharmony_ci    {
1438bf80f4bSopenharmony_ci        CallFunction(c, this, &TestClass::RefFunc);
1448bf80f4bSopenharmony_ci    }
1458bf80f4bSopenharmony_ci
1468bf80f4bSopenharmony_ci    IObject::Ptr GetSelf() const override
1478bf80f4bSopenharmony_ci    {
1488bf80f4bSopenharmony_ci        return self_;
1498bf80f4bSopenharmony_ci    }
1508bf80f4bSopenharmony_ci
1518bf80f4bSopenharmony_ci    ObjectId GetClassId() const override
1528bf80f4bSopenharmony_ci    {
1538bf80f4bSopenharmony_ci        return {};
1548bf80f4bSopenharmony_ci    }
1558bf80f4bSopenharmony_ci    BASE_NS::string_view GetClassName() const override
1568bf80f4bSopenharmony_ci    {
1578bf80f4bSopenharmony_ci        return {};
1588bf80f4bSopenharmony_ci    }
1598bf80f4bSopenharmony_ci    InstanceId GetInstanceId() const override
1608bf80f4bSopenharmony_ci    {
1618bf80f4bSopenharmony_ci        return {};
1628bf80f4bSopenharmony_ci    }
1638bf80f4bSopenharmony_ci    BASE_NS::string GetName() const override
1648bf80f4bSopenharmony_ci    {
1658bf80f4bSopenharmony_ci        return {};
1668bf80f4bSopenharmony_ci    }
1678bf80f4bSopenharmony_ci    IObject::Ptr Resolve(const RefUri& uri) const override
1688bf80f4bSopenharmony_ci    {
1698bf80f4bSopenharmony_ci        return {};
1708bf80f4bSopenharmony_ci    }
1718bf80f4bSopenharmony_ci    BASE_NS::vector<BASE_NS::Uid> GetInterfaces() const override
1728bf80f4bSopenharmony_ci    {
1738bf80f4bSopenharmony_ci        return {};
1748bf80f4bSopenharmony_ci    }
1758bf80f4bSopenharmony_ci    const IInterface* GetInterface(const BASE_NS::Uid& uid) const override
1768bf80f4bSopenharmony_ci    {
1778bf80f4bSopenharmony_ci        return this;
1788bf80f4bSopenharmony_ci    }
1798bf80f4bSopenharmony_ci    IInterface* GetInterface(const BASE_NS::Uid& uid) override
1808bf80f4bSopenharmony_ci    {
1818bf80f4bSopenharmony_ci        return this;
1828bf80f4bSopenharmony_ci    }
1838bf80f4bSopenharmony_ci
1848bf80f4bSopenharmony_ci    void Ref() override {}
1858bf80f4bSopenharmony_ci    void Unref() override {}
1868bf80f4bSopenharmony_ci
1878bf80f4bSopenharmony_ci    IObject::Ptr self_ { this, [](void*) {} };
1888bf80f4bSopenharmony_ci};
1898bf80f4bSopenharmony_ci} // namespace
1908bf80f4bSopenharmony_ci
1918bf80f4bSopenharmony_ci/**
1928bf80f4bSopenharmony_ci * @tc.name: IntfCallContextTest
1938bf80f4bSopenharmony_ci * @tc.desc: test CreateCallContext function
1948bf80f4bSopenharmony_ci * @tc.type: FUNC
1958bf80f4bSopenharmony_ci * @tc.require: I7DMS1
1968bf80f4bSopenharmony_ci */
1978bf80f4bSopenharmony_ciHWTEST_F(IntfCallContextTest, CreateCallContext, TestSize.Level1)
1988bf80f4bSopenharmony_ci{
1998bf80f4bSopenharmony_ci    BASE_NS::string_view arr[] = { "1", "2", "3" };
2008bf80f4bSopenharmony_ci    EXPECT_FALSE(CreateCallContext(&TestClass::TestFunc2, arr));
2018bf80f4bSopenharmony_ci    auto c = CreateCallContext(&TestClass::TestFunc, arr);
2028bf80f4bSopenharmony_ci    ASSERT_TRUE(c);
2038bf80f4bSopenharmony_ci    auto params = c->GetParameters();
2048bf80f4bSopenharmony_ci    ASSERT_EQ(params.size(), 3);
2058bf80f4bSopenharmony_ci    for (int i = 0; i != 3; ++i) {
2068bf80f4bSopenharmony_ci        EXPECT_EQ(params[i].name, arr[i]);
2078bf80f4bSopenharmony_ci    }
2088bf80f4bSopenharmony_ci    EXPECT_TRUE(IsCompatibleWith<int>(*params[0].value));
2098bf80f4bSopenharmony_ci    EXPECT_TRUE(IsCompatibleWith<int>(*params[1].value));
2108bf80f4bSopenharmony_ci    EXPECT_TRUE(IsCompatibleWith<BASE_NS::string>(*params[2].value));
2118bf80f4bSopenharmony_ci
2128bf80f4bSopenharmony_ci    TestClass t;
2138bf80f4bSopenharmony_ci    auto f = CreateFunction("test", &t, &TestClass::TestFuncMeta, [] {
2148bf80f4bSopenharmony_ci        BASE_NS::string_view arr[] = { "1", "2", "3" };
2158bf80f4bSopenharmony_ci        return CreateCallContext(&TestClass::TestFunc, arr);
2168bf80f4bSopenharmony_ci    });
2178bf80f4bSopenharmony_ci    ASSERT_TRUE(f);
2188bf80f4bSopenharmony_ci    auto res = CallMetaFunction<int, int, int, BASE_NS::string>(f, 1, 2, "12");
2198bf80f4bSopenharmony_ci    ASSERT_TRUE(res);
2208bf80f4bSopenharmony_ci    EXPECT_EQ(res.value, 5);
2218bf80f4bSopenharmony_ci}
2228bf80f4bSopenharmony_ci
2238bf80f4bSopenharmony_ci/**
2248bf80f4bSopenharmony_ci * @tc.name: IntfCallContextTest
2258bf80f4bSopenharmony_ci * @tc.desc: test BadCallFunction function
2268bf80f4bSopenharmony_ci * @tc.type: FUNC
2278bf80f4bSopenharmony_ci * @tc.require: I7DMS1
2288bf80f4bSopenharmony_ci */
2298bf80f4bSopenharmony_ciHWTEST_F(IntfCallContextTest, BadCallFunction, TestSize.Level1)
2308bf80f4bSopenharmony_ci{
2318bf80f4bSopenharmony_ci    BASE_NS::string_view arr[] = { "1", "2", "3" };
2328bf80f4bSopenharmony_ci    auto c = CreateCallContext(&TestClass::TestFunc, arr);
2338bf80f4bSopenharmony_ci    TestClass t;
2348bf80f4bSopenharmony_ci    EXPECT_FALSE(CallFunction(c, &t, &TestClass::TestFunc2));
2358bf80f4bSopenharmony_ci    EXPECT_FALSE(c->Succeeded());
2368bf80f4bSopenharmony_ci    EXPECT_FALSE(CallFunction(c, &t, &TestClass::TestFunc3));
2378bf80f4bSopenharmony_ci    EXPECT_FALSE(c->Succeeded());
2388bf80f4bSopenharmony_ci}
2398bf80f4bSopenharmony_ci
2408bf80f4bSopenharmony_ci/**
2418bf80f4bSopenharmony_ci * @tc.name: IntfCallContextTest
2428bf80f4bSopenharmony_ci * @tc.desc: test ConstFunctions function
2438bf80f4bSopenharmony_ci * @tc.type: FUNC
2448bf80f4bSopenharmony_ci * @tc.require: I7DMS1
2458bf80f4bSopenharmony_ci */
2468bf80f4bSopenharmony_ciHWTEST_F(IntfCallContextTest, ConstFunctions, TestSize.Level1)
2478bf80f4bSopenharmony_ci{
2488bf80f4bSopenharmony_ci    auto context = [] { return CreateCallContext(&TestClass::ConstFunc, {}); };
2498bf80f4bSopenharmony_ci    auto c = context();
2508bf80f4bSopenharmony_ci    ASSERT_TRUE(c);
2518bf80f4bSopenharmony_ci    auto params = c->GetParameters();
2528bf80f4bSopenharmony_ci    ASSERT_EQ(params.size(), 0);
2538bf80f4bSopenharmony_ci    {
2548bf80f4bSopenharmony_ci        TestClass t;
2558bf80f4bSopenharmony_ci        auto f = CreateFunction("test", &t, &TestClass::ConstFuncMeta, context);
2568bf80f4bSopenharmony_ci        ASSERT_TRUE(f);
2578bf80f4bSopenharmony_ci        auto res = CallMetaFunction<void>(f);
2588bf80f4bSopenharmony_ci        ASSERT_TRUE(res);
2598bf80f4bSopenharmony_ci    }
2608bf80f4bSopenharmony_ci}
2618bf80f4bSopenharmony_ci
2628bf80f4bSopenharmony_ci/**
2638bf80f4bSopenharmony_ci * @tc.name: IntfCallContextTest
2648bf80f4bSopenharmony_ci * @tc.desc: test ReferenceArgs function
2658bf80f4bSopenharmony_ci * @tc.type: FUNC
2668bf80f4bSopenharmony_ci * @tc.require: I7DMS1
2678bf80f4bSopenharmony_ci */
2688bf80f4bSopenharmony_ciHWTEST_F(IntfCallContextTest, ReferenceArgs, TestSize.Level1)
2698bf80f4bSopenharmony_ci{
2708bf80f4bSopenharmony_ci    auto context = [] {
2718bf80f4bSopenharmony_ci        BASE_NS::string_view arr[] = { "in", "out" };
2728bf80f4bSopenharmony_ci        return CreateCallContext(&TestClass::RefFunc, arr);
2738bf80f4bSopenharmony_ci    };
2748bf80f4bSopenharmony_ci    auto c = context();
2758bf80f4bSopenharmony_ci    TestClass t;
2768bf80f4bSopenharmony_ci    auto f = CreateFunction("test", &t, &TestClass::RefFuncMeta, context);
2778bf80f4bSopenharmony_ci    ASSERT_TRUE(f);
2788bf80f4bSopenharmony_ci    auto res = CallMetaFunction<void>(f, 1, 0);
2798bf80f4bSopenharmony_ci    ASSERT_TRUE(res);
2808bf80f4bSopenharmony_ci    ASSERT_TRUE(res.context);
2818bf80f4bSopenharmony_ci
2828bf80f4bSopenharmony_ci    auto p1 = Get<int>(res.context, "in");
2838bf80f4bSopenharmony_ci    ASSERT_TRUE(p1);
2848bf80f4bSopenharmony_ci    EXPECT_EQ(*p1, 1);
2858bf80f4bSopenharmony_ci    auto p2 = Get<int>(res.context, "out");
2868bf80f4bSopenharmony_ci    ASSERT_TRUE(p2);
2878bf80f4bSopenharmony_ci    EXPECT_EQ(*p2, 2);
2888bf80f4bSopenharmony_ci}
2898bf80f4bSopenharmony_ci
2908bf80f4bSopenharmony_ci/**
2918bf80f4bSopenharmony_ci * @tc.name: IntfCallContextTest
2928bf80f4bSopenharmony_ci * @tc.desc: test CallArgumentOrder function
2938bf80f4bSopenharmony_ci * @tc.type: FUNC
2948bf80f4bSopenharmony_ci * @tc.require: I7DMS1
2958bf80f4bSopenharmony_ci */
2968bf80f4bSopenharmony_ciHWTEST_F(IntfCallContextTest, CallArgumentOrder, TestSize.Level1)
2978bf80f4bSopenharmony_ci{
2988bf80f4bSopenharmony_ci    BASE_NS::string_view arr[] = { "1", "2" };
2998bf80f4bSopenharmony_ci
3008bf80f4bSopenharmony_ci    {
3018bf80f4bSopenharmony_ci        auto c = CreateCallContext(&TestClass::TestFunc2, arr);
3028bf80f4bSopenharmony_ci        Set<int>(c, "1", 1);
3038bf80f4bSopenharmony_ci        Set<int>(c, "2", 2);
3048bf80f4bSopenharmony_ci        TestClass t;
3058bf80f4bSopenharmony_ci        EXPECT_TRUE(CallFunction(c, &t, &TestClass::TestFunc2));
3068bf80f4bSopenharmony_ci        auto res = GetResult<int>(c);
3078bf80f4bSopenharmony_ci        ASSERT_TRUE(res);
3088bf80f4bSopenharmony_ci        EXPECT_EQ(*res, 21);
3098bf80f4bSopenharmony_ci    }
3108bf80f4bSopenharmony_ci    {
3118bf80f4bSopenharmony_ci        auto c = CreateCallContext(&TestClass::TestFunc2, arr);
3128bf80f4bSopenharmony_ci        Set<int>(c, "1", 1);
3138bf80f4bSopenharmony_ci        Set<int>(c, "2", 2);
3148bf80f4bSopenharmony_ci        TestClass t;
3158bf80f4bSopenharmony_ci        BASE_NS::string_view order[] = { "2", "1" };
3168bf80f4bSopenharmony_ci        EXPECT_TRUE(CallFunction(c, &t, &TestClass::TestFunc2, order));
3178bf80f4bSopenharmony_ci        auto res = GetResult<int>(c);
3188bf80f4bSopenharmony_ci        ASSERT_TRUE(res);
3198bf80f4bSopenharmony_ci        EXPECT_EQ(*res, 12);
3208bf80f4bSopenharmony_ci    }
3218bf80f4bSopenharmony_ci}
3228bf80f4bSopenharmony_ci
3238bf80f4bSopenharmony_ciMETA_END_NAMESPACE()