com.albedinsky.android.support.intent.IntentBaseTest.java Source code

Java tutorial

Introduction

Here is the source code for com.albedinsky.android.support.intent.IntentBaseTest.java

Source

/*
 * =================================================================================================
 *                             Copyright (C) 2015 Martin Albedinsky
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License 
 * you may obtain at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You can redistribute, modify or publish any part of the code written within this file but as it 
 * is described in the License, the software distributed under the License is distributed on an 
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 * 
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.albedinsky.android.support.intent;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.test.ActivityInstrumentationTestCase2;
import android.util.AndroidRuntimeException;

import com.albedinsky.android.support.intent.inner.TestActivity;
import com.albedinsky.android.support.intent.inner.TestFragment;
import com.albedinsky.android.support.intent.internal.IntentActivityWrapper;
import com.albedinsky.android.support.intent.internal.IntentFragmentWrapper;

/**
 * @author Martin Albedinsky
 */
public abstract class IntentBaseTest<I extends BaseIntent> extends ActivityInstrumentationTestCase2<TestActivity> {

    @SuppressWarnings("unused")
    private static final String TAG = "IntentBaseTest";

    private final Class<I> mIntentClass;
    Activity mActivity;
    I mIntent;

    public IntentBaseTest(Class<I> classOfIntent) {
        super(TestActivity.class);
        this.mIntentClass = classOfIntent;
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        this.mActivity = getActivity();
        this.createIntent();
    }

    public void testInstantiationWithActivity() {
        if (mIntentClass != null) {
            try {
                final BaseIntent intent = mIntentClass.getConstructor(Activity.class).newInstance(mActivity);
                assertTrue(intent.mContextWrapper instanceof IntentActivityWrapper);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void testInstantiationWithFragment() {
        if (mIntentClass != null) {
            try {
                final BaseIntent intent = mIntentClass.getConstructor(Fragment.class)
                        .newInstance(new TestFragment());
                assertTrue(intent.mContextWrapper instanceof IntentFragmentWrapper);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void createIntent() {
        if (mIntentClass != null) {
            try {
                this.mIntent = mIntentClass.getConstructor(Activity.class).newInstance(mActivity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    String getString(int resId) {
        return mActivity.getString(resId);
    }

    CharSequence getText(int resId) {
        return mActivity.getText(resId);
    }

    static void assertBuildThrowsExceptionWithCause(BaseIntent intent, String cause) {
        try {
            intent.build();
        } catch (AndroidRuntimeException e) {
            final String message = "Cannot build " + intent.getClass().getSimpleName() + ". " + cause;
            final String eMessage = e.getMessage();
            if (!message.contentEquals(eMessage)) {
                throw new AssertionError(
                        "Expected exception with message <" + message + "> but message was <" + eMessage + ">");
            }
            return;
        }
        final String intentName = intent.getClass().getSimpleName();
        throw new AssertionError("No exception has been thrown while building intent(" + intentName + ").");
    }
}