3

I'm trying to test my activity (HomeActivity) which has repeating animation based on NineOldAndroids lib with Espresso. I turned off system animation as described here, but it doesn't help and I'm getting an error (see below). The only thing that help is to remove animation manually. So question is do I need to manually turn off animations (using BuildConfig seems hassle free) or maybe I'm doing something wrong? Thank you in advance!

 java.lang.RuntimeException: Could not launch intent Intent {
 act=android.intent.action.MAIN flg=0x14000000
 cmp=com.package.en/com.package.ui.HomeActivity } within 45 seconds.
 Perhaps the main thread has not gone idle within a reasonable amount
 of time? There could be an animation or something constantly
 repainting the screen. Or the activity is doing network calls on
 creation? See the threaddump logs. For your reference the last time
 the event queue was idle before your activity launch request was
 1392052899081 and and now the last time the queue went idle was:
 1392052899081. If these numbers are the same your activity might be hogging the event 
 queue.
Rajendra arora
  • 2,186
  • 1
  • 16
  • 23
Roman
  • 898
  • 1
  • 10
  • 24

2 Answers2

3

Fix for issue:

    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        Intent intent = getIntent();
        if (intent == null) {
            intent = new Intent();
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        setActivityIntent(intent);
    }

    @SuppressLint("NewApi")
    @Override
    public T getActivity() {
        final T activity = super.getActivity();
        activity.overridePendingTransition(0, 0);
        return activity;
    }
Vova K.
  • 678
  • 6
  • 19
  • consider using [ActivityTestRule](http://developer.android.com/reference/android/support/test/rule/ActivityTestRule.html) to avoid using deprecated API and reduce boilerplate code. – Nick Korostelev Apr 15 '16 at 16:48
1

I don't know much about 9olddroids, but for Espresso you should disable animations in order to your tests become reliable, which you probably already did.

So maybe this is the case to increase your app "testability" by adding some code which disable animations. For instance, your activity could have a method to disable animations, like:

 public void disableAnimations() {
     this.mAnimationsEnabled = false;
 }

And before every animation, you check if they're enabled. Once your test begins, you disable your animations:

 public void setUp () {
    super.setUp();
     YourActivity activity = getActivity();
     activity.disableAnimations();
 }

 public void testXYZ() {
     // your test code
 }

I hope this will work, as 9OldDroids will stop interfering with Espresso

Bolhoso
  • 895
  • 1
  • 7
  • 14
  • Thank you for suggestion. I thought GoogleInstrumentationTestRunner handles it on system level – Roman Feb 17 '14 at 11:10
  • 1
    Actually no, your obliged to disable the animation on the emulator or programatically (with stock animations). Now I think this is specially true when using a thirdparty library. Did that worked for you? – Bolhoso Feb 17 '14 at 15:57
  • 1
    I used a bit different approach, but main idea it the same. The problem is that it requires a lot of boilerplate code if you have complex animation with listeners, etc. I'll wait for few days and if no one suggest better solution I accept your answer. – Roman Feb 17 '14 at 18:01
  • Great! Afterwords, I would appreciate if you edit your question and post your solution, so I can learn a little bit :) – Bolhoso Feb 18 '14 at 02:29
  • 1
    I simply used BuildConfig.DEBUG to turn off animation in debug mode. – Roman Feb 27 '14 at 06:29
  • 2
    Having to add code to your application that exists solely for the purposes of testing is so bad. This is a smell. – Christopher Perry May 07 '15 at 22:11