Skip to content

Commit

Permalink
Fixed lifecycle in AppUpdateStateMachine.kt
Browse files Browse the repository at this point in the history
Resolves: #20
  • Loading branch information
motorro committed Dec 18, 2021
1 parent 6f42104 commit a80a0dd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions appupdatewrapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ android {
defaultConfig {
minSdkVersion androidMinSdkVersion
targetSdkVersion androidTargetSdkVersion
versionCode versionCode
versionName versionName

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
debug {
debuggable true
minifyEnabled false
}
release {
Expand All @@ -62,7 +59,7 @@ dependencies {
api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

api 'androidx.core:core-ktx:1.7.0'
kapt 'androidx.lifecycle:lifecycle-common-java8:2.4.0'
api 'androidx.lifecycle:lifecycle-common:2.4.0'
api 'com.google.android.play:core:1.10.2'

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

dokkaJavadoc.configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
package com.motorro.appupdatewrapper

import androidx.annotation.VisibleForTesting
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.Event.*
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.LifecycleOwner
import com.google.android.play.core.appupdate.AppUpdateManager

/**
Expand Down Expand Up @@ -59,7 +58,7 @@ internal class AppUpdateLifecycleStateMachine(
override val updateManager: AppUpdateManager,
override val view: AppUpdateView,
override val flowBreaker: UpdateFlowBreaker = UpdateFlowBreaker.alwaysOn()
): AppUpdateStateMachine, AppUpdateWrapper, LifecycleObserver, Tagged {
): AppUpdateStateMachine, AppUpdateWrapper, DefaultLifecycleObserver, Tagged {
/**
* Current update state
*/
Expand Down Expand Up @@ -94,23 +93,19 @@ internal class AppUpdateLifecycleStateMachine(
}
}

@OnLifecycleEvent(ON_START)
fun onStart() {
override fun onStart(owner: LifecycleOwner) {
currentUpdateState.onStart()
}

@OnLifecycleEvent(ON_RESUME)
fun onResume() {
override fun onResume(owner: LifecycleOwner) {
currentUpdateState.onResume()
}

@OnLifecycleEvent(ON_RESUME)
fun onPause() {
override fun onPause(owner: LifecycleOwner) {
currentUpdateState.onPause()
}

@OnLifecycleEvent(ON_STOP)
fun onStop() {
override fun onStop(owner: LifecycleOwner) {
currentUpdateState.onStop()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
package com.motorro.appupdatewrapper

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.*
import androidx.lifecycle.Lifecycle.State.INITIALIZED
import androidx.lifecycle.testing.TestLifecycleOwner
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.nhaarman.mockitokotlin2.*
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.spy
import com.nhaarman.mockitokotlin2.verify
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -27,16 +31,14 @@ import kotlin.test.assertTrue

@RunWith(AndroidJUnit4::class)
class AppUpdateLifecycleStateMachineTest: TestAppTest() {
private lateinit var lifecycle: Lifecycle
private lateinit var lifecycleOwner: TestLifecycleOwner
private lateinit var stateMachine: AppUpdateLifecycleStateMachine
private lateinit var state: AppUpdateState

@Before
fun init() {
lifecycle = mock {
on { currentState } doReturn DESTROYED
}
stateMachine = AppUpdateLifecycleStateMachine(lifecycle, mock(), mock(), mock())
lifecycleOwner = TestLifecycleOwner(INITIALIZED)
stateMachine = AppUpdateLifecycleStateMachine(lifecycleOwner.lifecycle, mock(), mock(), mock())

state = spy()
}
Expand All @@ -46,7 +48,7 @@ class AppUpdateLifecycleStateMachineTest: TestAppTest() {
stateMachine.setUpdateState(state)
assertEquals(stateMachine, state.stateMachine)

stateMachine.onStart()
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START)
verify(state).onStart()
}

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

@Test
fun callsStateOnStartIfLifecycleStarted() {
whenever(lifecycle.currentState).thenReturn(STARTED)
fun followsLifecycle() {
stateMachine.setUpdateState(state)
verify(state).onStart()
verify(state, never()).onResume()
}

@Test
fun callsStateOnStartAndOnResumedIfLifecycleResumed() {
whenever(lifecycle.currentState).thenReturn(RESUMED)
stateMachine.setUpdateState(state)
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START)
verify(state).onStart()
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
verify(state).onResume()
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
verify(state).onPause()
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
verify(state).onStop()
}

@Test
fun initializes() {
verify(lifecycle).addObserver(stateMachine)
assertEquals(1, lifecycleOwner.observerCount)
assertTrue { stateMachine.currentUpdateState is None}
}

@Test
fun cleansUp() {
stateMachine.setUpdateState(state)
stateMachine.cleanup()
verify(lifecycle).removeObserver(stateMachine)
assertTrue { stateMachine.currentUpdateState is None}
assertEquals(0, lifecycleOwner.observerCount)
assertTrue { stateMachine.currentUpdateState is None }
}
}
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply from: 'gradle/maven-publish-config.gradle'
apply plugin: 'io.github.gradle-nexus.publish-plugin'

buildscript {
ext.kotlin_version = '1.6.0'
ext.kotlin_version = '1.6.10'
ext.dokka_version = '1.6.0'
repositories {
google()
Expand All @@ -21,9 +21,9 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'org.ajoberstar.grgit:grgit-gradle:3.0.0'
classpath 'org.ajoberstar.grgit:grgit-gradle:4.1.1'
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
classpath "io.github.gradle-nexus:publish-plugin:1.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class TestUpdateActivity : AppCompatActivity(), AppUpdateView {

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

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

0 comments on commit a80a0dd

Please sign in to comment.