CrossFadeAnimation implements MirageAnimation<View> { @Override public boolean animate(View view, Drawable targetDrawable, Mirage.Source source) { if (source == Mirage.Source.MEMORY) return false; Drawable currentDrawable = view.getBackground(); if (currentDrawable == null) { currentDrawable = new ColorDrawable(Color.TRANSPARENT); } Drawable[] fades = new Drawable[2]; fades[0] = currentDrawable; fades[1] = targetDrawable; TransitionDrawable transitionDrawable = new TransitionDrawable(fades); transitionDrawable.setCrossFadeEnabled(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(transitionDrawable); } else { view.setBackgroundDrawable(transitionDrawable); } transitionDrawable.startTransition(duration); return true; } CrossFadeAnimation();  CrossFadeAnimation(int duration); @Override boolean animate(View view, Drawable targetDrawable, Mirage.Source source);  }
@SuppressLint("NewApi") @Test public void testImageViewTarget() { CrossFadeAnimation anim = new CrossFadeAnimation(); View view = Mockito.mock(View.class); boolean setsDirectly = anim.animate(view, new ColorDrawable(Color.BLUE), Mirage.Source.DISK); Assert.assertTrue(setsDirectly); Mockito.verify(view, Mockito.times(1)).getBackground(); Mockito.verify(view, Mockito.times(1)).setBackground((Drawable) Mockito.any()); }
@Test public void testSourceAsMemory() { CrossFadeAnimation anim = new CrossFadeAnimation(); View view = Mockito.mock(View.class); boolean setsDirectly = anim.animate(view, new ColorDrawable(Color.BLUE), Mirage.Source.MEMORY); Assert.assertFalse(setsDirectly); Mockito.verify(view, Mockito.times(0)).getBackground(); }