Skip to content

Commit a80a0dd

Browse files
committed
Fixed lifecycle in AppUpdateStateMachine.kt
Resolves: #20
1 parent 6f42104 commit a80a0dd

6 files changed

Lines changed: 36 additions & 41 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

appupdatewrapper/build.gradle

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,12 @@ android {
3838
defaultConfig {
3939
minSdkVersion androidMinSdkVersion
4040
targetSdkVersion androidTargetSdkVersion
41-
versionCode versionCode
42-
versionName versionName
4341

4442
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
4543
}
4644

4745
buildTypes {
4846
debug {
49-
debuggable true
5047
minifyEnabled false
5148
}
5249
release {
@@ -62,7 +59,7 @@ dependencies {
6259
api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
6360

6461
api 'androidx.core:core-ktx:1.7.0'
65-
kapt 'androidx.lifecycle:lifecycle-common-java8:2.4.0'
62+
api 'androidx.lifecycle:lifecycle-common:2.4.0'
6663
api 'com.google.android.play:core:1.10.2'
6764

6865
implementation 'com.jakewharton.timber:timber:5.0.1'
@@ -73,7 +70,8 @@ dependencies {
7370
testImplementation 'junit:junit:4.13.2'
7471
testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
7572
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
76-
testImplementation 'org.robolectric:robolectric:4.7.2'
73+
testImplementation 'org.robolectric:robolectric:4.7.3'
74+
testImplementation 'androidx.lifecycle:lifecycle-runtime-testing:2.4.0'
7775
}
7876

7977
dokkaJavadoc.configure {

appupdatewrapper/src/main/java/com/motorro/appupdatewrapper/AppUpdateStateMachine.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
package com.motorro.appupdatewrapper
1717

1818
import androidx.annotation.VisibleForTesting
19+
import androidx.lifecycle.DefaultLifecycleObserver
1920
import androidx.lifecycle.Lifecycle
20-
import androidx.lifecycle.Lifecycle.Event.*
21-
import androidx.lifecycle.LifecycleObserver
22-
import androidx.lifecycle.OnLifecycleEvent
21+
import androidx.lifecycle.LifecycleOwner
2322
import com.google.android.play.core.appupdate.AppUpdateManager
2423

2524
/**
@@ -59,7 +58,7 @@ internal class AppUpdateLifecycleStateMachine(
5958
override val updateManager: AppUpdateManager,
6059
override val view: AppUpdateView,
6160
override val flowBreaker: UpdateFlowBreaker = UpdateFlowBreaker.alwaysOn()
62-
): AppUpdateStateMachine, AppUpdateWrapper, LifecycleObserver, Tagged {
61+
): AppUpdateStateMachine, AppUpdateWrapper, DefaultLifecycleObserver, Tagged {
6362
/**
6463
* Current update state
6564
*/
@@ -94,23 +93,19 @@ internal class AppUpdateLifecycleStateMachine(
9493
}
9594
}
9695

97-
@OnLifecycleEvent(ON_START)
98-
fun onStart() {
96+
override fun onStart(owner: LifecycleOwner) {
9997
currentUpdateState.onStart()
10098
}
10199

102-
@OnLifecycleEvent(ON_RESUME)
103-
fun onResume() {
100+
override fun onResume(owner: LifecycleOwner) {
104101
currentUpdateState.onResume()
105102
}
106103

107-
@OnLifecycleEvent(ON_RESUME)
108-
fun onPause() {
104+
override fun onPause(owner: LifecycleOwner) {
109105
currentUpdateState.onPause()
110106
}
111107

112-
@OnLifecycleEvent(ON_STOP)
113-
fun onStop() {
108+
override fun onStop(owner: LifecycleOwner) {
114109
currentUpdateState.onStop()
115110
}
116111

appupdatewrapper/src/test/java/com/motorro/appupdatewrapper/AppUpdateLifecycleStateMachineTest.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
package com.motorro.appupdatewrapper
1717

1818
import androidx.lifecycle.Lifecycle
19-
import androidx.lifecycle.Lifecycle.State.*
19+
import androidx.lifecycle.Lifecycle.State.INITIALIZED
20+
import androidx.lifecycle.testing.TestLifecycleOwner
2021
import androidx.test.ext.junit.runners.AndroidJUnit4
21-
import com.nhaarman.mockitokotlin2.*
22+
import com.nhaarman.mockitokotlin2.mock
23+
import com.nhaarman.mockitokotlin2.never
24+
import com.nhaarman.mockitokotlin2.spy
25+
import com.nhaarman.mockitokotlin2.verify
2226
import org.junit.Before
2327
import org.junit.Test
2428
import org.junit.runner.RunWith
@@ -27,16 +31,14 @@ import kotlin.test.assertTrue
2731

2832
@RunWith(AndroidJUnit4::class)
2933
class AppUpdateLifecycleStateMachineTest: TestAppTest() {
30-
private lateinit var lifecycle: Lifecycle
34+
private lateinit var lifecycleOwner: TestLifecycleOwner
3135
private lateinit var stateMachine: AppUpdateLifecycleStateMachine
3236
private lateinit var state: AppUpdateState
3337

3438
@Before
3539
fun init() {
36-
lifecycle = mock {
37-
on { currentState } doReturn DESTROYED
38-
}
39-
stateMachine = AppUpdateLifecycleStateMachine(lifecycle, mock(), mock(), mock())
40+
lifecycleOwner = TestLifecycleOwner(INITIALIZED)
41+
stateMachine = AppUpdateLifecycleStateMachine(lifecycleOwner.lifecycle, mock(), mock(), mock())
4042

4143
state = spy()
4244
}
@@ -46,7 +48,7 @@ class AppUpdateLifecycleStateMachineTest: TestAppTest() {
4648
stateMachine.setUpdateState(state)
4749
assertEquals(stateMachine, state.stateMachine)
4850

49-
stateMachine.onStart()
51+
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START)
5052
verify(state).onStart()
5153
}
5254

@@ -65,32 +67,30 @@ class AppUpdateLifecycleStateMachineTest: TestAppTest() {
6567
}
6668

6769
@Test
68-
fun callsStateOnStartIfLifecycleStarted() {
69-
whenever(lifecycle.currentState).thenReturn(STARTED)
70+
fun followsLifecycle() {
7071
stateMachine.setUpdateState(state)
71-
verify(state).onStart()
72-
verify(state, never()).onResume()
73-
}
7472

75-
@Test
76-
fun callsStateOnStartAndOnResumedIfLifecycleResumed() {
77-
whenever(lifecycle.currentState).thenReturn(RESUMED)
78-
stateMachine.setUpdateState(state)
73+
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START)
7974
verify(state).onStart()
75+
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
8076
verify(state).onResume()
77+
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
78+
verify(state).onPause()
79+
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
80+
verify(state).onStop()
8181
}
8282

8383
@Test
8484
fun initializes() {
85-
verify(lifecycle).addObserver(stateMachine)
85+
assertEquals(1, lifecycleOwner.observerCount)
8686
assertTrue { stateMachine.currentUpdateState is None}
8787
}
8888

8989
@Test
9090
fun cleansUp() {
9191
stateMachine.setUpdateState(state)
9292
stateMachine.cleanup()
93-
verify(lifecycle).removeObserver(stateMachine)
94-
assertTrue { stateMachine.currentUpdateState is None}
93+
assertEquals(0, lifecycleOwner.observerCount)
94+
assertTrue { stateMachine.currentUpdateState is None }
9595
}
9696
}

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply from: 'gradle/maven-publish-config.gradle'
44
apply plugin: 'io.github.gradle-nexus.publish-plugin'
55

66
buildscript {
7-
ext.kotlin_version = '1.6.0'
7+
ext.kotlin_version = '1.6.10'
88
ext.dokka_version = '1.6.0'
99
repositories {
1010
google()
@@ -21,9 +21,9 @@ buildscript {
2121
}
2222
}
2323
dependencies {
24-
classpath 'com.android.tools.build:gradle:7.0.3'
24+
classpath 'com.android.tools.build:gradle:7.0.4'
2525
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
26-
classpath 'org.ajoberstar.grgit:grgit-gradle:3.0.0'
26+
classpath 'org.ajoberstar.grgit:grgit-gradle:4.1.1'
2727
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
2828
classpath "io.github.gradle-nexus:publish-plugin:1.0.0"
2929
}

testapp/src/main/java/com/motorro/appupdatewrapper/testapp/TestUpdateActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ class TestUpdateActivity : AppCompatActivity(), AppUpdateView {
3434

3535
// To pass 'activity result' as fake update manager does not start activities
3636
fun passActivityResult(requestCode: Int, resultCode: Int) {
37+
@Suppress("DEPRECATION")
3738
onActivityResult(requestCode, resultCode, null)
3839
}
3940

4041
// Passes an activity result to wrapper to check for play-core interaction
4142
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
43+
@Suppress("DEPRECATION")
4244
super.onActivityResult(requestCode, resultCode, data)
4345
if (updateWrapper.checkActivityResult(requestCode, resultCode)) {
4446
// Result handled and processed

0 commit comments

Comments
 (0)