/*
 * 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_test;

import org.junit.Test;
import org.junit.runner.RunWith;

import android.app.Instrumentation;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.filters.LargeTest;
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 android.support.test.espresso.matcher.ViewMatchers.withText;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNotSame;
import static junit.framework.Assert.assertSame;
import static net.grandcentrix.thirtyinch.plugin_test.TestUtils.rotateOrientation;
import static org.hamcrest.Matchers.allOf;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class TiPluginTest {

    /**
     * Tests the full Activity lifecycle. Guarantees every lifecycle method gets called
     */
    @Test
    public void testFullLifecycleIncludingConfigurationChange() throws Throwable {
        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();

        // register monitor to track activity startups
        final Instrumentation.ActivityMonitor activityMonitor =
                new Instrumentation.ActivityMonitor(TestActivity.class.getName(), null, false);
        instrumentation.addMonitor(activityMonitor);

        // start the activity for the first time
        final Intent intent = new Intent(instrumentation.getTargetContext(), TestActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        instrumentation.startActivitySync(intent);

        // get activity reference
        final TestActivity first = (TestActivity) activityMonitor.waitForActivityWithTimeout(5000);
        assertNotNull(first);

        // make sure the attached presenter filled the UI
        Espresso.onView(withId(R.id.helloworld_text))
                .check(matches(allOf(isDisplayed(), withText("Hello World 1"))));

        // restart the activity
        rotateOrientation(first);

        // the monitor get's hit when onDestroy gets called for the first time. It's the old
        // activity reference
        final TestActivity destroyedActivity =
                (TestActivity) activityMonitor.waitForActivityWithTimeout(5000);
        // make sure it's the previously started activity, and ignore it
        assertSame(destroyedActivity, first);

        // next hit is the recreated activity
        final TestActivity second = (TestActivity) activityMonitor.waitForActivityWithTimeout(5000);
        assertNotNull(second);
        // is has to be a different Activity object
        assertNotSame(first, second);

        // assert the activity was bound to the presenter. The presenter should update the UI
        // correctly
        Espresso.onView(withId(R.id.helloworld_text))
                .check(matches(allOf(isDisplayed(), withText("Hello World 2"))));
    }
}

@@ -701,16 +701,45 @@
 nWith;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -1303,161 +1303,8 @@
 xt;%0A
-import static junit.framework.Assert.assertNotNull;%0Aimport static junit.framework.Assert.assertNotSame;%0Aimport static junit.framework.Assert.assertSame;%0A
 impo
@@ -1504,179 +1504,47 @@
     
-/**%0A     * Tests the full Activity lifecycle. Guarantees every lifecycle method gets called%0A     */%0A    @Test%0A    public void testFullLifecycleIncludingConfigurationChange
+@Test%0A    public void startTestActivity
 () t
@@ -1553,17 +1553,17 @@
 ows 
-Throwable
+Exception
  %7B%0A 
@@ -1660,277 +1660,8 @@
 );%0A%0A
-        // register monitor to track activity startups%0A        final Instrumentation.ActivityMonitor activityMonitor =%0A                new Instrumentation.ActivityMonitor(TestActivity.class.getName(), null, false);%0A        instrumentation.addMonitor(activityMonitor);%0A%0A
     
@@ -1867,18 +1867,44 @@
         
+f
 in
+al Activity activity = in
 strument
@@ -1936,173 +1936,8 @@
 nten
-t);%0A%0A        // get activity reference%0A        final TestActivity first = (TestActivity) activityMonitor.waitForActivityWithTimeout(5000);%0A        assertNotNull(firs
 t);%0A
@@ -2144,318 +2144,326 @@
     
-// restart the activity%0A        rotateOrientation(first
+activity.finish(
 );%0A
-%0A
     
+%7D%0A%0A
     /
-/ the monitor get's hit when onDestroy gets called for the first time. It's the old%0A        // activity reference%0A        final TestActivity destroyedActivity =%0A                (TestActivity) activityMonitor.waitForActivityWithTimeout(5000
+**%0A     * Tests the full Activity lifecycle. Guarantees every lifecycle method gets called%0A     */%0A    @Test%0A    public void testFullLifecycleIncludingConfigurationChange() throws Throwable %7B%0A        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(
 );%0A
+%0A
     
@@ -2473,106 +2473,135 @@
  // 
-make sure it's the previously started activity, and ignore it%0A        assertSame(destroyed
+start the activity for the first time%0A        final Intent intent = new Intent(instrumentation.getTargetContext(), Test
 Activity
 , fi
@@ -2600,27 +2600,25 @@
 vity
-, first
+.class
 );%0A
-%0A
         
 // n
@@ -2617,45 +2617,55 @@
     
-// next hit is the recreated activity
+intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 %0A   
@@ -2688,22 +2688,24 @@
 ctivity 
-second
+activity
  = (Test
@@ -2718,66 +2718,60 @@
 ty) 
-activityMonitor.waitForActivityWithTimeout(5000
+instrumentation.startActivitySync(intent
 );%0A
+%0A
         
 asse
@@ -2770,118 +2770,261 @@
     
-assertNotNull(second);%0A        // is has to be a different Activity object%0A        assertNotSame(first, second
+// make sure the attached presenter filled the UI%0A        Espresso.onView(withId(R.id.helloworld_text))%0A                .check(matches(allOf(isDisplayed(), withText(%22Hello World 1%22))));%0A%0A        // restart the activity%0A        rotateOrientation(activity
 );%0A%0A
@@ -3273,16 +3273,44 @@
  2%22))));
+%0A%0A        activity.finish();
 %0A    %7D%0A%7D

Simplify UI tests, only check the textview text