Files
react-native/ReactAndroid/src/main/java/com/facebook/react/animated/StyleAnimatedNode.java
Krzysztof Magiera 21b3180a4c Java unit tests for native animated module.
Summary:This change adds some basic unit tests for native animated traversal algorithm. The following tests are added:
1) Build simple animated nodes graph, verify that frame-based animation execute updates and when it runs out of the frames we no longer schedule updates for the native view
2) Build simple animated nodes graph and start short timing animation, verify that animation JS callback gets called.

As a part of this change I'm fixing an issue that tests allowed me to discover, where I forgot to clear updates queue at the end of `runUpdates` method. It was causing the view to be updated even if there was no active animation for it (actually it was mitigated by another bug in `hasActiveAnimations` I'm fixing here too).

I'm also adding Nullable annotation in a bunch of places. To lazy to send it as a separate change - sorry.

Going forward I'm planning on adding more tests. Currently the number of nodes is pretty limited so it's difficult to construct more complex graphs, but once I land Add/Multiply
Closes https://github.com/facebook/react-native/pull/6858

Differential Revision: D3168549

Pulled By: astreet

fb-gh-sync-id: 5295c75f3c7817775b5154bb808888650ff74e12
fbshipit-source-id: 5295c75f3c7817775b5154bb808888650ff74e12
2016-04-12 10:09:30 -07:00

55 lines
1.9 KiB
Java

/**
* Copyright (c) 2015-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.react.animated;
import com.facebook.react.bridge.JavaOnlyMap;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Native counterpart of style animated node (see AnimatedStyle class in AnimatedImplementation.js)
*/
/*package*/ class StyleAnimatedNode extends AnimatedNode {
private final NativeAnimatedNodesManager mNativeAnimatedNodesManager;
private final Map<String, Integer> mPropMapping;
StyleAnimatedNode(ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) {
ReadableMap style = config.getMap("style");
ReadableMapKeySetIterator iter = style.keySetIterator();
mPropMapping = new HashMap<>();
while (iter.hasNextKey()) {
String propKey = iter.nextKey();
int nodeIndex = style.getInt(propKey);
mPropMapping.put(propKey, nodeIndex);
}
mNativeAnimatedNodesManager = nativeAnimatedNodesManager;
}
public void collectViewUpdates(JavaOnlyMap propsMap) {
for (Map.Entry<String, Integer> entry : mPropMapping.entrySet()) {
@Nullable AnimatedNode node = mNativeAnimatedNodesManager.getNodeById(entry.getValue());
if (node == null) {
throw new IllegalArgumentException("Mapped style node does not exists");
} else if (node instanceof ValueAnimatedNode) {
propsMap.putDouble(entry.getKey(), ((ValueAnimatedNode) node).mValue);
} else {
throw new IllegalArgumentException("Unsupported type of node used in property node " +
node.getClass());
}
}
}
}