Skip to content

Commit 6d783cd

Browse files
Merge pull request #1527 from smartdevicelink/feature/lockscreen_ui_testing
Feature/lockscreen ui testing
2 parents d7e54de + 4628386 commit 6d783cd

3 files changed

Lines changed: 262 additions & 6 deletions

File tree

android/sdl_android/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ dependencies {
4343
api fileTree(dir: 'libs', include: ['*.jar'])
4444
api 'com.smartdevicelink:bson_java_port:RC1_1.2.2'
4545
api 'com.livio.taskmaster:taskmaster:0.3.0'
46-
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
47-
exclude group: 'com.android.support', module: 'support-annotations'
48-
})
46+
api 'androidx.lifecycle:lifecycle-extensions:2.2.0'
47+
api 'androidx.annotation:annotation:1.1.0'
48+
annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0'
49+
4950
testImplementation 'junit:junit:4.12'
5051
testImplementation 'org.mockito:mockito-core:3.0.0'
5152
androidTestImplementation 'org.mockito:mockito-core:3.0.0'
5253
androidTestImplementation 'org.mockito:mockito-android:3.0.0'
5354
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
54-
api 'androidx.annotation:annotation:1.1.0'
55-
api 'androidx.lifecycle:lifecycle-extensions:2.2.0'
56-
annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0'
55+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
56+
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
5757
}
5858

