Skip to content

Commit

Permalink
Add flutter_tizen_texture_registrar unittest (#141)
Browse files Browse the repository at this point in the history
* Add flutter_tizen_texture_registrar unittest

Signed-off-by: MuHong Byun <mh.byun@samsung.com>

* Apply review's comment

* Remove redundant code

Signed-off-by: MuHong Byun <mh.byun@samsung.com>

* Apply review's comment #2

Signed-off-by: MuHong Byun <mh.byun@samsung.com>
  • Loading branch information
bwikbs authored and swift-kim committed Dec 9, 2021
1 parent 3c4cc8b commit 2b871f4
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 8 deletions.
3 changes: 3 additions & 0 deletions shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ template("embedder_executable") {
"channels/localization_channel_stub.cc",
"channels/platform_channel_stub.cc",
"channels/settings_channel_stub.cc",
"external_texture_pixel_gl_stub.cc",
"external_texture_surface_gl_stub.cc",
"tizen_log_stub.cc",
"tizen_renderer_evas_gl.cc",
]
Expand Down Expand Up @@ -303,6 +305,7 @@ embedder_executable("flutter_tizen_unittests") {

sources = [
"flutter_tizen_engine_unittest.cc",
"flutter_tizen_texture_registrar_unittests.cc",
"testing/mock_engine.cc",
]

Expand Down
4 changes: 4 additions & 0 deletions shell/platform/tizen/external_texture_pixel_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ ExternalTexturePixelGL::ExternalTexturePixelGL(
user_data_(user_data) {}

bool ExternalTexturePixelGL::CopyPixelBuffer(size_t& width, size_t& height) {
if (!texture_callback_) {
return false;
}

const FlutterDesktopPixelBuffer* pixel_buffer =
texture_callback_(width, height, user_data_);

Expand Down
29 changes: 29 additions & 0 deletions shell/platform/tizen/external_texture_pixel_gl_stub.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/shell/platform/tizen/external_texture_pixel_gl.h"

namespace flutter {

ExternalTexturePixelGL::ExternalTexturePixelGL(
FlutterDesktopPixelBufferTextureCallback texture_callback,
void* user_data)
: ExternalTexture(),
texture_callback_(texture_callback),
user_data_(user_data) {}

bool ExternalTexturePixelGL::PopulateTexture(
size_t width,
size_t height,
FlutterOpenGLTexture* opengl_texture) {
return CopyPixelBuffer(width, height);
}

bool ExternalTexturePixelGL::CopyPixelBuffer(size_t& width, size_t& height) {
if (texture_callback_ && user_data_) {
return true;
}
return false;
}
} // namespace flutter
7 changes: 6 additions & 1 deletion shell/platform/tizen/external_texture_surface_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
size_t width,
size_t height,
FlutterOpenGLTexture* opengl_texture) {
if (!texture_callback_) {
return false;
}
const FlutterDesktopGpuBuffer* gpu_buffer =
texture_callback_(width, height, user_data_);
if (!gpu_buffer) {
Expand Down Expand Up @@ -153,7 +156,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
}

void ExternalTextureSurfaceGL::OnDestruction() {
destruction_callback_(user_data_);
if (destruction_callback_) {
destruction_callback_(user_data_);
}
}

} // namespace flutter
38 changes: 38 additions & 0 deletions shell/platform/tizen/external_texture_surface_gl_stub.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "external_texture_surface_gl.h"

namespace flutter {

ExternalTextureSurfaceGL::ExternalTextureSurfaceGL(
FlutterDesktopGpuBufferTextureCallback texture_callback,
FlutterDesktopGpuBufferDestructionCallback destruction_callback,
void* user_data)
: ExternalTexture(),
texture_callback_(texture_callback),
destruction_callback_(destruction_callback),
user_data_(user_data) {}

ExternalTextureSurfaceGL::~ExternalTextureSurfaceGL() {
state_.release();
}

bool ExternalTextureSurfaceGL::PopulateTexture(
size_t width,
size_t height,
FlutterOpenGLTexture* opengl_texture) {
if (texture_callback_ && destruction_callback_ && user_data_) {
return true;
}
return false;
}

void ExternalTextureSurfaceGL::OnDestruction() {
if (destruction_callback_) {
destruction_callback_(user_data_);
}
}

} // namespace flutter
2 changes: 2 additions & 0 deletions shell/platform/tizen/flutter_tizen_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class FlutterTizenEngine : public TizenRenderer::Delegate {
std::unique_ptr<PlatformViewChannel> platform_view_channel;

private:
friend class EngineModifier;

// Whether the engine is running in headed or headless mode.
bool IsHeaded() { return renderer != nullptr; }

Expand Down
7 changes: 0 additions & 7 deletions shell/platform/tizen/flutter_tizen_texture_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
#include <iostream>
#include <mutex>

#ifndef __X64_SHELL__
#include "flutter/shell/platform/tizen/external_texture_pixel_gl.h"
#include "flutter/shell/platform/tizen/external_texture_surface_gl.h"
#endif
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include "flutter/shell/platform/tizen/tizen_log.h"

Expand Down Expand Up @@ -90,10 +88,6 @@ bool FlutterTizenTextureRegistrar::PopulateTexture(
std::unique_ptr<ExternalTexture>
FlutterTizenTextureRegistrar::CreateExternalTexture(
const FlutterDesktopTextureInfo* texture_info) {
#ifdef __X64_SHELL__
FT_UNIMPLEMENTED();
return nullptr;
#else
switch (texture_info->type) {
case kFlutterDesktopPixelBufferTexture:
return std::make_unique<ExternalTexturePixelGL>(
Expand All @@ -110,7 +104,6 @@ FlutterTizenTextureRegistrar::CreateExternalTexture(
FT_LOGE("Invalid texture type.");
return nullptr;
}
#endif
}

} // namespace flutter
126 changes: 126 additions & 0 deletions shell/platform/tizen/flutter_tizen_texture_registrar_unittests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <iostream>

#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include "flutter/shell/platform/tizen/flutter_tizen_texture_registrar.h"
#include "flutter/shell/platform/tizen/testing/engine_modifier.h"
#include "gtest/gtest.h"

namespace flutter {
namespace testing {

class FlutterTizenTextureRegistrarTest : public ::testing::Test {
public:
FlutterTizenTextureRegistrarTest() {
ecore_init();
elm_init(0, nullptr);
}

protected:
void SetUp() {
FlutterDesktopEngineProperties engine_prop = {};
engine_prop.assets_path = "foo/flutter_assets";
engine_prop.icu_data_path = "foo/icudtl.dat";
engine_prop.aot_library_path = "foo/libapp.so";

FlutterProjectBundle project(engine_prop);
auto engine = std::make_unique<FlutterTizenEngine>(project);
engine_ = engine.release();
}

void TearDown() {
if (engine_) {
delete engine_;
}
engine_ = nullptr;
}

FlutterTizenEngine* engine_;
};

TEST_F(FlutterTizenTextureRegistrarTest, CreateDestroy) {
FlutterTizenTextureRegistrar registrar(engine_);

EXPECT_TRUE(true);
}

TEST_F(FlutterTizenTextureRegistrarTest, RegisterUnregisterTexture) {
EngineModifier modifier(engine_);

FlutterTizenTextureRegistrar registrar(engine_);

FlutterDesktopTextureInfo texture_info = {};
texture_info.type = kFlutterDesktopPixelBufferTexture;
texture_info.pixel_buffer_config.callback =
[](size_t width, size_t height,
void* user_data) -> const FlutterDesktopPixelBuffer* {
return nullptr;
};

int64_t registered_texture_id = 0;
bool register_called = false;
modifier.embedder_api().RegisterExternalTexture = MOCK_ENGINE_PROC(
RegisterExternalTexture, ([&register_called, &registered_texture_id](
auto engine, auto texture_id) {
register_called = true;
registered_texture_id = texture_id;
return kSuccess;
}));

bool unregister_called = false;
modifier.embedder_api().UnregisterExternalTexture = MOCK_ENGINE_PROC(
UnregisterExternalTexture, ([&unregister_called, &registered_texture_id](
auto engine, auto texture_id) {
unregister_called = true;
EXPECT_EQ(registered_texture_id, texture_id);
return kSuccess;
}));

bool mark_frame_available_called = false;
modifier.embedder_api().MarkExternalTextureFrameAvailable =
MOCK_ENGINE_PROC(MarkExternalTextureFrameAvailable,
([&mark_frame_available_called, &registered_texture_id](
auto engine, auto texture_id) {
mark_frame_available_called = true;
EXPECT_EQ(registered_texture_id, texture_id);
return kSuccess;
}));

auto texture_id = registrar.RegisterTexture(&texture_info);
EXPECT_TRUE(register_called);
EXPECT_NE(texture_id, -1);
EXPECT_EQ(texture_id, registered_texture_id);

EXPECT_TRUE(registrar.MarkTextureFrameAvailable(texture_id));
EXPECT_TRUE(mark_frame_available_called);

EXPECT_TRUE(registrar.UnregisterTexture(texture_id));
EXPECT_TRUE(unregister_called);
}

TEST_F(FlutterTizenTextureRegistrarTest, RegisterUnknownTextureType) {
EngineModifier modifier(engine_);

FlutterTizenTextureRegistrar registrar(engine_);

FlutterDesktopTextureInfo texture_info = {};
texture_info.type = static_cast<FlutterDesktopTextureType>(1234);

auto texture_id = registrar.RegisterTexture(&texture_info);

EXPECT_EQ(texture_id, -1);
}

TEST_F(FlutterTizenTextureRegistrarTest, PopulateInvalidTexture) {
FlutterTizenTextureRegistrar registrar(engine_);

auto result = registrar.PopulateTexture(1, 640, 480, nullptr);
EXPECT_FALSE(result);
}

} // namespace testing
} // namespace flutter
30 changes: 30 additions & 0 deletions shell/platform/tizen/testing/engine_modifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"

namespace flutter {

// A test utility class providing the ability to access and alter various
// private fields in an Engine instance.
//
// This simply provides a way to access the normally-private embedder proc
// table, so the lifetime of any changes made to the proc table is that of the
// engine object, not this helper.
class EngineModifier {
public:
explicit EngineModifier(FlutterTizenEngine* engine) : engine_(engine) {}

// Returns the engine's embedder API proc table, allowing for modification.
//
// Modifications are to the engine, and will last for the lifetime of the
// engine unless overwritten again.
FlutterEngineProcTable& embedder_api() { return engine_->embedder_api_; }

private:
FlutterTizenEngine* engine_;
};

} // namespace flutter

0 comments on commit 2b871f4

Please sign in to comment.