From 290583ada01c1656a0943398cdf1a325ffa8d3fa Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Wed, 5 Oct 2016 17:38:59 +0200 Subject: [PATCH 01/15] Test plugin activity recreate --- plugin/build.gradle | 4 + .../thirtyinch/plugin/TiPluginTest.java | 83 +++++++++++++++++ plugin/src/debug/java/AndroidManifest.xml | 8 ++ .../thirtyinch/plugin/TestActivity.java | 49 ++++++++++ .../thirtyinch/plugin/TestPresenter.java | 22 +++++ .../thirtyinch/plugin/TestView.java | 22 +++++ plugin/src/debug/res/layout/activity_test.xml | 14 +++ plugin/src/main/AndroidManifest.xml | 7 +- .../thirtyinch/plugin/TiActivityPlugin.java | 5 + .../thirtyinch/sample/HelloWorldActivity.java | 8 ++ .../main/res/layout/activity_hello_world.xml | 9 ++ .../internal/TiActivityDelegateTest.java | 10 ++ .../grandcentrix/thirtyinch/TiActivity.java | 5 - .../internal/DelegatedTiActivity.java | 7 +- .../internal/TiActivityDelegate.java | 93 ++++++++++++------- 15 files changed, 303 insertions(+), 43 deletions(-) create mode 100644 plugin/src/androidTest/java/net/grandcentrix/thirtyinch/plugin/TiPluginTest.java create mode 100644 plugin/src/debug/java/AndroidManifest.xml create mode 100644 plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestActivity.java create mode 100644 plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestPresenter.java create mode 100644 plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestView.java create mode 100644 plugin/src/debug/res/layout/activity_test.xml diff --git a/plugin/build.gradle b/plugin/build.gradle index 16c4316a..791a822c 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -19,6 +19,7 @@ android { targetSdkVersion TARGET_SDK_VERSION versionCode VERSION_CODE versionName VERSION_NAME + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { @@ -33,6 +34,9 @@ android { dependencies { compile project(':thirtyinch') compile "com.pascalwelsch.compositeandroid:activity:$supportLibraryVersion" + androidTestCompile 'com.android.support.test:runner:0.5'; + androidTestCompile 'com.android.support.test:rules:0.5'; + androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'; } publish { diff --git a/plugin/src/androidTest/java/net/grandcentrix/thirtyinch/plugin/TiPluginTest.java b/plugin/src/androidTest/java/net/grandcentrix/thirtyinch/plugin/TiPluginTest.java new file mode 100644 index 00000000..577ba929 --- /dev/null +++ b/plugin/src/androidTest/java/net/grandcentrix/thirtyinch/plugin/TiPluginTest.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2016 grandcentrix GmbH + * 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 net.grandcentrix.thirtyinch.plugin; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import android.app.Instrumentation; +import android.support.test.InstrumentationRegistry; +import android.support.test.espresso.Espresso; +import android.support.test.filters.LargeTest; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertNotEquals; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class TiPluginTest { + + @Rule + public ActivityTestRule mActivityRule = + new ActivityTestRule<>(TestActivity.class); + + @Test + public void recreate() throws Throwable { + final TestActivity first = mActivityRule.getActivity(); + + Espresso.onView(withId(R.id.helloworld_text)) + .check(matches(isDisplayed())); + + final Instrumentation.ActivityMonitor activityMonitor = new Instrumentation.ActivityMonitor( + TestActivity.class.getName(), null, false); + InstrumentationRegistry.getInstrumentation().addMonitor(activityMonitor); + + assertNull(activityMonitor.getLastActivity()); + + first.runOnUiThread(new Runnable() { + @Override + public void run() { + first.recreate(); + } + }); + /*Context context = InstrumentationRegistry.getTargetContext(); + int orientation = context.getResources().getConfiguration().orientation; + + first.setRequestedOrientation((orientation == Configuration.ORIENTATION_PORTRAIT) + ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);*/ + + assertNotNull(activityMonitor.getLastActivity()); + final TestActivity second = (TestActivity) activityMonitor.waitForActivityWithTimeout(5000); + assertNotNull(second); + + //final TestActivity second = mActivityRule.getActivity(); + + assertEquals(2, activityMonitor.getHits()); + assertNotEquals(first, second); + + Espresso.onView(withId(R.id.helloworld_text)) + .check(matches(isDisplayed())); + } +} diff --git a/plugin/src/debug/java/AndroidManifest.xml b/plugin/src/debug/java/AndroidManifest.xml new file mode 100644 index 00000000..f5a1a27a --- /dev/null +++ b/plugin/src/debug/java/AndroidManifest.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestActivity.java b/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestActivity.java new file mode 100644 index 00000000..342c345c --- /dev/null +++ b/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestActivity.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2016 grandcentrix GmbH + * 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 net.grandcentrix.thirtyinch.plugin; + +import com.pascalwelsch.compositeandroid.activity.CompositeActivity; + +import net.grandcentrix.thirtyinch.internal.TiPresenterProvider; + +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** + * only for tests + */ +public class TestActivity extends CompositeActivity implements TestView { + + + public TestActivity() { + addPlugin(new TiActivityPlugin<>(new TiPresenterProvider() { + @NonNull + @Override + public TestPresenter providePresenter() { + return new TestPresenter(); + } + })); + } + + + @Override + public void onCreate(@Nullable final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.activity_test); + } +} diff --git a/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestPresenter.java b/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestPresenter.java new file mode 100644 index 00000000..01e9469d --- /dev/null +++ b/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestPresenter.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2016 grandcentrix GmbH + * 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 net.grandcentrix.thirtyinch.plugin; + +import net.grandcentrix.thirtyinch.TiPresenter; + +public class TestPresenter extends TiPresenter { + +} diff --git a/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestView.java b/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestView.java new file mode 100644 index 00000000..29453dff --- /dev/null +++ b/plugin/src/debug/java/net/grandcentrix/thirtyinch/plugin/TestView.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2016 grandcentrix GmbH + * 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 net.grandcentrix.thirtyinch.plugin; + +import net.grandcentrix.thirtyinch.TiView; + +public interface TestView extends TiView { + +} diff --git a/plugin/src/debug/res/layout/activity_test.xml b/plugin/src/debug/res/layout/activity_test.xml new file mode 100644 index 00000000..15517959 --- /dev/null +++ b/plugin/src/debug/res/layout/activity_test.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/plugin/src/main/AndroidManifest.xml b/plugin/src/main/AndroidManifest.xml index 038cbd3e..d4b6525e 100644 --- a/plugin/src/main/AndroidManifest.xml +++ b/plugin/src/main/AndroidManifest.xml @@ -1,7 +1,12 @@ - + + diff --git a/plugin/src/main/java/net/grandcentrix/thirtyinch/plugin/TiActivityPlugin.java b/plugin/src/main/java/net/grandcentrix/thirtyinch/plugin/TiActivityPlugin.java index 9e13f8ed..a39fca1f 100644 --- a/plugin/src/main/java/net/grandcentrix/thirtyinch/plugin/TiActivityPlugin.java +++ b/plugin/src/main/java/net/grandcentrix/thirtyinch/plugin/TiActivityPlugin.java @@ -121,6 +121,11 @@ public void invalidateView() { mDelegate.invalidateView(); } + @Override + public boolean isActivityChangingConfigurations() { + return getActivity().isChangingConfigurations(); + } + @Override public boolean isActivityFinishing() { return getActivity().isFinishing(); diff --git a/sample/src/main/java/net/grandcentrix/thirtyinch/sample/HelloWorldActivity.java b/sample/src/main/java/net/grandcentrix/thirtyinch/sample/HelloWorldActivity.java index 632f5865..8429fcab 100644 --- a/sample/src/main/java/net/grandcentrix/thirtyinch/sample/HelloWorldActivity.java +++ b/sample/src/main/java/net/grandcentrix/thirtyinch/sample/HelloWorldActivity.java @@ -23,6 +23,7 @@ import android.os.Bundle; import android.support.annotation.NonNull; +import android.view.View; import android.widget.Button; import android.widget.TextView; @@ -76,5 +77,12 @@ protected void onCreate(final Bundle savedInstanceState) { .replace(R.id.fragment_container, new SampleFragment()) .commit(); } + + findViewById(R.id.recreate).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(final View v) { + recreate(); + } + }); } } diff --git a/sample/src/main/res/layout/activity_hello_world.xml b/sample/src/main/res/layout/activity_hello_world.xml index 8362808f..2ddc652b 100644 --- a/sample/src/main/res/layout/activity_hello_world.xml +++ b/sample/src/main/res/layout/activity_hello_world.xml @@ -38,4 +38,13 @@ android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentBottom="true" /> + +