5959
buildscript {

android/sdl_android/src/androidTest/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<application android:debuggable="true">
1818
<uses-library android:name="android.test.runner" />
19+
<activity android:name=".managers.lockscreen.SDLLockScreenActivity"/>
1920
</application>
2021

2122
</manifest>
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
package com.smartdevicelink.managers.lockscreen;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.IntentFilter;
7+
import android.os.Handler;
8+
import android.os.Looper;
9+
10+
import androidx.test.espresso.action.ViewActions;
11+
import androidx.test.espresso.intent.rule.IntentsTestRule;
12+
import androidx.test.espresso.matcher.ViewMatchers;
13+
import androidx.test.ext.junit.rules.ActivityScenarioRule;
14+
import androidx.test.ext.junit.runners.AndroidJUnit4;
15+
import androidx.test.filters.LargeTest;
16+
17+
import com.smartdevicelink.R;
18+
import com.smartdevicelink.managers.CompletionListener;
19+
import com.smartdevicelink.managers.ISdl;
20+
import com.smartdevicelink.protocol.enums.FunctionID;
21+
import com.smartdevicelink.proxy.rpc.OnDriverDistraction;
22+
import com.smartdevicelink.proxy.rpc.OnHMIStatus;
23+
import com.smartdevicelink.proxy.rpc.enums.DriverDistractionState;
24+
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
25+
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
26+
27+
import org.junit.Ignore;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.mockito.invocation.InvocationOnMock;
32+
import org.mockito.stubbing.Answer;
33+
34+
import static androidx.test.espresso.Espresso.onView;
35+
import static androidx.test.espresso.assertion.ViewAssertions.matches;
36+
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
37+
import static androidx.test.espresso.matcher.ViewMatchers.withText;
38+
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
39+
import static org.junit.Assert.assertEquals;
40+
import static org.junit.Assert.fail;
41+
import static org.mockito.ArgumentMatchers.any;
42+
import static org.mockito.ArgumentMatchers.eq;
43+
import static org.mockito.Mockito.doAnswer;
44+
import static org.mockito.Mockito.mock;
45+
46+
47+
//These tests are used to ensure the lockScreen UI Behavior
48+
//They are ignored for CICD purposes and should be run manually during release testing
49+
@RunWith(AndroidJUnit4.class)
50+
@LargeTest
51+
@Ignore //Remove this annotation before running these tests
52+
public class SDLLockScreenActivityEspressoTest {
53+
54+
private OnRPCNotificationListener onDDListener;
55+
private OnRPCNotificationListener onHMIListener;
56+
57+
@Rule
58+
public ActivityScenarioRule<SDLLockScreenActivity> activityRule =
59+
new ActivityScenarioRule<>(SDLLockScreenActivity.class);
60+
61+
@Rule
62+
public IntentsTestRule<SDLLockScreenActivity> intentsTestRule =
63+
new IntentsTestRule<>(SDLLockScreenActivity.class);
64+
65+
@Test
66+
public void test1() {
67+
testLockScreenBehavior(DriverDistractionState.DD_OFF, null, true, null, false);
68+
}
69+
70+
@Test
71+
public void test2() {
72+
testLockScreenBehavior(DriverDistractionState.DD_OFF, false, true, null, false);
73+
}
74+
75+
@Test
76+
public void test3() {
77+
testLockScreenBehavior(DriverDistractionState.DD_OFF, true, true, null, true);
78+
}
79+
80+
@Test
81+
public void test4() {
82+
testLockScreenBehavior(DriverDistractionState.DD_OFF, null, false, false, false);
83+
}
84+
85+
@Test
86+
public void test5() {
87+
testLockScreenBehavior(DriverDistractionState.DD_OFF, null, false, true, true);
88+
}
89+
90+
@Test
91+
public void test6() {
92+
testLockScreenBehavior(DriverDistractionState.DD_OFF, false, false, false, false);
93+
}
94+
95+
@Test
96+
public void test7() {
97+
testLockScreenBehavior(DriverDistractionState.DD_OFF, false, false, true, false);
98+
}
99+
100+
@Test
101+
public void test8() {
102+
testLockScreenBehavior(DriverDistractionState.DD_OFF, true, false, true, true);
103+
}
104+
105+
@Test
106+
public void test9() {
107+
testLockScreenBehavior(DriverDistractionState.DD_ON, true, false, false, true);
108+
}
109+
110+
@Test
111+
public void test10() {
112+
testLockScreenBehavior(DriverDistractionState.DD_ON, null, true, null, false);
113+
}
114+
115+
@Test
116+
public void test11() {
117+
testLockScreenBehavior(DriverDistractionState.DD_ON, false, true, null, false);
118+
}
119+
120+
@Test
121+
public void test12() {
122+
testLockScreenBehavior(DriverDistractionState.DD_ON, true, true, null, true);
123+
}
124+
125+
@Test
126+
public void test13() {
127+
testLockScreenBehavior(DriverDistractionState.DD_ON, null, false, false, false);
128+
}
129+
130+
@Test
131+
public void test14() {
132+
testLockScreenBehavior(DriverDistractionState.DD_ON, null, false, true, true);
133+
}
134+
135+
@Test
136+
public void test15() {
137+
testLockScreenBehavior(DriverDistractionState.DD_ON, false, false, false, false);
138+
}
139+
140+
@Test
141+
public void test16() {
142+
testLockScreenBehavior(DriverDistractionState.DD_ON, false, false, true, false);
143+
}
144+
145+
@Test
146+
public void test17() {
147+
testLockScreenBehavior(DriverDistractionState.DD_ON, true, false, true, true);
148+
}
149+
150+
@Test
151+
public void test18() {
152+
testLockScreenBehavior(DriverDistractionState.DD_ON, true, false, false, true);
153+
}
154+
155+
public void testLockScreenBehavior(final DriverDistractionState dd, final Boolean lockScreenDismissibility, final boolean firstDD, final Boolean previousLockScreenDismissibility, final boolean dismissEnabled) {
156+
LockScreenConfig lockScreenConfig = new LockScreenConfig();
157+
lockScreenConfig.setDisplayMode(LockScreenConfig.DISPLAY_MODE_ALWAYS);
158+
lockScreenConfig.enableDismissGesture(true);
159+
lockScreenConfig.setCustomView(0);
160+
lockScreenConfig.setAppIcon(0);
161+
162+
if (Looper.myLooper() == null) {
163+
Looper.prepare();
164+
}
165+
LockScreenManager lockScreenManager = setupLockScreenManager(lockScreenConfig);
166+
167+
lockScreenManager.start(new CompletionListener() {
168+
@Override
169+
public void onComplete(boolean success) {
170+
OnHMIStatus onHMIStatus = new OnHMIStatus();
171+
onHMIStatus.setHmiLevel(HMILevel.HMI_FULL);
172+
onHMIListener.onNotified(onHMIStatus);
173+
174+
if (!firstDD) {
175+
OnDriverDistraction firstOnDriverDistraction = new OnDriverDistraction();
176+
if (dd == DriverDistractionState.DD_OFF) {
177+
firstOnDriverDistraction.setState(DriverDistractionState.DD_ON);
178+
} else {
179+
firstOnDriverDistraction.setState(DriverDistractionState.DD_OFF);
180+
}
181+
182+
if (previousLockScreenDismissibility != null) {
183+
firstOnDriverDistraction.setLockscreenDismissibility(previousLockScreenDismissibility);
184+
}
185+
186+
onDDListener.onNotified(firstOnDriverDistraction);
187+
}
188+
189+
OnDriverDistraction onDriverDistraction = new OnDriverDistraction();
190+
if (lockScreenDismissibility != null) {
191+
onDriverDistraction.setLockscreenDismissibility(lockScreenDismissibility);
192+
}
193+
onDriverDistraction.setState(dd);
194+
onDDListener.onNotified(onDriverDistraction);
195+
196+
197+
new Handler().postDelayed(new Runnable() {
198+
@Override
199+
public void run() {
200+
if (dismissEnabled) {
201+
onView(withText(R.string.default_lockscreen_warning_message)).check(matches(isDisplayed()));
202+
} else {
203+
onView(withText(R.string.lockscreen_text)).check(matches(isDisplayed()));
204+
}
205+
206+
BroadcastReceiver receiver = new BroadcastReceiver() {
207+
@Override
208+
public void onReceive(Context context, Intent intent) {
209+
if (dismissEnabled) {
210+
assertEquals(intent.getAction(), SDLLockScreenActivity.KEY_LOCKSCREEN_DISMISSED);
211+
} else {
212+
//Activity should not be dismissible test failed due to lock screen being dismissed
213+
fail();
214+
}
215+
}
216+
};
217+
218+
intentsTestRule.getActivity().registerReceiver(receiver, new IntentFilter(SDLLockScreenActivity.KEY_LOCKSCREEN_DISMISSED));
219+
220+
onView(ViewMatchers.withId(R.id.lockscreen_linear_layout)).perform(ViewActions.swipeDown());
221+
}
222+
}, 1000);
223+
}
224+
});
225+
}
226+
227+
private LockScreenManager setupLockScreenManager(LockScreenConfig lockScreenConfig) {
228+
ISdl internalInterface = mock(ISdl.class);
229+
230+
Answer<Void> onDDStatusAnswer = new Answer<Void>() {
231+
@Override
232+
public Void answer(InvocationOnMock invocation) {
233+
Object[] args = invocation.getArguments();
234+
onDDListener = (OnRPCNotificationListener) args[1];
235+
return null;
236+
}
237+
};
238+
239+
Answer<Void> onHMIStatusAnswer = new Answer<Void>() {
240+
@Override
241+
public Void answer(InvocationOnMock invocation) {
242+
Object[] args = invocation.getArguments();
243+
onHMIListener = (OnRPCNotificationListener) args[1];
244+
return null;
245+
}
246+
};
247+
248+
doAnswer(onDDStatusAnswer).when(internalInterface).addOnRPCNotificationListener(eq(FunctionID.ON_DRIVER_DISTRACTION), any(OnRPCNotificationListener.class));
249+
doAnswer(onHMIStatusAnswer).when(internalInterface).addOnRPCNotificationListener(eq(FunctionID.ON_HMI_STATUS), any(OnRPCNotificationListener.class));
250+
251+
Context context = getInstrumentation().getContext();
252+
253+
return new LockScreenManager(lockScreenConfig, context, internalInterface);
254+
}
255+
}

0 commit comments

Comments
 (0)