geekdroid/src/test/java/com/geekorum/geekdroid/battery/PowerSaveModeFlowTest.kt
changeset 86 84e46eaf1ea0
equal deleted inserted replaced
85:3b8739febbfe 86:84e46eaf1ea0
       
     1 /*
       
     2  * Geekdroid is a utility library for development on the Android
       
     3  * Platform.
       
     4  *
       
     5  * Copyright (C) 2017-2025 by Frederic-Charles Barthelery.
       
     6  *
       
     7  * This file is part of Geekdroid.
       
     8  *
       
     9  * Geekdroid is free software: you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation, either version 3 of the License, or
       
    12  * (at your option) any later version.
       
    13  *
       
    14  * Geekdroid is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  * GNU General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with Geekdroid.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 package com.geekorum.geekdroid.battery
       
    23 
       
    24 import android.app.Application
       
    25 import android.content.Intent
       
    26 import android.os.Build
       
    27 import android.os.Looper.getMainLooper
       
    28 import android.os.PowerManager
       
    29 import androidx.core.content.getSystemService
       
    30 import androidx.test.core.app.ApplicationProvider
       
    31 import androidx.test.ext.junit.runners.AndroidJUnit4
       
    32 import app.cash.turbine.test
       
    33 import com.google.common.truth.Truth.assertThat
       
    34 import kotlinx.coroutines.test.runTest
       
    35 import org.junit.Test
       
    36 import org.junit.runner.RunWith
       
    37 import org.robolectric.Shadows.shadowOf
       
    38 import org.robolectric.annotation.Config
       
    39 import org.robolectric.shadows.ShadowPowerManager
       
    40 import kotlin.test.BeforeTest
       
    41 
       
    42 
       
    43 @RunWith(AndroidJUnit4::class)
       
    44 @Config(minSdk = Build.VERSION_CODES.Q)
       
    45 class PowerSaveModeFlowTest {
       
    46     lateinit var shadowPowerManager: ShadowPowerManager
       
    47     lateinit var application: Application
       
    48     lateinit var powerManager: PowerManager
       
    49 
       
    50     @BeforeTest
       
    51     fun setUp() {
       
    52         application = ApplicationProvider.getApplicationContext()
       
    53         powerManager = application.getSystemService()!!
       
    54         shadowPowerManager = shadowOf(powerManager)
       
    55     }
       
    56 
       
    57     @Test
       
    58     fun testThatWhenPowerSaveModeChangedEmitValue() = runTest {
       
    59         isPowerSaveModeFlow(application, powerManager).test {
       
    60             assertThat(awaitItem()).isFalse()
       
    61 
       
    62             shadowPowerManager.setIsPowerSaveMode(true)
       
    63             application.sendBroadcast(Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED))
       
    64             shadowOf(getMainLooper()).idle()
       
    65             assertThat(awaitItem()).isTrue()
       
    66 
       
    67             shadowPowerManager.setIsPowerSaveMode(false)
       
    68             application.sendBroadcast(Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED))
       
    69             shadowOf(getMainLooper()).idle()
       
    70             assertThat(awaitItem()).isFalse()
       
    71 
       
    72         }
       
    73     }
       
    74 
       
    75 }