-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more tests
- Loading branch information
Showing
3 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...oneadapter/tests/modules/selection/state/WhenDisabled_ThenOnSelected_ShouldNotBeCalled.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,56 @@ | ||
@file:Suppress("ClassName") | ||
|
||
package com.idanatz.oneadapter.tests.modules.selection.state | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.idanatz.oneadapter.external.modules.ItemSelectionModule | ||
import com.idanatz.oneadapter.external.states.SelectionState | ||
import com.idanatz.oneadapter.helpers.getViewLocationOnScreen | ||
import com.idanatz.oneadapter.helpers.BaseTest | ||
import com.idanatz.oneadapter.models.TestModel | ||
import org.amshove.kluent.shouldEqualTo | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class WhenDisabled_ThenOnSelected_ShouldNotBeCalled : BaseTest() { | ||
|
||
private var onSelectedCalls = 0 | ||
|
||
@Test | ||
fun test() { | ||
configure { | ||
prepareOnActivity { | ||
oneAdapter.apply { | ||
attachItemModule(modulesGenerator.generateValidItemModule().apply { states += TestSelectionState() }) | ||
attachItemSelectionModule(ItemSelectionModule()) | ||
oneAdapter.internalAdapter.data = mutableListOf(modelGenerator.generateModel()) | ||
} | ||
} | ||
actOnActivity { | ||
runWithDelay { | ||
val holderRootView = recyclerView.findViewHolderForAdapterPosition(0)?.itemView | ||
holderRootView?.post { | ||
val (x, y) = holderRootView.getViewLocationOnScreen() | ||
|
||
touchSimulator.simulateLongTouch(recyclerView, x, y) | ||
} | ||
} | ||
} | ||
untilAsserted(assertDelay = 750) { | ||
onSelectedCalls shouldEqualTo 0 | ||
} | ||
} | ||
} | ||
|
||
private inner class TestSelectionState : SelectionState<TestModel>() { | ||
init { | ||
config { | ||
enabled = false | ||
} | ||
onSelected { _, _ -> | ||
onSelectedCalls++ | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
.../selection/state/WhenTriggerIsLongClick_ThenLongClickingOnItem_ShouldTriggerOnSelected.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,58 @@ | ||
@file:Suppress("ClassName") | ||
|
||
package com.idanatz.oneadapter.tests.modules.selection.state | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.idanatz.oneadapter.external.modules.ItemSelectionModule | ||
import com.idanatz.oneadapter.external.states.SelectionState | ||
import com.idanatz.oneadapter.external.states.SelectionStateConfig | ||
import com.idanatz.oneadapter.helpers.getViewLocationOnScreen | ||
import com.idanatz.oneadapter.helpers.BaseTest | ||
import com.idanatz.oneadapter.models.TestModel | ||
import org.amshove.kluent.shouldEqualTo | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class WhenTriggerIsLongClick_ThenLongClickingOnItem_ShouldTriggerOnSelected : BaseTest() { | ||
|
||
private var onSelectedCalls = 0 | ||
|
||
@Test | ||
fun test() { | ||
configure { | ||
prepareOnActivity { | ||
oneAdapter.apply { | ||
attachItemModule(modulesGenerator.generateValidItemModule().apply { states += TestSelectionState() }) | ||
attachItemSelectionModule(ItemSelectionModule()) | ||
oneAdapter.internalAdapter.data = mutableListOf(modelGenerator.generateModel()) | ||
} | ||
} | ||
actOnActivity { | ||
runWithDelay { | ||
val holderRootView = recyclerView.findViewHolderForAdapterPosition(0)?.itemView | ||
holderRootView?.post { | ||
val (x, y) = holderRootView.getViewLocationOnScreen() | ||
|
||
touchSimulator.simulateTouch(recyclerView, x, y) // should not do anything in this config | ||
touchSimulator.simulateLongTouch(recyclerView, x, y) | ||
} | ||
} | ||
} | ||
untilAsserted(assertDelay = 750) { | ||
onSelectedCalls shouldEqualTo 1 | ||
} | ||
} | ||
} | ||
|
||
private inner class TestSelectionState : SelectionState<TestModel>() { | ||
init { | ||
config { | ||
selectionTrigger = SelectionStateConfig.SelectionTrigger.LongClick | ||
} | ||
onSelected { _, _ -> | ||
onSelectedCalls++ | ||
} | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
sample/app/src/main/java/com/idanatz/sample/examples/BasicJavaExampleActivity.java
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,81 @@ | ||
package com.idanatz.sample.examples; | ||
|
||
import android.os.Bundle; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.airbnb.lottie.LottieAnimationView; | ||
import com.bumptech.glide.Glide; | ||
import com.idanatz.oneadapter.OneAdapter; | ||
import com.idanatz.oneadapter.external.event_hooks.ClickEventHook; | ||
import com.idanatz.oneadapter.external.modules.EmptinessModule; | ||
import com.idanatz.oneadapter.external.modules.ItemModule; | ||
import com.idanatz.oneadapter.sample.R; | ||
import com.idanatz.sample.models.MessageModel; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
class BasicJavaExampleActivity | ||
extends BaseExampleActivity { | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
OneAdapter oneAdapter = new OneAdapter(recyclerView) | ||
.attachItemModule(new MessageItem()) | ||
.attachEmptinessModule(new EmptinessModuleImpl()); | ||
|
||
oneAdapter.setItems(getModelGenerator().generateMessages(10)); | ||
} | ||
|
||
private static class MessageItem extends ItemModule<MessageModel> { | ||
|
||
public MessageItem() { | ||
config(builder -> { | ||
builder.setLayoutResource(R.layout.message_model); | ||
return null; | ||
}); | ||
onBind((model, viewBinder, metadata) -> { | ||
TextView title = viewBinder.findViewById(R.id.title); | ||
TextView body = viewBinder.findViewById(R.id.body); | ||
ImageView image = viewBinder.findViewById(R.id.avatarImage); | ||
|
||
title.setText(model.title); | ||
body.setText(model.body); | ||
Glide.with(viewBinder.getRootView()) | ||
.load(model.avatarImageId) | ||
.into(image); | ||
return null; | ||
}); | ||
eventHooks.add(new ClickEventHook<MessageModel>() {{ | ||
onClick((model, viewBinder, metadata) -> { | ||
Toast.makeText(viewBinder.getRootView().getContext(), model.title + " clicked", Toast.LENGTH_SHORT).show(); | ||
return null; | ||
}); | ||
}}); | ||
} | ||
} | ||
|
||
private static class EmptinessModuleImpl extends EmptinessModule { | ||
|
||
public EmptinessModuleImpl() { | ||
config(builder -> { | ||
builder.setLayoutResource(R.layout.empty_state); | ||
return null; | ||
}); | ||
onBind((viewBinder, metadata) -> { | ||
LottieAnimationView animation = viewBinder.findViewById(R.id.animation_view); | ||
animation.setAnimation(R.raw.empty_list); | ||
animation.playAnimation(); | ||
return null; | ||
}); | ||
onUnbind((viewBinder, metadata) -> { | ||
LottieAnimationView animation = viewBinder.findViewById(R.id.animation_view); | ||
animation.pauseAnimation(); | ||
return null; | ||
}); | ||
} | ||
} | ||
} |