2016-10-12 09:55:04 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2016-12-05 02:56:20 -08:00
|
|
|
package com.facebook.yoga;
|
2016-10-12 09:55:04 -07:00
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
2016-11-17 09:10:47 -08:00
|
|
|
import static org.junit.Assert.assertTrue;
|
2016-10-12 09:55:04 -07:00
|
|
|
|
2016-12-03 04:40:21 -08:00
|
|
|
public class YogaNodeTest {
|
2016-10-12 09:55:04 -07:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testInit() {
|
2016-12-03 04:40:23 -08:00
|
|
|
final int refCount = YogaNode.jni_YGNodeGetInstanceCount();
|
|
|
|
final YogaNode node = new YogaNode();
|
|
|
|
assertEquals(refCount + 1, YogaNode.jni_YGNodeGetInstanceCount());
|
2016-10-12 09:55:04 -07:00
|
|
|
}
|
|
|
|
|
2017-01-10 07:03:56 -08:00
|
|
|
@Test
|
|
|
|
public void testBaseline() {
|
|
|
|
final YogaNode root = new YogaNode();
|
|
|
|
root.setFlexDirection(YogaFlexDirection.ROW);
|
|
|
|
root.setAlignItems(YogaAlign.BASELINE);
|
|
|
|
root.setWidth(100);
|
|
|
|
root.setHeight(100);
|
|
|
|
|
|
|
|
final YogaNode child1 = new YogaNode();
|
|
|
|
child1.setWidth(40);
|
|
|
|
child1.setHeight(40);
|
|
|
|
root.addChildAt(child1, 0);
|
|
|
|
|
|
|
|
final YogaNode child2 = new YogaNode();
|
|
|
|
child2.setWidth(40);
|
|
|
|
child2.setHeight(40);
|
|
|
|
child2.setBaselineFunction(new YogaBaselineFunction() {
|
|
|
|
public float baseline(YogaNodeAPI node, float width, float height) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
root.addChildAt(child2, 1);
|
|
|
|
|
|
|
|
root.calculateLayout();
|
|
|
|
|
|
|
|
assertEquals(0, (int) child1.getLayoutY());
|
|
|
|
assertEquals(40, (int) child2.getLayoutY());
|
|
|
|
}
|
|
|
|
|
2016-10-12 09:55:04 -07:00
|
|
|
@Test
|
|
|
|
public void testMeasure() {
|
2016-12-03 04:40:23 -08:00
|
|
|
final YogaNode node = new YogaNode();
|
|
|
|
node.setMeasureFunction(new YogaMeasureFunction() {
|
2016-10-27 10:52:09 -07:00
|
|
|
public long measure(
|
2016-12-03 04:40:23 -08:00
|
|
|
YogaNodeAPI node,
|
2016-10-12 09:55:04 -07:00
|
|
|
float width,
|
2016-12-02 05:47:43 -08:00
|
|
|
YogaMeasureMode widthMode,
|
2016-10-12 09:55:04 -07:00
|
|
|
float height,
|
2016-12-02 05:47:43 -08:00
|
|
|
YogaMeasureMode heightMode) {
|
2016-12-03 04:40:23 -08:00
|
|
|
return YogaMeasureOutput.make(100, 100);
|
2016-10-12 09:55:04 -07:00
|
|
|
}
|
|
|
|
});
|
2016-11-29 12:23:02 -08:00
|
|
|
node.calculateLayout();
|
2016-10-12 09:55:04 -07:00
|
|
|
assertEquals(100, (int) node.getLayoutWidth());
|
|
|
|
assertEquals(100, (int) node.getLayoutHeight());
|
|
|
|
}
|
2016-11-11 08:08:28 -08:00
|
|
|
|
2016-12-29 04:52:20 -08:00
|
|
|
@Test
|
|
|
|
public void testMeasureFloat() {
|
|
|
|
final YogaNode node = new YogaNode();
|
|
|
|
node.setMeasureFunction(new YogaMeasureFunction() {
|
|
|
|
public long measure(
|
|
|
|
YogaNodeAPI node,
|
|
|
|
float width,
|
|
|
|
YogaMeasureMode widthMode,
|
|
|
|
float height,
|
|
|
|
YogaMeasureMode heightMode) {
|
|
|
|
return YogaMeasureOutput.make(100.5f, 100.5f);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
node.calculateLayout();
|
|
|
|
assertEquals(100.5f, node.getLayoutWidth(), 0.0f);
|
|
|
|
assertEquals(100.5f, node.getLayoutHeight(), 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testMeasureFloatMin() {
|
|
|
|
final YogaNode node = new YogaNode();
|
|
|
|
node.setMeasureFunction(new YogaMeasureFunction() {
|
|
|
|
public long measure(
|
|
|
|
YogaNodeAPI node,
|
|
|
|
float width,
|
|
|
|
YogaMeasureMode widthMode,
|
|
|
|
float height,
|
|
|
|
YogaMeasureMode heightMode) {
|
|
|
|
return YogaMeasureOutput.make(Float.MIN_VALUE, Float.MIN_VALUE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
node.calculateLayout();
|
|
|
|
assertEquals(Float.MIN_VALUE, node.getLayoutWidth(), 0.0f);
|
|
|
|
assertEquals(Float.MIN_VALUE, node.getLayoutHeight(), 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testMeasureFloatMax() {
|
|
|
|
final YogaNode node = new YogaNode();
|
|
|
|
node.setMeasureFunction(new YogaMeasureFunction() {
|
|
|
|
public long measure(
|
|
|
|
YogaNodeAPI node,
|
|
|
|
float width,
|
|
|
|
YogaMeasureMode widthMode,
|
|
|
|
float height,
|
|
|
|
YogaMeasureMode heightMode) {
|
|
|
|
return YogaMeasureOutput.make(Float.MAX_VALUE, Float.MAX_VALUE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
node.calculateLayout();
|
|
|
|
assertEquals(Float.MAX_VALUE, node.getLayoutWidth(), 0.0f);
|
|
|
|
assertEquals(Float.MAX_VALUE, node.getLayoutHeight(), 0.0f);
|
|
|
|
}
|
|
|
|
|
2016-12-02 05:47:43 -08:00
|
|
|
private YogaLogLevel mLogLevel;
|
2016-11-11 08:08:28 -08:00
|
|
|
private String mLogMessage;
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testLogger() {
|
2016-12-03 04:40:23 -08:00
|
|
|
YogaNode.setLogger(new YogaLogger() {
|
2016-12-02 05:47:43 -08:00
|
|
|
public void log(YogaLogLevel level, String message) {
|
2016-11-11 08:08:28 -08:00
|
|
|
mLogLevel = level;
|
|
|
|
mLogMessage = message;
|
|
|
|
}
|
|
|
|
});
|
2016-12-03 04:40:23 -08:00
|
|
|
YogaNode.jni_YGLog(YogaLogLevel.DEBUG.intValue(), "Hello");
|
2016-12-02 05:47:43 -08:00
|
|
|
assertEquals(YogaLogLevel.DEBUG, mLogLevel);
|
2016-11-11 08:08:28 -08:00
|
|
|
assertEquals("Hello", mLogMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testUpdateLogger() {
|
2016-12-03 04:40:23 -08:00
|
|
|
YogaNode.setLogger(new YogaLogger() {
|
2016-12-02 05:47:43 -08:00
|
|
|
public void log(YogaLogLevel level, String message) {}
|
2016-11-11 08:08:28 -08:00
|
|
|
});
|
2016-12-03 04:40:23 -08:00
|
|
|
YogaNode.setLogger(new YogaLogger() {
|
2016-12-02 05:47:43 -08:00
|
|
|
public void log(YogaLogLevel level, String message) {
|
2016-11-11 08:08:28 -08:00
|
|
|
mLogLevel = level;
|
|
|
|
mLogMessage = message;
|
|
|
|
}
|
|
|
|
});
|
2016-12-03 04:40:23 -08:00
|
|
|
YogaNode.jni_YGLog(YogaLogLevel.VERBOSE.intValue(), "Flexbox");
|
2016-12-02 05:47:43 -08:00
|
|
|
assertEquals(YogaLogLevel.VERBOSE, mLogLevel);
|
2016-11-11 08:08:28 -08:00
|
|
|
assertEquals("Flexbox", mLogMessage);
|
|
|
|
}
|
2016-11-17 09:10:47 -08:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCopyStyle() {
|
2016-12-03 04:40:23 -08:00
|
|
|
final YogaNode node0 = new YogaNode();
|
2016-12-02 05:47:43 -08:00
|
|
|
assertTrue(YogaConstants.isUndefined(node0.getMaxHeight()));
|
2016-11-17 09:10:47 -08:00
|
|
|
|
2016-12-03 04:40:23 -08:00
|
|
|
final YogaNode node1 = new YogaNode();
|
2016-11-29 09:04:43 -08:00
|
|
|
node1.setMaxHeight(100);
|
2016-11-17 09:10:47 -08:00
|
|
|
|
|
|
|
node0.copyStyle(node1);
|
Add feature to use percentage as value unit
Summary:
Adds the feature to use percentage as a value unit.
You can use the function ```YGPx(float)``` and ```YGPercent(float)``` for convenience.
I did some benchmarks:
```
Without Percentage Feature - Release x86:
Stack with flex: median: 0.000000 ms, stddev: 0.146683 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.136525 ms
Nested flex: median: 0.000000 ms, stddev: 0.490101 ms
Huge nested layout: median: 23.000000 ms, stddev: 0.928291 ms
Stack with flex: median: 0.000000 ms, stddev: 0.170587 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.143384 ms
Nested flex: median: 0.000000 ms, stddev: 0.477791 ms
Huge nested layout: median: 22.000000 ms, stddev: 2.129779 ms
With Percentage Feature - Release x86:
Stack with flex: median: 0.000000 ms, stddev: 0.132951 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.136525 ms
Nested flex: median: 0.000000 ms, stddev: 0.489570 ms
Huge nested layout: median: 21.000000 ms, stddev: 1.390476 ms
Closes https://github.com/facebook/yoga/pull/258
Reviewed By: dshahidehpour
Differential Revision: D4361945
Pulled By: emilsjolander
fbshipit-source-id: a8f5bc63ad352eb9410d792729e56664468cd76a
2017-01-02 05:20:37 -08:00
|
|
|
assertEquals(100, (int) node0.getMaxHeight().value);
|
2016-11-17 09:10:47 -08:00
|
|
|
}
|
2017-01-05 12:48:10 -08:00
|
|
|
|
2017-01-15 15:16:10 -08:00
|
|
|
@Test
|
|
|
|
public void testLayoutMargin() {
|
|
|
|
final YogaNode node = new YogaNode();
|
|
|
|
node.setWidth(100);
|
|
|
|
node.setHeight(100);
|
|
|
|
node.setMargin(YogaEdge.START, 1);
|
|
|
|
node.setMargin(YogaEdge.END, 2);
|
|
|
|
node.setMargin(YogaEdge.TOP, 3);
|
|
|
|
node.setMargin(YogaEdge.BOTTOM, 4);
|
|
|
|
node.calculateLayout();
|
|
|
|
|
|
|
|
assertEquals(1, (int) node.getLayoutMargin(YogaEdge.LEFT));
|
|
|
|
assertEquals(2, (int) node.getLayoutMargin(YogaEdge.RIGHT));
|
|
|
|
assertEquals(3, (int) node.getLayoutMargin(YogaEdge.TOP));
|
|
|
|
assertEquals(4, (int) node.getLayoutMargin(YogaEdge.BOTTOM));
|
|
|
|
}
|
|
|
|
|
2017-01-05 12:48:10 -08:00
|
|
|
@Test
|
|
|
|
public void testLayoutPadding() {
|
|
|
|
final YogaNode node = new YogaNode();
|
|
|
|
node.setWidth(100);
|
|
|
|
node.setHeight(100);
|
|
|
|
node.setPadding(YogaEdge.START, 1);
|
|
|
|
node.setPadding(YogaEdge.END, 2);
|
|
|
|
node.setPadding(YogaEdge.TOP, 3);
|
|
|
|
node.setPadding(YogaEdge.BOTTOM, 4);
|
|
|
|
node.calculateLayout();
|
|
|
|
|
|
|
|
assertEquals(1, (int) node.getLayoutPadding(YogaEdge.LEFT));
|
|
|
|
assertEquals(2, (int) node.getLayoutPadding(YogaEdge.RIGHT));
|
|
|
|
assertEquals(3, (int) node.getLayoutPadding(YogaEdge.TOP));
|
|
|
|
assertEquals(4, (int) node.getLayoutPadding(YogaEdge.BOTTOM));
|
|
|
|
}
|
2016-10-12 09:55:04 -07:00
|
|
|
}
|