Skip to content

Commit

Permalink
Assert view background with a plain color (#371)
Browse files Browse the repository at this point in the history
* Rename the existing view to have a more specific name

* Add a new view with a color background instead of a drawable one

* Create tests to reproduce the expected behavior

* Manage plain ColorDrawables in addition to common Drawables
  • Loading branch information
rocboronat authored Oct 1, 2020
1 parent b5c0ac7 commit 8c38129
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.schibsted.spain.barista.internal.matcher

import android.graphics.drawable.ColorDrawable
import androidx.annotation.DrawableRes
import android.view.View
import com.schibsted.spain.barista.internal.util.BitmapComparator
Expand Down Expand Up @@ -44,9 +45,16 @@ class BackgroundMatcher private constructor(@DrawableRes private val expectedDra
return false
}

val viewBitmap = DrawableToBitmapConverter.getBitmap(view.background)
val expectedBitmap = DrawableToBitmapConverter.getBitmap(expectedDrawable)
return BitmapComparator.compare(viewBitmap, expectedBitmap)
if (expectedDrawable is ColorDrawable) {
val viewDrawable = view.background as ColorDrawable
return viewDrawable.color == expectedDrawable.color &&
viewDrawable.alpha == expectedDrawable.alpha &&
viewDrawable.opacity == expectedDrawable.opacity
} else {
val viewBitmap = DrawableToBitmapConverter.getBitmap(view.background)
val expectedBitmap = DrawableToBitmapConverter.getBitmap(expectedDrawable)
return BitmapComparator.compare(viewBitmap, expectedBitmap)
}
}

override fun describeTo(description: Description) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,28 @@ public void checkDrawable_withoutDrawable_failure() throws Exception {
}

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

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

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

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

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

@Test(expected = BaristaException.class)
Expand All @@ -331,7 +341,7 @@ public void checkBackground_withoutDrawable() throws Exception {

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

@Test
Expand Down
9 changes: 8 additions & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,19 @@
/>

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

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

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

0 comments on commit 8c38129

Please sign in to comment.