2

私がテストしているビューにはアニメーションがあり、Robolectric でテストするとうまくいかないようです。アニメーションは、互換性のために Nineoldandroids を使用して簡単にスライドできます。Robolectric 以外ではすべて正常に動作しますが、単体テストが null ポインターでクラッシュします。

10:34:35.419 [DEBUG] [TestEventLogger]     java.lang.NullPointerException
10:34:35.419 [DEBUG] [TestEventLogger]      at com.nineoldandroids.view.animation.AnimatorProxy.applyTransformation(AnimatorProxy.java:316)
10:34:35.419 [DEBUG] [TestEventLogger]      at android.view.animation.Animation.getTransformation(Animation.java:870)
10:34:35.420 [DEBUG] [TestEventLogger]      at org.robolectric.shadows.ShadowView$2.run(ShadowView.java:492)
10:34:35.420 [DEBUG] [TestEventLogger]      at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:218)
10:34:35.420 [DEBUG] [TestEventLogger]      at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:73)
10:34:35.421 [DEBUG] [TestEventLogger]      at org.robolectric.shadows.ShadowChoreographer.postCallbackDelayed(ShadowChoreographer.java:44)
10:34:35.421 [DEBUG] [TestEventLogger]      at android.view.Choreographer.postCallbackDelayed(Choreographer.java)
10:34:35.427 [DEBUG] [TestEventLogger]      at org.robolectric.shadows.ShadowView.setAnimation(ShadowView.java:487)
10:34:35.429 [DEBUG] [TestEventLogger]      at android.view.View.setAnimation(View.java)
10:34:35.431 [DEBUG] [TestEventLogger]      at com.nineoldandroids.view.animation.AnimatorProxy.<init>(AnimatorProxy.java:66)
10:34:35.432 [DEBUG] [TestEventLogger]      at com.nineoldandroids.view.animation.AnimatorProxy.wrap(AnimatorProxy.java:38)
10:34:35.432 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.PreHoneycombCompat$14.get(PreHoneycombCompat.java:161)
10:34:35.432 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.PreHoneycombCompat$14.get(PreHoneycombCompat.java:153)
10:34:35.432 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:510)
10:34:35.433 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
10:34:35.433 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
10:34:35.433 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
10:34:35.433 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10:34:35.433 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10:34:35.433 [DEBUG] [TestEventLogger]      at com.nineoldandroids.animation.AnimatorSet.start(AnimatorSet.java:502)

コードにチェックを追加して、API <11 に nineoldandroids のみを使用し、デフォルトで Android ネイティブ アニメーションを使用すると、テストは問題なくパスします。nineoldandroids ソースから、次の行でヌル ポインターになっていることがわかります。

View view = mView.get();

mView の場所mView = new WeakReference<View>(view);

私のアプリコードではなく、テストケースから理想的にこれを回避する方法について何か提案はありますか?

4

1 に答える 1

2

起こっているように見えるのは、リスト要素をアニメーション化する Nineoldandroid アニメーションが、AnimationProxy完全に初期化される前に UI/メイン ルーパーによって実行されることです。これは、Robolectric 3 の Android スレッド アーキテクチャのシミュレーションが改善されたことが原因である可能性があります。

私はこれを呼び出すことで解決しました:

ShadowLooper.pauseMainLooper();

Nineoldandroids 内でアニメーションを開始できるメソッドの前にこの呼び出しを配置し​​ます。

アップデート:

これは私にとってより面倒であり、上記の回避策はアニメーションを実行するすべてのコードに展開するのが面倒だったので、フォークして修正しました。非常に小さな変更です:

https://github.com/apinder/NineOldAndroids/commit/6d399685dc5b3932e3b806d4d7d2f27123e1ca36

于 2015-07-05T14:52:35.163 に答える