This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bootreceiver started, not tested on device yet * bootreceiver test
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |