diff --git a/CSSLAYOUT_DEFS b/CSSLAYOUT_DEFS index 6c45022f..c73f54ff 100644 --- a/CSSLAYOUT_DEFS +++ b/CSSLAYOUT_DEFS @@ -9,6 +9,11 @@ GTEST_TARGET = '//lib/gtest:gtest' GTEST_DL_URL = 'https://github.com/google/googletest/archive/release-1.7.0.zip' JNI_DEPS = ['//lib/fb:fbjni'] +CXX_LIBRARY_WHITELIST = [ + '//lib/fb:fbjni', + '//java:jni', +] + def csslayout_dep(dep): return '//' + dep diff --git a/java/BUCK b/java/BUCK index 84b79bc5..67c1217e 100644 --- a/java/BUCK +++ b/java/BUCK @@ -43,3 +43,14 @@ java_library( ], visibility = ['PUBLIC'], ) + +java_test( + name = 'tests', + srcs = glob(['tests/**/*.java']), + deps = [ + ':java', + JUNIT_TARGET, + ], + use_cxx_libraries = True, + cxx_library_whitelist = CXX_LIBRARY_WHITELIST, +) diff --git a/java/com/facebook/csslayout/CSSNode.java b/java/com/facebook/csslayout/CSSNode.java index a065c2e7..935be095 100644 --- a/java/com/facebook/csslayout/CSSNode.java +++ b/java/com/facebook/csslayout/CSSNode.java @@ -29,6 +29,11 @@ public class CSSNode implements CSSNodeAPI { } } + /** + * Get native instance count. Useful for testing only. + */ + static native int jni_CSSNodeGetInstanceCount(); + private CSSNode mParent; private List mChildren; private MeasureFunction mMeasureFunction; diff --git a/java/jni/CSSJNI.cpp b/java/jni/CSSJNI.cpp index d75e37f4..41e4198e 100644 --- a/java/jni/CSSJNI.cpp +++ b/java/jni/CSSJNI.cpp @@ -41,6 +41,10 @@ static inline CSSNodeRef _jlong2CSSNodeRef(jlong addr) { return reinterpret_cast(static_cast(addr)); } +jint jni_CSSNodeGetInstanceCount(alias_ref clazz) { + return CSSNodeGetInstanceCount(); +} + jlong jni_CSSNodeNew(alias_ref thiz) { const CSSNodeRef node = CSSNodeNew(); auto globalThiz = make_global(thiz); @@ -234,6 +238,8 @@ jint JNI_OnLoad(JavaVM *vm, void *) { CSSMakeNativeMethod(jni_CSSNodeLayoutGetWidth), CSSMakeNativeMethod(jni_CSSNodeLayoutGetHeight), CSSMakeNativeMethod(jni_CSSNodeLayoutGetDirection), + + CSSMakeNativeMethod(jni_CSSNodeGetInstanceCount), }); }); } diff --git a/java/tests/com/facebook/csslayout/CSSNodeTest.java b/java/tests/com/facebook/csslayout/CSSNodeTest.java new file mode 100644 index 00000000..a2d223df --- /dev/null +++ b/java/tests/com/facebook/csslayout/CSSNodeTest.java @@ -0,0 +1,61 @@ +/** + * 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. + */ + +package com.facebook.csslayout; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CSSNodeTest { + + @Test + public void testInit() { + final int refCount = CSSNode.jni_CSSNodeGetInstanceCount(); + final CSSNode node = new CSSNode(); + assertEquals(refCount + 1, CSSNode.jni_CSSNodeGetInstanceCount()); + } + + @Test + public void testFree() { + final int refCount = CSSNode.jni_CSSNodeGetInstanceCount(); + final CSSNode node = new CSSNode(); + node.free(); + assertEquals(refCount, CSSNode.jni_CSSNodeGetInstanceCount()); + } + + @Test + public void testReinit() { + final int refCount = CSSNode.jni_CSSNodeGetInstanceCount(); + final CSSNode node = new CSSNode(); + node.free(); + node.reinit(); + assertEquals(refCount + 1, CSSNode.jni_CSSNodeGetInstanceCount()); + } + + @Test + public void testMeasure() { + final CSSNode node = new CSSNode(); + node.setMeasureFunction(new CSSNodeAPI.MeasureFunction() { + public void measure( + CSSNodeAPI node, + float width, + CSSMeasureMode widthMode, + float height, + CSSMeasureMode heightMode, + MeasureOutput measureOutput) { + measureOutput.width = 100; + measureOutput.height = 100; + } + }); + node.calculateLayout(null); + assertEquals(100, (int) node.getLayoutWidth()); + assertEquals(100, (int) node.getLayoutHeight()); + } +}