|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <gtest/gtest.h> |
| 11 | +#include <yoga/Yoga.h> |
| 12 | +#include <stdarg.h> |
| 13 | + |
| 14 | +namespace { |
| 15 | + char writeBuffer[4096]; |
| 16 | + int _unmanagedLogger(YGLogLevel level, const char *format, va_list args) { |
| 17 | + return vsnprintf(writeBuffer + strlen(writeBuffer), sizeof(writeBuffer) - strlen(writeBuffer), format, args); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +TEST(YogaTest, logger_default_node_should_print_no_style_info) { |
| 22 | + writeBuffer[0] = '\0'; |
| 23 | + YGSetLogger(_unmanagedLogger); |
| 24 | + const YGNodeRef root = YGNodeNew(); |
| 25 | + YGNodeCalculateLayout(root, YGUnitUndefined, YGUnitUndefined, YGDirectionLTR); |
| 26 | + YGNodePrint(root, (YGPrintOptions)(YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle)); |
| 27 | + YGSetLogger(NULL); |
| 28 | + YGNodeFree(root); |
| 29 | + |
| 30 | + const char * expected = "<div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"\" ></div>"; |
| 31 | + ASSERT_STREQ(expected, writeBuffer); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(YogaTest, logger_node_with_percentage_absolute_position_and_margin) { |
| 35 | + writeBuffer[0] = '\0'; |
| 36 | + YGSetLogger(_unmanagedLogger); |
| 37 | + const YGNodeRef root = YGNodeNew(); |
| 38 | + YGNodeStyleSetPositionType(root, YGPositionTypeAbsolute); |
| 39 | + YGNodeStyleSetWidthPercent(root, 50); |
| 40 | + YGNodeStyleSetHeightPercent(root, 75); |
| 41 | + YGNodeStyleSetFlex(root, 1); |
| 42 | + YGNodeStyleSetMargin(root, YGEdgeRight, 10); |
| 43 | + YGNodeStyleSetMarginAuto(root, YGEdgeLeft); |
| 44 | + YGNodeCalculateLayout(root, YGUnitUndefined, YGUnitUndefined, YGDirectionLTR); |
| 45 | + YGNodePrint(root, (YGPrintOptions)(YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle)); |
| 46 | + YGSetLogger(NULL); |
| 47 | + YGNodeFree(root); |
| 48 | + |
| 49 | + const char * expected = "<div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"flex: 1; margin-left: auto; margin-right: 10px; width: 50%; height: 75%; position: absolute; \" ></div>"; |
| 50 | + ASSERT_STREQ(expected, writeBuffer); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(YogaTest, logger_node_with_children_should_print_indented) { |
| 54 | + writeBuffer[0] = '\0'; |
| 55 | + YGSetLogger(_unmanagedLogger); |
| 56 | + const YGNodeRef root = YGNodeNew(); |
| 57 | + const YGNodeRef child0 = YGNodeNew(); |
| 58 | + const YGNodeRef child1 = YGNodeNew(); |
| 59 | + YGNodeInsertChild(root, child0, 0); |
| 60 | + YGNodeInsertChild(root, child1, 1); |
| 61 | + YGNodeCalculateLayout(root, YGUnitUndefined, YGUnitUndefined, YGDirectionLTR); |
| 62 | + YGNodePrint(root, (YGPrintOptions)(YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle)); |
| 63 | + YGSetLogger(NULL); |
| 64 | + YGNodeFreeRecursive(root); |
| 65 | + |
| 66 | + const char * expected = "<div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"\" >\n <div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"\" ></div>\n <div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"\" ></div>\n</div>"; |
| 67 | + ASSERT_STREQ(expected, writeBuffer); |
| 68 | +} |
0 commit comments