diff --git a/test/unittest/interfaces/ets/ani/ani_test.h b/test/unittest/interfaces/ets/ani/ani_test.h index 86d7395e85fa99b1f3a44b257d940c7d14a0b223..bee422f49bf1601dfc0126454b6634126648e112 100644 --- a/test/unittest/interfaces/ets/ani/ani_test.h +++ b/test/unittest/interfaces/ets/ani/ani_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -44,29 +44,47 @@ typedef enum { typedef struct AniEnv { ani_status FindNamespace(const char *namespace_descriptor, ani_namespace *result) { + if (strlen(namespace_descriptor) == 0) { + return ANI_ERROR; + } return ANI_OK; } ani_status Namespace_BindNativeFunctions(ani_namespace ns, const ani_native_function *functions, ani_size nr_functions) { + if (nr_functions == 0) { + return ANI_ERROR; + } return ANI_OK; } ani_status EnumItem_GetValue_Int(ani_enum_item enum_item, ani_int *result) { + if (enum_item < 0) { + return ANI_ERROR; + } return ANI_OK; } ani_status String_GetUTF8Size(ani_string string, ani_size *result) { + if (string.length() == 0) { + return ANI_ERROR; + } return ANI_OK; } ani_status String_GetUTF8(ani_string string, char *utf8_buffer, ani_size utf8_buffer_size, ani_size *result) { + if (string.length() == 0) { + return ANI_ERROR; + } return ANI_OK; } } ani_env; typedef struct AniVm { ani_status GetEnv(uint32_t version, ani_env **result) { + if (version == 0) { + return ANI_ERROR; + } return ANI_OK; } } ani_vm; diff --git a/test/unittest/interfaces/ets/ani/performance_monitor_test.cpp b/test/unittest/interfaces/ets/ani/performance_monitor_test.cpp index f38aa410b0ca5ff6cc213fa760560caba201feda..cc5bfaf409f7354dd014181264dccfd7b04fd18e 100644 --- a/test/unittest/interfaces/ets/ani/performance_monitor_test.cpp +++ b/test/unittest/interfaces/ets/ani/performance_monitor_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,8 +17,8 @@ #define private public #define protected public #include "ani_test.h" -#include "performanceMonitor.cpp" #include "frameworks/base/perfmonitor/perf_monitor.h" +#include "performanceMonitor.cpp" using namespace testing; using namespace testing::ext; @@ -40,10 +40,11 @@ HWTEST_F(PerformanceMonitorTest, BeginTest001, TestSize.Level1) ani_env *env = new ani_env; ASSERT_NE(env, nullptr); ani_string scene("LAUNCHER_APP_LAUNCH_FROM_ICON"); - ani_enum_item startInputType(2); + ani_enum_item startInputType(-2); ani_string note("APP_START_BEGIN"); Begin(env, scene, startInputType, note); - ASSERT_NE(PerfMonitor::GetPerfMonitor(), nullptr); + PerfMonitor* perf = PerfMonitor::GetPerfMonitor(); + ASSERT_NE(perf, nullptr); } /** @@ -86,4 +87,189 @@ HWTEST_F(PerformanceMonitorTest, RecordInputEventTimeTest001, TestSize.Level1) End(env, scene); ASSERT_EQ(PerfMonitor::GetPerfMonitor()->mRecords.count(scene), 0); } + +/** + * @tc.name: RecordInputEventTimeTest002 + * @tc.desc: Test RecordInputEventTime + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, RecordInputEventTimeTest002, TestSize.Level1) +{ + ani_env *env = new ani_env; + ASSERT_NE(env, nullptr); + ani_string scene("LAUNCHER_APP_LAUNCH_FROM_ICON"); + ani_enum_item startInputType(2); + ani_string note("APP_START_BEGIN"); + ani_enum_item type(-1); + ani_enum_item sourceType(0); + ani_double time(60); + Begin(env, scene, startInputType, note); + ASSERT_NE(PerfMonitor::GetPerfMonitor(), nullptr); + RecordInputEventTime(env, type, sourceType, time); + ASSERT_EQ(PerfMonitor::GetPerfMonitor()->mInputTime[LAST_DOWN], 60); + End(env, scene); + ASSERT_EQ(PerfMonitor::GetPerfMonitor()->mRecords.count(scene), 0); +} + +/** + * @tc.name: RecordInputEventTimeTest003 + * @tc.desc: Test RecordInputEventTime + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, RecordInputEventTimeTest003, TestSize.Level1) +{ + ani_env *env = new ani_env; + ASSERT_NE(env, nullptr); + ani_string scene("LAUNCHER_APP_LAUNCH_FROM_ICON"); + ani_enum_item startInputType(2); + ani_string note("APP_START_BEGIN"); + ani_enum_item type(-1); + ani_enum_item sourceType(0); + ani_double time(60); + Begin(env, scene, startInputType, note); + ASSERT_NE(PerfMonitor::GetPerfMonitor(), nullptr); + RecordInputEventTime(env, type, sourceType, time); + ASSERT_EQ(PerfMonitor::GetPerfMonitor()->mInputTime[LAST_DOWN], 60); + End(env, scene); + ASSERT_EQ(PerfMonitor::GetPerfMonitor()->mRecords.count(scene), 0); +} + +/** + * @tc.name: RecordInputEventTimeTest004 + * @tc.desc: Test RecordInputEventTime + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, RecordInputEventTimeTest004, TestSize.Level1) +{ + ani_env *env = new ani_env; + ASSERT_NE(env, nullptr); + ani_string scene("LAUNCHER_APP_LAUNCH_FROM_ICON"); + ani_enum_item startInputType(2); + ani_string note("APP_START_BEGIN"); + ani_enum_item type(1); + ani_enum_item sourceType(-1); + ani_double time(60); + Begin(env, scene, startInputType, note); + ASSERT_NE(PerfMonitor::GetPerfMonitor(), nullptr); + RecordInputEventTime(env, type, sourceType, time); + ASSERT_EQ(PerfMonitor::GetPerfMonitor()->mInputTime[LAST_DOWN], 60); + End(env, scene); + ASSERT_EQ(PerfMonitor::GetPerfMonitor()->mRecords.count(scene), 0); +} + +/** + * @tc.name: RecordInputEventTimeTest005 + * @tc.desc: Test RecordInputEventTime + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, RecordInputEventTimeTest005, TestSize.Level1) +{ + ani_env *env = new ani_env; + ASSERT_NE(env, nullptr); + ani_string scene("LAUNCHER_APP_LAUNCH_FROM_ICON"); + ani_enum_item startInputType(2); + ani_string note("APP_START_BEGIN"); + ani_enum_item type(1); + ani_enum_item sourceType(1); + ani_double time(-1); + Begin(env, scene, startInputType, note); + ASSERT_NE(PerfMonitor::GetPerfMonitor(), nullptr); + RecordInputEventTime(env, type, sourceType, time); + ASSERT_NE(PerfMonitor::GetPerfMonitor()->mInputTime[LAST_DOWN], 0); + End(env, scene); + ASSERT_EQ(PerfMonitor::GetPerfMonitor()->mRecords.count(scene), 0); +} + +/** + * @tc.name: ANI_ConstructorTest001 + * @tc.desc: Test ANI_Constructor + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, ANI_ConstructorTest001, TestSize.Level1) +{ + ani_vm vm; + uint32_t result = 0; + ani_status sta = ANI_Constructor(&vm, &result); + ASSERT_EQ(result, ANI_VERSION_1); + ASSERT_EQ(sta, ANI_OK); +} + +/** + * @tc.name: ANI_ConstructorTest002 + * @tc.desc: Test ANI_Constructor + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, ANI_ConstructorTest002, TestSize.Level0) { + ani_vm vm; + uint32_t result; + EXPECT_EQ(vm.GetEnv(ANI_VERSION_1, nullptr), ANI_OK); + + ani_env env; + ani_status sta = env.FindNamespace("L@ohos/arkui/performanceMonitor/performanceMonitor;", nullptr); + EXPECT_EQ(sta, ANI_OK); + + ani_status status = ANI_Constructor(&vm, &result); + EXPECT_EQ(status, ANI_OK); + EXPECT_EQ(result, ANI_VERSION_1); +} + +/** + * @tc.name: ANI_ConstructorTest003 + * @tc.desc: Test ANI_Constructor + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, ANI_ConstructorTest003, TestSize.Level0) { + ani_vm vm; + uint32_t result; + ani_status sta = vm.GetEnv(ANI_VERSION_1, nullptr); + EXPECT_EQ(sta, ANI_OK); + + ani_status status = ANI_Constructor(&vm, &result); + EXPECT_EQ(status, ANI_OK); +} + +/** + * @tc.name: ANI_ConstructorTest004 + * @tc.desc: Test ANI_Constructor + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, ANI_ConstructorTest004, TestSize.Level0) { + ani_vm vm; + uint32_t result; + ani_status sta = vm.GetEnv(ANI_VERSION_1, nullptr); + EXPECT_EQ(sta, ANI_OK); + + ani_env env; + sta = env.FindNamespace("L@ohos/arkui/performanceMonitor/performanceMonitor;", nullptr); + EXPECT_EQ(sta, ANI_OK); + + ani_status status = ANI_Constructor(&vm, &result); + EXPECT_EQ(status, ANI_OK); +} + +/** + * @tc.name: ANI_ConstructorTest005 + * @tc.desc: Test ANI_Constructor + * @tc.type: FUNC + */ +HWTEST_F(PerformanceMonitorTest, ANI_ConstructorTest005, TestSize.Level0) { + ani_vm vm; + uint32_t result; + ani_status sta = vm.GetEnv(ANI_VERSION_1, nullptr); + EXPECT_EQ(sta, ANI_OK); + + ani_env env; + sta = env.FindNamespace("L@ohos/arkui/performanceMonitor/performanceMonitor;", nullptr); + EXPECT_EQ(sta, ANI_OK); + + ani_namespace ns = 1; + std::array methods = { + ani_native_function {"begin", nullptr, reinterpret_cast(Begin)}, + }; + sta = env.Namespace_BindNativeFunctions(ns, methods.data(), methods.size()); + EXPECT_EQ(sta, ANI_OK); + + ani_status status = ANI_Constructor(&vm, &result); + EXPECT_EQ(status, ANI_OK); +} } // namespace OHOS::Ace