Add robolectric test

Configure the build target and migrate LinkSpanTest to Robolectric.
Move existing instrumentation tests into test/instrumentation

Bug: 32975598
Test: ./gradlew test
Change-Id: I9bb39142c616a2c44fe38f48d92e89ab8ebda7ba
This commit is contained in:
Maurice Lam 2016-12-28 17:34:10 -08:00
parent 4819fa7e3e
commit 2e634640f3
36 changed files with 54 additions and 32 deletions

View file

@ -9,9 +9,9 @@ apply from: 'standalone-rules.gradle'
// Add targets for tests
android.sourceSets {
androidTest {
manifest.srcFile 'test/AndroidManifest.xml'
java.srcDirs = ['test/src']
res.srcDirs = ['test/res']
manifest.srcFile 'test/instrumentation/AndroidManifest.xml'
java.srcDirs = ['test/instrumentation/src']
res.srcDirs = ['test/instrumentation/res']
dependencies {
androidTestCompile 'com.android.support.test:rules:0.5'
@ -36,6 +36,17 @@ android.sourceSets {
java.srcDirs = ['full-support/test/src', 'eclair-mr1/test/src']
res.srcDirs = ['full-support/test/res']
}
test {
java.srcDirs = ['test/robotest/src']
dependencies {
testCompile 'org.robolectric:robolectric:3.+'
testCompile 'org.robolectric:shadows-core:3.+'
testCompile 'junit:junit:4.+'
testCompile 'org.mockito:mockito-core:1.9.5'
}
}
}
android.defaultConfig.testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

View file

@ -46,23 +46,23 @@ public class BottomScrollViewTest {
@Test
public void testNoNeedScroll() {
createScrollView(20);
assertTrue("Scroll should not be required", mListener.scrolledToBottom);
assertTrue("Scroll should not be required", mListener.mScrolledToBottom);
}
@Test
public void testNeedScroll() {
createScrollView(110);
assertFalse("Scroll should be required", mListener.scrolledToBottom);
assertFalse("Scroll should be required", mListener.mScrolledToBottom);
}
@Test
public void testScrollToBottom() {
final BottomScrollView bottomScrollView = createScrollView(110);
assertFalse("Scroll should be required", mListener.scrolledToBottom);
assertFalse("Scroll should be required", mListener.mScrolledToBottom);
bottomScrollView.scrollTo(0, 10);
assertTrue("Should already be scrolled to bottom", mListener.scrolledToBottom);
assertTrue("Should already be scrolled to bottom", mListener.mScrolledToBottom);
}
@Test
@ -90,7 +90,7 @@ public class BottomScrollViewTest {
private static final int WIDTH = 10;
private int mHeight;
public TestChildView(Context context, int height) {
TestChildView(Context context, int height) {
super(context);
mHeight = height;
}
@ -107,7 +107,7 @@ public class BottomScrollViewTest {
private static class TestBottomScrollView extends BottomScrollView {
public TestBottomScrollView(Context context) {
TestBottomScrollView(Context context) {
super(context);
}
@ -121,16 +121,16 @@ public class BottomScrollViewTest {
private static class TestBottomScrollListener implements BottomScrollView.BottomScrollListener {
boolean scrolledToBottom = true;
boolean mScrolledToBottom = true;
@Override
public void onScrolledToBottom() {
scrolledToBottom = true;
mScrolledToBottom = true;
}
@Override
public void onRequiresScroll() {
scrolledToBottom = false;
mScrolledToBottom = false;
}
}
}

View file

@ -131,7 +131,7 @@ public class DrawableLayoutDirectionHelperTest {
private static class ForceRtlView extends View {
public ForceRtlView(Context context) {
ForceRtlView(Context context) {
super(context);
}

View file

@ -104,7 +104,7 @@ public class ListViewRequireScrollHelperTest {
public int scrollDistance = 0;
public OnScrollListener listener;
public TestListView(Context context) {
TestListView(Context context) {
super(context);
setAdapter(new BaseAdapter() {
@Override
@ -149,7 +149,7 @@ public class ListViewRequireScrollHelperTest {
private static class TestNavigationBar extends NavigationBar {
public TestNavigationBar(Context context) {
TestNavigationBar(Context context) {
super(context);
}

View file

@ -181,7 +181,7 @@ public class PartnerTest {
private Context mTestContext;
private Resources mTestResources;
public TestPackageManager(Context testContext) {
TestPackageManager(Context testContext) {
mTestContext = testContext;
mTestResources = new TestResources();
}
@ -209,7 +209,7 @@ public class PartnerTest {
public List<ResolveInfo> partnerList;
public TestContext(Context context) {
TestContext(Context context) {
super(context);
}

View file

@ -90,7 +90,7 @@ public class RequireScrollHelperTest {
public BottomScrollListener listener;
public int page = 0;
public TestBottomScrollView(Context context) {
TestBottomScrollView(Context context) {
super(context);
}
@ -112,7 +112,7 @@ public class RequireScrollHelperTest {
private static class TestNavigationBar extends NavigationBar {
public TestNavigationBar(Context context) {
TestNavigationBar(Context context) {
super(context);
}

View file

@ -237,7 +237,7 @@ public class SystemBarHelperTest {
private int mNavigationBarColor = -1;
private int mStatusBarColor = -1;
public TestWindow(Context context, View decorView) {
TestWindow(Context context, View decorView) {
super(context);
mDecorView = decorView;
}

View file

@ -1,3 +1,19 @@
/*
* Copyright (C) 2016 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.test.util;
import android.content.Context;

View file

@ -14,29 +14,25 @@
* limitations under the License.
*/
package com.android.setupwizardlib.test;
package com.android.setupwizardlib.span;
import static org.junit.Assert.assertSame;
import static org.robolectric.RuntimeEnvironment.application;
import android.content.Context;
import android.content.ContextWrapper;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.widget.TextView;
import com.android.setupwizardlib.span.LinkSpan;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(AndroidJUnit4.class)
@SmallTest
@RunWith(RobolectricTestRunner.class)
public class LinkSpanTest {
@Test
public void testOnClick() {
final TestContext context = new TestContext(InstrumentationRegistry.getContext());
final TestContext context = new TestContext(application);
final TextView textView = new TextView(context);
final LinkSpan linkSpan = new LinkSpan("test_id");
@ -47,8 +43,7 @@ public class LinkSpanTest {
@Test
public void testNonImplementingContext() {
final Context context = InstrumentationRegistry.getContext();
final TextView textView = new TextView(context);
final TextView textView = new TextView(application);
final LinkSpan linkSpan = new LinkSpan("test_id");
linkSpan.onClick(textView);
@ -61,7 +56,7 @@ public class LinkSpanTest {
public LinkSpan clickedSpan = null;
public TestContext(Context base) {
TestContext(Context base) {
super(base);
}