|
| 1 | +/******************************************************************************* |
| 2 | + * Created by Patricio Jofré |
| 3 | + * Copyright 2026 Prey Inc. All rights reserved. |
| 4 | + * License: GPLv3 |
| 5 | + * Full license at "/LICENSE" |
| 6 | + ******************************************************************************/ |
| 7 | +package com.prey.receivers; |
| 8 | + |
| 9 | +import android.content.Context; |
| 10 | +import android.content.Intent; |
| 11 | + |
| 12 | +import com.prey.PreyConfig; |
| 13 | + |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.robolectric.RobolectricTestRunner; |
| 18 | +import org.robolectric.annotation.Config; |
| 19 | + |
| 20 | +import androidx.test.core.app.ApplicationProvider; |
| 21 | + |
| 22 | +import java.lang.reflect.Field; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | + |
| 26 | +/** |
| 27 | + * Robolectric test suite for the {@link PreyBootController} class. |
| 28 | + * <p> |
| 29 | + * Tests that a missing report restarted after boot keeps its stored settings. |
| 30 | + * Runs on the JVM without an emulator. |
| 31 | + */ |
| 32 | +@RunWith(RobolectricTestRunner.class) |
| 33 | +@Config(sdk = 30) |
| 34 | +public class PreyBootControllerRobolectricTest { |
| 35 | + |
| 36 | + private Context context; |
| 37 | + private PreyConfig preyConfig; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() throws Exception { |
| 41 | + context = ApplicationProvider.getApplicationContext(); |
| 42 | + Field cached = PreyConfig.class.getDeclaredField("cachedInstance"); |
| 43 | + cached.setAccessible(true); |
| 44 | + cached.set(null, null); |
| 45 | + preyConfig = PreyConfig.getPreyConfig(context); |
| 46 | + preyConfig.setMissing(false); |
| 47 | + preyConfig.setIntervalReport(""); |
| 48 | + preyConfig.setExcludeReport(""); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void givenMissingReportWithExcludedPicture_whenBootCompleted_thenExcludeIsKept() { |
| 53 | + preyConfig.setIntervalReport("10"); |
| 54 | + preyConfig.setExcludeReport("picture"); |
| 55 | + |
| 56 | + bootCompleted(); |
| 57 | + |
| 58 | + assertEquals("picture", preyConfig.getExcludeReport()); |
| 59 | + assertEquals("10", preyConfig.getIntervalReport()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenMissingReportWithoutExclusions_whenBootCompleted_thenNothingIsExcluded() { |
| 64 | + preyConfig.setIntervalReport("10"); |
| 65 | + |
| 66 | + bootCompleted(); |
| 67 | + |
| 68 | + assertEquals("", preyConfig.getExcludeReport()); |
| 69 | + assertEquals("10", preyConfig.getIntervalReport()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void givenNoMissingReport_whenBootCompleted_thenExcludeIsUntouched() { |
| 74 | + preyConfig.setExcludeReport("picture"); |
| 75 | + |
| 76 | + bootCompleted(); |
| 77 | + |
| 78 | + assertEquals("picture", preyConfig.getExcludeReport()); |
| 79 | + assertEquals("", preyConfig.getIntervalReport()); |
| 80 | + } |
| 81 | + |
| 82 | + private void bootCompleted() { |
| 83 | + new PreyBootController().onReceive(context, new Intent(Intent.ACTION_BOOT_COMPLETED)); |
| 84 | + } |
| 85 | +} |
0 commit comments