|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <react/fabric/SurfaceHandlerBinding.h> |
| 9 | + |
| 10 | +#include <folly/dynamic.h> |
| 11 | +#include <gtest/gtest.h> |
| 12 | +#include <react/renderer/core/LayoutContext.h> |
| 13 | +#include <react/renderer/scheduler/SurfaceHandler.h> |
| 14 | + |
| 15 | +namespace facebook::react { |
| 16 | + |
| 17 | +class SurfaceHandlerBindingTest : public ::testing::Test { |
| 18 | + protected: |
| 19 | + SurfaceHandlerBinding makeBinding() { |
| 20 | + return SurfaceHandlerBinding(/*surfaceId=*/101, "SampleModule"); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +TEST_F( |
| 25 | + SurfaceHandlerBindingTest, |
| 26 | + testSetLayoutConstraintsMapsJavaArgumentsToSurfaceParameters) { |
| 27 | + auto binding = makeBinding(); |
| 28 | + |
| 29 | + binding.setLayoutConstraints( |
| 30 | + /*minWidth=*/10.0f, |
| 31 | + /*maxWidth=*/320.0f, |
| 32 | + /*minHeight=*/20.0f, |
| 33 | + /*maxHeight=*/640.0f, |
| 34 | + /*offsetX=*/3.0f, |
| 35 | + /*offsetY=*/4.0f, |
| 36 | + /*doLeftAndRightSwapInRTL=*/1, |
| 37 | + /*isRTL=*/1, |
| 38 | + /*pixelDensity=*/2.0f, |
| 39 | + /*fontScale=*/1.25f); |
| 40 | + |
| 41 | + const auto& surfaceHandler = binding.getSurfaceHandler(); |
| 42 | + const auto layoutConstraints = surfaceHandler.getLayoutConstraints(); |
| 43 | + const auto layoutContext = surfaceHandler.getLayoutContext(); |
| 44 | + |
| 45 | + EXPECT_FLOAT_EQ(10.0f, layoutConstraints.minimumSize.width); |
| 46 | + EXPECT_FLOAT_EQ(20.0f, layoutConstraints.minimumSize.height); |
| 47 | + EXPECT_FLOAT_EQ(320.0f, layoutConstraints.maximumSize.width); |
| 48 | + EXPECT_FLOAT_EQ(640.0f, layoutConstraints.maximumSize.height); |
| 49 | + EXPECT_EQ(LayoutDirection::RightToLeft, layoutConstraints.layoutDirection); |
| 50 | + |
| 51 | + EXPECT_FLOAT_EQ(3.0f, layoutContext.viewportOffset.x); |
| 52 | + EXPECT_FLOAT_EQ(4.0f, layoutContext.viewportOffset.y); |
| 53 | + EXPECT_FLOAT_EQ(320.0f, layoutContext.viewportSize.width); |
| 54 | + EXPECT_FLOAT_EQ(640.0f, layoutContext.viewportSize.height); |
| 55 | + EXPECT_TRUE(layoutContext.swapLeftAndRightInRTL); |
| 56 | + EXPECT_FLOAT_EQ(2.0f, layoutContext.pointScaleFactor); |
| 57 | + EXPECT_FLOAT_EQ(1.25f, layoutContext.fontSizeMultiplier); |
| 58 | +} |
| 59 | + |
| 60 | +TEST_F( |
| 61 | + SurfaceHandlerBindingTest, |
| 62 | + testSetLayoutConstraintsMapsFalsyJavaBooleansToLeftToRightLayout) { |
| 63 | + auto binding = makeBinding(); |
| 64 | + |
| 65 | + binding.setLayoutConstraints( |
| 66 | + /*minWidth=*/0.0f, |
| 67 | + /*maxWidth=*/200.0f, |
| 68 | + /*minHeight=*/0.0f, |
| 69 | + /*maxHeight=*/400.0f, |
| 70 | + /*offsetX=*/0.0f, |
| 71 | + /*offsetY=*/0.0f, |
| 72 | + /*doLeftAndRightSwapInRTL=*/0, |
| 73 | + /*isRTL=*/0, |
| 74 | + /*pixelDensity=*/1.0f, |
| 75 | + /*fontScale=*/1.0f); |
| 76 | + |
| 77 | + const auto& surfaceHandler = binding.getSurfaceHandler(); |
| 78 | + |
| 79 | + EXPECT_EQ( |
| 80 | + LayoutDirection::LeftToRight, |
| 81 | + surfaceHandler.getLayoutConstraints().layoutDirection); |
| 82 | + EXPECT_FALSE(surfaceHandler.getLayoutContext().swapLeftAndRightInRTL); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F( |
| 86 | + SurfaceHandlerBindingTest, |
| 87 | + testSetDisplayModeUpdatesUnderlyingSurfaceDisplayMode) { |
| 88 | + auto binding = makeBinding(); |
| 89 | + |
| 90 | + binding.setDisplayMode(static_cast<jint>(DisplayMode::Suspended)); |
| 91 | + EXPECT_EQ( |
| 92 | + DisplayMode::Suspended, binding.getSurfaceHandler().getDisplayMode()); |
| 93 | + |
| 94 | + binding.setDisplayMode(static_cast<jint>(DisplayMode::Visible)); |
| 95 | + EXPECT_EQ(DisplayMode::Visible, binding.getSurfaceHandler().getDisplayMode()); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(SurfaceHandlerBindingTest, testSetPropsWithNullClearsSurfaceProps) { |
| 99 | + auto binding = makeBinding(); |
| 100 | + binding.getSurfaceHandler().setProps(folly::dynamic::object("stale", 1)); |
| 101 | + |
| 102 | + binding.setProps(nullptr); |
| 103 | + |
| 104 | + const auto props = binding.getSurfaceHandler().getProps(); |
| 105 | + ASSERT_TRUE(props.isObject()); |
| 106 | + EXPECT_EQ(0, props.count("stale")); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace facebook::react |
0 commit comments