Skip to content

Commit

Permalink
Hash code and equals (#354)
Browse files Browse the repository at this point in the history
* add hashcode on Image

path info on sample app image preview

* add hash code

fix ui test
  • Loading branch information
esafirm committed Apr 24, 2021
1 parent d543894 commit 5b4108d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import com.esafirm.imagepicker.features.imageloader.DefaultImageLoader
import com.esafirm.imagepicker.features.imageloader.ImageLoader

interface ImagePickerComponents {
val appContext: Context
val imageLoader: ImageLoader
val imageFileLoader: ImageFileLoader
val cameraModule: CameraModule
}

open class DefaultImagePickerComponents(context: Context) : ImagePickerComponents {
override val appContext: Context = context.applicationContext
override val imageLoader: ImageLoader by lazy { DefaultImageLoader() }
override val imageFileLoader: ImageFileLoader by lazy { DefaultImageFileLoader(context.applicationContext) }
override val cameraModule: CameraModule by lazy { DefaultCameraModule() }
Expand All @@ -24,6 +26,9 @@ object ImagePickerComponentsHolder : ImagePickerComponents {

private lateinit var internalComponents: ImagePickerComponents

override val appContext: Context
get() = internalComponents.appContext

override val imageLoader: ImageLoader
get() = internalComponents.imageLoader

Expand Down
16 changes: 12 additions & 4 deletions imagepicker/src/main/java/com/esafirm/imagepicker/model/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ class Image(
}
}

override fun equals(o: Any?): Boolean {
override fun equals(other: Any?): Boolean {
return when {
this === o -> true
o == null || javaClass != o.javaClass -> return false
this === other -> true
other == null || javaClass != other.javaClass -> return false
else -> {
val image = o as Image
val image = other as Image
image.path.equals(path, ignoreCase = true)
}
}
}

override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + name.hashCode()
result = 31 * result + path.hashCode()
result = 31 * result + (uriHolder?.hashCode() ?: 0)
return result
}
}
3 changes: 3 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ package com.esafirm.sample
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.filters.LargeTest
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import androidx.test.rule.ActivityTestRule
import androidx.test.rule.GrantPermissionRule
import com.esafirm.sample.matchers.hasDrawable
import com.esafirm.sample.utils.Views
import com.schibsted.spain.barista.interaction.BaristaClickInteractions.clickOn
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.TypeSafeMatcher
import org.junit.Rule
import org.junit.Test
Expand All @@ -39,17 +36,18 @@ class PickImageSingleTest {

@Test
fun pickImage() {
val appCompatButton = Views.pickImageButton()
appCompatButton.perform(click())
clickOn(R.id.switch_return_after_capture)
clickOn(R.id.switch_single)

Views.pickImageButton().perform(click())

val recyclerView = Views.recyclersView()
recyclerView.perform(actionOnItemAtPosition<ViewHolder>(0, click()))

val appCompatTextView = onView(allOf(withId(R.id.text_view)))
appCompatTextView.perform(scrollTo(), click())
Views.waitFor(1_000)
clickOn(R.id.text_view)

val imageView = Views.imageDetail()

imageView.check(matches(isDisplayed()))
imageView.check(matches(hasDrawable()))
}
Expand Down
26 changes: 4 additions & 22 deletions sample/src/androidTest/java/com/esafirm/sample/PickImageTest.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.esafirm.sample


import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers.isClickable
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.filters.LargeTest
Expand All @@ -17,10 +16,7 @@ import androidx.test.rule.ActivityTestRule
import androidx.test.rule.GrantPermissionRule
import com.esafirm.sample.matchers.hasDrawable
import com.esafirm.sample.utils.Views
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.TypeSafeMatcher
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -48,28 +44,14 @@ class PickImageTest {
val actionMenuItemView = Views.pickerDoneButton()
actionMenuItemView.perform(click())

Views.waitFor(500)

val appCompatTextView = onView(allOf(withId(R.id.text_view)))
appCompatTextView.check(matches(isClickable()))
appCompatTextView.perform(scrollTo(), click())

val imageView = Views.imageDetail()
imageView.check(matches(isDisplayed()))
imageView.check(matches(hasDrawable()))
}

private fun childAtPosition(
parentMatcher: Matcher<View>, position: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("Child at position $position in parent ")
parentMatcher.describeTo(description)
}

public override fun matchesSafely(view: View): Boolean {
val parent = view.parent
return parent is ViewGroup && parentMatcher.matches(parent)
&& view == parent.getChildAt(position)
}
}
}
}
14 changes: 13 additions & 1 deletion sample/src/androidTest/java/com/esafirm/sample/utils/Views.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.esafirm.sample.utils

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ScrollView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.ViewInteraction
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
Expand All @@ -18,6 +20,14 @@ import org.hamcrest.TypeSafeMatcher
import org.hamcrest.core.IsInstanceOf

object Views {
fun waitFor(timeInMs: Long) {
Thread.sleep(timeInMs)
}

/* --------------------------------------------------- */
/* > Specific view */
/* --------------------------------------------------- */

fun pickImageButton(): ViewInteraction {
return onView(
allOf(withId(R.id.button_pick_image), withText("PICK IMAGE"),
Expand Down Expand Up @@ -52,7 +62,9 @@ object Views {
return onView(
allOf(ViewMatchers.withParent(allOf(withId(R.id.container),
ViewMatchers.withParent(IsInstanceOf.instanceOf(ScrollView::class.java)))),
isDisplayed()))
isDisplayed(),
isAssignableFrom(ImageView::class.java)
))
}

fun childAtPosition(
Expand Down
18 changes: 17 additions & 1 deletion sample/src/main/java/com/esafirm/sample/ImageViewerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package com.esafirm.sample

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Parcelable
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.setPadding
import com.bumptech.glide.Glide
import com.esafirm.imagepicker.model.Image
import java.util.*
import java.io.File
import java.util.ArrayList

class ImageViewerActivity : AppCompatActivity() {

Expand All @@ -26,6 +30,18 @@ class ImageViewerActivity : AppCompatActivity() {
.load(it.uri)
.into(imageView)
linearLayout.addView(imageView)

val textView = TextView(this).apply {
setPadding(24)
}

textView.setTextColor(Color.BLACK)
textView.text = """
Path: ${it.path}
Absolute Path: ${File(it.path).absolutePath}
Uri: ${it.uri}
""".trimIndent()
linearLayout.addView(textView)
}
}

Expand Down
2 changes: 0 additions & 2 deletions sample/src/main/java/com/esafirm/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import android.os.Environment
import androidx.appcompat.app.AppCompatActivity
import com.esafirm.imagepicker.features.*
import com.esafirm.imagepicker.features.cameraonly.CameraOnlyConfig
import com.esafirm.imagepicker.features.imageloader.DefaultImageLoader
import com.esafirm.imagepicker.features.imageloader.ImageLoader
import com.esafirm.imagepicker.model.Image
import com.esafirm.sample.databinding.ActivityMainBinding

Expand Down

0 comments on commit 5b4108d

Please sign in to comment.