-
Notifications
You must be signed in to change notification settings - Fork 136
/
ApplicationContextUtilsTest.java
122 lines (92 loc) · 3.83 KB
/
ApplicationContextUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
package com.microsoft.appcenter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.UserManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
@RunWith(PowerMockRunner.class)
public class ApplicationContextUtilsTest {
@Mock
private Context mDeviceProtectedStorageContext;
@Mock
private Application mApplication;
@Mock
private UserManager mUserManager;
@Before
public void setUp() {
when(mApplication.isDeviceProtectedStorage()).thenReturn(false);
when(mApplication.createDeviceProtectedStorageContext()).thenReturn(mDeviceProtectedStorageContext);
when(mApplication.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mDeviceProtectedStorageContext.isDeviceProtectedStorage()).thenReturn(true);
}
@SuppressWarnings("InstantiationOfUtilityClass")
@Test
public void constructorCoverage() {
new ApplicationContextUtils();
}
@Test
public void contextOnNewLockedDevice() {
/* Mock SDK_INT to N. */
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.N);
/* When user is locked. */
when(mUserManager.isUserUnlocked()).thenReturn(false);
/* It should be device-protected storage context. */
assertEquals(mDeviceProtectedStorageContext, ApplicationContextUtils.getApplicationContext(mApplication));
/* Verify create protected storage. */
verify(mApplication).createDeviceProtectedStorageContext();
}
@Test
public void contextOnNewUnlockedDevice() {
/* Mock SDK_INT to N. */
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.N);
/* When user is unlocked. */
when(mUserManager.isUserUnlocked()).thenReturn(true);
/* It should be regular context. */
assertEquals(mApplication, ApplicationContextUtils.getApplicationContext(mApplication));
}
@Test
public void contextOnOldDevice() {
/* Mock SDK_INT to M. */
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.M);
/* When user is locked. */
when(mUserManager.isUserUnlocked()).thenReturn(false);
/* It should be regular context. */
assertEquals(mApplication, ApplicationContextUtils.getApplicationContext(mApplication));
}
@Test
public void checkDeviceProtectedStorageOnNewDevice() {
/* Mock SDK_INT to N. */
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.N);
/* It should rely on context. */
assertTrue(ApplicationContextUtils.isDeviceProtectedStorage(mDeviceProtectedStorageContext));
assertFalse(ApplicationContextUtils.isDeviceProtectedStorage(mApplication));
}
@Test
public void checkDeviceProtectedStorageOnOldDevice() {
/* Mock SDK_INT to M. */
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.M);
/* It cannot be device-protected storage on old devices. */
assertFalse(ApplicationContextUtils.isDeviceProtectedStorage(mApplication));
verifyNoInteractions(mApplication);
}
@After
public void tearDown() {
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", 0);
}
}