Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add unit test for FragmentStack #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions robolectric.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
android.testOptions.unitTests.all {
// configure the set of classes for JUnit tests
include project.hasProperty("testFilter") ? "**/*${project.ext.testFilter}*Test.class" : '**/*Test.class'
exclude '**/espresso/**/*.class'

// configure max heap size of the test JVM
maxHeapSize = '2048m'

// configure the test JVM arguments
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'

// Specify max number of processes (default is 1)
maxParallelForks = 1

// Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 150

// configure whether failing tests should fail the build
ignoreFailures false

testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}

ext {
assertjVersion = '1.1.1'
robolectricVersion = '3.0'
}

dependencies {
testCompile "org.robolectric:robolectric:$robolectricVersion",
"org.robolectric:shadows-support-v4:$robolectricVersion",
'org.mockito:mockito-core:1.9.5+',
'junit:junit:4.12'
testCompile("com.squareup.assertj:assertj-android:$assertjVersion") {
exclude group: 'com.android.support', module: 'support-annotations'
}
testCompile("com.squareup.assertj:assertj-android-support-v4:$assertjVersion") {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'support-v4'
// see robolectric/robolectric#1633
}
testCompile("com.squareup.assertj:assertj-android-appcompat-v7:$assertjVersion") {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'appcompat-v7'
// see robolectric/robolectric#1633
}
}
2 changes: 1 addition & 1 deletion stacklibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
apply from: "${rootProject.getRootDir()}/robolectric.gradle"
}


Expand Down
43 changes: 43 additions & 0 deletions stacklibrary/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="40sp"
android:textColor="@android:color/black"
android:text="HomeFragment"/>
<Button
android:id="@+id/standard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="standard"
android:layout_marginBottom="187dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/single_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="single_top"
android:layout_alignTop="@+id/standard"
android:layout_alignLeft="@+id/standard"
android:layout_alignStart="@+id/standard"
android:layout_marginTop="56dp" />
<Button
android:id="@+id/single_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="single_task"
android:layout_below="@+id/single_top"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/single_instance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="single_instance"
android:layout_below="@+id/single_task"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mrwang.stacklibrary;

/*
* Copyright Farble Dast. All rights reserved.
*
* 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.
*/

/**
* Created by dell on 2015/12/23.
*/
public class CustomBuildConfig {
public static final boolean DEBUG = BuildConfig.DEBUG;
public static final String APPLICATION_ID = "com.mrwang.stacklibrary";
public static final String BUILD_TYPE = BuildConfig.BUILD_TYPE;
public static final String FLAVOR = BuildConfig.FLAVOR;
public static final int VERSION_CODE = BuildConfig.VERSION_CODE;
public static final String VERSION_NAME = BuildConfig.VERSION_NAME;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mrwang.stacklibrary;

import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;

/**
* Created by dell on 2016/7/19.
*/
public class CustomRobolectricGradleTestRunner extends RobolectricGradleTestRunner {

public CustomRobolectricGradleTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}

@Override
protected AndroidManifest getAppManifest(Config config) {
AndroidManifest androidManifest = super.getAppManifest(config);
androidManifest.setPackageName("com.mrwang.stacklibrary");
return androidManifest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mrwang.stacklibrary.data;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mrwang.stacklibrary.R;
import com.mrwang.stacklibrary.RootFragment;

/**
* Created by dell on 2016/7/19.
*/
public class RootFragmentIml extends RootFragment {
//for test
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.activity_main, null);
return inflate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mrwang.stacklibrary.data;


import android.os.Bundle;

import com.mrwang.stacklibrary.RootActivity;
import com.mrwang.stacklibrary.RootFragment;

/**
* User: chengwangyong(chengwangyong@vcinema.com)
* Date: 2016-01-19
* Time: 09:48
*/
public class TestActivity extends RootActivity {

@Override
protected RootFragment getRootFragment() {
return new RootFragmentIml();
}

@Override
public void onCreateNow(Bundle savedInstanceState) {
}

/**
* Set the time to click to Prevent repeated clicks,default 500ms
*
* @param CLICK_SPACE Repeat click time(ms)
*/
public void setClickSpace(long CLICK_SPACE) {
manager.setClickSpace(CLICK_SPACE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mrwang.stacklibrary.data;

import com.mrwang.stacklibrary.RootFragment;

/**
* Created by dell on 2016/7/19.
*/
public class TestRootFragment extends RootFragment {
//just for test
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.mrwang.stacklibrary.test;

import com.mrwang.stacklibrary.CustomBuildConfig;
import com.mrwang.stacklibrary.FragmentStack;
import com.mrwang.stacklibrary.data.RootFragmentIml;
import com.mrwang.stacklibrary.data.TestRootFragment;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertEquals;

/**
* Created by dell on 2016/7/19.
*/
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = CustomBuildConfig.class, sdk = 21, packageName = "com.mrwang.stacklibrary")
public class FragmentStackTest {
FragmentStack stack;

@Before
public void setUp() {
stack = new FragmentStack();
}


@Test
public void putSingleTask() {
TestRootFragment rootFragment = new TestRootFragment();
assertEquals(stack.putSingleTask(rootFragment), false);
assertEquals(stack.putSingleTask(rootFragment), true);
}

@Test
public void putSingleTop() {
TestRootFragment rootFragment = new TestRootFragment();
assertEquals(stack.putSingleTop(rootFragment), false);
assertEquals(stack.putSingleTop(rootFragment), true);
}

@Test
public void putStandard() {
TestRootFragment rootFragment = new TestRootFragment();
stack.putStandard(rootFragment);
assertEquals(stack.putSingleTop(rootFragment), true);

RootFragmentIml iml = new RootFragmentIml();
assertEquals(stack.putSingleTop(iml), false);
}

@Test
public void putSingleInstance() {
TestRootFragment rootFragment = new TestRootFragment();
stack.putSingleInstance(rootFragment);
assertEquals(stack.putSingleTask(rootFragment), true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mrwang.stacklibrary.test;

import com.mrwang.stacklibrary.CustomBuildConfig;
import com.mrwang.stacklibrary.data.TestActivity;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

/**
* Created by dell on 2016/7/19.
*/
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = CustomBuildConfig.class, sdk = 21, packageName = "com.mrwang.stacklibrary")
public class StackManagerTest {

private TestActivity activity;
@Before
public void setUp(){
activity = Robolectric.buildActivity(TestActivity.class).create().get();
}


}