-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1028 from square/ray/3-new-back-handler
3/4 `fun View.setBackHandler()` replaces `val View.backPressedHandler`
- Loading branch information
Showing
22 changed files
with
222 additions
and
38 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
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
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
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
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
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
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
2 changes: 2 additions & 0 deletions
2
...-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/BackPressedHandlerTest.kt
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
@file:Suppress("DEPRECATION") | ||
|
||
package com.squareup.workflow1.ui | ||
|
||
import android.view.View | ||
|
54 changes: 54 additions & 0 deletions
54
...low-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/ViewBackHandlerTest.kt
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,54 @@ | ||
package com.squareup.workflow1.ui | ||
|
||
import android.view.View | ||
import androidx.activity.ComponentActivity | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule | ||
import com.google.common.truth.Truth.assertThat | ||
import com.squareup.workflow1.ui.androidx.WorkflowLifecycleOwner | ||
import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule | ||
import leakcanary.DetectLeaksAfterTestSuccess | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.RuleChain | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
internal class ViewBackHandlerTest { | ||
private val scenarioRule = ActivityScenarioRule(ComponentActivity::class.java) | ||
private val scenario get() = scenarioRule.scenario | ||
|
||
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) | ||
.around(scenarioRule) | ||
.around(IdlingDispatcherRule) | ||
|
||
private var viewHandlerCount = 0 | ||
private fun viewBackHandler() { | ||
viewHandlerCount++ | ||
} | ||
|
||
@Test fun itWorksWhenHandlerIsAddedBeforeAttach() { | ||
scenario.onActivity { activity -> | ||
val view = View(activity) | ||
WorkflowLifecycleOwner.installOn(view, activity) | ||
view.setBackHandler { viewBackHandler() } | ||
|
||
activity.setContentView(view) | ||
assertThat(viewHandlerCount).isEqualTo(0) | ||
|
||
activity.onBackPressedDispatcher.onBackPressed() | ||
assertThat(viewHandlerCount).isEqualTo(1) | ||
} | ||
} | ||
|
||
@Test fun itWorksWhenHandlerIsAddedAfterAttach() { | ||
scenario.onActivity { activity -> | ||
val view = View(activity) | ||
activity.setContentView(view) | ||
|
||
view.setBackHandler { viewBackHandler() } | ||
assertThat(viewHandlerCount).isEqualTo(0) | ||
|
||
activity.onBackPressedDispatcher.onBackPressed() | ||
assertThat(viewHandlerCount).isEqualTo(1) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.