-
-
Save MichaelEvans/0aa091f83d2df36c9a56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.appli; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.test.ActivityInstrumentationTestCase2; | |
import com.example.appli.TestUtils; | |
public class MainActivityTest extends ActivityInstrumentationTestCase2<TestActivity> { | |
private static final String TAG = MainActivityTest.class.getSimpleName(); | |
private Activity mActivity; | |
public MainActivityTest() { | |
super(TestActivity.class); | |
} | |
@Override | |
public void setUp() throws Exception { | |
super.setUp(); | |
mActivity = getActivity(); | |
TestUtils.toggleAnimationEnable(mActivity, TAG, false); | |
} | |
@Override | |
public TestActivity getActivity() { | |
Intent intent = new Intent(); | |
intent.putExtra("key", "value"); | |
setActivityIntent(intent); | |
return super.getActivity(); | |
} | |
@Override | |
protected void tearDown() throws Exception { | |
TestUtils.toggleAnimationEnable(mActivity, TAG, true); | |
super.tearDown(); | |
} | |
public void testStory() throws Exception { | |
// TODO ここにテストを書く | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onView(withId(R.id.button)).check(matches(not(isDisplayed()))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.SET_ANIMATION_SCALE" /> | |
<instrumentation | |
android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" | |
android:targetPackage="com.example.appli" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Espresso.onView(ViewMatchers.withId(R.id.button)).check(ViewAssertions.matches(Matchers.not(ViewMatchers.isDisplayed()))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// R.id.button が表示されていないことをチェックします. | |
// notメソッドは org.hamcrest.Matchers.not | |
Espresso.onView(ViewMatchers.withId(R.id.button)).check(ViewAssertions.matches(Matchers.not(ViewMatchers.isDisplayed()))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.appli.espresso; | |
import static org.hamcrest.Matchers.is; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.Matchers; | |
import android.view.View; | |
import android.widget.ProgressBar; | |
import android.widget.TextView; | |
import com.google.android.apps.common.testing.ui.espresso.matcher.BoundedMatcher; | |
public final class EspressoUtils { | |
/** | |
* TextViewの文字列を前方一致で検査 | |
* | |
* @param expectString | |
* @return | |
*/ | |
public static Matcher<View> startsWith(final String expectString) { | |
final Matcher<String> textMatcher = is(expectString); | |
return new BoundedMatcher<View, TextView>(TextView.class) { | |
@Override | |
protected boolean matchesSafely(TextView textView) { | |
CharSequence text = textView.getText(); | |
if (text == null) { | |
return text == expectString; | |
} | |
return expectString.startsWith(text.toString()); | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("start text : "); | |
textMatcher.describeTo(description); | |
} | |
}; | |
} | |
/** | |
* TextViewの文字列を後方一致で検査 | |
* | |
* @param expectString | |
* @return | |
*/ | |
public static Matcher<View> endsWith(final String expectString) { | |
final Matcher<String> textMatcher = is(expectString); | |
return new BoundedMatcher<View, TextView>(TextView.class) { | |
@Override | |
protected boolean matchesSafely(TextView textView) { | |
CharSequence text = textView.getText(); | |
if (text == null) { | |
return text == expectString; | |
} | |
return expectString.endsWith(text.toString()); | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("end text : "); | |
textMatcher.describeTo(description); | |
} | |
}; | |
} | |
/** | |
* ProgressBarのプログレスを前方一致で検査 | |
* | |
* @param expectString | |
* @return | |
*/ | |
public static Matcher<View> withProgress(final Integer expectProgress) { | |
final Matcher<Integer> progressMatcher = is(expectProgress); | |
return new BoundedMatcher<View, ProgressBar>(ProgressBar.class) { | |
@Override | |
protected boolean matchesSafely(ProgressBar progressBar) { | |
return progressMatcher.matches(progressBar.getProgress()); | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("with progress : "); | |
progressMatcher.describeTo(description); | |
} | |
}; | |
} | |
private EspressoUtils() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.ViewGroup.LayoutParams; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.LinearLayout; | |
public class MainActivity extends Activity { | |
private static final int WC = LayoutParams.WRAP_CONTENT; | |
private static final int MP = LayoutParams.MATCH_PARENT; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final LinearLayout rootLayout = new LinearLayout(this); | |
rootLayout.setOrientation(LinearLayout.VERTICAL); | |
rootLayout.setLayoutParams(new LayoutParams(MP, MP)); | |
setContentView(rootLayout); | |
// EditText | |
final EditText editText = new EditText(this); | |
editText.setId(R.id.editText); | |
editText.setLayoutParams(new LayoutParams(MP, WC)); | |
editText.setHint("'test'で始まる文字列か,'123'で終わる文字列か,'abc'を入力してください"); | |
rootLayout.addView(editText); | |
// Button | |
final Button button = new Button(this); | |
button.setId(R.id.button); | |
button.setLayoutParams(new LayoutParams(WC, WC)); | |
button.setText("OK"); | |
rootLayout.addView(button); | |
button.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String input = editText.getText().toString(); | |
Intent intent = new Intent(); | |
intent.putExtra("input", input); | |
intent.setClassName(getApplicationContext(), "com.example.test.SubActivity"); | |
startActivity(intent); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test; | |
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView; | |
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click; | |
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.typeText; | |
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches; | |
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.test.ActivityInstrumentationTestCase2; | |
import com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers; | |
import com.example.test.TestUtils; | |
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> { | |
private static final String TAG = MainActivityTest.class.getSimpleName(); | |
private Activity mActivity; | |
public MainActivityTest() { | |
super(MainActivity.class); | |
} | |
@Override | |
public void setUp() throws Exception { | |
super.setUp(); | |
mActivity = getActivity(); | |
TestUtils.toggleAnimationEnable(mActivity, TAG, false); | |
} | |
@Override | |
protected void tearDown() throws Exception { | |
TestUtils.toggleAnimationEnable(mActivity, TAG, true); | |
super.tearDown(); | |
} | |
public void testStory() throws Exception { | |
onView(withId(R.id.editText)).perform(typeText("123456")); | |
onView(withId(R.id.button)).perform(click()); | |
onView(withId(R.id.text)).check(matches(ViewMatchers.withText("123456"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.ViewGroup.LayoutParams; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
public class SubActivity extends Activity { | |
private static final int WC = LayoutParams.WRAP_CONTENT; | |
private static final int MP = LayoutParams.MATCH_PARENT; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final LinearLayout rootLayout = new LinearLayout(this); | |
rootLayout.setOrientation(LinearLayout.VERTICAL); | |
rootLayout.setLayoutParams(new LayoutParams(MP, MP)); | |
setContentView(rootLayout); | |
Bundle bundle = getIntent().getExtras(); | |
String text = bundle.getString("input"); | |
// TextView | |
final TextView textView = new TextView(this); | |
textView.setId(R.id.text); | |
textView.setLayoutParams(new LayoutParams(WC, WC)); | |
textView.setText(text); | |
rootLayout.addView(textView); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.appli; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.os.IBinder; | |
import android.util.Log; | |
public final class TestUtils { | |
private static final int RESULT_ENABLED = 0; | |
private static final int RESULT_DISABLED = 1; | |
private static final int RESULT_NG = -1; | |
/** | |
* EspressoでUIテストを実行する場合は setUp メソッドで無効にし,tearDownで有効にしてください | |
* | |
* @param context | |
* @param TAG | |
* @param isEnable | |
*/ | |
public static void toggleAnimationEnable(Context context, String TAG, boolean isEnable) { | |
int permStatus = context | |
.checkCallingOrSelfPermission("android.permission.SET_ANIMATION_SCALE"); | |
if (permStatus == PackageManager.PERMISSION_GRANTED) { | |
int result; | |
if ((result = reflectivelyToggleAnimation(TAG, isEnable)) != RESULT_NG) { | |
if (result == RESULT_ENABLED) { | |
Log.i(TAG, "All animations enabled."); | |
} else { | |
Log.i(TAG, "All animations disabled."); | |
} | |
} else { | |
Log.i(TAG, "Could not toggle animations."); | |
} | |
} else { | |
Log.i(TAG, "Cannot disable animations due to lack of permission."); | |
} | |
} | |
private static int reflectivelyToggleAnimation(String TAG, boolean isEnable) { | |
try { | |
Class<?> windowManagerStubClazz = Class.forName("android.view.IWindowManager$Stub"); | |
Method asInterface = windowManagerStubClazz.getDeclaredMethod("asInterface", | |
IBinder.class); | |
Class<?> serviceManagerClazz = Class.forName("android.os.ServiceManager"); | |
Method getService = serviceManagerClazz.getDeclaredMethod("getService", String.class); | |
Class<?> windowManagerClazz = Class.forName("android.view.IWindowManager"); | |
Method setAnimationScales = windowManagerClazz.getDeclaredMethod("setAnimationScales", | |
float[].class); | |
Method getAnimationScales = windowManagerClazz.getDeclaredMethod("getAnimationScales"); | |
IBinder windowManagerBinder = (IBinder) getService.invoke(null, "window"); | |
Object windowManagerObj = asInterface.invoke(null, windowManagerBinder); | |
float[] currentScales = (float[]) getAnimationScales.invoke(windowManagerObj); | |
for (int i = 0; i < currentScales.length; i++) { | |
if (isEnable) { | |
currentScales[i] = 1.0f; | |
} else { | |
currentScales[i] = 0.0f; | |
} | |
} | |
setAnimationScales.invoke(windowManagerObj, currentScales); | |
return isEnable ? RESULT_ENABLED : RESULT_DISABLED; | |
} catch (ClassNotFoundException cnfe) { | |
Log.w(TAG, "Cannot disable animations reflectively.", cnfe); | |
} catch (NoSuchMethodException mnfe) { | |
Log.w(TAG, "Cannot disable animations reflectively.", mnfe); | |
} catch (SecurityException se) { | |
Log.w(TAG, "Cannot disable animations reflectively.", se); | |
} catch (InvocationTargetException ite) { | |
Log.w(TAG, "Cannot disable animations reflectively.", ite); | |
} catch (IllegalAccessException iae) { | |
Log.w(TAG, "Cannot disable animations reflectively.", iae); | |
} catch (RuntimeException re) { | |
Log.w(TAG, "Cannot disable animations reflectively.", re); | |
} | |
return RESULT_NG; | |
} | |
private TestUtils() { | |
// インスタンス化の禁止 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<item name="editText" type="id"/> | |
<item name="button" type="id"/> | |
<item name="text" type="id"/> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment