From c9ec88cc4374eecedf4868ecc7f2054ee6164934 Mon Sep 17 00:00:00 2001 From: changleipeng Date: Mon, 21 Oct 2024 17:18:57 +0800 Subject: [PATCH] test Signed-off-by: changleipeng --- BUILD.gn | 6 ++ shell/platform/ohos/BUILD.gn | 15 +++ .../ohos_surface_gl_skia_unittests.cpp | 102 ++++++++++++++++++ .../ohos_surface_software_unittests.cpp | 65 +++++++++++ .../ohos_touch_processor_unittests.cpp | 62 +++++++++++ .../ohos_xcomponent_adapter_unittests.cpp | 35 ++++++ .../testing/vsync_waiter_ohos_unittests.cpp | 48 +++++++++ 7 files changed, 333 insertions(+) create mode 100644 shell/platform/ohos/testing/ohos_surface_gl_skia_unittests.cpp create mode 100644 shell/platform/ohos/testing/ohos_surface_software_unittests.cpp create mode 100644 shell/platform/ohos/testing/ohos_touch_processor_unittests.cpp create mode 100644 shell/platform/ohos/testing/ohos_xcomponent_adapter_unittests.cpp create mode 100644 shell/platform/ohos/testing/vsync_waiter_ohos_unittests.cpp diff --git a/BUILD.gn b/BUILD.gn index 7a102d594b..773b42acf8 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -162,6 +162,12 @@ group("unittests") { "//flutter/third_party/txt:txt_unittests", ] + if (is_ohos) { + public_deps += [ + "//flutter/shell/platform/ohos:flutter_ohos_unittests", + ] + } + # The accessibility library only supports Mac and Windows at the moment. if (is_mac || is_win) { public_deps += diff --git a/shell/platform/ohos/BUILD.gn b/shell/platform/ohos/BUILD.gn index c8e36f5796..ff52f307f0 100644 --- a/shell/platform/ohos/BUILD.gn +++ b/shell/platform/ohos/BUILD.gn @@ -176,10 +176,16 @@ executable("flutter_ohos_unittests") { sources = [ #"testing/mock_texture_registrar.cc", + "testing/ohos_surface_gl_skia_unittests.cpp", + "testing/ohos_surface_software_unittests.cpp", + "testing/ohos_touch_processor_unittests.cpp", + "testing/ohos_xcomponent_adapter_unittests.cpp", + "testing/vsync_waiter_ohos_unittests.cpp", ] public_configs = [ "//flutter:config" ] + cflags = ["-Wno-deprecated-declarations"] defines = [ "FLUTTER_ENGINE_NO_PROTOTYPES", @@ -197,6 +203,15 @@ executable("flutter_ohos_unittests") { "//flutter/shell/platform/embedder:embedder_test_utils", "//flutter/testing", ] + + ldflags = ["-lace_ndk.z"] + ldflags += ["-lnative_window"] + ldflags += ["-lnative_vsync"] + ldflags += ["-limage_ndk.z"] + ldflags += ["-lrawfile.z"] + ldflags += ["-lnative_image"] + ldflags += ["-lpixelmap_ndk.z"] + ldflags += ["-lqos"] } shared_library("flutter_shell_native") { diff --git a/shell/platform/ohos/testing/ohos_surface_gl_skia_unittests.cpp b/shell/platform/ohos/testing/ohos_surface_gl_skia_unittests.cpp new file mode 100644 index 0000000000..f1d98fb765 --- /dev/null +++ b/shell/platform/ohos/testing/ohos_surface_gl_skia_unittests.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include "flutter/common/task_runners.h" +#include "flutter/fml/message_loop.h" +#include "flutter/shell/common/thread_host.h" +#include "flutter/shell/platform/ohos/ohos_context_gl_skia.h" +#include "flutter/shell/platform/ohos/ohos_surface_gl_skia.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { +namespace { +TaskRunners CreateTaskRunners(const std::string& threadLabel) +{ + fml::MessageLoop::EnsureInitializedForCurrentThread(); + auto& loop = fml::MessageLoop::GetCurrent(); + return { + threadLabel, + loop.GetTaskRunner(), // platform + loop.GetTaskRunner(), // raster + loop.GetTaskRunner(), // ui + loop.GetTaskRunner() // io + }; +} + +std::unique_ptr CreateOhosContext() +{ + auto environment = fml::MakeRefCounted(); + std::string threadLabel = ::testing::UnitTest::GetInstance()->current_test_info()->name(); + TaskRunners task_runners = CreateTaskRunners(threadLabel); + return std::make_unique(OHOSRenderingAPI::kOpenGLES, environment, task_runners, 0); +} +} // namespace + +TEST(OhosSurfaceGLSkiaTest, Create) +{ + std::shared_ptr context = CreateOhosContext(); + EXPECT_TRUE(context != nullptr); + std::unique_ptr surfaceGL = std::make_unique(context); + EXPECT_TRUE(surfaceGL != nullptr); + EXPECT_TRUE(surfaceGL->IsValid()); +} + +TEST(OhosSurfaceGLSkiaTest, ResourceContext) +{ + std::shared_ptr context = CreateOhosContext(); + EXPECT_TRUE(context != nullptr); + + std::unique_ptr surfaceGL = std::make_unique(context); + EXPECT_TRUE(surfaceGL != nullptr); + EXPECT_TRUE(surfaceGL->IsValid()); + + EXPECT_TRUE(surfaceGL->ResourceContextMakeCurrent()); + EXPECT_TRUE(surfaceGL->ResourceContextClearCurrent()); +} + +TEST(OhosSurfaceGLSkiaTest, GetContext) +{ + std::shared_ptr context = CreateOhosContext(); + EXPECT_TRUE(context != nullptr); + std::unique_ptr surfaceGL = std::make_unique(context); + EXPECT_TRUE(surfaceGL != nullptr); + EXPECT_TRUE(surfaceGL->IsValid()); + + EXPECT_TRUE(surfaceGL->CreateSnapshotSurface() != nullptr); + + std::unique_ptr result = surfaceGL->GLContextMakeCurrent(); + EXPECT_TRUE(result != nullptr); + + SurfaceFrame::FramebufferInfo info = surfaceGL->GLContextFramebufferInfo(); + EXPECT_TRUE(info.supports_readback == true); + + std::optional frameDamage(SkIRect::MakeEmpty()); + std::optional bufferDamage(SkIRect::MakeEmpty()); + GLPresentInfo presentInfo = {0, frameDamage, std::nullopt, bufferDamage}; + EXPECT_TRUE(surfaceGL->GLContextPresent(presentInfo)); + + GLFrameInfo frameInfo; + EXPECT_TRUE(surfaceGL->GLContextFBO(frameInfo).fbo_id == 0); + + sk_sp grInterface = surfaceGL->GetGLInterface(); + EXPECT_TRUE(grInterface != nullptr); +} +} +} \ No newline at end of file diff --git a/shell/platform/ohos/testing/ohos_surface_software_unittests.cpp b/shell/platform/ohos/testing/ohos_surface_software_unittests.cpp new file mode 100644 index 0000000000..c84c182661 --- /dev/null +++ b/shell/platform/ohos/testing/ohos_surface_software_unittests.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include "flutter/shell/platform/ohos/ohos_surface_software.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { +namespace { +std::shared_ptr CreateOhosContext() +{ + return std::make_shared(OHOSRenderingAPI::kSoftware); +} +} // namespace + +TEST(OHOSSurfaceSoftwareTest, Create) +{ + std::shared_ptr context = CreateOhosContext(); + EXPECT_TRUE(context != nullptr); + std::unique_ptr surfaceSoftWare = std::make_unique(context); + EXPECT_TRUE(surfaceSoftWare != nullptr); + EXPECT_TRUE(surfaceSoftWare->IsValid()); +} + +TEST(OHOSSurfaceSoftwareTest, CreateGPUSurface) +{ + std::shared_ptr context = CreateOhosContext(); + EXPECT_TRUE(context != nullptr); + std::unique_ptr surfaceSoftWare = std::make_unique(context); + EXPECT_TRUE(surfaceSoftWare != nullptr); + + sk_sp grContext = GrDirectContext::MakeMock(nullptr); + std::unique_ptr gpuSurface = surfaceSoftWare->CreateGPUSurface(grContext.get()); + EXPECT_TRUE(gpuSurface != nullptr); +} + +TEST(OHOSSurfaceSoftwareTest, AcquireBackingStore) +{ + std::shared_ptr context = CreateOhosContext(); + EXPECT_TRUE(context != nullptr); + std::unique_ptr surfaceSoftWare = std::make_unique(context); + EXPECT_TRUE(surfaceSoftWare != nullptr); + EXPECT_TRUE(surfaceSoftWare->IsValid()); + + sk_sp skSurface = surfaceSoftWare->AcquireBackingStore({100, 100}); + EXPECT_TRUE(skSurface != nullptr); +} +} +} \ No newline at end of file diff --git a/shell/platform/ohos/testing/ohos_touch_processor_unittests.cpp b/shell/platform/ohos/testing/ohos_touch_processor_unittests.cpp new file mode 100644 index 0000000000..c9b2a74de2 --- /dev/null +++ b/shell/platform/ohos/testing/ohos_touch_processor_unittests.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include "flutter/shell/platform/ohos/ohos_touch_processor.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +TEST(OhosTouchProcessorTest, getPointerChangeForAction) +{ + OhosTouchProcessor touchProcessor; + int maskedAction = OH_NATIVEXCOMPONENT_DOWN; + EXPECT_EQ(touchProcessor.getPointerChangeForAction(maskedAction), PointerData::Change::kDown); + maskedAction = OH_NATIVEXCOMPONENT_UP; + EXPECT_EQ(touchProcessor.getPointerChangeForAction(maskedAction), PointerData::Change::kUp); + maskedAction = OH_NATIVEXCOMPONENT_MOVE; + EXPECT_EQ(touchProcessor.getPointerChangeForAction(maskedAction), PointerData::Change::kMove); + maskedAction = OH_NATIVEXCOMPONENT_CANCEL; + EXPECT_EQ(touchProcessor.getPointerChangeForAction(maskedAction), PointerData::Change::kCancel); +} + +TEST(OhosTouchProcessorTest, getPointerDeviceTypeForToolType) +{ + OhosTouchProcessor touchProcessor; + int toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_FINGER; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kTouch); + toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_PEN; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kStylus); + toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_RUBBER; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kInvertedStylus); + toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_BRUSH; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kStylus); + toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_PENCIL; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kStylus); + toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_AIRBRUSH; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kStylus); + toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_MOUSE; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kMouse); + toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_LENS; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kTouch); + toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; + EXPECT_EQ(touchProcessor.getPointerDeviceTypeForToolType(toolType), PointerData::DeviceKind::kTouch); +} +} +} \ No newline at end of file diff --git a/shell/platform/ohos/testing/ohos_xcomponent_adapter_unittests.cpp b/shell/platform/ohos/testing/ohos_xcomponent_adapter_unittests.cpp new file mode 100644 index 0000000000..aa1538956c --- /dev/null +++ b/shell/platform/ohos/testing/ohos_xcomponent_adapter_unittests.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include "flutter/shell/platform/ohos/ohos_xcomponent_adapter.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { +TEST(XComponentAdapterTest, SetNativeXComponent) +{ + XComponentAdapter* xcomponentAdapter = XComponentAdapter::GetInstance(); + std::string id = "test"; + OH_NativeXComponent* nativeXComponent = nullptr; + xcomponentAdapter->SetNativeXComponent(id, nativeXComponent); + + EXPECT_TRUE(xcomponentAdapter->xcomponetMap_.size() > 0); +} +} +} diff --git a/shell/platform/ohos/testing/vsync_waiter_ohos_unittests.cpp b/shell/platform/ohos/testing/vsync_waiter_ohos_unittests.cpp new file mode 100644 index 0000000000..48245deba3 --- /dev/null +++ b/shell/platform/ohos/testing/vsync_waiter_ohos_unittests.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include "flutter/common/task_runners.h" +#include "flutter/shell/platform/ohos/vsync_waiter_ohos.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { +namespace { +TaskRunners CreateTaskRunners(const std::string& threadLabel) +{ + fml::MessageLoop::EnsureInitializedForCurrentThread(); + auto& loop = fml::MessageLoop::GetCurrent(); + return { + threadLabel, + loop.GetTaskRunner(), // platform + loop.GetTaskRunner(), // raster + loop.GetTaskRunner(), // ui + loop.GetTaskRunner() // io + }; +} +} +TEST(VsyncWaiterOHOSTest, Create) +{ + std::string threadLabel = ::testing::UnitTest::GetInstance()->current_test_info()->name(); + TaskRunners runners = CreateTaskRunners(threadLabel); + std::shared_ptr waiter = std::make_shared(runners); + EXPECT_TRUE(waiter != nullptr); +} +} +} \ No newline at end of file -- Gitee