PassCodeManagerIT.kt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Álvaro Brey
  5. * Copyright (C) 2023 Álvaro Brey
  6. * Copyright (C) 2023 Nextcloud GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.authentication
  23. import android.app.Activity
  24. import android.os.PowerManager
  25. import com.nextcloud.client.core.Clock
  26. import com.nextcloud.client.preferences.AppPreferences
  27. import com.owncloud.android.ui.activity.SettingsActivity
  28. import io.mockk.MockKAnnotations
  29. import io.mockk.every
  30. import io.mockk.impl.annotations.MockK
  31. import io.mockk.just
  32. import io.mockk.mockk
  33. import io.mockk.runs
  34. import org.junit.Assert.assertTrue
  35. import org.junit.Before
  36. import org.junit.Test
  37. /**
  38. * This class should really be unit tests, but PassCodeManager needs a refactor
  39. * to decouple the locking logic from the platform classes
  40. */
  41. class PassCodeManagerIT {
  42. @MockK
  43. lateinit var appPreferences: AppPreferences
  44. @MockK
  45. lateinit var clockImpl: Clock
  46. lateinit var sut: PassCodeManager
  47. @Before
  48. fun before() {
  49. MockKAnnotations.init(this, relaxed = true)
  50. sut = PassCodeManager(appPreferences, clockImpl)
  51. }
  52. @Test
  53. fun testResumeDuplicateActivity() {
  54. // mock activity instead of using real one to avoid dealing with activity transitions
  55. val activity: Activity = mockk()
  56. val powerManager: PowerManager = mockk()
  57. every { powerManager.isScreenOn } returns true
  58. every { activity.getSystemService(Activity.POWER_SERVICE) } returns powerManager
  59. every { activity.window } returns null
  60. every { activity.startActivityForResult(any(), any()) } just runs
  61. every { activity.moveTaskToBack(any()) } returns true
  62. // set locked state
  63. every { appPreferences.lockPreference } returns SettingsActivity.LOCK_PASSCODE
  64. every { appPreferences.lockTimestamp } returns 200
  65. every { clockImpl.millisSinceBoot } returns 10000
  66. // resume activity twice
  67. var askedForPin = sut.onActivityResumed(activity)
  68. assertTrue("Passcode not requested on first launch", askedForPin)
  69. sut.onActivityResumed(activity)
  70. // stop it once
  71. sut.onActivityStopped(activity)
  72. // resume again. should ask for passcode
  73. askedForPin = sut.onActivityResumed(activity)
  74. assertTrue("Passcode not requested on subsequent launch after stop", askedForPin)
  75. }
  76. }