|
| 1 | +// SPDX-License-Identifier: GPL-3.0-only |
| 2 | +// Copyright (C) 2026 The Somewhere Authors |
| 3 | + |
| 4 | +package eu.nodepass.somewhere.subscription |
| 5 | + |
| 6 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 7 | +import androidx.test.platform.app.InstrumentationRegistry |
| 8 | +import eu.nodepass.somewhere.SomewhereApplication |
| 9 | +import org.junit.After |
| 10 | +import org.junit.Assert.assertEquals |
| 11 | +import org.junit.Assert.assertFalse |
| 12 | +import org.junit.Assert.assertTrue |
| 13 | +import org.junit.Test |
| 14 | +import org.junit.runner.RunWith |
| 15 | + |
| 16 | +/** |
| 17 | + * That the platform actually took the schedule. |
| 18 | + * |
| 19 | + * The preference is unit-tested and needs no device. What needs one is the |
| 20 | + * other half: `JobScheduler.schedule` returns a result nobody reads, a job with |
| 21 | + * an interval the platform will not honour is silently adjusted, and a job that |
| 22 | + * was never accepted looks exactly like one that has not fired yet. |
| 23 | + * |
| 24 | + * So this asks the scheduler what it is holding, which is the only answer that |
| 25 | + * is not this app agreeing with itself. |
| 26 | + */ |
| 27 | +@RunWith(AndroidJUnit4::class) |
| 28 | +class SubscriptionRefreshScheduleTest { |
| 29 | + private val context get() = InstrumentationRegistry.getInstrumentation().targetContext |
| 30 | + private val application get() = context.applicationContext as SomewhereApplication |
| 31 | + |
| 32 | + private var saved = RefreshPreferences.Settings() |
| 33 | + |
| 34 | + @After |
| 35 | + fun restoreThePreference() { |
| 36 | + application.refreshPreferences.save(saved) |
| 37 | + SubscriptionRefreshJob.apply(context) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun turningItOnPutsAJobInTheScheduler() { |
| 42 | + saved = application.refreshPreferences.load() |
| 43 | + application.refreshPreferences.save(RefreshPreferences.Settings(automatic = true, intervalHours = 6)) |
| 44 | + SubscriptionRefreshJob.apply(context) |
| 45 | + assertTrue("the platform did not accept the schedule", SubscriptionRefreshJob.isScheduled(context)) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun turningItOffTakesTheJobAway() { |
| 50 | + // A schedule that outlived its preference would be a switch that says |
| 51 | + // off while a job runs every six hours — the exact shape of the |
| 52 | + // switches this screen deleted for describing behaviour that was not |
| 53 | + // there, facing the other way. |
| 54 | + saved = application.refreshPreferences.load() |
| 55 | + application.refreshPreferences.save(RefreshPreferences.Settings(automatic = true)) |
| 56 | + SubscriptionRefreshJob.apply(context) |
| 57 | + assertTrue(SubscriptionRefreshJob.isScheduled(context)) |
| 58 | + |
| 59 | + application.refreshPreferences.save(RefreshPreferences.Settings(automatic = false)) |
| 60 | + SubscriptionRefreshJob.apply(context) |
| 61 | + assertFalse("the schedule outlived the preference", SubscriptionRefreshJob.isScheduled(context)) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun schedulingTwiceLeavesOneJobRatherThanTwo() { |
| 66 | + // Same id, so a reschedule replaces. Without that, every visit to the |
| 67 | + // settings screen would add a job and the refresh would speed up the |
| 68 | + // more often somebody looked at the switch. |
| 69 | + saved = application.refreshPreferences.load() |
| 70 | + application.refreshPreferences.save(RefreshPreferences.Settings(automatic = true)) |
| 71 | + SubscriptionRefreshJob.apply(context) |
| 72 | + SubscriptionRefreshJob.apply(context) |
| 73 | + |
| 74 | + val scheduler = context.getSystemService(android.app.job.JobScheduler::class.java)!! |
| 75 | + val ours = scheduler.allPendingJobs.filter { it.id == SubscriptionRefreshJob.JOB_ID } |
| 76 | + assertEquals("one job, not one per call", 1, ours.size) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun theScheduledJobSurvivesAReboot() { |
| 81 | + // The interval is measured in hours. A schedule a restart cancelled |
| 82 | + // would be a switch that works until the phone is turned off, which is |
| 83 | + // worse than no switch at all because nothing says it stopped. |
| 84 | + saved = application.refreshPreferences.load() |
| 85 | + application.refreshPreferences.save(RefreshPreferences.Settings(automatic = true)) |
| 86 | + SubscriptionRefreshJob.apply(context) |
| 87 | + |
| 88 | + val scheduler = context.getSystemService(android.app.job.JobScheduler::class.java)!! |
| 89 | + val job = scheduler.allPendingJobs.first { it.id == SubscriptionRefreshJob.JOB_ID } |
| 90 | + assertTrue("the job is not persisted", job.isPersisted) |
| 91 | + assertTrue("the job does not require a network", job.networkType != android.app.job.JobInfo.NETWORK_TYPE_NONE) |
| 92 | + } |
| 93 | +} |
0 commit comments