Skip to content

Commit

Permalink
dispatch-internal-test-android module (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
RBusarow authored Jun 28, 2020
1 parent 855dd6a commit fdf3638
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 84 deletions.
7 changes: 6 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,10 @@ extensions.configure<ApiValidationExtension> {
/**
* Sub-projects that are excluded from API validation
*/
ignoredProjects = mutableSetOf("dispatch-internal-test", "dispatch-sample", "samples")
ignoredProjects = mutableSetOf(
"dispatch-internal-test",
"dispatch-internal-test-android",
"dispatch-sample",
"samples"
)
}
15 changes: 8 additions & 7 deletions buildSrc/src/main/kotlin/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ object Modules {
":dispatch-core:samples",
":dispatch-detekt",
":dispatch-internal-test",
":dispatch-internal-test-android",
":dispatch-sample",
"dispatch-test",
"dispatch-test-junit4",
"dispatch-test-junit4:samples",
"dispatch-test-junit5",
"dispatch-test-junit5:samples",
"dispatch-test:samples",
"dispatch-test:samples"
":dispatch-test",
":dispatch-test-junit4",
":dispatch-test-junit4:samples",
":dispatch-test-junit5",
":dispatch-test-junit5:samples",
":dispatch-test:samples",
":dispatch-test:samples"
)
val allInternalPaths = allPaths.filter { it.matches(internalRegex) }
val allProductionPaths = allPaths.filter { !it.matches(internalRegex, sampleRegex) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
implementation(project(":dispatch-android-lifecycle"))
implementation(project(":dispatch-android-lifecycle-extensions"))
implementation(project(":dispatch-core"))
implementation(project(":dispatch-internal-test-android"))
implementation(project(":dispatch-test"))
implementation(project(":dispatch-test-junit5"))
testImplementation(Libs.JUnit.jUnit5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package samples

import androidx.lifecycle.*
import dispatch.internal.test.android.*

abstract class Fragment(
initialState: Lifecycle.State = Lifecycle.State.INITIALIZED
Expand Down
1 change: 1 addition & 0 deletions dispatch-android-lifecycle/samples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
implementation(project(":dispatch-android-lifecycle"))
implementation(project(":dispatch-android-lifecycle-extensions"))
implementation(project(":dispatch-core"))
implementation(project(":dispatch-internal-test-android"))
implementation(project(":dispatch-test"))
implementation(project(":dispatch-test-junit5"))
testImplementation(Libs.JUnit.jUnit5)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package samples

import androidx.lifecycle.*
import dispatch.internal.test.android.*

abstract class Fragment(
initialState: Lifecycle.State = Lifecycle.State.INITIALIZED
Expand Down
51 changes: 51 additions & 0 deletions dispatch-internal-test-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2020 Rick Busarow
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id(Plugins.androidLibrary)
id(Plugins.kotlinAndroid)
id(Plugins.kotlinAndroidExtensions)
}

android {
compileSdkVersion(Versions.compileSdk)

defaultConfig {
minSdkVersion(Versions.minSdk)
targetSdkVersion(Versions.targetSdk)
versionName = Versions.versionName

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}
dependencies {
implementation(Libs.AndroidX.Fragment.core)
implementation(Libs.AndroidX.Lifecycle.common)
implementation(Libs.AndroidX.Lifecycle.runtime)
implementation(Libs.Kotlin.reflect)
implementation(Libs.Kotlin.stdlib)
implementation(Libs.Kotlinx.Coroutines.android)
implementation(Libs.Kotlinx.Coroutines.core)
}
16 changes: 16 additions & 0 deletions dispatch-internal-test-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
~ Copyright (C) 2020 Rick Busarow
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<manifest package="dispatch.internal.test.android" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2020 Rick Busarow
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dispatch.internal.test.android;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import org.jetbrains.annotations.Nullable;

public class FakeFragment extends Fragment {

LifecycleOwner fragmentLifecycleOwner;
MutableLiveData<LifecycleOwner> fakeViewLifecycleOwnerLiveData = new MutableLiveData<>(null);
private LifecycleOwner fakeViewLifecycleOwner = null;

public FakeFragment(LifecycleOwner fragmentLifecycleOwner) {
this.fragmentLifecycleOwner = fragmentLifecycleOwner;
}

public void setFakeViewLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner) {
fakeViewLifecycleOwner = lifecycleOwner;
fakeViewLifecycleOwnerLiveData.postValue(lifecycleOwner);
}

@NonNull
@Override
public LifecycleOwner getViewLifecycleOwner() {
return fakeViewLifecycleOwner;
}

@NonNull
@Override
public LiveData<LifecycleOwner> getViewLifecycleOwnerLiveData() {
return fakeViewLifecycleOwnerLiveData;
}

@NonNull
@Override
public Lifecycle getLifecycle() {
return fragmentLifecycleOwner.getLifecycle();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* limitations under the License.
*/

package samples
package dispatch.internal.test.android

import androidx.lifecycle.*
import kotlinx.coroutines.*

@Suppress("EXPERIMENTAL_API_USAGE")
open class FakeLifecycleOwner(
private val mainDispatcher: CoroutineDispatcher = fakeMainDispatcher(),
initialState: Lifecycle.State = Lifecycle.State.INITIALIZED
initialState: Lifecycle.State = Lifecycle.State.INITIALIZED,
private val mainDispatcher: CoroutineDispatcher = fakeMainDispatcher()
) : LifecycleOwner {

private val registry: LifecycleRegistry by lazy { LifecycleRegistry(this) }
Expand All @@ -38,6 +38,24 @@ open class FakeLifecycleOwner(

override fun getLifecycle(): LifecycleRegistry = registry

fun stepDown() = when (lifecycle.currentState) {
Lifecycle.State.DESTROYED -> throw IllegalArgumentException("already destroyed")
Lifecycle.State.INITIALIZED -> throw IllegalArgumentException(
"cannot transition straight from initialized to destroyed"
)
Lifecycle.State.CREATED -> destroy()
Lifecycle.State.STARTED -> stop()
Lifecycle.State.RESUMED -> pause()
}

fun stepUp() = when (lifecycle.currentState) {
Lifecycle.State.DESTROYED -> throw IllegalArgumentException("already destroyed")
Lifecycle.State.INITIALIZED -> create()
Lifecycle.State.CREATED -> start()
Lifecycle.State.STARTED -> resume()
Lifecycle.State.RESUMED -> throw IllegalArgumentException("already resumed")
}

fun create() = runBlocking(mainDispatcher) {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright (C) 2020 Rick Busarow
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
junit.jupiter.testinstance.lifecycle.default=per_class
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ include(":dispatch-core:samples")
include(":dispatch-detekt")
include(":dispatch-detekt:samples")
include(":dispatch-internal-test")
include(":dispatch-internal-test-android")
include(":dispatch-sample")

0 comments on commit fdf3638

Please sign in to comment.