Import updated Android Setup Wizard Library 216634194

PiperOrigin-RevId: 216634194
Change-Id: I0a942bf9475129aebb77110e121b1b08af3cfe96
This commit is contained in:
Setup Wizard Team 2018-10-10 19:36:49 -07:00 committed by Maurice Lam
parent 233897fc17
commit 9368922267
22 changed files with 400 additions and 419 deletions

View file

@ -27,14 +27,14 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import com.android.setupwizardlib.view.CheckableLinearLayout; import com.android.setupwizardlib.view.CheckableLinearLayout;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class ExpandableSwitchItemTest { public class ExpandableSwitchItemTest {

View file

@ -34,14 +34,14 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Assume; import org.junit.Assume;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class SwitchItemTest { public class SwitchItemTest {

View file

@ -25,13 +25,13 @@ import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = Config.ALL_SDKS) @Config(sdk = Config.ALL_SDKS)
public class DimensionConsistencyTest { public class DimensionConsistencyTest {

View file

@ -26,17 +26,17 @@ import static org.robolectric.RuntimeEnvironment.application;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.OnScrollListener; import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class RecyclerViewScrollHandlingDelegateTest { public class RecyclerViewScrollHandlingDelegateTest {
@Mock private RequireScrollMixin mRequireScrollMixin; @Mock private RequireScrollMixin mRequireScrollMixin;

View file

@ -16,14 +16,8 @@
package com.android.setupwizardlib; package com.android.setupwizardlib;
import static org.hamcrest.Matchers.instanceOf; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import android.content.Context; import android.content.Context;
@ -40,7 +34,6 @@ import android.view.View;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.ScrollView; import android.widget.ScrollView;
import android.widget.TextView; import android.widget.TextView;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import com.android.setupwizardlib.template.ColoredHeaderMixin; import com.android.setupwizardlib.template.ColoredHeaderMixin;
import com.android.setupwizardlib.template.HeaderMixin; import com.android.setupwizardlib.template.HeaderMixin;
import com.android.setupwizardlib.template.IconMixin; import com.android.setupwizardlib.template.IconMixin;
@ -50,16 +43,17 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class GlifLayoutTest { public class GlifLayoutTest {
private Context context; private Context context;
@Before @Before
public void setUp() throws Exception { public void setUpContext() {
context = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); context = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light);
} }
@ -72,9 +66,11 @@ public class GlifLayoutTest {
@Test @Test
public void testSetHeaderText() { public void testSetHeaderText() {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
TextView title = (TextView) layout.findViewById(R.id.suw_layout_title); TextView title = layout.findViewById(R.id.suw_layout_title);
layout.setHeaderText("Abracadabra"); layout.setHeaderText("Abracadabra");
assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText()); assertWithMessage("Header text should be \"Abracadabra\"")
.that(title.getText().toString())
.isEqualTo("Abracadabra");
} }
@Test @Test
@ -86,14 +82,15 @@ public class GlifLayoutTest {
layout.addView(tv); layout.addView(tv);
assertDefaultTemplateInflated(layout); assertDefaultTemplateInflated(layout);
View view = layout.findViewById(testViewId); View view = layout.findViewById(testViewId);
assertSame("The view added should be the same text view", tv, view); assertThat(view).named("Text view added").isSameAs(tv);
} }
@Test @Test
public void testGetScrollView() { public void testGetScrollView() {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
assertNotNull( assertWithMessage("Get scroll view should not be null with default template")
"Get scroll view should not be null with default template", layout.getScrollView()); .that(layout.getScrollView())
.isNotNull();
} }
@Test @Test
@ -101,19 +98,18 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
layout.setProgressBarShown(true); layout.setProgressBarShown(true);
layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); layout.setPrimaryColor(ColorStateList.valueOf(Color.RED));
assertEquals( assertWithMessage("Primary color should be red")
"Primary color should be red", ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); .that(layout.getPrimaryColor())
.isEqualTo(ColorStateList.valueOf(Color.RED));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress); ProgressBar progressBar = layout.findViewById(R.id.suw_layout_progress);
assertEquals( assertThat(progressBar.getIndeterminateTintList())
"Progress bar should be tinted red", .named("indeterminate progress bar tint")
ColorStateList.valueOf(Color.RED), .isEqualTo(ColorStateList.valueOf(Color.RED));
progressBar.getIndeterminateTintList()); assertThat(progressBar.getProgressBackgroundTintList())
assertEquals( .named("determinate progress bar tint")
"Determinate progress bar should also be tinted red", .isEqualTo(ColorStateList.valueOf(Color.RED));
ColorStateList.valueOf(Color.RED),
progressBar.getProgressBackgroundTintList());
} }
} }
@ -123,22 +119,21 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
layout.setProgressBarShown(true); layout.setProgressBarShown(true);
layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); layout.setPrimaryColor(ColorStateList.valueOf(Color.RED));
assertEquals( assertWithMessage("Primary color should be red")
"Primary color should be red", ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); .that(layout.getPrimaryColor())
.isEqualTo(ColorStateList.valueOf(Color.RED));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress); ProgressBar progressBar = layout.findViewById(R.id.suw_layout_progress);
assertEquals( assertWithMessage("Progress bar should be tinted red")
"Progress bar should be tinted red", .that(progressBar.getIndeterminateTintList())
ColorStateList.valueOf(Color.RED), .isEqualTo(ColorStateList.valueOf(Color.RED));
progressBar.getIndeterminateTintList()); assertWithMessage("Determinate progress bar should also be tinted red")
assertEquals( .that(progressBar.getProgressBackgroundTintList())
"Determinate progress bar should also be tinted red", .isEqualTo(ColorStateList.valueOf(Color.RED));
ColorStateList.valueOf(Color.RED),
progressBar.getProgressBackgroundTintList());
} }
assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor()); assertThat(((GlifPatternDrawable) getTabletBackground(layout)).getColor()).isEqualTo(Color.RED);
} }
@Test @Test
@ -147,8 +142,8 @@ public class GlifLayoutTest {
layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE));
layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED));
assertEquals(Color.RED, ((GlifPatternDrawable) getPhoneBackground(layout)).getColor()); assertThat(((GlifPatternDrawable) getPhoneBackground(layout)).getColor()).isEqualTo(Color.RED);
assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor()); assertThat(layout.getBackgroundBaseColor().getDefaultColor()).isEqualTo(Color.RED);
} }
@Config(qualifiers = "sw600dp") @Config(qualifiers = "sw600dp")
@ -158,8 +153,8 @@ public class GlifLayoutTest {
layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE));
layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED));
assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor()); assertThat(((GlifPatternDrawable) getTabletBackground(layout)).getColor()).isEqualTo(Color.RED);
assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor()); assertThat(layout.getBackgroundBaseColor().getDefaultColor()).isEqualTo(Color.RED);
} }
@Test @Test
@ -167,8 +162,8 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
layout.setBackgroundPatterned(true); layout.setBackgroundPatterned(true);
assertThat(getPhoneBackground(layout), instanceOf(GlifPatternDrawable.class)); assertThat(getPhoneBackground(layout)).isInstanceOf(GlifPatternDrawable.class);
assertTrue("Background should be patterned", layout.isBackgroundPatterned()); assertThat(layout.isBackgroundPatterned()).named("background is patterned").isTrue();
} }
@Test @Test
@ -176,8 +171,8 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
layout.setBackgroundPatterned(false); layout.setBackgroundPatterned(false);
assertThat(getPhoneBackground(layout), instanceOf(ColorDrawable.class)); assertThat(getPhoneBackground(layout)).isInstanceOf(ColorDrawable.class);
assertFalse("Background should not be patterned", layout.isBackgroundPatterned()); assertThat(layout.isBackgroundPatterned()).named("background is patterned").isFalse();
} }
@Config(qualifiers = "sw600dp") @Config(qualifiers = "sw600dp")
@ -186,8 +181,8 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
layout.setBackgroundPatterned(true); layout.setBackgroundPatterned(true);
assertThat(getTabletBackground(layout), instanceOf(GlifPatternDrawable.class)); assertThat(getTabletBackground(layout)).isInstanceOf(GlifPatternDrawable.class);
assertTrue("Background should be patterned", layout.isBackgroundPatterned()); assertThat(layout.isBackgroundPatterned()).named("background is patterned").isTrue();
} }
@Config(qualifiers = "sw600dp") @Config(qualifiers = "sw600dp")
@ -196,8 +191,8 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
layout.setBackgroundPatterned(false); layout.setBackgroundPatterned(false);
assertThat(getTabletBackground(layout), instanceOf(ColorDrawable.class)); assertThat(getTabletBackground(layout)).isInstanceOf(ColorDrawable.class);
assertFalse("Background should not be patterned", layout.isBackgroundPatterned()); assertThat(layout.isBackgroundPatterned()).named("background is patterned").isFalse();
} }
@Test @Test
@ -210,32 +205,32 @@ public class GlifLayoutTest {
@Test @Test
public void testPeekProgressBarNull() { public void testPeekProgressBarNull() {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
assertNull("PeekProgressBar should return null initially", layout.peekProgressBar()); assertWithMessage("PeekProgressBar should return null initially")
.that(layout.peekProgressBar())
.isNull();
} }
@Test @Test
public void testPeekProgressBar() { public void testPeekProgressBar() {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
layout.setProgressBarShown(true); layout.setProgressBarShown(true);
assertNotNull( assertWithMessage("Peek progress bar should return the bar after setProgressBarShown(true)")
"Peek progress bar should return the bar after setProgressBarShown(true)", .that(layout.peekProgressBar())
layout.peekProgressBar()); .isNotNull();
} }
@Test @Test
public void testMixins() { public void testMixins() {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
final HeaderMixin header = layout.getMixin(HeaderMixin.class); final HeaderMixin header = layout.getMixin(HeaderMixin.class);
assertTrue( assertThat(header).named("header").isInstanceOf(ColoredHeaderMixin.class);
"Header should be instance of ColoredHeaderMixin. "
+ "Found "
+ header.getClass()
+ " instead.",
header instanceof ColoredHeaderMixin);
assertNotNull("GlifLayout should have icon mixin", layout.getMixin(IconMixin.class)); assertWithMessage("GlifLayout should have icon mixin")
assertNotNull( .that(layout.getMixin(IconMixin.class))
"GlifLayout should have progress bar mixin", layout.getMixin(ProgressBarMixin.class)); .isNotNull();
assertWithMessage("GlifLayout should have progress bar mixin")
.that(layout.getMixin(ProgressBarMixin.class))
.isNotNull();
} }
@Test @Test
@ -243,8 +238,8 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
final View view = layout.inflateFooter(android.R.layout.simple_list_item_1); final View view = layout.inflateFooter(android.R.layout.simple_list_item_1);
assertEquals(android.R.id.text1, view.getId()); assertThat(view.getId()).isEqualTo(android.R.id.text1);
assertNotNull(layout.findViewById(android.R.id.text1)); assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull();
} }
@Config(qualifiers = "sw600dp") @Config(qualifiers = "sw600dp")
@ -258,8 +253,8 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context, R.layout.suw_glif_blank_template); GlifLayout layout = new GlifLayout(context, R.layout.suw_glif_blank_template);
final View view = layout.inflateFooter(android.R.layout.simple_list_item_1); final View view = layout.inflateFooter(android.R.layout.simple_list_item_1);
assertEquals(android.R.id.text1, view.getId()); assertThat(view.getId()).isEqualTo(android.R.id.text1);
assertNotNull(layout.findViewById(android.R.id.text1)); assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull();
} }
@Config(qualifiers = "sw600dp") @Config(qualifiers = "sw600dp")
@ -277,7 +272,7 @@ public class GlifLayoutTest {
.addAttribute(R.attr.suwFooter, "@android:layout/simple_list_item_1") .addAttribute(R.attr.suwFooter, "@android:layout/simple_list_item_1")
.build()); .build());
assertNotNull(layout.findViewById(android.R.id.text1)); assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull();
} }
@Test @Test
@ -285,8 +280,8 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context); GlifLayout layout = new GlifLayout(context);
final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1); final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1);
assertEquals(android.R.id.text1, view.getId()); assertThat(view.getId()).isEqualTo(android.R.id.text1);
assertNotNull(layout.findViewById(android.R.id.text1)); assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull();
} }
@Config(qualifiers = "sw600dp") @Config(qualifiers = "sw600dp")
@ -304,7 +299,7 @@ public class GlifLayoutTest {
.addAttribute(R.attr.suwStickyHeader, "@android:layout/simple_list_item_1") .addAttribute(R.attr.suwStickyHeader, "@android:layout/simple_list_item_1")
.build()); .build());
assertNotNull(layout.findViewById(android.R.id.text1)); assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull();
} }
@Test @Test
@ -312,8 +307,8 @@ public class GlifLayoutTest {
GlifLayout layout = new GlifLayout(context, R.layout.suw_glif_blank_template); GlifLayout layout = new GlifLayout(context, R.layout.suw_glif_blank_template);
final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1); final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1);
assertEquals(android.R.id.text1, view.getId()); assertThat(view.getId()).isEqualTo(android.R.id.text1);
assertNotNull(layout.findViewById(android.R.id.text1)); assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull();
} }
@Config(qualifiers = "sw600dp") @Config(qualifiers = "sw600dp")
@ -327,9 +322,8 @@ public class GlifLayoutTest {
public void createFromXml_shouldSetLayoutFullscreen_whenLayoutFullscreenIsNotSet() { public void createFromXml_shouldSetLayoutFullscreen_whenLayoutFullscreenIsNotSet() {
GlifLayout layout = new GlifLayout(context, Robolectric.buildAttributeSet().build()); GlifLayout layout = new GlifLayout(context, Robolectric.buildAttributeSet().build());
if (VERSION.SDK_INT >= VERSION_CODES.M) { if (VERSION.SDK_INT >= VERSION_CODES.M) {
assertEquals( assertThat(layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, .isEqualTo(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} }
} }
@ -342,12 +336,11 @@ public class GlifLayoutTest {
.addAttribute(R.attr.suwLayoutFullscreen, "false") .addAttribute(R.attr.suwLayoutFullscreen, "false")
.build()); .build());
assertEquals(0, layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); assertThat(layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN).isEqualTo(0);
} }
private Drawable getPhoneBackground(GlifLayout layout) { private Drawable getPhoneBackground(GlifLayout layout) {
final StatusBarBackgroundLayout patternBg = final StatusBarBackgroundLayout patternBg = layout.findManagedViewById(R.id.suw_pattern_bg);
(StatusBarBackgroundLayout) layout.findManagedViewById(R.id.suw_pattern_bg);
return patternBg.getStatusBarBackground(); return patternBg.getStatusBarBackground();
} }
@ -358,12 +351,14 @@ public class GlifLayoutTest {
private void assertDefaultTemplateInflated(GlifLayout layout) { private void assertDefaultTemplateInflated(GlifLayout layout) {
View title = layout.findViewById(R.id.suw_layout_title); View title = layout.findViewById(R.id.suw_layout_title);
assertNotNull("@id/suw_layout_title should not be null", title); assertWithMessage("@id/suw_layout_title should not be null").that(title).isNotNull();
View icon = layout.findViewById(R.id.suw_layout_icon); View icon = layout.findViewById(R.id.suw_layout_icon);
assertNotNull("@id/suw_layout_icon should not be null", icon); assertWithMessage("@id/suw_layout_icon should not be null").that(icon).isNotNull();
View scrollView = layout.findViewById(R.id.suw_scroll_view); View scrollView = layout.findViewById(R.id.suw_scroll_view);
assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView); assertWithMessage("@id/suw_scroll_view should be a ScrollView")
.that(scrollView instanceof ScrollView)
.isTrue();
} }
} }

