com.blogspot.ksoichiro.android.sample.transition.test.NormalTransitionTest.java Source code

Java tutorial

Introduction

Here is the source code for com.blogspot.ksoichiro.android.sample.transition.test.NormalTransitionTest.java

Source

/*
 * Copyright (c) 2013 Soichiro Kashima
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.blogspot.ksoichiro.android.sample.transition.test;

import com.blogspot.ksoichiro.android.sample.transition.Fragment1;
import com.blogspot.ksoichiro.android.sample.transition.Fragment2;
import com.blogspot.ksoichiro.android.sample.transition.MainActivity;
import com.blogspot.ksoichiro.android.sample.transition.R;
import com.blogspot.ksoichiro.android.sample.transition.SecondActivity;

import android.app.Instrumentation.ActivityMonitor;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.view.KeyEvent;

/**
 * ???
 * 
 * @author Soichiro Kashima
 */
public class NormalTransitionTest extends ActivityInstrumentationTestCase2<MainActivity> {

    public NormalTransitionTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // ?????
        setActivityInitialTouchMode(true);
    }

    public void testNormalTransition() {
        // Activity?
        ActivityMonitor monitorMain = new ActivityMonitor(MainActivity.class.getCanonicalName(), null, false);
        ActivityMonitor monitorSecond = new ActivityMonitor(SecondActivity.class.getCanonicalName(), null, false);
        getInstrumentation().addMonitor(monitorMain);
        getInstrumentation().addMonitor(monitorSecond);

        // ??Fragment?BackStack??????
        FragmentActivity activity = (FragmentActivity) getActivity();
        FragmentManager fm = activity.getSupportFragmentManager();
        assertEquals(0, fm.getBackStackEntryCount());

        // ??Fragment?
        Fragment frag = fm.findFragmentByTag("tag");
        assertNotNull(frag);
        assertEquals(Fragment1.class, frag.getClass());

        // ??(Fragment)?
        TouchUtils.tapView(this, getActivity().findViewById(R.id.btnFrag1));
        getInstrumentation().waitForIdleSync();

        // BackStack?Fragment???(?)???
        assertEquals(1, fm.getBackStackEntryCount());

        // ??(Fragment)?????
        frag = fm.findFragmentByTag("tag");
        assertNotNull(frag);
        assertEquals(Fragment2.class, frag.getClass());

        // ??(Activity)?
        TouchUtils.tapView(this, getActivity().findViewById(R.id.btnFrag2));
        getInstrumentation().waitForMonitorWithTimeout(monitorSecond, 2000);

        // ?Activity???????
        assertEquals(1, monitorSecond.getHits());

        // ?
        sendKeys(KeyEvent.KEYCODE_BACK);
        getInstrumentation().waitForIdleSync();

        // ??(Activity)??????
        assertEquals(1, monitorMain.getHits());

        // ???Fragment?BackStack????????
        activity = (FragmentActivity) monitorMain.getLastActivity();
        fm = activity.getSupportFragmentManager();
        assertEquals(1, fm.getBackStackEntryCount());
        frag = fm.findFragmentByTag("tag");
        assertNotNull(frag);
        assertEquals(Fragment2.class, frag.getClass());

        // ?
        sendKeys(KeyEvent.KEYCODE_BACK);
        getInstrumentation().waitForIdleSync();

        // ???Fragment????BackStack?????????
        assertEquals(0, fm.getBackStackEntryCount());
        frag = fm.findFragmentByTag("tag");
        assertNotNull(frag);
        assertEquals(Fragment1.class, frag.getClass());
    }

}