Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic implementation of ShadowProcess #861

Merged
merged 1 commit into from Dec 13, 2013

Conversation

Projects
None yet
3 participants
@benelog
Copy link
Contributor

commented Dec 4, 2013

ShadowProcess can be used when mocking android.os.Process.myPid().

I found a identical class from the android git repository.

https://android.googlesource.com/platform/external/robolectric/+/df5951775e9c13526de56166ccdb841aa910a674%5E/src/main/java/com/xtremelabs/robolectric/shadows/ShadowProcess.java

@SuperJugy

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

What happens if this shadow doesn't exist?

@benelog

This comment has been minimized.

Copy link
Contributor Author

commented Dec 4, 2013

'android.os.Process.myPid()' will return 0.

Sometimes it's enough. But it limits testability sometimes.

For examples, when I write the following method

public static boolean isContextForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    int pid = android.os.Process.myPid();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.pid == pid) {
            return appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
        }
    }
    return false;
}

I tested it as following

@Test
public void shouldBeTrueWhenThisContextIsForeground(){
    // android.os.Process.myPid() returns 0 in Robolectric
    createRunningAppProcessInfo(0);
    boolean foreground = ActivityUtils.isContextForeground(context);
    assertThat(foreground, is(true));
}

private void createRunningAppProcessInfo(int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ShadowActivityManager shadowAm = Robolectric.shadowOf(am);

    RunningAppProcessInfo proc = new RunningAppProcessInfo();
    proc.importance = RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
    proc.pid = pid;
    shadowAm.setProcesses(Arrays.asList(proc));
}

With ShadowProcess, it is possible to set pid to non-zero value. It can produce more test conditions.

@Test
public void shouldBeTrueWhenThisContextIsForeground(){
    int pid = 3;
    ShadowProcess.setPid(pid);
    createRunningAppProcessInfo(pid);
    boolean foreground = ActivityUtils.isContextForeground(context);
    assertThat(foreground, is(true));
}
@benelog

This comment has been minimized.

Copy link
Contributor Author

commented Dec 12, 2013

Rebased to the current HEAD of the master.

erd added a commit that referenced this pull request Dec 13, 2013

Merge pull request #861 from benelog/ShadowProcess
Add basic implementation of ShadowProcess

@erd erd merged commit 9d2722f into robolectric:master Dec 13, 2013

1 check passed

default The Travis CI build passed
Details

@benelog benelog deleted the benelog:ShadowProcess branch Dec 13, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.