View file

@ -23,17 +23,17 @@ import static org.robolectric.RuntimeEnvironment.application;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewConfiguration; import android.view.ViewConfiguration;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ConsecutiveTapsGestureDetectorTest { public class ConsecutiveTapsGestureDetectorTest {
@Mock private ConsecutiveTapsGestureDetector.OnConsecutiveTapsListener listener; @Mock private ConsecutiveTapsGestureDetector.OnConsecutiveTapsListener listener;

View file

@ -16,12 +16,8 @@
package com.android.setupwizardlib.items; package com.android.setupwizardlib.items;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertFalse; import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.same; import static org.mockito.Matchers.same;
@ -31,7 +27,6 @@ import static org.mockito.Mockito.verify;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import android.content.Context; import android.content.Context;
import android.text.TextUtils;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -40,13 +35,13 @@ import android.widget.FrameLayout;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.items.ButtonItem.OnClickListener; import com.android.setupwizardlib.items.ButtonItem.OnClickListener;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class ButtonItemTest { public class ButtonItemTest {
@ -63,12 +58,11 @@ public class ButtonItemTest {
public void testDefaultItem() { public void testDefaultItem() {
ButtonItem item = new ButtonItem(); ButtonItem item = new ButtonItem();
assertTrue("ButtonItem should be enabled by default", item.isEnabled()); assertThat(item.isEnabled()).named("enabled").isTrue();
assertEquals("ButtonItem should return count = 0", 0, item.getCount()); assertThat(item.getCount()).named("count").isEqualTo(0);
assertEquals("ButtonItem should return layout resource = 0", 0, item.getLayoutResource()); assertThat(item.getLayoutResource()).named("layout resource").isEqualTo(0);
assertEquals( assertThat(item.getTheme()).named("theme").isEqualTo(R.style.SuwButtonItem);
"Default theme should be @style/SuwButtonItem", R.style.SuwButtonItem, item.getTheme()); assertThat(item.getText()).named("text").isNull();
assertNull("Default text should be null", item.getText());
} }
@Test @Test
@ -88,8 +82,8 @@ public class ButtonItemTest {
TestButtonItem item = new TestButtonItem(); TestButtonItem item = new TestButtonItem();
final Button button = item.createButton(parent); final Button button = item.createButton(parent);
assertTrue("Default button should be enabled", button.isEnabled()); assertThat(button.isEnabled()).named("enabled").isTrue();
assertTrue("Default button text should be empty", TextUtils.isEmpty(button.getText())); assertThat(button.getText().toString()).isEmpty();
} }
@Test @Test
@ -98,7 +92,9 @@ public class ButtonItemTest {
final int id = 12345; final int id = 12345;
item.setId(id); item.setId(id);
assertEquals("Button's id should be set", id, item.createButton(parent).getId()); assertWithMessage("Button's id should be set")
.that(item.createButton(parent).getId())
.isEqualTo(id);
} }
@Test @Test
@ -110,8 +106,10 @@ public class ButtonItemTest {
frameLayout.addView(button); frameLayout.addView(button);
final Button button2 = item.createButton(parent); final Button button2 = item.createButton(parent);
assertSame("createButton should be reused", button, button2); assertWithMessage("createButton should be reused").that(button2).isSameAs(button);
assertNull("Should be removed from parent after createButton", button2.getParent()); assertWithMessage("Should be removed from parent after createButton")
.that(button2.getParent())
.isNull();
} }
@Test @Test
@ -120,8 +118,8 @@ public class ButtonItemTest {
item.setEnabled(true); item.setEnabled(true);
final Button button = item.createButton(parent); final Button button = item.createButton(parent);
assertTrue("ButtonItem should be enabled", item.isEnabled()); assertWithMessage("ButtonItem should be enabled").that(item.isEnabled()).isTrue();
assertTrue("Button should be enabled", button.isEnabled()); assertWithMessage("Button should be enabled").that(button.isEnabled()).isTrue();
} }
@Test @Test
@ -130,8 +128,8 @@ public class ButtonItemTest {
item.setEnabled(false); item.setEnabled(false);
final Button button = item.createButton(parent); final Button button = item.createButton(parent);
assertFalse("ButtonItem should be disabled", item.isEnabled()); assertWithMessage("ButtonItem should be disabled").that(item.isEnabled()).isFalse();
assertFalse("Button should be disabled", button.isEnabled()); assertWithMessage("Button should be disabled").that(button.isEnabled()).isFalse();
} }
@Test @Test
@ -140,8 +138,12 @@ public class ButtonItemTest {
item.setText("lorem ipsum"); item.setText("lorem ipsum");
final Button button = item.createButton(parent); final Button button = item.createButton(parent);
assertEquals("ButtonItem text should be \"lorem ipsum\"", "lorem ipsum", item.getText()); assertWithMessage("ButtonItem text should be \"lorem ipsum\"")
assertEquals("Button text should be \"lorem ipsum\"", "lorem ipsum", button.getText()); .that(item.getText().toString())
.isEqualTo("lorem ipsum");
assertWithMessage("Button text should be \"lorem ipsum\"")
.that(button.getText().toString())
.isEqualTo("lorem ipsum");
} }
@Test @Test
@ -150,11 +152,10 @@ public class ButtonItemTest {
item.setTheme(R.style.SuwButtonItem_Colored); item.setTheme(R.style.SuwButtonItem_Colored);
final Button button = item.createButton(parent); final Button button = item.createButton(parent);
assertEquals( assertWithMessage("ButtonItem theme should be SuwButtonItem.Colored")
"ButtonItem theme should be SuwButtonItem.Colored", .that(item.getTheme())
R.style.SuwButtonItem_Colored, .isEqualTo(R.style.SuwButtonItem_Colored);
item.getTheme()); assertThat(button.getContext().getTheme()).isNotNull();
assertNotNull(button.getContext().getTheme());
} }
@Test @Test

