Skip to content

Commit

Permalink
更新 sample 代码
Browse files Browse the repository at this point in the history
  • Loading branch information
douo committed Mar 2, 2018
1 parent 4d430e4 commit 8c9c3d0
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 11 deletions.
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Activity> resumedActivities =
Expand All @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>

<activity android:name=".CaptureTestActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +50,8 @@ private void setupEditor() {
view ->
EditorActivityBuilder.create(this)
.hint("say something!")
.forCancel(
(mainActivity, intent) -> showContent("cancel"))
.forContent(this::showContent)
.start());
}
Expand Down Expand Up @@ -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());
}

Expand Down
11 changes: 11 additions & 0 deletions sample/src/main/res/layout/activity_capture_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.dourok.esactivity.sample.CaptureTestActivity"
>

</android.support.constraint.ConstraintLayout>
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/item_book.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>
</android.support.constraint.ConstraintLayout>
Expand Down

0 comments on commit 8c9c3d0

Please sign in to comment.