Issue
Given I have a simple Android activity with the following view code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame"
android:keepScreenOn="true">
<!-- more views -->
</FrameLayout>
Then:
How can I test that this activity will keep it's screen on and prevents the tablet from sleep?
Sidenote:
If I set the FLAG_KEEP_SCREEN_ON
flag in the Activity (onCreate) like this:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Then I can easily read it via Robolectric like this:
public static int getWindowFlags(final Activity activity) throws Exception {
Class clazz = Class.forName(Window.class.getName());
Method m = clazz.getMethod("getForcedWindowFlags");
m.setAccessible(true);
return (int) m.invoke(activity.getWindow());
}
However, the flag is not being set when a single view within the view hierarchy defines the flag.
Do I need to iterate all views, just to find a view that defines this?
Solution
I searched Robolectric sources for this flag and didn't find anything. I've created issue for it.
You already found the workaround - set it programmatically in this case you can test it. And you can test it without reflection:
assertThat( shadowWindow.getFlag( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ), is( true ) );
Answered By - Eugen Martynov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.