From 9d36af8e7770725748caca6aa92377cfaba79e48 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 12:05:29 +0200 Subject: [PATCH 01/12] testUtils plugin --- .../Android/app/CMakeLists.txt | 7 +- Apps/ValidationTests/CMakeLists.txt | 9 +-- Apps/ValidationTests/UWP/App.cpp | 5 +- Apps/ValidationTests/Win32/App.cpp | 7 +- Apps/ValidationTests/X11/App.cpp | 8 +- Apps/ValidationTests/iOS/LibNativeBridge.mm | 2 +- Plugins/CMakeLists.txt | 5 +- Plugins/TestUtils/CMakeLists.txt | 18 +++++ .../Include/Babylon/Plugins/TestUtils.h | 10 +++ .../TestUtils/Source/Android/TestUtils.cpp | 22 ++++++ Plugins/TestUtils/Source/Apple/TestUtils.cpp | 46 ++++++++++++ Plugins/TestUtils/Source/TestUtils.cpp | 75 +++++++++++++++++++ Plugins/TestUtils/Source/TestUtils.h | 74 ++++++++++++++++++ Plugins/TestUtils/Source/UWP/TestUtils.cpp | 28 +++++++ Plugins/TestUtils/Source/Unix/TestUtils.cpp | 39 ++++++++++ Plugins/TestUtils/Source/Win32/TestUtils.cpp | 51 +++++++++++++ .../TestUtils/Source/__TestUtils.h | 0 17 files changed, 383 insertions(+), 23 deletions(-) create mode 100644 Plugins/TestUtils/CMakeLists.txt create mode 100644 Plugins/TestUtils/Include/Babylon/Plugins/TestUtils.h create mode 100644 Plugins/TestUtils/Source/Android/TestUtils.cpp create mode 100644 Plugins/TestUtils/Source/Apple/TestUtils.cpp create mode 100644 Plugins/TestUtils/Source/TestUtils.cpp create mode 100644 Plugins/TestUtils/Source/TestUtils.h create mode 100644 Plugins/TestUtils/Source/UWP/TestUtils.cpp create mode 100644 Plugins/TestUtils/Source/Unix/TestUtils.cpp create mode 100644 Plugins/TestUtils/Source/Win32/TestUtils.cpp rename Apps/ValidationTests/Shared/TestUtils.h => Plugins/TestUtils/Source/__TestUtils.h (100%) diff --git a/Apps/ValidationTests/Android/app/CMakeLists.txt b/Apps/ValidationTests/Android/app/CMakeLists.txt index 975189659..f9f18d592 100644 --- a/Apps/ValidationTests/Android/app/CMakeLists.txt +++ b/Apps/ValidationTests/Android/app/CMakeLists.txt @@ -44,9 +44,7 @@ add_subdirectory(${BABYLON_NATIVE_VALIDATIONTESTS_DIR}/../../ ${BABYLON_NATIVE_V add_library(BabylonNativeJNI SHARED src/main/cpp/BabylonNativeJNI.cpp) -target_include_directories(BabylonNativeJNI - PRIVATE - ${BABYLON_NATIVE_VALIDATIONTESTS_DIR}/Shared) +target_include_directories(BabylonNativeJNI) add_definitions(-DANDROID_STL=c++_shared) @@ -65,7 +63,8 @@ target_link_libraries(BabylonNativeJNI Window ScriptLoader bgfx - XMLHttpRequest) + XMLHttpRequest + TestUtils) configure_file( "${ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ABI}/libc++_shared.so" diff --git a/Apps/ValidationTests/CMakeLists.txt b/Apps/ValidationTests/CMakeLists.txt index 146ee915c..7231ae0e0 100644 --- a/Apps/ValidationTests/CMakeLists.txt +++ b/Apps/ValidationTests/CMakeLists.txt @@ -32,7 +32,6 @@ if(WINDOWS_STORE) set_property(SOURCE ${APPX_ASSETS} PROPERTY VS_DEPLOYMENT_CONTENT 1) set_property(SOURCE ${APPX_ASSETS} PROPERTY VS_DEPLOYMENT_LOCATION "Assets") set(SOURCES - "Shared/TestUtils.h" ${APPX_FILES} ${APPX_ASSETS} "UWP/App.cpp" @@ -42,7 +41,6 @@ if(WINDOWS_STORE) add_executable(ValidationTests WIN32 ${REFERENCE_IMAGES} ${BABYLON_SCRIPTS} ${SCRIPTS} ${SOURCES} ${RESOURCE_FILES}) elseif(WIN32) set(SOURCES - "Shared/TestUtils.h" "Win32/App.cpp" "Win32/App.h" "Win32/App.ico" @@ -66,8 +64,7 @@ elseif(APPLE) "iOS/AppDelegate.swift" "iOS/ViewController.swift" "iOS/LibNativeBridge.h" - "iOS/LibNativeBridge.mm" - "Shared/TestUtils.h") + "iOS/LibNativeBridge.mm") set_source_files_properties(${SCRIPTS} ${BABYLON_SCRIPTS} PROPERTIES MACOSX_PACKAGE_LOCATION "Scripts") set_source_files_properties(${REFERENCE_IMAGES} PROPERTIES MACOSX_PACKAGE_LOCATION "ReferenceImages") else() @@ -79,8 +76,7 @@ elseif(APPLE) "macOS/AppDelegate.mm" "macOS/AppDelegate.h" "macOS/ViewController.mm" - "macOS/ViewController.h" - "Shared/TestUtils.h") + "macOS/ViewController.h") set_source_files_properties(${SCRIPTS} ${BABYLON_SCRIPTS} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources/Scripts") set_source_files_properties(${REFERENCE_IMAGES} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources/ReferenceImages") endif() @@ -124,6 +120,7 @@ target_link_to_dependencies(ValidationTests PRIVATE Window PRIVATE ScriptLoader PRIVATE Canvas + PRIVATE TestUtils ${ADDITIONAL_LIBRARIES} PRIVATE XMLHttpRequest) diff --git a/Apps/ValidationTests/UWP/App.cpp b/Apps/ValidationTests/UWP/App.cpp index 8e2ba1d73..866725932 100644 --- a/Apps/ValidationTests/UWP/App.cpp +++ b/Apps/ValidationTests/UWP/App.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -15,8 +16,6 @@ #include -#include - using namespace Windows::ApplicationModel; using namespace Windows::ApplicationModel::Core; using namespace Windows::ApplicationModel::Activation; @@ -218,7 +217,7 @@ void App::RestartRuntime(Windows::Foundation::Rect bounds) Babylon::Plugins::NativeXr::Initialize(env); - Babylon::TestUtils::CreateInstance(env, windowPtr); + Babylon::Plugins::TestUtils::Initialize(env, windowPtr); }); Babylon::ScriptLoader loader{*m_runtime}; diff --git a/Apps/ValidationTests/Win32/App.cpp b/Apps/ValidationTests/Win32/App.cpp index 57d394935..bcc19a010 100644 --- a/Apps/ValidationTests/Win32/App.cpp +++ b/Apps/ValidationTests/Win32/App.cpp @@ -11,14 +11,13 @@ #include #include -#include - #include #include #include #include #include #include +#include #include #include #include @@ -90,7 +89,7 @@ namespace Babylon::Plugins::NativeXr::Initialize(env); - Babylon::TestUtils::CreateInstance(env, hWnd); + Babylon::Plugins::TestUtils::Initialize(env, hWnd); }); Babylon::ScriptLoader loader{ *runtime }; @@ -228,7 +227,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_DESTROY: { Uninitialize(); - PostQuitMessage(errorCode); + PostQuitMessage(Babylon::Plugins::TestUtils::errorCode); break; } default: diff --git a/Apps/ValidationTests/X11/App.cpp b/Apps/ValidationTests/X11/App.cpp index 813598e34..77d4bcc6e 100644 --- a/Apps/ValidationTests/X11/App.cpp +++ b/Apps/ValidationTests/X11/App.cpp @@ -7,13 +7,12 @@ #undef None #include -#include - #include #include #include #include #include +#include #include #include #include @@ -82,7 +81,7 @@ namespace fflush(stdout); }); - Babylon::TestUtils::CreateInstance(env, (void*)(uintptr_t)window); + Babylon::Plugins::TestUtils::Initialize(env, (env, (void*)(uintptr_t)window); Babylon::Polyfills::Window::Initialize(env); Babylon::Polyfills::XMLHttpRequest::Initialize(env); @@ -181,6 +180,7 @@ int main(int /*_argc*/, const char* const* /*_argv*/) InitBabylon(window); UpdateWindowSize(width, height); + bool doExit{false}; while (!doExit) { if (!XPending(display) && graphics) @@ -217,5 +217,5 @@ int main(int /*_argc*/, const char* const* /*_argv*/) XUnmapWindow(display, window); XDestroyWindow(display, window); - return errorCode; + return Babylon::Plugins::TestUtils::errorCode; } diff --git a/Apps/ValidationTests/iOS/LibNativeBridge.mm b/Apps/ValidationTests/iOS/LibNativeBridge.mm index 22b8e236d..d7f5fd87c 100644 --- a/Apps/ValidationTests/iOS/LibNativeBridge.mm +++ b/Apps/ValidationTests/iOS/LibNativeBridge.mm @@ -14,7 +14,7 @@ std::unique_ptr graphics{}; std::unique_ptr runtime{}; -#import +#import @implementation LibNativeBridge diff --git a/Plugins/CMakeLists.txt b/Plugins/CMakeLists.txt index 4f53b7a4e..e4fff5122 100644 --- a/Plugins/CMakeLists.txt +++ b/Plugins/CMakeLists.txt @@ -18,4 +18,7 @@ add_subdirectory(NativeXr) add_subdirectory(NativeCamera) # Add NativeOptimizations -add_subdirectory(NativeOptimizations) \ No newline at end of file +add_subdirectory(NativeOptimizations) + +# Add TestUtils +add_subdirectory(TestUtils) \ No newline at end of file diff --git a/Plugins/TestUtils/CMakeLists.txt b/Plugins/TestUtils/CMakeLists.txt new file mode 100644 index 000000000..f65153fdb --- /dev/null +++ b/Plugins/TestUtils/CMakeLists.txt @@ -0,0 +1,18 @@ +set(SOURCES "Include/Babylon/Plugins/TestUtils.h" + "Source/TestUtils.h" + "Source/TestUtils.cpp") + +set(SOURCES ${SOURCES} "Source/${BABYLON_NATIVE_PLATFORM}/TestUtils.cpp") + +add_library(TestUtils ${SOURCES}) +warnings_as_errors(TestUtils) + +target_include_directories(TestUtils INTERFACE "Include") +target_include_directories(TestUtils PRIVATE "Source") + +target_link_to_dependencies(TestUtils + PUBLIC JsRuntime + PRIVATE GraphicsInternal) + +set_property(TARGET TestUtils PROPERTY FOLDER Plugins) +source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES}) diff --git a/Plugins/TestUtils/Include/Babylon/Plugins/TestUtils.h b/Plugins/TestUtils/Include/Babylon/Plugins/TestUtils.h new file mode 100644 index 000000000..028ec9428 --- /dev/null +++ b/Plugins/TestUtils/Include/Babylon/Plugins/TestUtils.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +namespace Babylon::Plugins::TestUtils +{ + extern int errorCode; + void Initialize(Napi::Env env, WindowType nativeWindowPtr); +} diff --git a/Plugins/TestUtils/Source/Android/TestUtils.cpp b/Plugins/TestUtils/Source/Android/TestUtils.cpp new file mode 100644 index 000000000..cd1f14cee --- /dev/null +++ b/Plugins/TestUtils/Source/Android/TestUtils.cpp @@ -0,0 +1,22 @@ +#include "TestUtils.h" + +namespace Babylon::Plugins::Internal +{ + void TestUtils::Exit(const Napi::CallbackInfo& /*info*/) + { + } + + void TestUtils::UpdateSize(const Napi::CallbackInfo& /*info*/) + { + } + + void TestUtils::SetTitle(const Napi::CallbackInfo& /*info*/) + { + } + + Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) + { + auto path = "/data/data/com.android.babylonnative.validationtests/cache"; + return Napi::Value::From(info.Env(), path); + } +} \ No newline at end of file diff --git a/Plugins/TestUtils/Source/Apple/TestUtils.cpp b/Plugins/TestUtils/Source/Apple/TestUtils.cpp new file mode 100644 index 000000000..b9601f65e --- /dev/null +++ b/Plugins/TestUtils/Source/Apple/TestUtils.cpp @@ -0,0 +1,46 @@ +#include "TestUtils.h" + +namespace Babylon::Plugins::Internal +{ + void TestUtils::Exit(const Napi::CallbackInfo& info) + { +#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR + dispatch_async(dispatch_get_main_queue(), ^{ + if (graphics) + { + graphics->FinishRenderingCurrentFrame(); + } + runtime.reset(); + graphics.reset(); + UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Validation Tests" + message:(errorCode == 0)?@"Success!":@"Errors: Check logs!" + preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault + handler:^(UIAlertAction * ) {}]; + + [alert addAction:defaultAction]; + UIViewController *rootController = [[[[UIApplication sharedApplication]delegate] window] rootViewController]; + [rootController presentViewController:alert animated:YES completion:nil]; + }); +#else + dispatch_async(dispatch_get_main_queue(), ^{ + [[_nativeWindowPtr window]close]; + }); +#endif + } + + void TestUtils::UpdateSize(const Napi::CallbackInfo& /*info*/) + { + } + + void TestUtils::SetTitle(const Napi::CallbackInfo& /*info*/) + { + } + + Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) + { + std::string path = getenv("HOME"); + return Napi::Value::From(info.Env(), path); + } +} \ No newline at end of file diff --git a/Plugins/TestUtils/Source/TestUtils.cpp b/Plugins/TestUtils/Source/TestUtils.cpp new file mode 100644 index 000000000..44cba1be2 --- /dev/null +++ b/Plugins/TestUtils/Source/TestUtils.cpp @@ -0,0 +1,75 @@ +#include "TestUtils.h" + +#include +#include + + +#include +#include + + + +#include +#include +#include + +namespace Babylon::Plugins::Internal +{ + void TestUtils::WritePNG(const Napi::CallbackInfo& info) + { + const auto buffer = info[0].As(); + const auto width = info[1].As().Uint32Value(); + const auto height = info[2].As().Uint32Value(); + const auto filename = info[3].As().Utf8Value(); + + if (buffer.ByteLength() < (width * height * 4)) + { + return; + } + + bx::MemoryBlock mb(&allocator); + bx::FileWriter writer; + bx::FilePath filepath(filename.c_str()); + bx::Error err; + if (writer.open(filepath, false, &err)) + { + bimg::imageWritePng(&writer, width, height, width * 4, buffer.Data(), bimg::TextureFormat::RGBA8, false); + writer.close(); + } + } + + Napi::Value TestUtils::DecodeImage(const Napi::CallbackInfo& info) + { + Image* image = new Image; + const auto buffer = info[0].As(); + + image->m_Image = bimg::imageParse(&allocator, buffer.Data(), static_cast(buffer.ByteLength())); + + auto finalizer = [](Napi::Env, Image* image) { delete image; }; + return Napi::External::New(info.Env(), image, std::move(finalizer)); + } + + Napi::Value TestUtils::GetImageData(const Napi::CallbackInfo& info) + { + const auto imageData = info[0].As>().Data(); + + if (!imageData || !imageData->m_Image || !imageData->m_Image->m_size) + { + return info.Env().Undefined(); + } + + auto data = Napi::Uint8Array::New(info.Env(), imageData->m_Image->m_size); + const auto ptr = static_cast(imageData->m_Image->m_data); + memcpy(data.Data(), ptr, imageData->m_Image->m_size); + + return Napi::Value::From(info.Env(), data); + } +} + +namespace Babylon::Plugins::TestUtils +{ + void Initialize(Napi::Env env, WindowType nativeWindowPtr) + { + Internal::TestUtils::CreateInstance(env, nativeWindowPtr); + } +} \ No newline at end of file diff --git a/Plugins/TestUtils/Source/TestUtils.h b/Plugins/TestUtils/Source/TestUtils.h new file mode 100644 index 000000000..a1756e1fd --- /dev/null +++ b/Plugins/TestUtils/Source/TestUtils.h @@ -0,0 +1,74 @@ +#include + +#if _MSC_VER +#pragma warning( disable : 4324 ) // 'bx::DirectoryReader': structure was padded due to alignment specifier +#endif + +#include +#include +#include + +namespace Babylon::Plugins::Internal +{ + class TestUtils final : public Napi::ObjectWrap + { + public: + static inline constexpr const char* JS_INSTANCE_NAME{ "TestUtils" }; + + using ParentT = Napi::ObjectWrap; + + static void CreateInstance(Napi::Env env, WindowType nativeWindowPtr) + { + _nativeWindowPtr = nativeWindowPtr; + Napi::HandleScope scope{ env }; + + Napi::Function func = ParentT::DefineClass( + env, + "TestUtilsClass", + { + ParentT::InstanceMethod("exit", &TestUtils::Exit), + ParentT::InstanceMethod("updateSize", &TestUtils::UpdateSize), + ParentT::InstanceMethod("setTitle", &TestUtils::SetTitle), + ParentT::InstanceMethod("writePNG", &TestUtils::WritePNG), + ParentT::InstanceMethod("decodeImage", &TestUtils::DecodeImage), + ParentT::InstanceMethod("getImageData", &TestUtils::GetImageData), + ParentT::InstanceMethod("getOutputDirectory", &TestUtils::GetOutputDirectory), + }); + env.Global().Set(JS_INSTANCE_NAME, func.New({})); + } + + explicit TestUtils(const Napi::CallbackInfo& info) + : ParentT{ info } + { + } + + private: + static inline Napi::FunctionReference constructor{}; + + inline static WindowType _nativeWindowPtr{}; + inline static bx::DefaultAllocator allocator{}; + + void Exit(const Napi::CallbackInfo& info); + void UpdateSize(const Napi::CallbackInfo& info); + void SetTitle(const Napi::CallbackInfo& info); + Napi::Value GetOutputDirectory(const Napi::CallbackInfo& info); + + void WritePNG(const Napi::CallbackInfo& info); + Napi::Value DecodeImage(const Napi::CallbackInfo& info); + Napi::Value GetImageData(const Napi::CallbackInfo& info); + + struct Image + { + Image() = default; + ~Image() + { + if (m_Image) + { + bimg::imageFree(m_Image); + m_Image = nullptr; + } + } + bimg::ImageContainer* m_Image{}; + }; + }; +} // namespace \ No newline at end of file diff --git a/Plugins/TestUtils/Source/UWP/TestUtils.cpp b/Plugins/TestUtils/Source/UWP/TestUtils.cpp new file mode 100644 index 000000000..4e6d3a019 --- /dev/null +++ b/Plugins/TestUtils/Source/UWP/TestUtils.cpp @@ -0,0 +1,28 @@ +#include "TestUtils.h" + +namespace Babylon::Plugins::Internal +{ + void TestUtils::Exit(const Napi::CallbackInfo& info) + { + const int32_t exitCode = info[0].As().Int32Value(); + // ceguille: I didn't find a better way to do it for UWP + exit(exitCode); + } + + void TestUtils::UpdateSize(const Napi::CallbackInfo& /*info*/) + { + } + + void TestUtils::SetTitle(const Napi::CallbackInfo& /*info*/) + { + } + + Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) + { + using namespace Windows::Storage; + StorageFolder^ localFolder = ApplicationData::Current->LocalFolder; + std::wstring wpath = localFolder->Path->Data(); + std::string path{winrt::to_string(wpath)}; + return Napi::Value::From(info.Env(), path); + } +} \ No newline at end of file diff --git a/Plugins/TestUtils/Source/Unix/TestUtils.cpp b/Plugins/TestUtils/Source/Unix/TestUtils.cpp new file mode 100644 index 000000000..7accd3f0c --- /dev/null +++ b/Plugins/TestUtils/Source/Unix/TestUtils.cpp @@ -0,0 +1,39 @@ +#include "TestUtils.h" + + +namespace Babylon::Plugins::TestUtils +{ + int errorCode{}; +} + +namespace Babylon::Plugins::Internal +{ + void TestUtils::Exit(const Napi::CallbackInfo& info) + { + const int32_t exitCode = info[0].As().Int32Value(); + Plugins::TestUtils::errorCode = exitCode; + Display* display = XOpenDisplay(NULL); + XClientMessageEvent dummyEvent; + memset(&dummyEvent, 0, sizeof(XClientMessageEvent)); + dummyEvent.type = ClientMessage; + dummyEvent.window = (Window)_nativeWindowPtr; + dummyEvent.format = 32; + XSendEvent(display, (Window)_nativeWindowPtr, 0, 0, (XEvent*)&dummyEvent); + XFlush(display); + } + + void TestUtils::UpdateSize(const Napi::CallbackInfo& /*info*/) + { + } + + void TestUtils::SetTitle(const Napi::CallbackInfo & info) + { + const auto title = info[0].As().Utf8Value(); + Display* display = XOpenDisplay(NULL); + XStoreName(display, (Window)_nativeWindowPtr, title.c_str()); + } + + Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) + { + } +} \ No newline at end of file diff --git a/Plugins/TestUtils/Source/Win32/TestUtils.cpp b/Plugins/TestUtils/Source/Win32/TestUtils.cpp new file mode 100644 index 000000000..cd1284a21 --- /dev/null +++ b/Plugins/TestUtils/Source/Win32/TestUtils.cpp @@ -0,0 +1,51 @@ +#include "TestUtils.h" +#include +#include + +namespace +{ + std::filesystem::path GetModulePath() + { + char buffer[1024]; + ::GetModuleFileNameA(nullptr, buffer, ARRAYSIZE(buffer)); + return std::filesystem::path{buffer}.parent_path(); + } +} + +namespace Babylon::Plugins::TestUtils +{ + int errorCode{}; +} + +namespace Babylon::Plugins::Internal +{ + void TestUtils::Exit(const Napi::CallbackInfo& info) + { + const int32_t exitCode = info[0].As().Int32Value(); + Plugins::TestUtils::errorCode = exitCode; + PostMessageW(_nativeWindowPtr, WM_DESTROY, 0, 0); + } + + void TestUtils::UpdateSize(const Napi::CallbackInfo& info) + { + const int32_t width = info[0].As().Int32Value(); + const int32_t height = info[1].As().Int32Value(); + + HWND hwnd = _nativeWindowPtr; + RECT rc{ 0, 0, width, height }; + AdjustWindowRectEx(&rc, GetWindowStyle(hwnd), GetMenu(hwnd) != NULL, GetWindowExStyle(hwnd)); + SetWindowPos(hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER); + } + + void TestUtils::SetTitle(const Napi::CallbackInfo& info) + { + const auto title = info[0].As().Utf8Value(); + SetWindowTextA(_nativeWindowPtr, title.c_str()); + } + + Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) + { + auto path = GetModulePath().parent_path().generic_string(); + return Napi::Value::From(info.Env(), path); + } +} \ No newline at end of file diff --git a/Apps/ValidationTests/Shared/TestUtils.h b/Plugins/TestUtils/Source/__TestUtils.h similarity index 100% rename from Apps/ValidationTests/Shared/TestUtils.h rename to Plugins/TestUtils/Source/__TestUtils.h From 830b3394fa8c91f3ef55d3dd6d02c78fc6bbd9e4 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 12:54:02 +0200 Subject: [PATCH 02/12] other platforms --- Apps/ValidationTests/Android/app/CMakeLists.txt | 2 -- Plugins/TestUtils/CMakeLists.txt | 6 +++++- .../TestUtils/Source/Apple/{TestUtils.cpp => TestUtils.mm} | 0 Plugins/TestUtils/Source/UWP/TestUtils.cpp | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) rename Plugins/TestUtils/Source/Apple/{TestUtils.cpp => TestUtils.mm} (100%) diff --git a/Apps/ValidationTests/Android/app/CMakeLists.txt b/Apps/ValidationTests/Android/app/CMakeLists.txt index f9f18d592..5d9c7bf15 100644 --- a/Apps/ValidationTests/Android/app/CMakeLists.txt +++ b/Apps/ValidationTests/Android/app/CMakeLists.txt @@ -44,8 +44,6 @@ add_subdirectory(${BABYLON_NATIVE_VALIDATIONTESTS_DIR}/../../ ${BABYLON_NATIVE_V add_library(BabylonNativeJNI SHARED src/main/cpp/BabylonNativeJNI.cpp) -target_include_directories(BabylonNativeJNI) - add_definitions(-DANDROID_STL=c++_shared) target_link_libraries(BabylonNativeJNI diff --git a/Plugins/TestUtils/CMakeLists.txt b/Plugins/TestUtils/CMakeLists.txt index f65153fdb..cff9fa115 100644 --- a/Plugins/TestUtils/CMakeLists.txt +++ b/Plugins/TestUtils/CMakeLists.txt @@ -2,7 +2,11 @@ set(SOURCES "Include/Babylon/Plugins/TestUtils.h" "Source/TestUtils.h" "Source/TestUtils.cpp") -set(SOURCES ${SOURCES} "Source/${BABYLON_NATIVE_PLATFORM}/TestUtils.cpp") +if(APPLE) + set(SOURCES ${SOURCES} "Source/${BABYLON_NATIVE_PLATFORM}/TestUtils.mm") +else() + set(SOURCES ${SOURCES} "Source/${BABYLON_NATIVE_PLATFORM}/TestUtils.cpp") +endif() add_library(TestUtils ${SOURCES}) warnings_as_errors(TestUtils) diff --git a/Plugins/TestUtils/Source/Apple/TestUtils.cpp b/Plugins/TestUtils/Source/Apple/TestUtils.mm similarity index 100% rename from Plugins/TestUtils/Source/Apple/TestUtils.cpp rename to Plugins/TestUtils/Source/Apple/TestUtils.mm diff --git a/Plugins/TestUtils/Source/UWP/TestUtils.cpp b/Plugins/TestUtils/Source/UWP/TestUtils.cpp index 4e6d3a019..e80cae7a4 100644 --- a/Plugins/TestUtils/Source/UWP/TestUtils.cpp +++ b/Plugins/TestUtils/Source/UWP/TestUtils.cpp @@ -1,4 +1,5 @@ #include "TestUtils.h" +#include namespace Babylon::Plugins::Internal { From d8ebb8bdfdad869bea621490560be9baac1f5c60 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 13:14:32 +0200 Subject: [PATCH 03/12] more fixes --- .../Android/app/src/main/cpp/BabylonNativeJNI.cpp | 4 ++-- Plugins/TestUtils/Source/UWP/TestUtils.cpp | 11 +++++++++++ Plugins/TestUtils/Source/Unix/TestUtils.cpp | 10 ++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp b/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp index c6fad769f..685743998 100644 --- a/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp +++ b/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp @@ -15,10 +15,10 @@ #include #include #include +#include #include #include #include -#include namespace { @@ -103,7 +103,7 @@ extern "C" Babylon::Polyfills::XMLHttpRequest::Initialize(env); - Babylon::TestUtils::CreateInstance(env, window); + Babylon::Plugins::TestUtils::CreateIInitializenstance(env, window); }); g_scriptLoader = std::make_unique(*g_runtime); diff --git a/Plugins/TestUtils/Source/UWP/TestUtils.cpp b/Plugins/TestUtils/Source/UWP/TestUtils.cpp index e80cae7a4..e5924051f 100644 --- a/Plugins/TestUtils/Source/UWP/TestUtils.cpp +++ b/Plugins/TestUtils/Source/UWP/TestUtils.cpp @@ -1,6 +1,17 @@ #include "TestUtils.h" +#include + #include +using namespace Windows::ApplicationModel; +using namespace Windows::ApplicationModel::Core; +using namespace Windows::ApplicationModel::Activation; +using namespace Windows::UI::Core; +using namespace Windows::UI::Input; +using namespace Windows::System; +using namespace Windows::Foundation; +using namespace Windows::Graphics::Display; + namespace Babylon::Plugins::Internal { void TestUtils::Exit(const Napi::CallbackInfo& info) diff --git a/Plugins/TestUtils/Source/Unix/TestUtils.cpp b/Plugins/TestUtils/Source/Unix/TestUtils.cpp index 7accd3f0c..fa65281a6 100644 --- a/Plugins/TestUtils/Source/Unix/TestUtils.cpp +++ b/Plugins/TestUtils/Source/Unix/TestUtils.cpp @@ -35,5 +35,15 @@ namespace Babylon::Plugins::Internal Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) { + char exe[1024]; + int ret = readlink("/proc/self/exe", exe, sizeof(exe)-1); + if(ret == -1) + { + throw Napi::Error::New(info.Env(), "Unable to get executable location"); + } + exe[ret] = 0; + + auto path = std::filesystem::path{exe}.parent_path().generic_string(); + return Napi::Value::From(info.Env(), path); } } \ No newline at end of file From f9f97a2a9cf89d3b66f7ca8e2df2f09d235e4c4c Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 13:32:12 +0200 Subject: [PATCH 04/12] fixes --- Plugins/TestUtils/Source/Apple/TestUtils.mm | 3 ++- Plugins/TestUtils/Source/Unix/TestUtils.cpp | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Plugins/TestUtils/Source/Apple/TestUtils.mm b/Plugins/TestUtils/Source/Apple/TestUtils.mm index b9601f65e..34c93557b 100644 --- a/Plugins/TestUtils/Source/Apple/TestUtils.mm +++ b/Plugins/TestUtils/Source/Apple/TestUtils.mm @@ -1,4 +1,5 @@ -#include "TestUtils.h" +#import +#import "TestUtils.h" namespace Babylon::Plugins::Internal { diff --git a/Plugins/TestUtils/Source/Unix/TestUtils.cpp b/Plugins/TestUtils/Source/Unix/TestUtils.cpp index fa65281a6..2a003658c 100644 --- a/Plugins/TestUtils/Source/Unix/TestUtils.cpp +++ b/Plugins/TestUtils/Source/Unix/TestUtils.cpp @@ -1,5 +1,10 @@ #include "TestUtils.h" - +#define XK_MISCELLANY +#define XK_LATIN1 +#include +#include // will include X11 which #defines None... Don't mess with order of includes. +#include +#include // syscall namespace Babylon::Plugins::TestUtils { From d215abc5c27ee0f400ab99fce4e4d63c2ec2dd8c Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 14:12:27 +0200 Subject: [PATCH 05/12] fixes --- .../Android/app/src/main/cpp/BabylonNativeJNI.cpp | 2 +- Plugins/TestUtils/Source/Apple/TestUtils.mm | 2 +- Plugins/TestUtils/Source/Unix/TestUtils.cpp | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp b/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp index 685743998..959034b1c 100644 --- a/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp +++ b/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp @@ -103,7 +103,7 @@ extern "C" Babylon::Polyfills::XMLHttpRequest::Initialize(env); - Babylon::Plugins::TestUtils::CreateIInitializenstance(env, window); + Babylon::Plugins::TestUtils::Initialize(env, window); }); g_scriptLoader = std::make_unique(*g_runtime); diff --git a/Plugins/TestUtils/Source/Apple/TestUtils.mm b/Plugins/TestUtils/Source/Apple/TestUtils.mm index 34c93557b..b4a4d0d33 100644 --- a/Plugins/TestUtils/Source/Apple/TestUtils.mm +++ b/Plugins/TestUtils/Source/Apple/TestUtils.mm @@ -39,7 +39,7 @@ { } - Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) + Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& /*info*/) { std::string path = getenv("HOME"); return Napi::Value::From(info.Env(), path); diff --git a/Plugins/TestUtils/Source/Unix/TestUtils.cpp b/Plugins/TestUtils/Source/Unix/TestUtils.cpp index 2a003658c..9e89b8fca 100644 --- a/Plugins/TestUtils/Source/Unix/TestUtils.cpp +++ b/Plugins/TestUtils/Source/Unix/TestUtils.cpp @@ -5,6 +5,8 @@ #include // will include X11 which #defines None... Don't mess with order of includes. #include #include // syscall +#undef None +#include namespace Babylon::Plugins::TestUtils { From 44beaffbb2495608fc7f3b0dbdb1913d81ca88bc Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 16:19:08 +0200 Subject: [PATCH 06/12] platform data abstraction --- Apps/ValidationTests/X11/App.cpp | 2 +- Plugins/TestUtils/CMakeLists.txt | 12 +- .../{TestUtils.cpp => TestUtilsImpl.cpp} | 9 + .../Apple/{TestUtils.mm => TestUtilsImpl.mm} | 11 +- Plugins/TestUtils/Source/TestUtils.cpp | 14 +- Plugins/TestUtils/Source/TestUtils.h | 11 +- Plugins/TestUtils/Source/TestUtilsImplData.h | 18 ++ .../UWP/{TestUtils.cpp => TestUtilsImpl.cpp} | 28 +- .../Unix/{TestUtils.cpp => TestUtilsImpl.cpp} | 17 +- .../{TestUtils.cpp => TestUtilsImpl.cpp} | 17 +- Plugins/TestUtils/Source/__TestUtils.h | 260 ------------------ 11 files changed, 96 insertions(+), 303 deletions(-) rename Plugins/TestUtils/Source/Android/{TestUtils.cpp => TestUtilsImpl.cpp} (66%) rename Plugins/TestUtils/Source/Apple/{TestUtils.mm => TestUtilsImpl.mm} (82%) create mode 100644 Plugins/TestUtils/Source/TestUtilsImplData.h rename Plugins/TestUtils/Source/UWP/{TestUtils.cpp => TestUtilsImpl.cpp} (58%) rename Plugins/TestUtils/Source/Unix/{TestUtils.cpp => TestUtilsImpl.cpp} (74%) rename Plugins/TestUtils/Source/Win32/{TestUtils.cpp => TestUtilsImpl.cpp} (73%) delete mode 100644 Plugins/TestUtils/Source/__TestUtils.h diff --git a/Apps/ValidationTests/X11/App.cpp b/Apps/ValidationTests/X11/App.cpp index 77d4bcc6e..807d79998 100644 --- a/Apps/ValidationTests/X11/App.cpp +++ b/Apps/ValidationTests/X11/App.cpp @@ -81,7 +81,7 @@ namespace fflush(stdout); }); - Babylon::Plugins::TestUtils::Initialize(env, (env, (void*)(uintptr_t)window); + Babylon::Plugins::TestUtils::Initialize(env, (void*)(uintptr_t)window); Babylon::Polyfills::Window::Initialize(env); Babylon::Polyfills::XMLHttpRequest::Initialize(env); diff --git a/Plugins/TestUtils/CMakeLists.txt b/Plugins/TestUtils/CMakeLists.txt index cff9fa115..49f6697b3 100644 --- a/Plugins/TestUtils/CMakeLists.txt +++ b/Plugins/TestUtils/CMakeLists.txt @@ -1,16 +1,22 @@ set(SOURCES "Include/Babylon/Plugins/TestUtils.h" "Source/TestUtils.h" - "Source/TestUtils.cpp") + "Source/TestUtils.cpp" + "Source/TestUtilsImplData.h") if(APPLE) - set(SOURCES ${SOURCES} "Source/${BABYLON_NATIVE_PLATFORM}/TestUtils.mm") + set(SOURCES ${SOURCES} "Source/${BABYLON_NATIVE_PLATFORM}/TestUtilsImpl.mm") else() - set(SOURCES ${SOURCES} "Source/${BABYLON_NATIVE_PLATFORM}/TestUtils.cpp") + set(SOURCES ${SOURCES} "Source/${BABYLON_NATIVE_PLATFORM}/TestUtilsImpl.cpp") endif() add_library(TestUtils ${SOURCES}) warnings_as_errors(TestUtils) +if(WINDOWS_STORE) + target_compile_options(TestUtils PRIVATE /ZW) + target_compile_options(TestUtils PRIVATE /await) +endif() + target_include_directories(TestUtils INTERFACE "Include") target_include_directories(TestUtils PRIVATE "Source") diff --git a/Plugins/TestUtils/Source/Android/TestUtils.cpp b/Plugins/TestUtils/Source/Android/TestUtilsImpl.cpp similarity index 66% rename from Plugins/TestUtils/Source/Android/TestUtils.cpp rename to Plugins/TestUtils/Source/Android/TestUtilsImpl.cpp index cd1f14cee..03bae0257 100644 --- a/Plugins/TestUtils/Source/Android/TestUtils.cpp +++ b/Plugins/TestUtils/Source/Android/TestUtilsImpl.cpp @@ -19,4 +19,13 @@ namespace Babylon::Plugins::Internal auto path = "/data/data/com.android.babylonnative.validationtests/cache"; return Napi::Value::From(info.Env(), path); } +} + +namespace Babylon::Plugins::TestUtils +{ + void Initialize(Napi::Env env, WindowType nativeWindowPtr) + { + auto implData{std::make_shared(nativeWindowPtr)}; + Internal::TestUtils::CreateInstance(env, implData); + } } \ No newline at end of file diff --git a/Plugins/TestUtils/Source/Apple/TestUtils.mm b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm similarity index 82% rename from Plugins/TestUtils/Source/Apple/TestUtils.mm rename to Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm index b4a4d0d33..92284f6cd 100644 --- a/Plugins/TestUtils/Source/Apple/TestUtils.mm +++ b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm @@ -26,7 +26,7 @@ }); #else dispatch_async(dispatch_get_main_queue(), ^{ - [[_nativeWindowPtr window]close]; + [[m_implData->m_nativeWindowPtr window]close]; }); #endif } @@ -44,4 +44,13 @@ std::string path = getenv("HOME"); return Napi::Value::From(info.Env(), path); } +} + +namespace Babylon::Plugins::TestUtils +{ + void Initialize(Napi::Env env, WindowType nativeWindowPtr) + { + auto implData{std::make_shared(nativeWindowPtr)}; + Internal::TestUtils::CreateInstance(env, implData); + } } \ No newline at end of file diff --git a/Plugins/TestUtils/Source/TestUtils.cpp b/Plugins/TestUtils/Source/TestUtils.cpp index 44cba1be2..368c1eff5 100644 --- a/Plugins/TestUtils/Source/TestUtils.cpp +++ b/Plugins/TestUtils/Source/TestUtils.cpp @@ -2,13 +2,9 @@ #include #include - - #include #include - - - +#include #include #include #include @@ -65,11 +61,3 @@ namespace Babylon::Plugins::Internal return Napi::Value::From(info.Env(), data); } } - -namespace Babylon::Plugins::TestUtils -{ - void Initialize(Napi::Env env, WindowType nativeWindowPtr) - { - Internal::TestUtils::CreateInstance(env, nativeWindowPtr); - } -} \ No newline at end of file diff --git a/Plugins/TestUtils/Source/TestUtils.h b/Plugins/TestUtils/Source/TestUtils.h index a1756e1fd..7c629a4da 100644 --- a/Plugins/TestUtils/Source/TestUtils.h +++ b/Plugins/TestUtils/Source/TestUtils.h @@ -4,22 +4,23 @@ #pragma warning( disable : 4324 ) // 'bx::DirectoryReader': structure was padded due to alignment specifier #endif -#include +#include #include -#include namespace Babylon::Plugins::Internal { class TestUtils final : public Napi::ObjectWrap { public: + class ImplData; + static inline constexpr const char* JS_INSTANCE_NAME{ "TestUtils" }; using ParentT = Napi::ObjectWrap; - static void CreateInstance(Napi::Env env, WindowType nativeWindowPtr) + static void CreateInstance(Napi::Env env, std::shared_ptr implData) { - _nativeWindowPtr = nativeWindowPtr; + implData = std::move(implData); Napi::HandleScope scope{ env }; Napi::Function func = ParentT::DefineClass( @@ -45,7 +46,7 @@ namespace Babylon::Plugins::Internal private: static inline Napi::FunctionReference constructor{}; - inline static WindowType _nativeWindowPtr{}; + inline static std::shared_ptr m_implData; inline static bx::DefaultAllocator allocator{}; void Exit(const Napi::CallbackInfo& info); diff --git a/Plugins/TestUtils/Source/TestUtilsImplData.h b/Plugins/TestUtils/Source/TestUtilsImplData.h new file mode 100644 index 000000000..c83b7cec7 --- /dev/null +++ b/Plugins/TestUtils/Source/TestUtilsImplData.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include "TestUtils.h" + +namespace Babylon::Plugins::Internal +{ + class TestUtils::ImplData final : public std::enable_shared_from_this + { + public: + TestUtils::ImplData(WindowType nativeWindowPtr) + : m_nativeWindowPtr{nativeWindowPtr} + { + } + + WindowType m_nativeWindowPtr{}; + }; + +} // namespace \ No newline at end of file diff --git a/Plugins/TestUtils/Source/UWP/TestUtils.cpp b/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp similarity index 58% rename from Plugins/TestUtils/Source/UWP/TestUtils.cpp rename to Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp index e5924051f..3f7ea5fc6 100644 --- a/Plugins/TestUtils/Source/UWP/TestUtils.cpp +++ b/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp @@ -1,16 +1,10 @@ -#include "TestUtils.h" +// NOMINMAX to prevent compilation errors with bgfx +#include #include - #include +#include "TestUtils.h" -using namespace Windows::ApplicationModel; -using namespace Windows::ApplicationModel::Core; -using namespace Windows::ApplicationModel::Activation; -using namespace Windows::UI::Core; -using namespace Windows::UI::Input; -using namespace Windows::System; -using namespace Windows::Foundation; -using namespace Windows::Graphics::Display; +using namespace winrt::Windows; namespace Babylon::Plugins::Internal { @@ -31,10 +25,18 @@ namespace Babylon::Plugins::Internal Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) { - using namespace Windows::Storage; - StorageFolder^ localFolder = ApplicationData::Current->LocalFolder; + Storage::StorageFolder^ localFolder = Storage::ApplicationData::Current->LocalFolder; std::wstring wpath = localFolder->Path->Data(); - std::string path{winrt::to_string(wpath)}; + std::string path{ winrt::to_string(wpath) }; return Napi::Value::From(info.Env(), path); } +} + +namespace Babylon::Plugins::TestUtils +{ + void Initialize(Napi::Env env, WindowType nativeWindowPtr) + { + auto implData{std::make_shared(nativeWindowPtr)}; + Internal::TestUtils::CreateInstance(env, implData); + } } \ No newline at end of file diff --git a/Plugins/TestUtils/Source/Unix/TestUtils.cpp b/Plugins/TestUtils/Source/Unix/TestUtilsImpl.cpp similarity index 74% rename from Plugins/TestUtils/Source/Unix/TestUtils.cpp rename to Plugins/TestUtils/Source/Unix/TestUtilsImpl.cpp index 9e89b8fca..638e08d3f 100644 --- a/Plugins/TestUtils/Source/Unix/TestUtils.cpp +++ b/Plugins/TestUtils/Source/Unix/TestUtilsImpl.cpp @@ -17,15 +17,16 @@ namespace Babylon::Plugins::Internal { void TestUtils::Exit(const Napi::CallbackInfo& info) { + auto window = (Window)m_implData->m_nativeWindowPtr; const int32_t exitCode = info[0].As().Int32Value(); Plugins::TestUtils::errorCode = exitCode; Display* display = XOpenDisplay(NULL); XClientMessageEvent dummyEvent; memset(&dummyEvent, 0, sizeof(XClientMessageEvent)); dummyEvent.type = ClientMessage; - dummyEvent.window = (Window)_nativeWindowPtr; + dummyEvent.window = window; dummyEvent.format = 32; - XSendEvent(display, (Window)_nativeWindowPtr, 0, 0, (XEvent*)&dummyEvent); + XSendEvent(display, window, 0, 0, (XEvent*)&dummyEvent); XFlush(display); } @@ -37,7 +38,8 @@ namespace Babylon::Plugins::Internal { const auto title = info[0].As().Utf8Value(); Display* display = XOpenDisplay(NULL); - XStoreName(display, (Window)_nativeWindowPtr, title.c_str()); + auto window = (Window)m_implData->m_nativeWindowPtr; + XStoreName(display, window, title.c_str()); } Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) @@ -53,4 +55,13 @@ namespace Babylon::Plugins::Internal auto path = std::filesystem::path{exe}.parent_path().generic_string(); return Napi::Value::From(info.Env(), path); } +} + +namespace Babylon::Plugins::TestUtils +{ + void Initialize(Napi::Env env, WindowType nativeWindowPtr) + { + auto implData{std::make_shared(nativeWindowPtr)}; + Internal::TestUtils::CreateInstance(env, implData); + } } \ No newline at end of file diff --git a/Plugins/TestUtils/Source/Win32/TestUtils.cpp b/Plugins/TestUtils/Source/Win32/TestUtilsImpl.cpp similarity index 73% rename from Plugins/TestUtils/Source/Win32/TestUtils.cpp rename to Plugins/TestUtils/Source/Win32/TestUtilsImpl.cpp index cd1284a21..869058a21 100644 --- a/Plugins/TestUtils/Source/Win32/TestUtils.cpp +++ b/Plugins/TestUtils/Source/Win32/TestUtilsImpl.cpp @@ -1,4 +1,4 @@ -#include "TestUtils.h" +#include "TestUtilsImplData.h" #include #include @@ -23,7 +23,7 @@ namespace Babylon::Plugins::Internal { const int32_t exitCode = info[0].As().Int32Value(); Plugins::TestUtils::errorCode = exitCode; - PostMessageW(_nativeWindowPtr, WM_DESTROY, 0, 0); + PostMessageW(m_implData->m_nativeWindowPtr, WM_DESTROY, 0, 0); } void TestUtils::UpdateSize(const Napi::CallbackInfo& info) @@ -31,7 +31,7 @@ namespace Babylon::Plugins::Internal const int32_t width = info[0].As().Int32Value(); const int32_t height = info[1].As().Int32Value(); - HWND hwnd = _nativeWindowPtr; + auto hwnd = m_implData->m_nativeWindowPtr; RECT rc{ 0, 0, width, height }; AdjustWindowRectEx(&rc, GetWindowStyle(hwnd), GetMenu(hwnd) != NULL, GetWindowExStyle(hwnd)); SetWindowPos(hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER); @@ -40,7 +40,7 @@ namespace Babylon::Plugins::Internal void TestUtils::SetTitle(const Napi::CallbackInfo& info) { const auto title = info[0].As().Utf8Value(); - SetWindowTextA(_nativeWindowPtr, title.c_str()); + SetWindowTextA(m_implData->m_nativeWindowPtr, title.c_str()); } Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) @@ -48,4 +48,13 @@ namespace Babylon::Plugins::Internal auto path = GetModulePath().parent_path().generic_string(); return Napi::Value::From(info.Env(), path); } +} + +namespace Babylon::Plugins::TestUtils +{ + void Initialize(Napi::Env env, WindowType nativeWindowPtr) + { + auto implData{std::make_shared(nativeWindowPtr)}; + Internal::TestUtils::CreateInstance(env, implData); + } } \ No newline at end of file diff --git a/Plugins/TestUtils/Source/__TestUtils.h b/Plugins/TestUtils/Source/__TestUtils.h deleted file mode 100644 index d98642d38..000000000 --- a/Plugins/TestUtils/Source/__TestUtils.h +++ /dev/null @@ -1,260 +0,0 @@ -#pragma once - -#include - -#include -#include - -#include -#include -#include - -#if _MSC_VER -#pragma warning( disable : 4324 ) // 'bx::DirectoryReader': structure was padded due to alignment specifier -#endif - -#include - -#include -#include -#include -#include -#include - -namespace -{ - std::atomic doExit{}; - int errorCode{}; - -#if WIN32 && !__cplusplus_winrt - std::filesystem::path GetModulePath() - { - char buffer[1024]; - ::GetModuleFileNameA(nullptr, buffer, ARRAYSIZE(buffer)); - return std::filesystem::path{ buffer }.parent_path(); - } -#endif -} - -// can't externalize variable with ObjC++. Using a function instead. -int GetExitCode() -{ - return errorCode; -} - -namespace Babylon -{ - class TestUtils final : public Napi::ObjectWrap - { - public: - static inline constexpr const char* JS_INSTANCE_NAME{ "TestUtils" }; - - using ParentT = Napi::ObjectWrap; - - static void CreateInstance(Napi::Env env, WindowType nativeWindowPtr) - { - _nativeWindowPtr = nativeWindowPtr; - Napi::HandleScope scope{ env }; - - Napi::Function func = ParentT::DefineClass( - env, - "TestUtilsClass", - { - ParentT::InstanceMethod("exit", &TestUtils::Exit), - ParentT::InstanceMethod("updateSize", &TestUtils::UpdateSize), - ParentT::InstanceMethod("setTitle", &TestUtils::SetTitle), - ParentT::InstanceMethod("writePNG", &TestUtils::WritePNG), - ParentT::InstanceMethod("decodeImage", &TestUtils::DecodeImage), - ParentT::InstanceMethod("getImageData", &TestUtils::GetImageData), - ParentT::InstanceMethod("getOutputDirectory", &TestUtils::GetOutputDirectory), - }); - env.Global().Set(JS_INSTANCE_NAME, func.New({})); - } - - explicit TestUtils(const Napi::CallbackInfo& info) - : ParentT{ info } - { - } - - private: - static inline Napi::FunctionReference constructor{}; - - void Exit(const Napi::CallbackInfo& info) - { - const int32_t exitCode = info[0].As().Int32Value(); - doExit = true; - errorCode = exitCode; -#if defined(__cplusplus_winrt) - // ceguille: I didn't find a better way to do it for UWP - exit(errorCode); -#elif ANDROID -#elif WIN32 - PostMessageW(_nativeWindowPtr, WM_DESTROY, 0, 0); -#elif __linux__ - Display* display = XOpenDisplay(NULL); - XClientMessageEvent dummyEvent; - memset(&dummyEvent, 0, sizeof(XClientMessageEvent)); - dummyEvent.type = ClientMessage; - dummyEvent.window = (Window)_nativeWindowPtr; - dummyEvent.format = 32; - XSendEvent(display, (Window)_nativeWindowPtr, 0, 0, (XEvent*)&dummyEvent); - XFlush(display); -#elif __APPLE__ -#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR - dispatch_async(dispatch_get_main_queue(), ^{ - if (graphics) - { - graphics->FinishRenderingCurrentFrame(); - } - runtime.reset(); - graphics.reset(); - UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Validation Tests" - message:(errorCode == 0)?@"Success!":@"Errors: Check logs!" - preferredStyle:UIAlertControllerStyleAlert]; - - UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault - handler:^(UIAlertAction * ) {}]; - - [alert addAction:defaultAction]; - UIViewController *rootController = [[[[UIApplication sharedApplication]delegate] window] rootViewController]; - [rootController presentViewController:alert animated:YES completion:nil]; - }); -#else - dispatch_async(dispatch_get_main_queue(), ^{ - [[_nativeWindowPtr window]close]; - }); -#endif -#else - // TODO: handle exit for other platforms -#endif - } - - void UpdateSize(const Napi::CallbackInfo& info) - { -#if defined(__cplusplus_winrt) - (void)info; -#elif WIN32 - const int32_t width = info[0].As().Int32Value(); - const int32_t height = info[1].As().Int32Value(); - - HWND hwnd = _nativeWindowPtr; - RECT rc{ 0, 0, width, height }; - AdjustWindowRectEx(&rc, GetWindowStyle(hwnd), GetMenu(hwnd) != NULL, GetWindowExStyle(hwnd)); - SetWindowPos(hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER); -#else - // TODO: handle resize for other platforms - (void)info; -#endif - } - - void SetTitle(const Napi::CallbackInfo& info) - { - const auto title = info[0].As().Utf8Value(); -#if defined(__cplusplus_winrt) -#elif WIN32 - SetWindowTextA(_nativeWindowPtr, title.c_str()); -#elif ANDROID - (void)info; -#elif __linux__ - Display* display = XOpenDisplay(NULL); - XStoreName(display, (Window)_nativeWindowPtr, title.c_str()); -#else - // TODO: handle title for other platforms -#endif - } - - void WritePNG(const Napi::CallbackInfo& info) - { - const auto buffer = info[0].As(); - const auto width = info[1].As().Uint32Value(); - const auto height = info[2].As().Uint32Value(); - const auto filename = info[3].As().Utf8Value(); - - if (buffer.ByteLength() < (width * height * 4)) - { - return; - } - - bx::MemoryBlock mb(&allocator); - bx::FileWriter writer; - bx::FilePath filepath(filename.c_str()); - bx::Error err; - if (writer.open(filepath, false, &err)) - { - bimg::imageWritePng(&writer, width, height, width * 4, buffer.Data(), bimg::TextureFormat::RGBA8, false); - writer.close(); - } - } - - struct Image - { - Image() = default; - ~Image() - { - if (m_Image) - { - bimg::imageFree(m_Image); - m_Image = nullptr; - } - } - bimg::ImageContainer* m_Image{}; - }; - - Napi::Value DecodeImage(const Napi::CallbackInfo& info) - { - Image* image = new Image; - const auto buffer = info[0].As(); - - image->m_Image = bimg::imageParse(&allocator, buffer.Data(), static_cast(buffer.ByteLength())); - - auto finalizer = [](Napi::Env, Image* image) { delete image;}; - return Napi::External::New(info.Env(), image, std::move(finalizer)); - } - - Napi::Value GetImageData(const Napi::CallbackInfo& info) - { - const auto imageData = info[0].As>().Data(); - - if (!imageData || !imageData->m_Image || !imageData->m_Image->m_size) - { - return info.Env().Undefined(); - } - - auto data = Napi::Uint8Array::New(info.Env(), imageData->m_Image->m_size); - const auto ptr = static_cast(imageData->m_Image->m_data); - memcpy(data.Data(), ptr, imageData->m_Image->m_size); - - return Napi::Value::From(info.Env(), data); - } - - Napi::Value GetOutputDirectory(const Napi::CallbackInfo& info) - { -#if defined(__cplusplus_winrt) - using namespace Windows::Storage; - StorageFolder^ localFolder = ApplicationData::Current->LocalFolder; - std::wstring wpath = localFolder->Path->Data(); - std::string path{winrt::to_string(wpath)}; -#elif ANDROID - auto path = "/data/data/com.android.babylonnative.validationtests/cache"; -#elif __APPLE__ - std::string path = getenv("HOME"); -#elif __linux__ - char exe[1024]; - int ret = readlink("/proc/self/exe", exe, sizeof(exe)-1); - if(ret == -1) - { - throw Napi::Error::New(info.Env(), "Unable to get executable location"); - } - exe[ret] = 0; - - auto path = std::filesystem::path{exe}.parent_path().generic_string(); -#elif WIN32 - auto path = GetModulePath().parent_path().generic_string(); -#endif - return Napi::Value::From(info.Env(), path); - } - - inline static WindowType _nativeWindowPtr{}; - inline static bx::DefaultAllocator allocator{}; - }; -} From 3586948d385e7f2277764b741178093bf5e85b5d Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 16:37:39 +0200 Subject: [PATCH 07/12] typo --- Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm | 2 +- Plugins/TestUtils/Source/TestUtils.h | 2 +- Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp | 4 ++-- Plugins/TestUtils/Source/Unix/TestUtilsImpl.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm index 92284f6cd..4be340243 100644 --- a/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm +++ b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm @@ -1,5 +1,5 @@ +#import "TestUtilsImplData.h" #import -#import "TestUtils.h" namespace Babylon::Plugins::Internal { diff --git a/Plugins/TestUtils/Source/TestUtils.h b/Plugins/TestUtils/Source/TestUtils.h index 7c629a4da..d99617b27 100644 --- a/Plugins/TestUtils/Source/TestUtils.h +++ b/Plugins/TestUtils/Source/TestUtils.h @@ -20,7 +20,7 @@ namespace Babylon::Plugins::Internal static void CreateInstance(Napi::Env env, std::shared_ptr implData) { - implData = std::move(implData); + m_implData = std::move(implData); Napi::HandleScope scope{ env }; Napi::Function func = ParentT::DefineClass( diff --git a/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp b/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp index 3f7ea5fc6..e4d346689 100644 --- a/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp +++ b/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp @@ -1,8 +1,8 @@ -// NOMINMAX to prevent compilation errors with bgfx +#include "TestUtilsImplData.h" #include #include #include -#include "TestUtils.h" + using namespace winrt::Windows; diff --git a/Plugins/TestUtils/Source/Unix/TestUtilsImpl.cpp b/Plugins/TestUtils/Source/Unix/TestUtilsImpl.cpp index 638e08d3f..9c759babf 100644 --- a/Plugins/TestUtils/Source/Unix/TestUtilsImpl.cpp +++ b/Plugins/TestUtils/Source/Unix/TestUtilsImpl.cpp @@ -1,4 +1,4 @@ -#include "TestUtils.h" +#include "TestUtilsImplData.h" #define XK_MISCELLANY #define XK_LATIN1 #include From f787fef9703becbbc034098148f44fb0434c36e7 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 17:49:26 +0200 Subject: [PATCH 08/12] Apple build --- Apps/ValidationTests/iOS/LibNativeBridge.mm | 5 ++--- Apps/ValidationTests/macOS/ViewController.mm | 4 ++-- Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm | 13 +++++++++++-- Plugins/TestUtils/Source/TestUtils.h | 4 ++-- Plugins/TestUtils/Source/TestUtilsImplData.h | 4 ++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Apps/ValidationTests/iOS/LibNativeBridge.mm b/Apps/ValidationTests/iOS/LibNativeBridge.mm index d7f5fd87c..81e5a6e15 100644 --- a/Apps/ValidationTests/iOS/LibNativeBridge.mm +++ b/Apps/ValidationTests/iOS/LibNativeBridge.mm @@ -6,6 +6,7 @@ #import #import #import +#import #import #import #import @@ -14,8 +15,6 @@ std::unique_ptr graphics{}; std::unique_ptr runtime{}; -#import - @implementation LibNativeBridge - (instancetype)init @@ -60,7 +59,7 @@ - (void)init:(MTKView*)view width:(int)inWidth height:(int)inHeight // Initialize NativeXr plugin. Babylon::Plugins::NativeXr::Initialize(env); - Babylon::TestUtils::CreateInstance(env, nullptr); + Babylon::Plugins::TestUtils::Initialize(env, nullptr); }); Babylon::ScriptLoader loader{ *runtime }; diff --git a/Apps/ValidationTests/macOS/ViewController.mm b/Apps/ValidationTests/macOS/ViewController.mm index 89387c629..bd8954d3f 100644 --- a/Apps/ValidationTests/macOS/ViewController.mm +++ b/Apps/ValidationTests/macOS/ViewController.mm @@ -6,12 +6,12 @@ #import #import #import +#import #import #import #import #import #import -#import std::unique_ptr graphics{}; std::unique_ptr runtime{}; @@ -94,7 +94,7 @@ - (void)initialize { Babylon::Plugins::NativeOptimizations::Initialize(env); - Babylon::TestUtils::CreateInstance(env, engineView); + Babylon::Plugins::TestUtils::Initialize(env, engineView); }); Babylon::ScriptLoader loader{ *runtime }; diff --git a/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm index 92284f6cd..2e16c1034 100644 --- a/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm +++ b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm @@ -1,10 +1,19 @@ #import +#import "TestUtilsImplData.h" #import "TestUtils.h" +// can't externalize variable with ObjC++. Using a function instead. +int errorCode{}; +int GetExitCode() +{ + return errorCode; +} + namespace Babylon::Plugins::Internal { void TestUtils::Exit(const Napi::CallbackInfo& info) { + errorCode = info[0].As().Int32Value(); #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR dispatch_async(dispatch_get_main_queue(), ^{ if (graphics) @@ -39,7 +48,7 @@ { } - Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& /*info*/) + Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) { std::string path = getenv("HOME"); return Napi::Value::From(info.Env(), path); @@ -53,4 +62,4 @@ void Initialize(Napi::Env env, WindowType nativeWindowPtr) auto implData{std::make_shared(nativeWindowPtr)}; Internal::TestUtils::CreateInstance(env, implData); } -} \ No newline at end of file +} diff --git a/Plugins/TestUtils/Source/TestUtils.h b/Plugins/TestUtils/Source/TestUtils.h index 7c629a4da..be8d1d3df 100644 --- a/Plugins/TestUtils/Source/TestUtils.h +++ b/Plugins/TestUtils/Source/TestUtils.h @@ -20,7 +20,7 @@ namespace Babylon::Plugins::Internal static void CreateInstance(Napi::Env env, std::shared_ptr implData) { - implData = std::move(implData); + m_implData = std::move(implData); Napi::HandleScope scope{ env }; Napi::Function func = ParentT::DefineClass( @@ -72,4 +72,4 @@ namespace Babylon::Plugins::Internal bimg::ImageContainer* m_Image{}; }; }; -} // namespace \ No newline at end of file +} // namespace diff --git a/Plugins/TestUtils/Source/TestUtilsImplData.h b/Plugins/TestUtils/Source/TestUtilsImplData.h index c83b7cec7..72d10e1ce 100644 --- a/Plugins/TestUtils/Source/TestUtilsImplData.h +++ b/Plugins/TestUtils/Source/TestUtilsImplData.h @@ -7,7 +7,7 @@ namespace Babylon::Plugins::Internal class TestUtils::ImplData final : public std::enable_shared_from_this { public: - TestUtils::ImplData(WindowType nativeWindowPtr) + ImplData(WindowType nativeWindowPtr) : m_nativeWindowPtr{nativeWindowPtr} { } @@ -15,4 +15,4 @@ namespace Babylon::Plugins::Internal WindowType m_nativeWindowPtr{}; }; -} // namespace \ No newline at end of file +} // namespace From db8c8d6cbdb13de1fb4b6908b047d92f8c860103 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 17:54:26 +0200 Subject: [PATCH 09/12] android build --- Plugins/TestUtils/Source/Android/TestUtilsImpl.cpp | 2 +- Plugins/TestUtils/Source/TestUtilsImplData.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/TestUtils/Source/Android/TestUtilsImpl.cpp b/Plugins/TestUtils/Source/Android/TestUtilsImpl.cpp index 03bae0257..2bb54aad7 100644 --- a/Plugins/TestUtils/Source/Android/TestUtilsImpl.cpp +++ b/Plugins/TestUtils/Source/Android/TestUtilsImpl.cpp @@ -1,4 +1,4 @@ -#include "TestUtils.h" +#include "TestUtilsImplData.h" namespace Babylon::Plugins::Internal { diff --git a/Plugins/TestUtils/Source/TestUtilsImplData.h b/Plugins/TestUtils/Source/TestUtilsImplData.h index c83b7cec7..3f2457090 100644 --- a/Plugins/TestUtils/Source/TestUtilsImplData.h +++ b/Plugins/TestUtils/Source/TestUtilsImplData.h @@ -7,7 +7,7 @@ namespace Babylon::Plugins::Internal class TestUtils::ImplData final : public std::enable_shared_from_this { public: - TestUtils::ImplData(WindowType nativeWindowPtr) + TestUtils::ImplData(WindowType nativeWindowPtr) : m_nativeWindowPtr{nativeWindowPtr} { } From ba619b3543806b65a915b7a78cb08cad77e2a94e Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 18:32:34 +0200 Subject: [PATCH 10/12] uwp build --- Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm | 1 - Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm index 77f66a48d..66cddc110 100644 --- a/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm +++ b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm @@ -1,5 +1,4 @@ #import "TestUtilsImplData.h" -#import // can't externalize variable with ObjC++. Using a function instead. int errorCode{}; diff --git a/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp b/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp index e4d346689..80c1f825e 100644 --- a/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp +++ b/Plugins/TestUtils/Source/UWP/TestUtilsImpl.cpp @@ -25,9 +25,10 @@ namespace Babylon::Plugins::Internal Napi::Value TestUtils::GetOutputDirectory(const Napi::CallbackInfo& info) { - Storage::StorageFolder^ localFolder = Storage::ApplicationData::Current->LocalFolder; + using namespace Windows::Storage; + StorageFolder^ localFolder = ApplicationData::Current->LocalFolder; std::wstring wpath = localFolder->Path->Data(); - std::string path{ winrt::to_string(wpath) }; + std::string path{winrt::to_string(wpath)}; return Napi::Value::From(info.Env(), path); } } From 7b3714f1255653dfc2049bd17acdeea3ba23e38e Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Tue, 5 Oct 2021 19:00:21 +0200 Subject: [PATCH 11/12] ios build --- Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm index 66cddc110..b2e81f87a 100644 --- a/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm +++ b/Plugins/TestUtils/Source/Apple/TestUtilsImpl.mm @@ -14,12 +14,6 @@ int GetExitCode() errorCode = info[0].As().Int32Value(); #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR dispatch_async(dispatch_get_main_queue(), ^{ - if (graphics) - { - graphics->FinishRenderingCurrentFrame(); - } - runtime.reset(); - graphics.reset(); UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Validation Tests" message:(errorCode == 0)?@"Success!":@"Errors: Check logs!" preferredStyle:UIAlertControllerStyleAlert]; From f3c70256042fbb1974a3e7f8478fc4820181d017 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Wed, 6 Oct 2021 09:25:14 +0200 Subject: [PATCH 12/12] removed empty space --- .../Android/app/src/main/cpp/BabylonNativeJNI.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp b/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp index 959034b1c..1005dd9f1 100644 --- a/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp +++ b/Apps/ValidationTests/Android/app/src/main/cpp/BabylonNativeJNI.cpp @@ -103,7 +103,7 @@ extern "C" Babylon::Polyfills::XMLHttpRequest::Initialize(env); - Babylon::Plugins::TestUtils::Initialize(env, window); + Babylon::Plugins::TestUtils::Initialize(env, window); }); g_scriptLoader = std::make_unique(*g_runtime);