View file

@ -16,24 +16,22 @@
package com.android.setupwizardlib.items; package com.android.setupwizardlib.items;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class ItemGroupTest { public class ItemGroupTest {
@ -58,9 +56,13 @@ public class ItemGroupTest {
itemGroup.addChild(CHILD_1); itemGroup.addChild(CHILD_1);
itemGroup.addChild(CHILD_2); itemGroup.addChild(CHILD_2);
assertSame("Item at position 0 should be child1", CHILD_1, itemGroup.getItemAt(0)); assertWithMessage("Item at position 0 should be child1")
assertSame("Item at position 1 should be child2", CHILD_2, itemGroup.getItemAt(1)); .that(itemGroup.getItemAt(0))
assertEquals("Should have 2 children", 2, itemGroup.getCount()); .isSameAs(CHILD_1);
assertWithMessage("Item at position 1 should be child2")
.that(itemGroup.getItemAt(1))
.isSameAs(CHILD_2);
assertWithMessage("Should have 2 children").that(itemGroup.getCount()).isEqualTo(2);
final InOrder inOrder = inOrder(observer); final InOrder inOrder = inOrder(observer);
inOrder.verify(observer).onItemRangeInserted(eq(itemGroup), eq(0), eq(1)); inOrder.verify(observer).onItemRangeInserted(eq(itemGroup), eq(0), eq(1));
@ -75,9 +77,13 @@ public class ItemGroupTest {
itemGroup.removeChild(CHILD_2); itemGroup.removeChild(CHILD_2);
assertSame("Item at position 0 should be child1", CHILD_1, itemGroup.getItemAt(0)); assertWithMessage("Item at position 0 should be child1")
assertSame("Item at position 1 should be child3", CHILD_3, itemGroup.getItemAt(1)); .that(itemGroup.getItemAt(0))
assertEquals("Should have 2 children", 2, itemGroup.getCount()); .isSameAs(CHILD_1);
assertWithMessage("Item at position 1 should be child3")
.that(itemGroup.getItemAt(1))
.isSameAs(CHILD_3);
assertWithMessage("Should have 2 children").that(itemGroup.getCount()).isEqualTo(2);
verify(observer).onItemRangeRemoved(eq(itemGroup), eq(1), eq(1)); verify(observer).onItemRangeRemoved(eq(itemGroup), eq(1), eq(1));
} }
@ -89,7 +95,7 @@ public class ItemGroupTest {
itemGroup.clear(); itemGroup.clear();
assertEquals("Should have 0 child", 0, itemGroup.getCount()); assertWithMessage("Should have 0 child").that(itemGroup.getCount()).isEqualTo(0);
verify(observer).onItemRangeRemoved(eq(itemGroup), eq(0), eq(2)); verify(observer).onItemRangeRemoved(eq(itemGroup), eq(0), eq(2));
} }
@ -106,10 +112,18 @@ public class ItemGroupTest {
parentGroup.addChild(childGroup); parentGroup.addChild(childGroup);
parentGroup.addChild(CHILD_4); parentGroup.addChild(CHILD_4);
assertSame("Position 0 should be child 1", CHILD_1, parentGroup.getItemAt(0)); assertWithMessage("Position 0 should be child 1")
assertSame("Position 1 should be child 2", CHILD_2, parentGroup.getItemAt(1)); .that(parentGroup.getItemAt(0))
assertSame("Position 2 should be child 3", CHILD_3, parentGroup.getItemAt(2)); .isSameAs(CHILD_1);
assertSame("Position 3 should be child 4", CHILD_4, parentGroup.getItemAt(3)); assertWithMessage("Position 1 should be child 2")
.that(parentGroup.getItemAt(1))
.isSameAs(CHILD_2);
assertWithMessage("Position 2 should be child 3")
.that(parentGroup.getItemAt(2))
.isSameAs(CHILD_3);
assertWithMessage("Position 3 should be child 4")
.that(parentGroup.getItemAt(3))
.isSameAs(CHILD_4);
final InOrder inOrder = inOrder(observer); final InOrder inOrder = inOrder(observer);
inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(0), eq(1));
@ -246,8 +260,12 @@ public class ItemGroupTest {
parentGroup.addChild(childGroup); parentGroup.addChild(childGroup);
parentGroup.addChild(CHILD_2); parentGroup.addChild(CHILD_2);
assertSame("Position 0 should be child 1", CHILD_1, parentGroup.getItemAt(0)); assertWithMessage("Position 0 should be child 1")
assertSame("Position 1 should be child 2", CHILD_2, parentGroup.getItemAt(1)); .that(parentGroup.getItemAt(0))
.isSameAs(CHILD_1);
assertWithMessage("Position 1 should be child 2")
.that(parentGroup.getItemAt(1))
.isSameAs(CHILD_2);
} }
@Test @Test
@ -258,7 +276,9 @@ public class ItemGroupTest {
itemGroup.addChild(CHILD_1); itemGroup.addChild(CHILD_1);
itemGroup.addChild(CHILD_2); itemGroup.addChild(CHILD_2);
assertSame("Find item 23456 should return child 2", CHILD_2, itemGroup.findItemById(23456)); assertWithMessage("Find item 23456 should return child 2")
.that(itemGroup.findItemById(23456))
.isSameAs(CHILD_2);
} }
@Test @Test
@ -269,7 +289,9 @@ public class ItemGroupTest {
itemGroup.addChild(CHILD_1); itemGroup.addChild(CHILD_1);
itemGroup.addChild(CHILD_2); itemGroup.addChild(CHILD_2);
assertNull("ID not found should return null", itemGroup.findItemById(56789)); assertWithMessage("ID not found should return null")
.that(itemGroup.findItemById(56789))
.isNull();
} }
/** /**

View file

@ -1,35 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.setupwizardlib.robolectric;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
public class SuwLibRobolectricTestRunner extends RobolectricTestRunner {
public SuwLibRobolectricTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
System.out.println("===== Running " + method + " =====");
super.runChild(method, notifier);
}
}

View file

@ -17,7 +17,7 @@
package com.android.setupwizardlib.span; package com.android.setupwizardlib.span;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertSame; import static com.google.common.truth.Truth.assertWithMessage;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import android.content.Context; import android.content.Context;
@ -26,11 +26,11 @@ import android.text.Selection;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.widget.TextView; import android.widget.TextView;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class LinkSpanTest { public class LinkSpanTest {
@Test @Test
@ -41,7 +41,9 @@ public class LinkSpanTest {
linkSpan.onClick(textView); linkSpan.onClick(textView);
assertSame("Clicked LinkSpan should be passed to setup", linkSpan, context.clickedSpan); assertWithMessage("Clicked LinkSpan should be passed to setup")
.that(context.clickedSpan)
.isSameAs(linkSpan);
} }
@Test @Test
@ -63,7 +65,9 @@ public class LinkSpanTest {
final LinkSpan linkSpan = new LinkSpan("test_id"); final LinkSpan linkSpan = new LinkSpan("test_id");
linkSpan.onClick(textView); linkSpan.onClick(textView);
assertSame("Clicked LinkSpan should be passed to setup", linkSpan, context.clickedSpan); assertWithMessage("Clicked LinkSpan should be passed to setup")
.that(context.clickedSpan)
.isSameAs(linkSpan);
} }
@Test @Test

View file

@ -25,24 +25,22 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.ListView; import android.widget.ListView;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows; import org.robolectric.Shadows;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowListView;
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ListViewScrollHandlingDelegateTest { public class ListViewScrollHandlingDelegateTest {
@Mock private RequireScrollMixin requireScrollMixin; @Mock private RequireScrollMixin requireScrollMixin;
private ListView listView; private ListView listView;
private ShadowListView shadowListView;
private ListViewScrollHandlingDelegate delegate; private ListViewScrollHandlingDelegate delegate;
@Before @Before
@ -51,7 +49,6 @@ public class ListViewScrollHandlingDelegateTest {
listView = new TestListView(application); listView = new TestListView(application);
delegate = new ListViewScrollHandlingDelegate(requireScrollMixin, listView); delegate = new ListViewScrollHandlingDelegate(requireScrollMixin, listView);
shadowListView = Shadows.shadowOf(listView);
listView.layout(0, 0, 50, 50); listView.layout(0, 0, 50, 50);
} }
@ -69,7 +66,7 @@ public class ListViewScrollHandlingDelegateTest {
verify(requireScrollMixin).notifyScrollabilityChange(true); verify(requireScrollMixin).notifyScrollabilityChange(true);
shadowListView.getOnScrollListener().onScroll(listView, 2, 20, 20); Shadows.shadowOf(listView).getOnScrollListener().onScroll(listView, 2, 20, 20);
verify(requireScrollMixin).notifyScrollabilityChange(false); verify(requireScrollMixin).notifyScrollabilityChange(false);
} }
@ -77,7 +74,7 @@ public class ListViewScrollHandlingDelegateTest {
@Test @Test
public void testPageScrollDown() throws Throwable { public void testPageScrollDown() throws Throwable {
delegate.pageScrollDown(); delegate.pageScrollDown();
assertThat(shadowListView.getLastSmoothScrollByDistance()).isEqualTo(50); assertThat(Shadows.shadowOf(listView).getLastSmoothScrollByDistance()).isEqualTo(50);
} }
private static class TestListView extends ListView { private static class TestListView extends ListView {

View file

@ -16,10 +16,8 @@
package com.android.setupwizardlib.template; package com.android.setupwizardlib.template;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertFalse; import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -34,7 +32,6 @@ import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
import com.android.setupwizardlib.GlifLayout; import com.android.setupwizardlib.GlifLayout;
import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.TemplateLayout;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import com.android.setupwizardlib.template.RequireScrollMixin.OnRequireScrollStateChangedListener; import com.android.setupwizardlib.template.RequireScrollMixin.OnRequireScrollStateChangedListener;
import com.android.setupwizardlib.template.RequireScrollMixin.ScrollHandlingDelegate; import com.android.setupwizardlib.template.RequireScrollMixin.ScrollHandlingDelegate;
import com.android.setupwizardlib.view.NavigationBar; import com.android.setupwizardlib.view.NavigationBar;
@ -43,10 +40,11 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class RequireScrollMixinTest { public class RequireScrollMixinTest {
@Mock private ScrollHandlingDelegate delegate; @Mock private ScrollHandlingDelegate delegate;
@ -73,30 +71,31 @@ public class RequireScrollMixinTest {
public void testScrollStateChangedListener() { public void testScrollStateChangedListener() {
OnRequireScrollStateChangedListener listener = mock(OnRequireScrollStateChangedListener.class); OnRequireScrollStateChangedListener listener = mock(OnRequireScrollStateChangedListener.class);
requireScrollMixin.setOnRequireScrollStateChangedListener(listener); requireScrollMixin.setOnRequireScrollStateChangedListener(listener);
assertFalse( assertWithMessage("Scrolling should not be required initially")
"Scrolling should not be required initially", requireScrollMixin.isScrollingRequired()); .that(requireScrollMixin.isScrollingRequired())
.isFalse();
requireScrollMixin.notifyScrollabilityChange(true); requireScrollMixin.notifyScrollabilityChange(true);
verify(listener).onRequireScrollStateChanged(true); verify(listener).onRequireScrollStateChanged(true);
assertTrue( assertWithMessage("Scrolling should be required when there is more content below the fold")
"Scrolling should be required when there is more content below the fold", .that(requireScrollMixin.isScrollingRequired())
requireScrollMixin.isScrollingRequired()); .isTrue();
requireScrollMixin.notifyScrollabilityChange(false); requireScrollMixin.notifyScrollabilityChange(false);
verify(listener).onRequireScrollStateChanged(false); verify(listener).onRequireScrollStateChanged(false);
assertFalse( assertWithMessage("Scrolling should not be required after scrolling to bottom")
"Scrolling should not be required after scrolling to bottom", .that(requireScrollMixin.isScrollingRequired())
requireScrollMixin.isScrollingRequired()); .isFalse();
// Once the user has scrolled to the bottom, they should not be forced to scroll down again // Once the user has scrolled to the bottom, they should not be forced to scroll down again
requireScrollMixin.notifyScrollabilityChange(true); requireScrollMixin.notifyScrollabilityChange(true);
verifyNoMoreInteractions(listener); verifyNoMoreInteractions(listener);
assertFalse( assertWithMessage("Scrolling should not be required after scrolling to bottom once")
"Scrolling should not be required after scrolling to bottom once", .that(requireScrollMixin.isScrollingRequired())
requireScrollMixin.isScrollingRequired()); .isFalse();
assertSame(listener, requireScrollMixin.getOnRequireScrollStateChangedListener()); assertThat(requireScrollMixin.getOnRequireScrollStateChangedListener()).isSameAs(listener);
} }
@Test @Test
@ -123,23 +122,23 @@ public class RequireScrollMixinTest {
requireScrollMixin.requireScrollWithNavigationBar(navigationBar); requireScrollMixin.requireScrollWithNavigationBar(navigationBar);
requireScrollMixin.notifyScrollabilityChange(true); requireScrollMixin.notifyScrollabilityChange(true);
assertEquals( assertWithMessage("More button should be visible")
"More button should be visible", .that(navigationBar.getMoreButton().getVisibility())
View.VISIBLE, .isEqualTo(View.VISIBLE);
navigationBar.getMoreButton().getVisibility()); assertWithMessage("Next button should be hidden")
assertEquals( .that(navigationBar.getNextButton().getVisibility())
"Next button should be hidden", View.GONE, navigationBar.getNextButton().getVisibility()); .isEqualTo(View.GONE);
navigationBar.getMoreButton().performClick(); navigationBar.getMoreButton().performClick();
verify(delegate).pageScrollDown(); verify(delegate).pageScrollDown();
requireScrollMixin.notifyScrollabilityChange(false); requireScrollMixin.notifyScrollabilityChange(false);
assertEquals( assertWithMessage("More button should be hidden")
"More button should be hidden", View.GONE, navigationBar.getMoreButton().getVisibility()); .that(navigationBar.getMoreButton().getVisibility())
assertEquals( .isEqualTo(View.GONE);
"Next button should be visible", assertWithMessage("Next button should be visible")
View.VISIBLE, .that(navigationBar.getNextButton().getVisibility())
navigationBar.getNextButton().getVisibility()); .isEqualTo(View.VISIBLE);
} }
@SuppressLint("SetTextI18n") // It's OK for testing @SuppressLint("SetTextI18n") // It's OK for testing
@ -150,16 +149,18 @@ public class RequireScrollMixinTest {
OnClickListener wrappedListener = mock(OnClickListener.class); OnClickListener wrappedListener = mock(OnClickListener.class);
requireScrollMixin.requireScrollWithButton(button, "TestMoreLabel", wrappedListener); requireScrollMixin.requireScrollWithButton(button, "TestMoreLabel", wrappedListener);
assertEquals("Button label should be kept initially", "OriginalLabel", button.getText()); assertWithMessage("Button label should be kept initially")
.that(button.getText().toString())
.isEqualTo("OriginalLabel");
requireScrollMixin.notifyScrollabilityChange(true); requireScrollMixin.notifyScrollabilityChange(true);
assertEquals("TestMoreLabel", button.getText()); assertThat(button.getText().toString()).isEqualTo("TestMoreLabel");
button.performClick(); button.performClick();
verify(wrappedListener, never()).onClick(eq(button)); verify(wrappedListener, never()).onClick(eq(button));
verify(delegate).pageScrollDown(); verify(delegate).pageScrollDown();
requireScrollMixin.notifyScrollabilityChange(false); requireScrollMixin.notifyScrollabilityChange(false);
assertEquals("OriginalLabel", button.getText()); assertThat(button.getText().toString()).isEqualTo("OriginalLabel");
button.performClick(); button.performClick();
verify(wrappedListener).onClick(eq(button)); verify(wrappedListener).onClick(eq(button));
} }

View file

@ -21,17 +21,17 @@ import static org.mockito.Mockito.verify;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import android.view.View; import android.view.View;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import com.android.setupwizardlib.view.BottomScrollView; import com.android.setupwizardlib.view.BottomScrollView;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ScrollViewScrollHandlingDelegateTest { public class ScrollViewScrollHandlingDelegateTest {
@Mock private RequireScrollMixin requireScrollMixin; @Mock private RequireScrollMixin requireScrollMixin;

View file

@ -16,7 +16,7 @@
package com.android.setupwizardlib.util; package com.android.setupwizardlib.util;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertWithMessage;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import android.content.Context; import android.content.Context;
@ -26,13 +26,13 @@ import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = Config.ALL_SDKS) @Config(sdk = Config.ALL_SDKS)
public class GlifDimensionTest { public class GlifDimensionTest {
@ -61,17 +61,17 @@ public class GlifDimensionTest {
final int marginSides = a.getDimensionPixelSize(0, 0); final int marginSides = a.getDimensionPixelSize(0, 0);
a.recycle(); a.recycle();
assertEquals( assertWithMessage(
"Dimensions should satisfy constraint: " "Dimensions should satisfy constraint: "
+ "?attr/suwMarginSides = suw_items_glif_text_divider_inset", + "?attr/suwMarginSides = suw_items_glif_text_divider_inset")
marginSides, .that(res.getDimensionPixelSize(R.dimen.suw_items_glif_text_divider_inset))
res.getDimensionPixelSize(R.dimen.suw_items_glif_text_divider_inset)); .isEqualTo(marginSides);
assertEquals( assertWithMessage(
"Dimensions should satisfy constraint: ?attr/suwMarginSides + " "Dimensions should satisfy constraint: ?attr/suwMarginSides + "
+ "suw_items_icon_container_width = suw_items_glif_icon_divider_inset", + "suw_items_icon_container_width = suw_items_glif_icon_divider_inset")
marginSides + res.getDimensionPixelSize(R.dimen.suw_items_icon_container_width), .that(res.getDimensionPixelSize(R.dimen.suw_items_glif_icon_divider_inset))
res.getDimensionPixelSize(R.dimen.suw_items_glif_icon_divider_inset)); .isEqualTo(marginSides + res.getDimensionPixelSize(R.dimen.suw_items_icon_container_width));
} }
@Test @Test
@ -92,17 +92,17 @@ public class GlifDimensionTest {
final int marginSides = a.getDimensionPixelSize(0, 0); final int marginSides = a.getDimensionPixelSize(0, 0);
a.recycle(); a.recycle();
assertEquals( assertWithMessage(
"Dimensions should satisfy constraint: ?attr/suwMarginSides - " "Dimensions should satisfy constraint: ?attr/suwMarginSides - "
+ "4dp (internal padding of button) = suw_glif_button_margin_end", + "4dp (internal padding of button) = suw_glif_button_margin_end")
marginSides - dp2Px(4), .that(res.getDimensionPixelSize(R.dimen.suw_glif_button_margin_end))
res.getDimensionPixelSize(R.dimen.suw_glif_button_margin_end)); .isEqualTo(marginSides - dp2Px(4));
assertEquals( assertWithMessage(
"Dimensions should satisfy constraint: ?attr/suwMarginSides - " "Dimensions should satisfy constraint: ?attr/suwMarginSides - "
+ "suw_glif_button_padding = suw_glif_button_margin_start", + "suw_glif_button_padding = suw_glif_button_margin_start")
marginSides - res.getDimensionPixelSize(R.dimen.suw_glif_button_padding), .that(res.getDimensionPixelSize(R.dimen.suw_glif_button_margin_start))
res.getDimensionPixelSize(R.dimen.suw_glif_button_margin_start)); .isEqualTo(marginSides - res.getDimensionPixelSize(R.dimen.suw_glif_button_padding));
} }
private int dp2Px(float dp) { private int dp2Px(float dp) {

View file

@ -17,8 +17,6 @@
package com.android.setupwizardlib.util; package com.android.setupwizardlib.util;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import android.annotation.TargetApi; import android.annotation.TargetApi;
@ -30,18 +28,19 @@ import android.os.Bundle;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter; import org.robolectric.util.ReflectionHelpers.ClassParameter;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class GlifStyleTest { public class GlifStyleTest {
@ -58,7 +57,8 @@ public class GlifStyleTest {
if (VERSION.SDK_INT < VERSION_CODES.M) { if (VERSION.SDK_INT < VERSION_CODES.M) {
// Robolectric resolved the wrong theme attribute on versions >= M // Robolectric resolved the wrong theme attribute on versions >= M
// https://github.com/robolectric/robolectric/issues/2940 // https://github.com/robolectric/robolectric/issues/2940
assertEquals("ff4285f4", Integer.toHexString(button.getTextColors().getDefaultColor())); assertThat(Integer.toHexString(button.getTextColors().getDefaultColor()))
.isEqualTo("ff4285f4");
} }
} }
@ -67,7 +67,7 @@ public class GlifStyleTest {
@Test @Test
public void glifThemeLight_statusBarColorShouldBeTransparent() { public void glifThemeLight_statusBarColorShouldBeTransparent() {
GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class);
assertEquals(0x00000000, activity.getWindow().getStatusBarColor()); assertThat(activity.getWindow().getStatusBarColor()).isEqualTo(0x00000000);
} }
@Test @Test
@ -75,9 +75,8 @@ public class GlifStyleTest {
GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class);
activity.setContentView(R.layout.suw_glif_loading_screen); activity.setContentView(R.layout.suw_glif_loading_screen);
assertTrue( assertThat((View) activity.findViewById(R.id.suw_large_progress_bar))
"Progress bar should exist", .isInstanceOf(ProgressBar.class);
activity.findViewById(R.id.suw_large_progress_bar) instanceof ProgressBar);
} }
private Button createButton(Context context, AttributeSet attrs) { private Button createButton(Context context, AttributeSet attrs) {

View file

@ -17,8 +17,6 @@
package com.android.setupwizardlib.util; package com.android.setupwizardlib.util;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import android.app.Activity; import android.app.Activity;
import android.graphics.Color; import android.graphics.Color;
@ -30,13 +28,13 @@ import androidx.annotation.Nullable;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(minSdk = Config.OLDEST_SDK, maxSdk = Config.NEWEST_SDK) @Config(minSdk = Config.OLDEST_SDK, maxSdk = Config.NEWEST_SDK)
public class GlifV3StyleTest { public class GlifV3StyleTest {
@ -44,11 +42,11 @@ public class GlifV3StyleTest {
public void activityWithGlifV3Theme_shouldUseLightNavBarOnV27OrAbove() { public void activityWithGlifV3Theme_shouldUseLightNavBarOnV27OrAbove() {
GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class);
if (VERSION.SDK_INT >= VERSION_CODES.O_MR1) { if (VERSION.SDK_INT >= VERSION_CODES.O_MR1) {
assertEquals(Color.WHITE, activity.getWindow().getNavigationBarColor()); assertThat(activity.getWindow().getNavigationBarColor()).isEqualTo(Color.WHITE);
int vis = activity.getWindow().getDecorView().getSystemUiVisibility(); int vis = activity.getWindow().getDecorView().getSystemUiVisibility();
assertTrue((vis & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0); assertThat((vis & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0).isTrue();
} else if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { } else if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
assertEquals(Color.BLACK, activity.getWindow().getNavigationBarColor()); assertThat(activity.getWindow().getNavigationBarColor()).isEqualTo(Color.BLACK);
} }
// Nav bar color is not customizable pre-L // Nav bar color is not customizable pre-L
} }

View file

@ -29,15 +29,15 @@ import android.os.Build.VERSION_CODES;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.ExternalResources; import com.android.setupwizardlib.robolectric.ExternalResources;
import com.android.setupwizardlib.robolectric.ExternalResources.Resources; import com.android.setupwizardlib.robolectric.ExternalResources.Resources;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import com.android.setupwizardlib.util.Partner.ResourceEntry; import com.android.setupwizardlib.util.Partner.ResourceEntry;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class PartnerTest { public class PartnerTest {

View file

@ -23,14 +23,14 @@ import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import androidx.annotation.StyleRes; import androidx.annotation.StyleRes;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = Config.NEWEST_SDK) @Config(sdk = Config.NEWEST_SDK)
public class ThemeResolverTest { public class ThemeResolverTest {

View file

@ -17,9 +17,7 @@
package com.android.setupwizardlib.util; package com.android.setupwizardlib.util;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import static org.robolectric.Shadows.shadowOf; import static org.robolectric.Shadows.shadowOf;
@ -33,16 +31,16 @@ import android.provider.Settings.Global;
import android.provider.Settings.Secure; import android.provider.Settings.Secure;
import androidx.annotation.StyleRes; import androidx.annotation.StyleRes;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = Config.NEWEST_SDK) @Config(sdk = Config.NEWEST_SDK)
public class WizardManagerHelperTest { public class WizardManagerHelperTest {
@ -58,61 +56,63 @@ public class WizardManagerHelperTest {
data.putExtra("extraData", "shazam"); data.putExtra("extraData", "shazam");
final Intent nextIntent = WizardManagerHelper.getNextIntent(intent, Activity.RESULT_OK, data); final Intent nextIntent = WizardManagerHelper.getNextIntent(intent, Activity.RESULT_OK, data);
assertEquals( assertWithMessage("Next intent action should be NEXT")
"Next intent action should be NEXT", "com.android.wizard.NEXT", nextIntent.getAction()); .that(nextIntent.getAction())
assertEquals( .isEqualTo("com.android.wizard.NEXT");
"Script URI should be the same as original intent", assertWithMessage("Script URI should be the same as original intent")
"android-resource://test-script", .that(nextIntent.getStringExtra("scriptUri"))
nextIntent.getStringExtra("scriptUri")); .isEqualTo("android-resource://test-script");
assertEquals( assertWithMessage("Action ID should be the same as original intent")
"Action ID should be the same as original intent", .that(nextIntent.getStringExtra("actionId"))
"test_action_id", .isEqualTo("test_action_id");
nextIntent.getStringExtra("actionId")); assertWithMessage("Theme extra should be the same as original intent")
assertEquals( .that(nextIntent.getStringExtra("theme"))
"Theme extra should be the same as original intent", .isEqualTo("test_theme");
"test_theme", assertWithMessage("ignoreExtra should not be in nextIntent")
nextIntent.getStringExtra("theme")); .that(nextIntent.hasExtra("ignoreExtra"))
assertFalse("ignoreExtra should not be in nextIntent", nextIntent.hasExtra("ignoreExtra")); .isFalse();
assertEquals( assertWithMessage("Result code extra should be RESULT_OK")
"Result code extra should be RESULT_OK", .that(nextIntent.getIntExtra("com.android.setupwizard.ResultCode", 0))
Activity.RESULT_OK, .isEqualTo(Activity.RESULT_OK);
nextIntent.getIntExtra("com.android.setupwizard.ResultCode", 0)); assertWithMessage("Extra data should surface as extra in nextIntent")
assertEquals( .that(nextIntent.getStringExtra("extraData"))
"Extra data should surface as extra in nextIntent", .isEqualTo("shazam");
"shazam",
nextIntent.getStringExtra("extraData"));
} }
@Test @Test
public void testIsSetupWizardTrue() { public void testIsSetupWizardTrue() {
final Intent intent = new Intent(); final Intent intent = new Intent();
intent.putExtra("firstRun", true); intent.putExtra("firstRun", true);
assertTrue("Is setup wizard should be true", WizardManagerHelper.isSetupWizardIntent(intent)); assertWithMessage("Is setup wizard should be true")
.that(WizardManagerHelper.isSetupWizardIntent(intent))
.isTrue();
} }
@Test @Test
public void testIsDeferredSetupTrue() { public void testIsDeferredSetupTrue() {
final Intent intent = new Intent(); final Intent intent = new Intent();
intent.putExtra("deferredSetup", true); intent.putExtra("deferredSetup", true);
assertTrue( assertWithMessage("Is deferred setup wizard should be true")
"Is deferred setup wizard should be true", .that(WizardManagerHelper.isDeferredSetupWizard(intent))
WizardManagerHelper.isDeferredSetupWizard(intent)); .isTrue();
} }
@Test @Test
public void testIsPreDeferredSetupTrue() { public void testIsPreDeferredSetupTrue() {
final Intent intent = new Intent(); final Intent intent = new Intent();
intent.putExtra("preDeferredSetup", true); intent.putExtra("preDeferredSetup", true);
assertTrue( assertWithMessage("Is pre-deferred setup wizard should be true")
"Is pre-deferred setup wizard should be true", .that(WizardManagerHelper.isPreDeferredSetupWizard(intent))
WizardManagerHelper.isPreDeferredSetupWizard(intent)); .isTrue();
} }
@Test @Test
public void testIsSetupWizardFalse() { public void testIsSetupWizardFalse() {
final Intent intent = new Intent(); final Intent intent = new Intent();
intent.putExtra("firstRun", false); intent.putExtra("firstRun", false);
assertFalse("Is setup wizard should be true", WizardManagerHelper.isSetupWizardIntent(intent)); assertWithMessage("Is setup wizard should be true")
.that(WizardManagerHelper.isSetupWizardIntent(intent))
.isFalse();
} }
@Test @Test
@ -132,12 +132,12 @@ public class WizardManagerHelperTest {
unexpectedStringThemes.add(theme); unexpectedStringThemes.add(theme);
} }
} }
assertTrue( assertWithMessage("Intent themes " + unexpectedIntentThemes + " should be light")
"Intent themes " + unexpectedIntentThemes + " should be light", .that(unexpectedIntentThemes.isEmpty())
unexpectedIntentThemes.isEmpty()); .isTrue();
assertTrue( assertWithMessage("String themes " + unexpectedStringThemes + " should be light")
"String themes " + unexpectedStringThemes + " should be light", .that(unexpectedStringThemes.isEmpty())
unexpectedStringThemes.isEmpty()); .isTrue();
} }
@Test @Test
@ -155,111 +155,114 @@ public class WizardManagerHelperTest {
unexpectedStringThemes.add(theme); unexpectedStringThemes.add(theme);
} }
} }
assertTrue( assertWithMessage("Intent themes " + unexpectedIntentThemes + " should not be light")
"Intent themes " + unexpectedIntentThemes + " should not be light", .that(unexpectedIntentThemes.isEmpty())
unexpectedIntentThemes.isEmpty()); .isTrue();
assertTrue( assertWithMessage("String themes " + unexpectedStringThemes + " should not be light")
"String themes " + unexpectedStringThemes + " should not be light", .that(unexpectedStringThemes.isEmpty())
unexpectedStringThemes.isEmpty()); .isTrue();
} }
@Test @Test
public void getThemeRes_whenOldestSupportedThemeTakeEffect_shouldReturnDefault() { public void getThemeRes_whenOldestSupportedThemeTakeEffect_shouldReturnDefault() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.putExtra(WizardManagerHelper.EXTRA_THEME, "material"); intent.putExtra(WizardManagerHelper.EXTRA_THEME, "material");
assertEquals(0, WizardManagerHelper.getThemeRes(intent, 0, WizardManagerHelper.THEME_GLIF_V2)); assertThat(WizardManagerHelper.getThemeRes(intent, 0, WizardManagerHelper.THEME_GLIF_V2))
.isEqualTo(0);
} }
@Test @Test
public void getThemeRes_whenOldestSupportedThemeNotTakeEffect_shouldReturnCurrent() { public void getThemeRes_whenOldestSupportedThemeNotTakeEffect_shouldReturnCurrent() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v3"); intent.putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v3");
assertEquals( assertThat(WizardManagerHelper.getThemeRes(intent, 0, WizardManagerHelper.THEME_GLIF_V2))
R.style.SuwThemeGlifV3, .isEqualTo(R.style.SuwThemeGlifV3);
WizardManagerHelper.getThemeRes(intent, 0, WizardManagerHelper.THEME_GLIF_V2));
} }
@Test @Test
public void testIsLightThemeDefault() { public void testIsLightThemeDefault() {
final Intent intent = new Intent(); final Intent intent = new Intent();
intent.putExtra("theme", "abracadabra"); intent.putExtra("theme", "abracadabra");
assertTrue( assertWithMessage("isLightTheme should return default value true")
"isLightTheme should return default value true", .that(WizardManagerHelper.isLightTheme(intent, true))
WizardManagerHelper.isLightTheme(intent, true)); .isTrue();
assertFalse( assertWithMessage("isLightTheme should return default value false")
"isLightTheme should return default value false", .that(WizardManagerHelper.isLightTheme(intent, false))
WizardManagerHelper.isLightTheme(intent, false)); .isFalse();
} }
@Test @Test
public void testIsLightThemeUnspecified() { public void testIsLightThemeUnspecified() {
final Intent intent = new Intent(); final Intent intent = new Intent();
assertTrue( assertWithMessage("isLightTheme should return default value true")
"isLightTheme should return default value true", .that(WizardManagerHelper.isLightTheme(intent, true))
WizardManagerHelper.isLightTheme(intent, true)); .isTrue();
assertFalse( assertWithMessage("isLightTheme should return default value false")
"isLightTheme should return default value false", .that(WizardManagerHelper.isLightTheme(intent, false))
WizardManagerHelper.isLightTheme(intent, false)); .isFalse();
} }
@Test @Test
public void testGetThemeResGlifV3Light() { public void testGetThemeResGlifV3Light() {
assertEquals(R.style.SuwThemeGlifV3_Light, WizardManagerHelper.getThemeRes("glif_v3_light", 0)); assertThat(WizardManagerHelper.getThemeRes("glif_v3_light", 0))
.isEqualTo(R.style.SuwThemeGlifV3_Light);
} }
@Test @Test
public void testGetThemeResGlifV3() { public void testGetThemeResGlifV3() {
assertEquals(R.style.SuwThemeGlifV3, WizardManagerHelper.getThemeRes("glif_v3", 0)); assertThat(WizardManagerHelper.getThemeRes("glif_v3", 0)).isEqualTo(R.style.SuwThemeGlifV3);
} }
@Test @Test
public void testGetThemeResGlifV2Light() { public void testGetThemeResGlifV2Light() {
assertEquals(R.style.SuwThemeGlifV2_Light, WizardManagerHelper.getThemeRes("glif_v2_light", 0)); assertThat(WizardManagerHelper.getThemeRes("glif_v2_light", 0))
.isEqualTo(R.style.SuwThemeGlifV2_Light);
} }
@Test @Test
public void testGetThemeResGlifV2() { public void testGetThemeResGlifV2() {
assertEquals(R.style.SuwThemeGlifV2, WizardManagerHelper.getThemeRes("glif_v2", 0)); assertThat(WizardManagerHelper.getThemeRes("glif_v2", 0)).isEqualTo(R.style.SuwThemeGlifV2);
} }
@Test @Test
public void testGetThemeResGlifLight() { public void testGetThemeResGlifLight() {
assertEquals(R.style.SuwThemeGlif_Light, WizardManagerHelper.getThemeRes("glif_light", 0)); assertThat(WizardManagerHelper.getThemeRes("glif_light", 0))
.isEqualTo(R.style.SuwThemeGlif_Light);
} }
@Test @Test
public void testGetThemeResGlif() { public void testGetThemeResGlif() {
assertEquals(R.style.SuwThemeGlif, WizardManagerHelper.getThemeRes("glif", 0)); assertThat(WizardManagerHelper.getThemeRes("glif", 0)).isEqualTo(R.style.SuwThemeGlif);
} }
@Test @Test
public void testGetThemeResMaterialLight() { public void testGetThemeResMaterialLight() {
assertEquals( assertThat(WizardManagerHelper.getThemeRes("material_light", 0))
R.style.SuwThemeMaterial_Light, WizardManagerHelper.getThemeRes("material_light", 0)); .isEqualTo(R.style.SuwThemeMaterial_Light);
} }
@Test @Test
public void testGetThemeResMaterial() { public void testGetThemeResMaterial() {
assertEquals(R.style.SuwThemeMaterial, WizardManagerHelper.getThemeRes("material", 0)); assertThat(WizardManagerHelper.getThemeRes("material", 0)).isEqualTo(R.style.SuwThemeMaterial);
} }
@Test @Test
public void testGetThemeResDefault() { public void testGetThemeResDefault() {
@StyleRes int def = 123; @StyleRes int def = 123;
assertEquals(def, WizardManagerHelper.getThemeRes("abracadabra", def)); assertThat(WizardManagerHelper.getThemeRes("abracadabra", def)).isEqualTo(def);
} }
@Test @Test
public void testGetThemeResNull() { public void testGetThemeResNull() {
@StyleRes int def = 123; @StyleRes int def = 123;
assertEquals(def, WizardManagerHelper.getThemeRes((String) null, def)); assertThat(WizardManagerHelper.getThemeRes((String) null, def)).isEqualTo(def);
} }
@Test @Test
public void testGetThemeResFromIntent() { public void testGetThemeResFromIntent() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.putExtra(WizardManagerHelper.EXTRA_THEME, "material"); intent.putExtra(WizardManagerHelper.EXTRA_THEME, "material");
assertEquals(R.style.SuwThemeMaterial, WizardManagerHelper.getThemeRes(intent, 0)); assertThat(WizardManagerHelper.getThemeRes(intent, 0)).isEqualTo(R.style.SuwThemeMaterial);
} }
@Test @Test
@ -280,34 +283,35 @@ public class WizardManagerHelperTest {
Intent intent = new Intent("test.intent.action"); Intent intent = new Intent("test.intent.action");
WizardManagerHelper.copyWizardManagerExtras(originalIntent, intent); WizardManagerHelper.copyWizardManagerExtras(originalIntent, intent);
assertEquals("Intent action should be kept", "test.intent.action", intent.getAction()); assertWithMessage("Intent action should be kept")
assertEquals( .that(intent.getAction())
"EXTRA_THEME should be copied", .isEqualTo("test.intent.action");
"test_theme", assertWithMessage("EXTRA_THEME should be copied")
intent.getStringExtra(WizardManagerHelper.EXTRA_THEME)); .that(intent.getStringExtra(WizardManagerHelper.EXTRA_THEME))
.isEqualTo("test_theme");
Bundle copiedWizardBundle = intent.getParcelableExtra(WizardManagerHelper.EXTRA_WIZARD_BUNDLE); Bundle copiedWizardBundle = intent.getParcelableExtra(WizardManagerHelper.EXTRA_WIZARD_BUNDLE);
assertEquals("Wizard bundle should be copied", "bar", copiedWizardBundle.getString("foo")); assertWithMessage("Wizard bundle should be copied")
.that(copiedWizardBundle.getString("foo"))
.isEqualTo("bar");
assertTrue( assertWithMessage("EXTRA_IS_FIRST_RUN should be copied")
"EXTRA_IS_FIRST_RUN should be copied", .that(intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, false))
intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, false)); .isTrue();
assertTrue( assertWithMessage("EXTRA_IS_DEFERRED_SETUP should be copied")
"EXTRA_IS_DEFERRED_SETUP should be copied", .that(intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_DEFERRED_SETUP, false))
intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_DEFERRED_SETUP, false)); .isTrue();
assertTrue( assertWithMessage("EXTRA_IS_PRE_DEFERRED_SETUP should be copied")
"EXTRA_IS_PRE_DEFERRED_SETUP should be copied", .that(intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_PRE_DEFERRED_SETUP, false))
intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_PRE_DEFERRED_SETUP, false)); .isTrue();
// Script URI and Action ID are replaced by Wizard Bundle in M, but are kept for backwards // Script URI and Action ID are replaced by Wizard Bundle in M, but are kept for backwards
// compatibility // compatibility
assertEquals( assertWithMessage("EXTRA_SCRIPT_URI should be copied")
"EXTRA_SCRIPT_URI should be copied", .that(intent.getStringExtra(WizardManagerHelper.EXTRA_SCRIPT_URI))
"test_script_uri", .isEqualTo("test_script_uri");
intent.getStringExtra(WizardManagerHelper.EXTRA_SCRIPT_URI)); assertWithMessage("EXTRA_ACTION_ID should be copied")
assertEquals( .that(intent.getStringExtra(WizardManagerHelper.EXTRA_ACTION_ID))
"EXTRA_ACTION_ID should be copied", .isEqualTo("test_action_id");
"test_action_id",
intent.getStringExtra(WizardManagerHelper.EXTRA_ACTION_ID));
} }
@TargetApi(VERSION_CODES.JELLY_BEAN_MR1) @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
@ -315,38 +319,38 @@ public class WizardManagerHelperTest {
public void testIsUserSetupComplete() { public void testIsUserSetupComplete() {
Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1); Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 1); Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 1);
assertTrue(WizardManagerHelper.isUserSetupComplete(application)); assertThat(WizardManagerHelper.isUserSetupComplete(application)).isTrue();
Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 0); Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 0);
assertFalse(WizardManagerHelper.isUserSetupComplete(application)); assertThat(WizardManagerHelper.isUserSetupComplete(application)).isFalse();
} }
@Test @Test
@Config(sdk = VERSION_CODES.JELLY_BEAN) @Config(sdk = VERSION_CODES.JELLY_BEAN)
public void testIsUserSetupCompleteCompat() { public void testIsUserSetupCompleteCompat() {
Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1); Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1);
assertTrue(WizardManagerHelper.isUserSetupComplete(application)); assertThat(WizardManagerHelper.isUserSetupComplete(application)).isTrue();
Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0); Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0);
assertFalse(WizardManagerHelper.isUserSetupComplete(application)); assertThat(WizardManagerHelper.isUserSetupComplete(application)).isFalse();
} }
@TargetApi(VERSION_CODES.JELLY_BEAN_MR1) @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
@Test @Test
public void testIsDeviceProvisioned() { public void testIsDeviceProvisioned() {
Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1); Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
assertTrue(WizardManagerHelper.isDeviceProvisioned(application)); assertThat(WizardManagerHelper.isDeviceProvisioned(application)).isTrue();
Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 0); Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 0);
assertFalse(WizardManagerHelper.isDeviceProvisioned(application)); assertThat(WizardManagerHelper.isDeviceProvisioned(application)).isFalse();
} }
@Test @Test
@Config(sdk = VERSION_CODES.JELLY_BEAN) @Config(sdk = VERSION_CODES.JELLY_BEAN)
public void testIsDeviceProvisionedCompat() { public void testIsDeviceProvisionedCompat() {
Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1); Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1);
assertTrue(WizardManagerHelper.isDeviceProvisioned(application)); assertThat(WizardManagerHelper.isDeviceProvisioned(application)).isTrue();
Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0); Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0);
assertFalse(WizardManagerHelper.isDeviceProvisioned(application)); assertThat(WizardManagerHelper.isDeviceProvisioned(application)).isFalse();
} }
@Test @Test

View file

@ -16,18 +16,18 @@
package com.android.setupwizardlib.view; package com.android.setupwizardlib.view;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
import android.view.View; import android.view.View;
import android.view.View.MeasureSpec; import android.view.View.MeasureSpec;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class FillContentLayoutTest { public class FillContentLayoutTest {
@ -44,8 +44,8 @@ public class FillContentLayoutTest {
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
assertEquals(123, layout.getMeasuredWidth()); assertThat(layout.getMeasuredWidth()).isEqualTo(123);
assertEquals(123, layout.getMeasuredHeight()); assertThat(layout.getMeasuredHeight()).isEqualTo(123);
} }
@Test @Test
@ -63,8 +63,8 @@ public class FillContentLayoutTest {
MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY)); MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY));
assertEquals(123, child.getMeasuredWidth()); assertThat(child.getMeasuredWidth()).isEqualTo(123);
assertEquals(123, child.getMeasuredHeight()); assertThat(child.getMeasuredHeight()).isEqualTo(123);
} }
@Test @Test
@ -82,7 +82,7 @@ public class FillContentLayoutTest {
MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY)); MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY));
assertEquals(88, child.getMeasuredWidth()); assertThat(child.getMeasuredWidth()).isEqualTo(88);
assertEquals(88, child.getMeasuredHeight()); assertThat(child.getMeasuredHeight()).isEqualTo(88);
} }
} }

View file

@ -17,7 +17,6 @@
package com.android.setupwizardlib.view; package com.android.setupwizardlib.view;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.robolectric.RuntimeEnvironment.application; import static org.robolectric.RuntimeEnvironment.application;
@ -27,13 +26,13 @@ import android.net.Uri;
import androidx.annotation.RawRes; import androidx.annotation.RawRes;
import android.view.View; import android.view.View;
import com.android.setupwizardlib.R; import com.android.setupwizardlib.R;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows; import org.robolectric.Shadows;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowMediaPlayer; import org.robolectric.shadows.ShadowMediaPlayer;
@ -43,7 +42,7 @@ import org.robolectric.shadows.util.DataSource;
import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter; import org.robolectric.util.ReflectionHelpers.ClassParameter;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = Config.NEWEST_SDK) @Config(sdk = Config.NEWEST_SDK)
public class IllustrationVideoViewTest { public class IllustrationVideoViewTest {
@ -70,8 +69,8 @@ public class IllustrationVideoViewTest {
Robolectric.flushForegroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler();
view.start(); view.start();
assertNotNull(view.mMediaPlayer); assertThat(view.mMediaPlayer).isNotNull();
assertNotNull(view.surface); assertThat(view.surface).isNotNull();
view.onWindowFocusChanged(false); view.onWindowFocusChanged(false);
assertThat(getShadowMediaPlayer().getState()).isEqualTo(ShadowMediaPlayer.State.PAUSED); assertThat(getShadowMediaPlayer().getState()).isEqualTo(ShadowMediaPlayer.State.PAUSED);
@ -91,8 +90,8 @@ public class IllustrationVideoViewTest {
createDefaultView(); createDefaultView();
view.start(); view.start();
assertNotNull(view.mMediaPlayer); assertThat(view.mMediaPlayer).isNotNull();
assertNotNull(view.surface); assertThat(view.surface).isNotNull();
// MediaPlayer is set to null after destroy. Retrieve it first before we call destroy. // MediaPlayer is set to null after destroy. Retrieve it first before we call destroy.
ShadowMediaPlayer shadowMediaPlayer = getShadowMediaPlayer(); ShadowMediaPlayer shadowMediaPlayer = getShadowMediaPlayer();

View file

@ -17,10 +17,7 @@
package com.android.setupwizardlib.view; package com.android.setupwizardlib.view;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -37,16 +34,15 @@ import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.TextAppearanceSpan; import android.text.style.TextAppearanceSpan;
import android.view.MotionEvent; import android.view.MotionEvent;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import com.android.setupwizardlib.span.LinkSpan; import com.android.setupwizardlib.span.LinkSpan;
import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener; import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener;
import com.android.setupwizardlib.view.TouchableMovementMethod.TouchableLinkMovementMethod; import com.android.setupwizardlib.view.TouchableMovementMethod.TouchableLinkMovementMethod;
import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SuwLibRobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK})
public class RichTextViewTest { public class RichTextViewTest {
@ -60,17 +56,19 @@ public class RichTextViewTest {
textView.setText(ssb); textView.setText(ssb);
final CharSequence text = textView.getText(); final CharSequence text = textView.getText();
assertTrue("Text should be spanned", text instanceof Spanned); assertThat(text).isInstanceOf(Spanned.class);
assertThat(textView.getMovementMethod()).isInstanceOf(TouchableLinkMovementMethod.class); assertThat(textView.getMovementMethod()).isInstanceOf(TouchableLinkMovementMethod.class);
Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class); Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class);
assertEquals("Annotation should be removed " + Arrays.toString(spans), 0, spans.length); assertThat(spans).isEmpty();
spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class);
assertEquals("There should be one span " + Arrays.toString(spans), 1, spans.length); assertThat(spans).hasLength(1);
assertTrue("The span should be a LinkSpan", spans[0] instanceof LinkSpan); assertThat(spans[0]).isInstanceOf(LinkSpan.class);
assertEquals("The LinkSpan should have id \"foobar\"", "foobar", ((LinkSpan) spans[0]).getId()); assertWithMessage("The LinkSpan should have id \"foobar\"")
.that(((LinkSpan) spans[0]).getId())
.isEqualTo("foobar");
} }
@Test @Test
@ -85,7 +83,7 @@ public class RichTextViewTest {
OnLinkClickListener listener = mock(OnLinkClickListener.class); OnLinkClickListener listener = mock(OnLinkClickListener.class);
textView.setOnLinkClickListener(listener); textView.setOnLinkClickListener(listener);
assertSame(listener, textView.getOnLinkClickListener()); assertThat(textView.getOnLinkClickListener()).isSameAs(listener);
CharSequence text = textView.getText(); CharSequence text = textView.getText();
LinkSpan[] spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); LinkSpan[] spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class);
@ -159,14 +157,14 @@ public class RichTextViewTest {
textView.setText(ssb); textView.setText(ssb);
final CharSequence text = textView.getText(); final CharSequence text = textView.getText();
assertTrue("Text should be spanned", text instanceof Spanned); assertThat(text).isInstanceOf(Spanned.class);
Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class); Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class);
assertEquals("Annotation should be removed " + Arrays.toString(spans), 0, spans.length); assertThat(spans).isEmpty();
spans = ((Spanned) text).getSpans(0, text.length(), TextAppearanceSpan.class); spans = ((Spanned) text).getSpans(0, text.length(), TextAppearanceSpan.class);
assertEquals("There should be one span " + Arrays.toString(spans), 1, spans.length); assertThat(spans).hasLength(1);
assertTrue("The span should be a TextAppearanceSpan", spans[0] instanceof TextAppearanceSpan); assertThat(spans[0]).isInstanceOf(TextAppearanceSpan.class);
} }
@Test @Test
@ -178,7 +176,7 @@ public class RichTextViewTest {
RichTextView view = new RichTextView(application); RichTextView view = new RichTextView(application);
view.setText(spannableStringBuilder); view.setText(spannableStringBuilder);
assertTrue("TextView should be focusable since it contains spans", view.isFocusable()); assertThat(view.isFocusable()).named("view focusable").isTrue();
} }
@SuppressLint("SetTextI18n") // It's OK. This is just a test. @SuppressLint("SetTextI18n") // It's OK. This is just a test.
@ -187,9 +185,7 @@ public class RichTextViewTest {
RichTextView textView = new RichTextView(application); RichTextView textView = new RichTextView(application);
textView.setText("Thou shall not be focusable!"); textView.setText("Thou shall not be focusable!");
assertFalse( assertThat(textView.isFocusable()).named("view focusable").isFalse();
"TextView should not be focusable since it does not contain any span",
textView.isFocusable());
} }
// Based on the text contents of the text view, the "focusable" property of the element // Based on the text contents of the text view, the "focusable" property of the element
@ -200,18 +196,18 @@ public class RichTextViewTest {
RichTextView textView = new RichTextView(application); RichTextView textView = new RichTextView(application);
textView.setText("Thou shall not be focusable!"); textView.setText("Thou shall not be focusable!");
assertFalse(textView.isFocusable()); assertThat(textView.isFocusable()).isFalse();
assertFalse(textView.isFocusableInTouchMode()); assertThat(textView.isFocusableInTouchMode()).isFalse();
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("I am focusable"); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("I am focusable");
spannableStringBuilder.setSpan(new Annotation("link", "focus:on_me"), 0, 1, 0); spannableStringBuilder.setSpan(new Annotation("link", "focus:on_me"), 0, 1, 0);
textView.setText(spannableStringBuilder); textView.setText(spannableStringBuilder);
assertTrue(textView.isFocusable()); assertThat(textView.isFocusable()).isTrue();
if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) { if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
assertTrue(textView.isFocusableInTouchMode()); assertThat(textView.isFocusableInTouchMode()).isTrue();
assertFalse(textView.getRevealOnFocusHint()); assertThat(textView.getRevealOnFocusHint()).isFalse();
} else { } else {
assertFalse(textView.isFocusableInTouchMode()); assertThat(textView.isFocusableInTouchMode()).isFalse();
} }
} }