-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSettingsSecurityFragmentTest.kt
More file actions
499 lines (410 loc) · 16.5 KB
/
SettingsSecurityFragmentTest.kt
File metadata and controls
499 lines (410 loc) · 16.5 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
* openCloud Android client application
*
* @author Juan Carlos Garrote Gascón
*
* Copyright (C) 2021 ownCloud GmbH.
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.opencloud.android.settings.security
import android.app.Activity.RESULT_OK
import android.content.Context
import androidx.biometric.BiometricViewModel
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.preference.CheckBoxPreference
import androidx.preference.ListPreference
import androidx.preference.PreferenceManager
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isEnabled
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import eu.opencloud.android.R
import eu.opencloud.android.presentation.security.biometric.BiometricActivity
import eu.opencloud.android.presentation.security.biometric.BiometricManager
import eu.opencloud.android.presentation.security.PREFERENCE_LOCK_TIMEOUT
import eu.opencloud.android.presentation.security.pattern.PatternActivity
import eu.opencloud.android.presentation.security.passcode.PassCodeActivity
import eu.opencloud.android.presentation.settings.security.SettingsSecurityFragment
import eu.opencloud.android.presentation.settings.security.SettingsSecurityFragment.Companion.PREFERENCE_LOCK_ACCESS_FROM_DOCUMENT_PROVIDER
import eu.opencloud.android.presentation.settings.security.SettingsSecurityViewModel
import eu.opencloud.android.testutil.security.OC_PATTERN
import eu.opencloud.android.utils.matchers.verifyPreference
import eu.opencloud.android.utils.mockIntent
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import org.hamcrest.Matchers.not
import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.dsl.module
class SettingsSecurityFragmentTest {
private lateinit var fragmentScenario: FragmentScenario<SettingsSecurityFragment>
private lateinit var prefPasscode: CheckBoxPreference
private lateinit var prefPattern: CheckBoxPreference
private var prefBiometric: CheckBoxPreference? = null
private lateinit var prefLockApplication: ListPreference
private lateinit var prefLockAccessDocumentProvider: CheckBoxPreference
private lateinit var prefTouchesWithOtherVisibleWindows: CheckBoxPreference
private lateinit var securityViewModel: SettingsSecurityViewModel
private lateinit var biometricViewModel: BiometricViewModel
private lateinit var context: Context
@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().targetContext
securityViewModel = mockk(relaxed = true)
biometricViewModel = mockk(relaxed = true)
mockkObject(BiometricManager)
stopKoin()
startKoin {
context
allowOverride(override = true)
modules(
module {
viewModel {
securityViewModel
}
}
)
}
every { securityViewModel.isSecurityEnforcedEnabled() } returns false
every { securityViewModel.getBiometricsState() } returns false
every { securityViewModel.isLockDelayEnforcedEnabled() } returns false
Intents.init()
}
@After
fun tearDown() {
Intents.release()
PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit()
}
private fun launchTest(withBiometrics: Boolean = true) {
every { BiometricManager.isHardwareDetected() } returns withBiometrics
fragmentScenario = launchFragmentInContainer(themeResId = R.style.Theme_openCloud)
fragmentScenario.onFragment { fragment ->
prefPasscode = fragment.findPreference(PassCodeActivity.PREFERENCE_SET_PASSCODE)!!
prefPattern = fragment.findPreference(PatternActivity.PREFERENCE_SET_PATTERN)!!
prefBiometric = fragment.findPreference(BiometricActivity.PREFERENCE_SET_BIOMETRIC)
prefLockApplication = fragment.findPreference(PREFERENCE_LOCK_TIMEOUT)!!
prefLockAccessDocumentProvider = fragment.findPreference(PREFERENCE_LOCK_ACCESS_FROM_DOCUMENT_PROVIDER)!!
prefTouchesWithOtherVisibleWindows =
fragment.findPreference(SettingsSecurityFragment.PREFERENCE_TOUCHES_WITH_OTHER_VISIBLE_WINDOWS)!!
}
}
private fun checkCommonPreferences() {
prefPasscode.verifyPreference(
keyPref = PassCodeActivity.PREFERENCE_SET_PASSCODE,
titlePref = context.getString(R.string.prefs_passcode),
visible = true,
enabled = true
)
assertFalse(prefPasscode.isChecked)
prefPattern.verifyPreference(
keyPref = PatternActivity.PREFERENCE_SET_PATTERN,
titlePref = context.getString(R.string.prefs_pattern),
visible = true,
enabled = true
)
assertFalse(prefPattern.isChecked)
prefLockApplication.verifyPreference(
keyPref = PREFERENCE_LOCK_TIMEOUT,
titlePref = context.getString(R.string.prefs_lock_application),
visible = true,
enabled = false
)
prefLockAccessDocumentProvider.verifyPreference(
keyPref = PREFERENCE_LOCK_ACCESS_FROM_DOCUMENT_PROVIDER,
titlePref = context.getString(R.string.prefs_lock_access_from_document_provider),
summaryPref = context.getString(R.string.prefs_lock_access_from_document_provider_summary),
visible = true,
enabled = true
)
assertFalse(prefLockAccessDocumentProvider.isChecked)
prefTouchesWithOtherVisibleWindows.verifyPreference(
keyPref = SettingsSecurityFragment.PREFERENCE_TOUCHES_WITH_OTHER_VISIBLE_WINDOWS,
titlePref = context.getString(R.string.prefs_touches_with_other_visible_windows),
summaryPref = context.getString(R.string.prefs_touches_with_other_visible_windows_summary),
visible = true,
enabled = true
)
assertFalse(prefTouchesWithOtherVisibleWindows.isChecked)
}
@Test
fun securityViewDeviceWithBiometrics() {
launchTest()
checkCommonPreferences()
assertNotNull(prefBiometric)
prefBiometric?.run {
verifyPreference(
keyPref = BiometricActivity.PREFERENCE_SET_BIOMETRIC,
titlePref = context.getString(R.string.prefs_biometric),
summaryPref = context.getString(R.string.prefs_biometric_summary),
visible = true,
enabled = false
)
assertFalse(isChecked)
}
}
@Test
fun securityViewDeviceWithNoBiometrics() {
launchTest(withBiometrics = false)
checkCommonPreferences()
assertNull(prefBiometric)
}
@Ignore
@Test
fun passcodeOpen() {
every { securityViewModel.isPatternSet() } returns false
launchTest()
mockIntent(RESULT_OK, PassCodeActivity.ACTION_CREATE)
onView(withText(R.string.prefs_passcode)).perform(click())
intended(hasComponent(PassCodeActivity::class.java.name))
}
@Test
fun patternOpen() {
every { securityViewModel.isPasscodeSet() } returns false
launchTest()
onView(withText(R.string.prefs_pattern)).perform(click())
intended(hasComponent(PatternActivity::class.java.name))
}
@Ignore
@Test
fun passcodeLockEnabledOk() {
every { securityViewModel.isPatternSet() } returns false
launchTest()
mockIntent(
action = PassCodeActivity.ACTION_CREATE
)
onView(withText(R.string.prefs_passcode)).perform(click())
assertTrue(prefPasscode.isChecked)
}
@Test
fun patternLockEnabledOk() {
every { securityViewModel.isPasscodeSet() } returns false
launchTest()
mockIntent(
extras = Pair(PatternActivity.PREFERENCE_PATTERN, OC_PATTERN),
action = PatternActivity.ACTION_REQUEST_WITH_RESULT
)
onView(withText(R.string.prefs_pattern)).perform(click())
assertTrue(prefPattern.isChecked)
}
@Ignore
@Test
fun enablePasscodeEnablesBiometricLockAndLockApplication() {
launchTest()
firstEnablePasscode()
onView(withText(R.string.prefs_biometric)).check(matches(isEnabled()))
assertTrue(prefBiometric!!.isEnabled)
assertFalse(prefBiometric!!.isChecked)
assertTrue(prefLockApplication.isEnabled)
}
@Test
fun enablePatternEnablesBiometricLockAndLockApplication() {
launchTest()
firstEnablePattern()
onView(withText(R.string.prefs_biometric)).check(matches(isEnabled()))
assertTrue(prefBiometric!!.isEnabled)
assertFalse(prefBiometric!!.isChecked)
assertTrue(prefLockApplication.isEnabled)
}
@Ignore
@Test
fun onlyOneMethodEnabledPattern() {
every { securityViewModel.isPatternSet() } returns true
launchTest()
firstEnablePattern()
onView(withText(R.string.prefs_passcode)).perform(click())
onView(withText(R.string.pattern_already_set)).check(matches(isEnabled()))
}
@Test
fun onlyOneMethodEnabledPasscode() {
every { securityViewModel.isPasscodeSet() } returns true
launchTest()
firstEnablePasscode()
onView(withText(R.string.prefs_pattern)).perform(click())
onView(withText(R.string.passcode_already_set)).check(matches(isEnabled()))
}
@Test
fun disablePasscodeOk() {
launchTest()
firstEnablePasscode()
mockIntent(
action = PassCodeActivity.ACTION_REMOVE
)
onView(withText(R.string.prefs_passcode)).perform(click())
assertFalse(prefPasscode.isChecked)
onView(withText(R.string.prefs_biometric)).check(matches(not(isEnabled())))
assertFalse(prefBiometric!!.isEnabled)
assertFalse(prefBiometric!!.isChecked)
assertFalse(prefLockApplication.isEnabled)
}
@Test
fun disablePatternOk() {
launchTest()
firstEnablePattern()
mockIntent(
action = PatternActivity.ACTION_CHECK_WITH_RESULT
)
onView(withText(R.string.prefs_pattern)).perform(click())
assertFalse(prefPattern.isChecked)
onView(withText(R.string.prefs_biometric)).check(matches(not(isEnabled())))
assertFalse(prefBiometric!!.isEnabled)
assertFalse(prefBiometric!!.isChecked)
assertFalse(prefLockApplication.isEnabled)
}
@Ignore
@Test
fun enableBiometricLockWithPasscodeEnabled() {
every { BiometricManager.hasEnrolledBiometric() } returns true
launchTest()
firstEnablePasscode()
onView(withText(R.string.prefs_biometric)).perform(click())
assertTrue(prefBiometric!!.isChecked)
}
@Test
fun enableBiometricLockWithPatternEnabled() {
every { BiometricManager.hasEnrolledBiometric() } returns true
launchTest()
firstEnablePattern()
onView(withText(R.string.prefs_biometric)).perform(click())
assertTrue(prefBiometric!!.isChecked)
}
@Ignore
@Test
fun enableBiometricLockNoEnrolledBiometric() {
every { BiometricManager.hasEnrolledBiometric() } returns false
launchTest()
firstEnablePasscode()
onView(withText(R.string.prefs_biometric)).perform(click())
assertFalse(prefBiometric!!.isChecked)
onView(withText(R.string.biometric_not_enrolled)).check(matches(isEnabled()))
}
@Test
fun disableBiometricLock() {
every { BiometricManager.hasEnrolledBiometric() } returns true
launchTest()
firstEnablePasscode()
onView(withText(R.string.prefs_biometric)).perform(click())
onView(withText(R.string.prefs_biometric)).perform(click())
assertFalse(prefBiometric!!.isChecked)
}
@Test
fun lockAccessFromDocumentProviderEnable() {
launchTest()
onView(withText(R.string.prefs_lock_access_from_document_provider)).perform(click())
assertTrue(prefLockAccessDocumentProvider.isChecked)
}
@Test
fun lockAccessFromDocumentProviderDisable() {
launchTest()
onView(withText(R.string.prefs_lock_access_from_document_provider)).perform(click())
onView(withText(R.string.prefs_lock_access_from_document_provider)).perform(click())
assertFalse(prefLockAccessDocumentProvider.isChecked)
}
@Test
fun touchesDialog() {
launchTest()
onView(withText(R.string.prefs_touches_with_other_visible_windows)).perform(click())
onView(withText(R.string.confirmation_touches_with_other_windows_title)).check(matches(isDisplayed()))
onView(withText(R.string.confirmation_touches_with_other_windows_message)).check(matches(isDisplayed()))
}
@Test
fun touchesEnable() {
launchTest()
onView(withText(R.string.prefs_touches_with_other_visible_windows)).perform(click())
onView(withText(R.string.common_yes)).perform(click())
assertTrue(prefTouchesWithOtherVisibleWindows.isChecked)
}
@Test
fun touchesRefuse() {
launchTest()
onView(withText(R.string.prefs_touches_with_other_visible_windows)).perform(click())
onView(withText(R.string.common_no)).perform(click())
assertFalse(prefTouchesWithOtherVisibleWindows.isChecked)
}
@Test
fun touchesDisable() {
launchTest()
onView(withText(R.string.prefs_touches_with_other_visible_windows)).perform(click())
onView(withText(R.string.common_yes)).perform(click())
onView(withText(R.string.prefs_touches_with_other_visible_windows)).perform(click())
assertFalse(prefTouchesWithOtherVisibleWindows.isChecked)
}
@Test
fun passcodeLockNotVisible() {
every { securityViewModel.isSecurityEnforcedEnabled() } returns true
launchTest()
assertFalse(prefPasscode.isVisible)
}
@Test
fun patternLockNotVisible() {
every { securityViewModel.isSecurityEnforcedEnabled() } returns true
launchTest()
assertFalse(prefPattern.isVisible)
}
@Test
fun passcodeLockVisible() {
launchTest()
assertTrue(prefPasscode.isVisible)
}
@Test
fun patternLockVisible() {
launchTest()
assertTrue(prefPattern.isVisible)
}
@Ignore
@Test
fun checkIfUserEnabledBiometricRecommendation() {
every { securityViewModel.getBiometricsState() } returns true
launchTest()
firstEnablePasscode()
assertTrue(prefBiometric!!.isChecked)
assertTrue(prefBiometric!!.isEnabled)
}
@Test
fun checkIfUserNotEnabledBiometricRecommendation() {
launchTest()
firstEnablePasscode()
assertFalse(prefBiometric!!.isChecked)
}
private fun firstEnablePasscode() {
every { securityViewModel.isPatternSet() } returns false
mockIntent(
action = PassCodeActivity.ACTION_CREATE
)
onView(withText(R.string.prefs_passcode)).perform(click())
}
private fun firstEnablePattern() {
every { securityViewModel.isPasscodeSet() } returns false
mockIntent(
action = PatternActivity.ACTION_REQUEST_WITH_RESULT
)
onView(withText(R.string.prefs_pattern)).perform(click())
}
}