-
Notifications
You must be signed in to change notification settings - Fork 88
fix(android): defer wakelock toggle when no activity is attached W… #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,4 +8,5 @@ | |
| .buildlog/ | ||
| .history | ||
| .svn/ | ||
| .metadata | ||
| .metadata | ||
| CLAUDE.md | ||
This file contains hidden or 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 hidden or 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 hidden or 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
97 changes: 97 additions & 0 deletions
97
wakelock_plus/android/src/test/kotlin/dev/fluttercommunity/plus/wakelock/WakelockTest.kt
This file contains hidden or 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,97 @@ | ||
| package dev.fluttercommunity.plus.wakelock | ||
|
|
||
| import ToggleMessage | ||
| import android.app.Activity | ||
| import android.os.Build | ||
| import android.view.WindowManager | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.Robolectric | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| /** | ||
| * Unit tests for [Wakelock] covering the "no foreground activity attached" | ||
| * behaviour introduced to stop [toggle]/[isEnabled] from throwing when the app | ||
| * is backgrounded or mid lifecycle transition. The desired state is tracked in | ||
| * Kotlin and (re)applied to whichever activity window is attached. | ||
| */ | ||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(sdk = [Build.VERSION_CODES.UPSIDE_DOWN_CAKE]) | ||
| class WakelockTest { | ||
|
|
||
| private fun buildActivity(): Activity = | ||
| Robolectric.buildActivity(Activity::class.java).setup().get() | ||
|
|
||
| private val Activity.keepScreenOn: Boolean | ||
| get() = window.attributes.flags and | ||
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON != 0 | ||
|
|
||
| @Test | ||
| fun `toggle enable with no activity attached does not throw`() { | ||
| val wakelock = Wakelock() | ||
|
|
||
| wakelock.toggle(ToggleMessage(enable = true)) | ||
|
|
||
| assertTrue(wakelock.isEnabled().enabled == true) | ||
| } | ||
|
|
||
| @Test | ||
| fun `isEnabled with no activity attached does not throw and defaults to false`() { | ||
| assertFalse(Wakelock().isEnabled().enabled == true) | ||
| } | ||
|
|
||
| @Test | ||
| fun `enabling before an activity attaches applies the flag once it does`() { | ||
| val wakelock = Wakelock() | ||
|
|
||
| wakelock.toggle(ToggleMessage(enable = true)) | ||
| val activity = buildActivity() | ||
| wakelock.activity = activity | ||
|
|
||
| assertTrue(activity.keepScreenOn) | ||
| } | ||
|
|
||
| @Test | ||
| fun `attaching an activity while disabled leaves the flag untouched`() { | ||
| val wakelock = Wakelock() | ||
| val activity = buildActivity() | ||
|
|
||
| wakelock.activity = activity | ||
|
|
||
| assertFalse(activity.keepScreenOn) | ||
| } | ||
|
|
||
| @Test | ||
| fun `disabling clears the flag on the attached activity`() { | ||
| val wakelock = Wakelock() | ||
| val activity = buildActivity() | ||
| wakelock.activity = activity | ||
|
|
||
| wakelock.toggle(ToggleMessage(enable = true)) | ||
| assertTrue(activity.keepScreenOn) | ||
|
|
||
| wakelock.toggle(ToggleMessage(enable = false)) | ||
| assertFalse(activity.keepScreenOn) | ||
| } | ||
|
|
||
| @Test | ||
| fun `enabled state is re-applied to a new activity after detach`() { | ||
| val wakelock = Wakelock() | ||
| val first = buildActivity() | ||
| wakelock.activity = first | ||
| wakelock.toggle(ToggleMessage(enable = true)) | ||
| assertTrue(first.keepScreenOn) | ||
|
|
||
| // The activity goes away (e.g. the app is backgrounded). This used to throw. | ||
| wakelock.activity = null | ||
| assertTrue(wakelock.isEnabled().enabled == true) | ||
|
|
||
| // A fresh activity attaches; the requested state must be re-asserted on it. | ||
| val second = buildActivity() | ||
| wakelock.activity = second | ||
| assertTrue(second.keepScreenOn) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ name: wakelock_plus | |
| description: >-2 | ||
| Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on | ||
| Android, iOS, macOS, Windows, Linux, and web. | ||
| version: 1.6.1 | ||
| version: 1.6.2 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not eagerly increment the pubspec. I'll handle deploying the app when the PR is merged. |
||
| repository: https://github.com/fluttercommunity/wakelock_plus/tree/main/wakelock_plus | ||
|
|
||
| environment: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not eagerly increment the changelog.
I'll handle deploying the app when the PR is merged.