use of android.test.mock.MockApplication in project AndroidTraining by mixi-inc.
the class ObserverTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
mMockApplication = new MockApplication();
Proton.initialize(mMockApplication, new DefaultModule() {
@Override
protected void configure() {
super.configure();
bind(Observer.class);
}
});
mInjector = Proton.getInjector(new MockContext(mMockApplication));
}
use of android.test.mock.MockApplication in project android_frameworks_base by ResurrectionRemix.
the class ActivityUnitTestCase method startActivity.
/**
* Start the activity under test, in the same way as if it was started by
* {@link android.content.Context#startActivity Context.startActivity()}, providing the
* arguments it supplied. When you use this method to start the activity, it will automatically
* be stopped by {@link #tearDown}.
*
* <p>This method will call onCreate(), but if you wish to further exercise Activity life
* cycle methods, you must call them yourself from your test case.
*
* <p><i>Do not call from your setUp() method. You must call this method from each of your
* test methods.</i>
*
* @param intent The Intent as if supplied to {@link android.content.Context#startActivity}.
* @param savedInstanceState The instance state, if you are simulating this part of the life
* cycle. Typically null.
* @param lastNonConfigurationInstance This Object will be available to the
* Activity if it calls {@link android.app.Activity#getLastNonConfigurationInstance()}.
* Typically null.
* @return Returns the Activity that was created
*/
protected T startActivity(Intent intent, Bundle savedInstanceState, Object lastNonConfigurationInstance) {
assertFalse("Activity already created", mCreated);
if (!mAttached) {
assertNotNull(mActivityClass);
setActivity(null);
T newActivity = null;
try {
IBinder token = null;
if (mApplication == null) {
setApplication(new MockApplication());
}
ComponentName cn = new ComponentName(mActivityClass.getPackage().getName(), mActivityClass.getName());
intent.setComponent(cn);
ActivityInfo info = new ActivityInfo();
CharSequence title = mActivityClass.getName();
mMockParent = new MockParent();
String id = null;
newActivity = (T) getInstrumentation().newActivity(mActivityClass, mActivityContext, token, mApplication, intent, info, title, mMockParent, id, lastNonConfigurationInstance);
} catch (Exception e) {
Log.w(TAG, "Catching exception", e);
assertNotNull(newActivity);
}
assertNotNull(newActivity);
setActivity(newActivity);
mAttached = true;
}
T result = getActivity();
if (result != null) {
getInstrumentation().callActivityOnCreate(getActivity(), savedInstanceState);
mCreated = true;
}
return result;
}
use of android.test.mock.MockApplication in project android_frameworks_base by crdroidandroid.
the class ActivityUnitTestCase method startActivity.
/**
* Start the activity under test, in the same way as if it was started by
* {@link android.content.Context#startActivity Context.startActivity()}, providing the
* arguments it supplied. When you use this method to start the activity, it will automatically
* be stopped by {@link #tearDown}.
*
* <p>This method will call onCreate(), but if you wish to further exercise Activity life
* cycle methods, you must call them yourself from your test case.
*
* <p><i>Do not call from your setUp() method. You must call this method from each of your
* test methods.</i>
*
* @param intent The Intent as if supplied to {@link android.content.Context#startActivity}.
* @param savedInstanceState The instance state, if you are simulating this part of the life
* cycle. Typically null.
* @param lastNonConfigurationInstance This Object will be available to the
* Activity if it calls {@link android.app.Activity#getLastNonConfigurationInstance()}.
* Typically null.
* @return Returns the Activity that was created
*/
protected T startActivity(Intent intent, Bundle savedInstanceState, Object lastNonConfigurationInstance) {
assertFalse("Activity already created", mCreated);
if (!mAttached) {
assertNotNull(mActivityClass);
setActivity(null);
T newActivity = null;
try {
IBinder token = null;
if (mApplication == null) {
setApplication(new MockApplication());
}
ComponentName cn = new ComponentName(mActivityClass.getPackage().getName(), mActivityClass.getName());
intent.setComponent(cn);
ActivityInfo info = new ActivityInfo();
CharSequence title = mActivityClass.getName();
mMockParent = new MockParent();
String id = null;
newActivity = (T) getInstrumentation().newActivity(mActivityClass, mActivityContext, token, mApplication, intent, info, title, mMockParent, id, lastNonConfigurationInstance);
} catch (Exception e) {
Log.w(TAG, "Catching exception", e);
assertNotNull(newActivity);
}
assertNotNull(newActivity);
setActivity(newActivity);
mAttached = true;
}
T result = getActivity();
if (result != null) {
getInstrumentation().callActivityOnCreate(getActivity(), savedInstanceState);
mCreated = true;
}
return result;
}
use of android.test.mock.MockApplication in project HoloEverywhere by Prototik.
the class _HoloActivity method onInit.
/**
* Do not override this method. Use {@link #onPreInit(Holo, Bundle)} and
* {@link #onPostInit(Holo, Bundle)}
*/
protected void onInit(Holo config, Bundle savedInstanceState) {
if (mInited) {
throw new IllegalStateException("This instance was already inited");
}
mInited = true;
if (config == null) {
config = createConfig(savedInstanceState);
}
if (config == null) {
config = Holo.defaultConfig();
}
onPreInit(config, savedInstanceState);
if (!config.ignoreApplicationInstanceCheck && !(getApplication() instanceof Application)) {
boolean throwError = true;
if (config.allowMockApplicationInstance) {
try {
throwError = !(getApplication() instanceof MockApplication);
if (!throwError) {
Log.w("HoloEverywhere", "Application instance is MockApplication. Wow. Let's begin tests...");
}
} catch (Exception e) {
}
}
if (throwError) {
String text = "Application instance isn't HoloEverywhere.\n";
if (getApplication().getClass() == android.app.Application.class) {
text += "Put attr 'android:name=\"org.holoeverywhere.app.Application\"'" + " in <application> tag of AndroidManifest.xml";
} else {
text += "Please sure that you extend " + getApplication().getClass() + " from a org.holoeverywhere.app.Application";
}
throw new IllegalStateException(text);
}
}
getLayoutInflater().setFragmentActivity(this);
if (this instanceof Activity) {
final Activity activity = (Activity) this;
ThemeManager.applyTheme(activity, mLastThemeResourceId == 0);
if (!config.ignoreThemeCheck && ThemeManager.getThemeType(this) == ThemeManager.INVALID) {
throw new HoloThemeException(activity);
}
TypedArray a = obtainStyledAttributes(new int[] { android.R.attr.windowActionBarOverlay, R.attr.windowActionBarOverlay });
if (a.getBoolean(0, false) || a.getBoolean(1, false)) {
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
}
a.recycle();
a = obtainStyledAttributes(new int[] { android.R.attr.windowActionModeOverlay, R.attr.windowActionBarOverlay });
if (a.getBoolean(0, false) || a.getBoolean(1, false)) {
supportRequestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
}
a.recycle();
}
onPostInit(config, savedInstanceState);
lockAttaching();
}
use of android.test.mock.MockApplication in project android_frameworks_base by ParanoidAndroid.
the class ServiceTestCase method setupService.
/**
* Creates the service under test and attaches all injected dependencies
* (Context, Application) to it. This is called automatically by {@link #startService} or
* by {@link #bindService}.
* If you need to call {@link AndroidTestCase#setContext(Context) setContext()} or
* {@link #setApplication setApplication()}, do so before calling this method.
*/
protected void setupService() {
mService = null;
try {
mService = mServiceClass.newInstance();
} catch (Exception e) {
assertNotNull(mService);
}
if (getApplication() == null) {
setApplication(new MockApplication());
}
mService.attach(getContext(), // ActivityThread not actually used in Service
null, mServiceClass.getName(), // token not needed when not talking with the activity manager
null, getApplication(), // mocked services don't talk with the activity manager
null);
assertNotNull(mService);
mServiceId = new Random().nextInt();
mServiceAttached = true;
}
Aggregations