Skip to content

Commit

Permalink
Update BackgroundMatcher to compare GradientDrawables (#384)
Browse files Browse the repository at this point in the history
* Update BackgroundMatcher to compare GradientDrawables

* Refactoring for Pre-Android-N devices + Added Tests + Fixed typo in test code

Co-authored-by: Stefan Arnold <[email protected]>
Co-authored-by: Roc Boronat <[email protected]>
Co-authored-by: Bernat Borrás Paronella <[email protected]>
Co-authored-by: Rafa Vázquez <[email protected]>
  • Loading branch information
5 people authored Jul 5, 2021
1 parent 7434890 commit 1b61a36
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.adevinta.android.barista.internal.matcher

import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import androidx.annotation.DrawableRes
import android.view.View
import com.adevinta.android.barista.internal.util.BitmapComparator
Expand Down Expand Up @@ -45,15 +47,33 @@ class BackgroundMatcher private constructor(@DrawableRes private val expectedDra
return false
}

if (expectedDrawable is ColorDrawable) {
val viewDrawable = view.background as ColorDrawable
return viewDrawable.color == expectedDrawable.color &&
viewDrawable.alpha == expectedDrawable.alpha &&
viewDrawable.opacity == expectedDrawable.opacity
return when (expectedDrawable) {
is ColorDrawable -> isSameColorDrawable(view.background as ColorDrawable, expectedDrawable)
is GradientDrawable -> isSameGradientDrawable(view.background as GradientDrawable, expectedDrawable)
else -> hasSameBitmap(view, expectedDrawable)
}
}

private fun hasSameBitmap(view: View, expectedDrawable: Drawable?): Boolean {
val viewBitmap = DrawableToBitmapConverter.getBitmap(view.background)
val expectedBitmap = DrawableToBitmapConverter.getBitmap(expectedDrawable)
return BitmapComparator.compare(viewBitmap, expectedBitmap)
}

private fun isSameColorDrawable(viewDrawable: ColorDrawable, expectedDrawable: ColorDrawable) =
viewDrawable.color == expectedDrawable.color &&
viewDrawable.alpha == expectedDrawable.alpha &&
viewDrawable.opacity == expectedDrawable.opacity

private fun isSameGradientDrawable(viewDrawable: GradientDrawable, expectedDrawable: GradientDrawable): Boolean {
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
viewDrawable.alpha == expectedDrawable.alpha &&
viewDrawable.opacity == expectedDrawable.opacity &&
viewDrawable.color == expectedDrawable.color &&
viewDrawable.shape == expectedDrawable.shape
} else {
val viewBitmap = DrawableToBitmapConverter.getBitmap(view.background)
val expectedBitmap = DrawableToBitmapConverter.getBitmap(expectedDrawable)
return BitmapComparator.compare(viewBitmap, expectedBitmap)
viewDrawable.alpha == expectedDrawable.alpha &&
viewDrawable.opacity == expectedDrawable.opacity
}
}

Expand All @@ -66,4 +86,4 @@ class BackgroundMatcher private constructor(@DrawableRes private val expectedDra
description.appendText("]")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,42 +311,52 @@ public void checkDrawable_withoutDrawable_failure() throws Exception {

@Test
public void checkBackgroundDrawable_withId() throws Exception {
assertHasBackground(R.id.view_with_drawable_backbround, R.drawable.ic_barista);
assertHasBackground(R.id.view_with_drawable_background, R.drawable.ic_barista);
}

@Test
public void checkBackgroundGradientDrawable_withId() throws Exception {
assertHasBackground(R.id.view_with_gradient_drawable_background, R.drawable.gradient_drawable);
}

@Test
public void checkBackgroundColor_withId() throws Exception {
assertHasBackground(R.id.view_with_color_backbround, R.color.red);
assertHasBackground(R.id.view_with_color_background, R.color.red);
}

@Test(expected = BaristaException.class)
public void checkBackgroundDrawable_withId_failure() throws Exception {
assertHasBackground(R.id.view_without_backbround, R.drawable.ic_action_menu);
assertHasBackground(R.id.view_without_background, R.drawable.ic_action_menu);
}

@Test(expected = BaristaException.class)
public void checkBackgroundGradientDrawable_withId_failure() throws Exception {
assertHasBackground(R.id.view_without_background, R.drawable.gradient_drawable);
}

@Test(expected = BaristaException.class)
public void checkBackgroundColor_withId_failure() throws Exception {
assertHasBackground(R.id.view_with_color_backbround, R.color.blue);
assertHasBackground(R.id.view_with_color_background, R.color.blue);
}

@Test
public void checkBackground_withAnyDrawable() throws Exception {
assertHasAnyBackground(R.id.view_with_drawable_backbround);
assertHasAnyBackground(R.id.view_with_drawable_background);
}

@Test(expected = BaristaException.class)
public void checkBackground_withAnyDrawable_failure() throws Exception {
assertHasAnyBackground(R.id.view_without_backbround);
assertHasAnyBackground(R.id.view_without_background);
}

@Test
public void checkBackground_withoutDrawable() throws Exception {
assertHasNoBackground(R.id.view_without_backbround);
assertHasNoBackground(R.id.view_without_background);
}

@Test(expected = BaristaException.class)
public void checkBackground_withoutDrawable_failure() throws Exception {
assertHasNoBackground(R.id.view_with_drawable_backbround);
assertHasNoBackground(R.id.view_with_drawable_background);
}

@Test
Expand Down
6 changes: 6 additions & 0 deletions sample/src/main/res/drawable/gradient_drawable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/blue" />
</shape>
13 changes: 10 additions & 3 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,28 @@
/>

<View
android:id="@+id/view_with_drawable_backbround"
android:id="@+id/view_with_drawable_background"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@drawable/ic_barista"
/>

<View
android:id="@+id/view_with_color_backbround"
android:id="@+id/view_with_gradient_drawable_background"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@drawable/gradient_drawable"
/>

<View
android:id="@+id/view_with_color_background"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@color/red"
/>

<View
android:id="@+id/view_without_backbround"
android:id="@+id/view_without_background"
android:layout_width="48dp"
android:layout_height="48dp"
/>
Expand Down

0 comments on commit 1b61a36

Please sign in to comment.