/*
* Copyright (C) 2015 Tomasz Rozbicki
*
* 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 com.byoutline.kickmaterial.espressohelpers;
import android.app.Activity;
import android.app.Application;
import android.support.annotation.NonNull;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
/**
* {@link ActivityTestRule} which provides hook for
* {@link ActivityTestRule#beforeActivityLaunched()} method. Can be used for test dependency
* injection especially in Espresso based tests.
*
* @author Tomasz Rozbicki
*/
public class DaggerActivityTestRule<T extends Activity> extends ActivityTestRule<T> {
private final OnBeforeActivityLaunchedListener<T> mListener;
public DaggerActivityTestRule(Class<T> activityClass,
@NonNull OnBeforeActivityLaunchedListener<T> listener) {
this(activityClass, false, listener);
}
public DaggerActivityTestRule(Class<T> activityClass, boolean initialTouchMode,
@NonNull OnBeforeActivityLaunchedListener<T> listener) {
this(activityClass, initialTouchMode, true, listener);
}
public DaggerActivityTestRule(Class<T> activityClass, boolean initialTouchMode,
boolean launchActivity,
@NonNull OnBeforeActivityLaunchedListener<T> listener) {
super(activityClass, initialTouchMode, launchActivity);
mListener = listener;
}
@Override
protected void beforeActivityLaunched() {
super.beforeActivityLaunched();
mListener.beforeActivityLaunched((Application) InstrumentationRegistry.getInstrumentation()
.getTargetContext().getApplicationContext(), getActivity());
}
public interface OnBeforeActivityLaunchedListener<T> {
void beforeActivityLaunched(@NonNull Application application, @NonNull T activity);
}
}