如何在 Android Instrumentation 测试中强制改变方向?

发布于 2024-09-14 18:58:21 字数 312 浏览 4 评论 0原文

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

我知道我可以简单地测试 onSaveInstanceState/onRestoreInstanceState/onPause/onResume/etc。确保保留实例状态的方法。但是,我想知道是否真的有一种机制可以引起方向改变事件?

这是否涉及注入某种运动事件来欺骗设备/模拟器使其认为它已旋转,或者仪器是否提供了实际的方法?

谢谢&干杯!

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.

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?

Thanks & Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏至、离别 2024-09-21 18:58:21

实际上,您根本不必使用 Robotium 来实现此目的。事实上,如果你查看 Robotium 的源代码,当你调用时它所做的

solo.setActivityOrientation(Solo.LANDSCAPE);

就是

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

...

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

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);

is

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

...

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
难如初 2024-09-21 18:58:21

正如 AndrewKS 所写,您可以使用

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
assertTrue(...);

请求更改方向。但旋转本身是异步执行的。要真正测试方向更改后的状态,您需要在请求后等待一小会儿:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Thread.sleep(50); // depends on performance of the testing device/emulator
assertTrue(...);

As AndrewKS wrote you can use

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
assertTrue(...);

to request an orientation change. But the rotation itself is executed asynchronous. To really test the state after the orientation change you need to wait a short time after the request:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Thread.sleep(50); // depends on performance of the testing device/emulator
assertTrue(...);
终陌 2024-09-21 18:58:21

使用 Robotium 来实现它。
有一个名为 Solo 的类,使用它您只需调用一个方法即可更改方向:

solo.setActivityOrientation(Solo.LANDSCAPE);

就是这样!你的方向将会改变。
您可以 google Robotium 并获取其 jar 并将其添加到您的测试项目中。 Robotium 网站还提供了 Android 记事本应用程序上的示例测试项目(可作为 Android SDK 的示例项目),展示了它的强大功能以及使用的方便程度。

Use Robotium for it.
There is a class called Solo, using which you can change orientation by just calling a method:

solo.setActivityOrientation(Solo.LANDSCAPE);

That's it! Your orientation would get changed.
You can google Robotium and obtain its jar and add it to your Test project. The Robotium site also gives an example Test project on Android's Notepad App (which is available as a sample project with Android SDK) which shows how powerful it is and how easily it could be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文