您如何在Android Instrumentation测试中强制更改方向? [英] How do you force an orientation change in an Android Instrumentation test?

查看:110
本文介绍了您如何在Android Instrumentation测试中强制更改方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ActivityInstrumentationTestCase2类为应用程序编写一些验收测试.我想从测试中引起方向改变,以确保发生许多事情.其中包括确保保留Activity状态,但我还要确保使用适合方向的布局.

I'm writing some acceptance tests for an application using the ActivityInstrumentationTestCase2 class. I want to cause an orientation change from within the test to ensure that a number of things happen. Among these things are ensuring that Activity state is preserved, but also I'd like to ensure that the appropriate layout for the orientation is used.

我知道我可以简单地测试onSaveInstanceState/onRestoreInstanceState/onPause/onResume/etc.确保实例状态得以保留的方法.但是,我想知道实际上是否存在引起方向改变事件的机制?

I know I can simply test the onSaveInstanceState/onRestoreInstanceState/onPause/onResume/etc. methods to make sure instance state is preserved. However, I was wondering if there is actually a mechanism for causing an orientation change event?

这是否涉及注入某种运动事件以欺骗设备/仿真器以为它已经旋转,或者仪器是否提供了一种实际的方法?

Would this involve injecting some kind of motion event to trick the device/emulator into thinking that it has been rotated, or is there an actual method for this provided by the Instrumentation?

感谢&干杯!

推荐答案

您实际上根本不需要使用Robotium.实际上,如果您查看Robotium的来源,则在调用时会做所有

You do not actually have to use Robotium for this at all. In fact, if you view the source of Robotium all it is doing when you call

solo.setActivityOrientation(Solo.LANDSCAPE);

myActivity = this.getActivity(); // In your setUp method()

...

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

这篇关于您如何在Android Instrumentation测试中强制更改方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