简体   繁体   English

如何模拟自定义ImageView进行活动单元测试

[英]How to mock a custom ImageView for activity unit testing

I have a LoginActivity which loads a SplashFragment in onResume() . 我有一个LoginActivity ,它在onResume()加载了SplashFragment

The layout XML of the splash fragment contains a custom ImageView called RotatingImageView which, as the name suggests, starts rotating a source image as soon as it's attached to the Window. 初始片段的布局XML包含一个名为RotatingImageView的自定义ImageView ,顾名思义,该ImageView在将源图像附加到Window后即开始旋转。

Now this is causing problems when I try to write a unit test for LoginActivity using ActivityInstrumentationTestCase2<LoginActivity> . 现在,当我尝试使用ActivityInstrumentationTestCase2<LoginActivity>编写LoginActivity的单元测试时,这会引起问题。 I'm trying to run the tests on a real device(unrooted), not an emulator. 我正在尝试在真实设备(无根)而非模拟器上运行测试。

The getActivity() call starts the activity but because of the rotating image view (which is an animation btw), the espresso is stuck. getActivity()调用开始活动,但是由于旋转的图像视图(动画是顺便说一句),浓缩咖啡被卡住了。 I know that espresso doesn't like an animation going on there. 我知道意式浓缩咖啡不喜欢在那里播放动画。 I get exceptions that 我有例外

"espresso couldn't launch intent 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 thread dump logs. For your reference the last time the event queue was idle before your activity launch request was 1487296262885 and now the last time the queue went idle was: 1487296262885. If these numbers are the same your activity might be hogging the event queue..." “浓缩咖啡无法在45秒内启动意图。也许主线程在合理的时间内没有空闲?可能存在动画或不断在重新绘制屏幕的东西。或者活动正在创建时进行网络调用?请参阅作为参考,事件队列在活动启动请求之前的最后一次空闲时间为1487296262885,现在队列的最后空闲时间为:1487296262885。如果这些数字相同,则您的活动可能会占用事件队列。 ..”

Now obviously the problem is to somehow stop/mock the animation. 现在显然问题是要以某种方式停止/模拟动画。

  1. I have already disabled all animation options from Device -> Settings -> Developer Options but still when activity is launched I see the image spinning so this doesn't help. 我已经从设备->设置->开发人员选项中禁用了所有动画选项,但是仍然在启动活动时看到图像旋转,所以这无济于事。

  2. I've also looked into Disable animations for Espresso tests but this hasn't helped me much either. 我也研究了禁用动画以进行Espresso测试,但这也没有太大帮助。 I could have been doing it wrong but anyways, it's not helping. 我本来可能做错了,但无论如何,这没有帮助。

  3. Another option is to launch LoginActivity using a special intent which tells the activity that this is launching under test so when you load the fragment it disables the animation. 另一个选择是使用特殊意图启动LoginActivity ,该意图告诉活动它正在测试中启动,因此当您加载片段时,它将禁用动画。 This method works but it's not ideal because it involves adding code in the main class which is purely for testing. 该方法有效,但是并不理想,因为它涉及在主类中添加纯粹用于测试的代码。

One other solution could be to mock RotatingImageView and inject it into SplashFragment before it starts loading. 另一种解决方案是模拟RotatingImageView ,然后在开始加载之前将其注入SplashFragment I would have mocked out the call to startSpinningAnimation so when it's loaded into the Window it won't start the animation. 我本来可以嘲笑对startSpinningAnimation的调用,所以当它加载到Window中时将不会启动动画。

My question is: Is it possible? 我的问题是:可能吗? Can I mock and inject this custom imageView into my fragment somehow, before the call to getActivity() is done? 在完成对getActivity()的调用之前,是否可以以某种方式将此自定义imageView模拟并注入到片段中?

Yes, it is possible, you can create a class called AnimationUtil , put your animation methods in that class and mock them during test. 是的,有可能,您可以创建一个名为AnimationUtil的类,将您的动画方法放入该类中,并在测试过程中对其进行模拟。

public Animation getWhateverAnimation(int duration){
    RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(duration);
    return anim;
}

and create a MockAnimationUtil that extend AnimationUtil in your androidTest package(not main package) and override and method. 并在您的androidTest包(不是main包)中创建一个extend AnimationUtilMockAnimationUtil ,并重写和方法。

public Animation getWhateverAnimation(int duration){
    return super.getWhateverAnimation(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM