From 8c9c3d0fe38db75c8850f5a5484d61a05084e929 Mon Sep 17 00:00:00 2001 From: Tiou Lims Date: Fri, 2 Mar 2018 12:08:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20sample=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/build.gradle | 1 + .../sample/ExampleInstrumentedTest.java | 31 ++++++++++++++----- .../sample/OrientationChangeAction.java | 25 ++++++++++++++- sample/src/main/AndroidManifest.xml | 8 +++++ .../sample/CaptureTestActivity.java | 16 ++++++++++ .../esactivity/sample/MainActivity.java | 5 ++- .../main/res/layout/activity_capture_test.xml | 11 +++++++ sample/src/main/res/layout/item_book.xml | 2 +- 8 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 sample/src/main/java/info/dourok/esactivity/sample/CaptureTestActivity.java create mode 100644 sample/src/main/res/layout/activity_capture_test.xml diff --git a/sample/build.gradle b/sample/build.gradle index d8affda..8b2dd70 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -41,6 +41,7 @@ dependencies { implementation deps.arch.persistence.room + implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation deps.junit androidTestImplementation deps.support.test.runner androidTestImplementation deps.support.test.espresso diff --git a/sample/src/androidTest/java/info/dourok/esactivity/sample/ExampleInstrumentedTest.java b/sample/src/androidTest/java/info/dourok/esactivity/sample/ExampleInstrumentedTest.java index 3fdb088..26f4414 100644 --- a/sample/src/androidTest/java/info/dourok/esactivity/sample/ExampleInstrumentedTest.java +++ b/sample/src/androidTest/java/info/dourok/esactivity/sample/ExampleInstrumentedTest.java @@ -9,16 +9,13 @@ import org.junit.runner.RunWith; import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.Espresso.pressBack; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.replaceText; import static android.support.test.espresso.assertion.ViewAssertions.matches; -import static android.support.test.espresso.matcher.RootMatchers.withDecorView; -import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.isRoot; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; /** * Instrumented test, which will execute on an Android device. @@ -40,16 +37,34 @@ public void everythingIsOk() throws Exception { onView(withId(R.id.content)).check(matches(withText(text))); } + @Test + public void cancelIsOk() throws Exception { + final String text = "cancel"; + onView(withText("Editor")).perform(click()); + pressBack(); + onView(withId(R.id.content)).check(matches(withText(text))); + } + + @Test + public void cancelIsOkAfterActivityRecreate() throws Exception { + final String text = "cancel"; + onView(withText("Editor")).perform(click()); + //onView(isRoot()).perform(OrientationChangeAction.orientationLandscape()); + InstrumentationRegistry.getInstrumentation() + .runOnMainSync(() -> mActivityRule.getActivity().recreate()); + pressBack(); + onView(withId(R.id.content)).check(matches(withText(text))); + } + @Test public void shouldWorkAfterActivityRecreate() throws Exception { final String text = "text"; onView(withText("Editor")).perform(click()); - onView(isRoot()).perform(OrientationChangeAction.orientationLandscape()); + // onView(isRoot()).perform(OrientationChangeAction.orientationLandscape()); onView(withId(R.id.edit_text)).perform(replaceText(text)); - InstrumentationRegistry.getInstrumentation() - .runOnMainSync(() -> mActivityRule.getActivity().recreate()); + //InstrumentationRegistry.getInstrumentation() + // .runOnMainSync(() -> mActivityRule.getActivity().recreate()); onView(withId(R.id.action_ok)).perform(click()); - // onView(isRoot()).perform(OrientationChangeAction.orientationLandscape()); onView(withId(R.id.content)).check(matches(withText(text))); } } diff --git a/sample/src/androidTest/java/info/dourok/esactivity/sample/OrientationChangeAction.java b/sample/src/androidTest/java/info/dourok/esactivity/sample/OrientationChangeAction.java index b6a7824..85167ef 100644 --- a/sample/src/androidTest/java/info/dourok/esactivity/sample/OrientationChangeAction.java +++ b/sample/src/androidTest/java/info/dourok/esactivity/sample/OrientationChangeAction.java @@ -31,6 +31,7 @@ import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; import android.support.test.runner.lifecycle.Stage; import android.view.View; +import java.lang.reflect.Field; import java.util.Collection; import org.hamcrest.Matcher; @@ -57,7 +58,8 @@ public String getDescription() { @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); - final Activity activity = (Activity) view.getContext(); + final Activity activity = getActivity(view); + activity.setRequestedOrientation(orientation); Collection resumedActivities = @@ -67,6 +69,27 @@ public void perform(UiController uiController, View view) { } } + private static Activity getActivity(View view) { + Activity activity = null; + if (view.getContext() + .getClass() + .getName() + .contains("com.android.internal.policy.DecorContext")) { + try { + Field field = view.getContext().getClass().getDeclaredField("mPhoneWindow"); + field.setAccessible(true); + Object obj = field.get(view.getContext()); + java.lang.reflect.Method m1 = obj.getClass().getMethod("getContext"); + activity = (Activity) (m1.invoke(obj)); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + activity = (Activity) view.getContext(); + } + return activity; + } + public static ViewAction orientationLandscape() { return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 8108184..70dd661 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -42,6 +42,14 @@ android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> + + + + + + + + \ No newline at end of file diff --git a/sample/src/main/java/info/dourok/esactivity/sample/CaptureTestActivity.java b/sample/src/main/java/info/dourok/esactivity/sample/CaptureTestActivity.java new file mode 100644 index 0000000..002fea6 --- /dev/null +++ b/sample/src/main/java/info/dourok/esactivity/sample/CaptureTestActivity.java @@ -0,0 +1,16 @@ +package info.dourok.esactivity.sample; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; + +/** + * @author tiaolins + */ +public class CaptureTestActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_capture_test); + } +} diff --git a/sample/src/main/java/info/dourok/esactivity/sample/MainActivity.java b/sample/src/main/java/info/dourok/esactivity/sample/MainActivity.java index 093b514..681d7a1 100644 --- a/sample/src/main/java/info/dourok/esactivity/sample/MainActivity.java +++ b/sample/src/main/java/info/dourok/esactivity/sample/MainActivity.java @@ -15,6 +15,7 @@ import android.widget.TextView; import android.widget.Toast; import info.dourok.esactivity.BuilderUtil; +import info.dourok.esactivity.function.BiConsumer; import info.dourok.esactivity.sample.books.BookListActivity; import info.dourok.esactivity.sample.camera.CameraActivity; import info.dourok.esactivity.sample.editor.EditorActivity; @@ -49,6 +50,8 @@ private void setupEditor() { view -> EditorActivityBuilder.create(this) .hint("say something!") + .forCancel( + (mainActivity, intent) -> showContent("cancel")) .forContent(this::showContent) .start()); } @@ -85,7 +88,7 @@ private void goodPractice() { Snackbar.LENGTH_LONG) .setAction("Action", null) .show()) - .forText((context, s) -> Toast.makeText(context, "" + s, Toast.LENGTH_SHORT).show()) + .forText(s -> Toast.makeText(this, "" + s, Toast.LENGTH_SHORT).show()) .start()); } diff --git a/sample/src/main/res/layout/activity_capture_test.xml b/sample/src/main/res/layout/activity_capture_test.xml new file mode 100644 index 0000000..3f2d83b --- /dev/null +++ b/sample/src/main/res/layout/activity_capture_test.xml @@ -0,0 +1,11 @@ + + + + diff --git a/sample/src/main/res/layout/item_book.xml b/sample/src/main/res/layout/item_book.xml index 9183eb6..4d967fc 100644 --- a/sample/src/main/res/layout/item_book.xml +++ b/sample/src/main/res/layout/item_book.xml @@ -62,7 +62,7 @@ android:textAppearance="@style/TextAppearance.AppCompat.Headline" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/imageView2" - app:layout_constraintTop_toBottomOf="@+id/textView" + app:layout_constraintTop_toBottomOf="@+id/content" tools:text="Author" />