From c8afc8ba5321c5c3933fa28ec7bab2c6e430a569 Mon Sep 17 00:00:00 2001 From: Sasha Heinen Date: Tue, 2 Apr 2019 16:28:21 -0700 Subject: [PATCH] autolock on device restart (#568) * bootreceiver started, not tested on device yet * bootreceiver test --- app/src/main/AndroidManifest.xml | 7 ++ .../main/java/mozilla/lockbox/BootReceiver.kt | 30 ++++++++ .../java/mozilla/lockbox/BootReceiverTest.kt | 75 +++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 app/src/main/java/mozilla/lockbox/BootReceiver.kt create mode 100644 app/src/test/java/mozilla/lockbox/BootReceiverTest.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 848c6d366..7c340640a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/mozilla/lockbox/BootReceiver.kt b/app/src/main/java/mozilla/lockbox/BootReceiver.kt new file mode 100644 index 000000000..9091dad9f --- /dev/null +++ b/app/src/main/java/mozilla/lockbox/BootReceiver.kt @@ -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() + } + } +} \ No newline at end of file diff --git a/app/src/test/java/mozilla/lockbox/BootReceiverTest.kt b/app/src/test/java/mozilla/lockbox/BootReceiverTest.kt new file mode 100644 index 000000000..af56b87ac --- /dev/null +++ b/app/src/test/java/mozilla/lockbox/BootReceiverTest.kt @@ -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() + } +} \ No newline at end of file