Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert view background with a plain color #371

Merged
merged 5 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alpha != opacity? :S

Copy link
Member Author

@rocboronat rocboronat Sep 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🀷 honestly, I started comparing the values that seemed important. But don't know the difference between alpha and 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