Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
autolock on device restart (#568)
Browse files Browse the repository at this point in the history
* bootreceiver started, not tested on device yet

* bootreceiver test
  • Loading branch information
sashei authored Apr 2, 2019
1 parent f9c318d commit c8afc8b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
xmlns:tools="http://schemas.android.com/tools"
Expand Down Expand Up @@ -58,5 +59,11 @@
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>

<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
30 changes: 30 additions & 0 deletions app/src/main/java/mozilla/lockbox/BootReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package mozilla.lockbox

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.preference.PreferenceManager
import mozilla.lockbox.support.Constant

class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (!intent?.action.equals("android.intent.action.BOOT_COMPLETED")) {
return
}

context?.let {
val prefs = PreferenceManager.getDefaultSharedPreferences(it)

prefs
.edit()
.putLong(Constant.Key.autoLockTimerDate, 0)
.apply()
}
}
}
75 changes: 75 additions & 0 deletions app/src/test/java/mozilla/lockbox/BootReceiverTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package mozilla.lockbox

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.preference.PreferenceManager
import mozilla.lockbox.support.Constant
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.verify
import org.mockito.Mockito.verifyZeroInteractions
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.mockito.Mockito.`when` as whenCalled

@RunWith(PowerMockRunner::class)
@PrepareForTest(PreferenceManager::class)
class BootReceiverTest {
@Mock
val editor: SharedPreferences.Editor = Mockito.mock(SharedPreferences.Editor::class.java)

@Mock
val preferences: SharedPreferences = Mockito.mock(SharedPreferences::class.java)

@Mock
val context = Mockito.mock(Context::class.java)

@Mock
val intent = Mockito.mock(Intent::class.java)

val subject = BootReceiver()

@Test
fun `receiving unexpected intents`() {
subject.onReceive(context, intent)

verifyZeroInteractions(context)
verifyZeroInteractions(preferences)
}

@Test
fun `receiving expected intents with a null context`() {
whenCalled(intent.action).thenReturn("android.intent.action.BOOT_COMPLETED")
subject.onReceive(null, intent)

verifyZeroInteractions(context)
verifyZeroInteractions(preferences)
}

@Test
fun `receiving expected intents with a context object`() {
whenCalled(intent.action).thenReturn("android.intent.action.BOOT_COMPLETED")
Mockito.`when`(editor.putLong(anyString(), anyLong())).thenReturn(editor)
Mockito.`when`(preferences.edit()).thenReturn(editor)
PowerMockito.mockStatic(PreferenceManager::class.java)
Mockito.`when`(PreferenceManager.getDefaultSharedPreferences(context)).thenReturn(preferences)

subject.onReceive(context, intent)

verify(preferences).edit()
verify(editor).putLong(Constant.Key.autoLockTimerDate, 0)
verify(editor).apply()
}
}

0 comments on commit c8afc8b

Please sign in to comment.