2019-10-15 10:30:08 -07:00
|
|
|
/*
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-09-11 02:59:15 -07:00
|
|
|
*
|
2019-10-15 10:30:08 -07:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-09-11 02:59:15 -07:00
|
|
|
*/
|
2019-10-15 10:30:08 -07:00
|
|
|
|
2017-09-11 02:59:15 -07:00
|
|
|
package com.facebook.yoga;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
import java.lang.ref.WeakReference;
|
2020-07-06 03:41:19 -07:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.ArrayList;
|
2017-09-11 02:59:15 -07:00
|
|
|
|
2020-07-06 03:41:19 -07:00
|
|
|
import static org.junit.Assert.assertEquals;
|
2017-09-11 02:59:15 -07:00
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
|
|
public class YogaLoggerTest {
|
2020-07-06 03:41:19 -07:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testRemovingLoggerFromConfig() throws Exception {
|
|
|
|
final List<String> logs = new ArrayList<>();
|
|
|
|
|
|
|
|
final YogaConfig config = YogaConfigFactory.create();
|
|
|
|
YogaLogger logger = new YogaLogger() {
|
|
|
|
@Override
|
|
|
|
public void log(YogaLogLevel level, String message) {
|
|
|
|
logs.add(message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
config.setLogger(logger);
|
|
|
|
|
|
|
|
final YogaNode root = YogaNodeFactory.create(config);
|
|
|
|
root.setFlexDirection(YogaFlexDirection.ROW);
|
|
|
|
root.setAlignItems(YogaAlign.BASELINE);
|
|
|
|
|
|
|
|
final YogaNode child1 = YogaNodeFactory.create(config);
|
|
|
|
root.addChildAt(child1, 0);
|
|
|
|
|
|
|
|
final YogaNode child2 = YogaNodeFactory.create(config);
|
|
|
|
child2.setBaselineFunction(new YogaBaselineFunction() {
|
|
|
|
public float baseline(YogaNode node, float width, float height) {
|
|
|
|
return Float.NaN;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
root.addChildAt(child2, 1);
|
|
|
|
|
|
|
|
assertEquals(logs.size(), 0);
|
|
|
|
try {
|
|
|
|
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
|
|
|
|
fail("Expected calculateLayout to throw");
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(logs.size(), 1);
|
|
|
|
|
|
|
|
config.setLogger(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
|
|
|
|
fail("Expected calculateLayout to throw again");
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(logs.size(), 1);
|
|
|
|
}
|
|
|
|
|
2017-09-11 02:59:15 -07:00
|
|
|
@Test
|
|
|
|
public void testLoggerLeak() throws Exception {
|
2019-09-17 06:52:49 -07:00
|
|
|
final YogaConfig config = YogaConfigFactory.create();
|
2017-09-11 02:59:15 -07:00
|
|
|
YogaLogger logger = new YogaLogger() {
|
|
|
|
@Override
|
2019-09-25 09:10:51 -07:00
|
|
|
public void log(YogaLogLevel level, String message) {
|
2017-09-11 02:59:15 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
config.setLogger(logger);
|
|
|
|
config.setLogger(null);
|
|
|
|
WeakReference<Object> ref = new WeakReference<Object>(logger);
|
|
|
|
// noinspection UnusedAssignment
|
|
|
|
logger = null;
|
|
|
|
// try and free for the next 5 seconds, usually it works after the
|
|
|
|
// first GC attempt.
|
|
|
|
for (int i=0; i < 50; i++) {
|
|
|
|
System.gc();
|
|
|
|
if (ref.get() == null) {
|
|
|
|
// free successfully
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
fail("YogaLogger leaked");
|
|
|
|
}
|
|
|
|
}
|