Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android unit-testing when activity has animation

I'm writing a unit tests for activity with animation. Animation is simple rotation of spinner drawable with infinite duration(splash screen).

When i start unit tests, every test seems to wait until animation is finished, so they stucks infinte, When i set duration to 1 it all passes, cause animation ends quickly.

How can I override that, so tests don't wait for animation ending?

like image 549
tapchicoma Avatar asked Nov 14 '22 14:11

tapchicoma


1 Answers

I assume that you intend to test behavior other than the animation.

You will probably benefit the most from moving the animation off the path of the code you want to test. This means moving the other code--code that doesn't depend on the animation--out of the Activity. If not out of the Activity, then, at least out of the Activity lifecycle methods (onCreate(), ...). You can then check this behavior without running the behavior at all. Even better would be to run the behavior without starting the Activity at all (don't call onCreate()).

The problem you encountered is a special case of what happens when you put your code directly into a framework extension point: your code then becomes intermingled with the framework and you get stuck running something that you don't care about in order to run the part that you do care about. So don't do that. Put your code in classes that don't depend on the framework, then use the framework extension point (the Activity in this case) to connect Android to your code. The Activity knows that your code exists; your code remains blissfully ignorant of Android. Now things like your animation can't get in the way.

like image 79
J. B. Rainsberger Avatar answered Nov 16 '22 02:11

J. B. Rainsberger