Search in sources :

Example 51 with Application

use of android.app.Application in project mosby by sockeqwe.

the class ViewGroupMvpViewStateDelegateImplTest method initComponents.

@Before
public void initComponents() {
    view = new MvpView() {
    };
    savedState = null;
    viewState = Mockito.mock(ViewState.class);
    presenter = Mockito.mock(MvpPresenter.class);
    callback = Mockito.mock(PartialViewGroupMvpViewStateDelegateCallbackImpl.class);
    Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
    Mockito.doCallRealMethod().when(callback).getPresenter();
    Mockito.doCallRealMethod().when(callback).setViewState(viewState);
    Mockito.doCallRealMethod().when(callback).getViewState();
    Mockito.doCallRealMethod().when(callback).superOnSaveInstanceState();
    activity = Mockito.mock(FragmentActivity.class);
    application = Mockito.mock(Application.class);
    androidView = Mockito.mock(View.class);
    Mockito.when(callback.getMvpView()).thenReturn(view);
    Mockito.when(callback.getContext()).thenReturn(activity);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Mockito.when(callback.createPresenter()).thenReturn(presenter);
    Mockito.when(callback.createViewState()).thenReturn(viewState);
    Mockito.when(androidView.isInEditMode()).thenReturn(false);
    delegate = new ViewGroupMvpViewStateDelegateImpl<>(androidView, callback, true);
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) ViewState(com.hannesdorfmann.mosby3.mvp.viewstate.ViewState) Application(android.app.Application) View(android.view.View) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) Before(org.junit.Before)

Example 52 with Application

use of android.app.Application in project mosby by sockeqwe.

the class ViewGroupMvpDelegateImplTest method initComponents.

@Before
public void initComponents() {
    view = new MvpView() {
    };
    savedState = null;
    presenter = Mockito.mock(MvpPresenter.class);
    callback = Mockito.mock(PartialViewGroupMvpDelegateCallbackImpl.class);
    Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
    Mockito.doCallRealMethod().when(callback).getPresenter();
    Mockito.doCallRealMethod().when(callback).superOnSaveInstanceState();
    activity = Mockito.mock(FragmentActivity.class);
    application = Mockito.mock(Application.class);
    androidView = Mockito.mock(View.class);
    Mockito.when(callback.getMvpView()).thenReturn(view);
    Mockito.when(callback.getContext()).thenReturn(activity);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Mockito.when(androidView.isInEditMode()).thenReturn(false);
    delegate = new ViewGroupMvpDelegateImpl<>(androidView, callback, true);
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) Application(android.app.Application) View(android.view.View) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) Before(org.junit.Before)

Example 53 with Application

use of android.app.Application in project Shuttle by timusus.

the class InputMethodManagerLeaks method fixFocusedViewLeak.

/**
 * Fix for https://code.google.com/p/android/issues/detail?id=171190 .
 * <p>
 * When a view that has focus gets detached, we wait for the main thread to be idle and then
 * check if the InputMethodManager is leaking a view. If yes, we tell it that the decor view got
 * focus, which is what happens if you press home and come back from recent apps. This replaces
 * the reference to the detached view with a reference to the decor view.
 * <p>
 * Should be called from {@link Activity#onCreate(android.os.Bundle)} )}.
 */
@SuppressLint("PrivateApi")
public static void fixFocusedViewLeak(Application application) {
    // Still not fixed until android 23
    if (SDK_INT < KITKAT || SDK_INT > Build.VERSION_CODES.N_MR1) {
        return;
    }
    final InputMethodManager inputMethodManager = (InputMethodManager) application.getSystemService(INPUT_METHOD_SERVICE);
    final Field mServedViewField;
    final Field mHField;
    final Method finishInputLockedMethod;
    final Method focusInMethod;
    try {
        mServedViewField = InputMethodManager.class.getDeclaredField("mServedView");
        mServedViewField.setAccessible(true);
        mHField = InputMethodManager.class.getDeclaredField("mServedView");
        mHField.setAccessible(true);
        finishInputLockedMethod = InputMethodManager.class.getDeclaredMethod("finishInputLocked");
        finishInputLockedMethod.setAccessible(true);
        focusInMethod = InputMethodManager.class.getDeclaredMethod("focusIn", View.class);
        focusInMethod.setAccessible(true);
    } catch (NoSuchMethodException | NoSuchFieldException unexpected) {
        Log.e("IMMLeaks", "Unexpected reflection exception", unexpected);
        return;
    }
    application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        }

        @Override
        public void onActivityStarted(Activity activity) {
            ReferenceCleaner cleaner = new ReferenceCleaner(inputMethodManager, mHField, mServedViewField, finishInputLockedMethod);
            View rootView = activity.getWindow().getDecorView().getRootView();
            ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();
            viewTreeObserver.addOnGlobalFocusChangeListener(cleaner);
        }

        @Override
        public void onActivityResumed(Activity activity) {
        }

        @Override
        public void onActivityPaused(Activity activity) {
        }

        @Override
        public void onActivityStopped(Activity activity) {
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
        }
    });
}
Also used : Bundle(android.os.Bundle) Activity(android.app.Activity) InputMethodManager(android.view.inputmethod.InputMethodManager) Method(java.lang.reflect.Method) View(android.view.View) Field(java.lang.reflect.Field) Application(android.app.Application) ViewTreeObserver(android.view.ViewTreeObserver) SuppressLint(android.annotation.SuppressLint)

Example 54 with Application

use of android.app.Application in project android_frameworks_base by crdroidandroid.

the class FalsingLog method wtf.

public static synchronized void wtf(String tag, String s, Throwable here) {
    if (!ENABLED) {
        return;
    }
    e(tag, s);
    Application application = ActivityThread.currentApplication();
    String fileMessage = "";
    if (Build.IS_DEBUGGABLE && application != null) {
        File f = new File(application.getDataDir(), "falsing-" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt");
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(f);
            dump(pw);
            pw.close();
            fileMessage = "Log written to " + f.getAbsolutePath();
        } catch (IOException e) {
            Log.e(TAG, "Unable to write falsing log", e);
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
    } else {
        Log.e(TAG, "Unable to write log, build must be debuggable.");
    }
    Log.wtf(TAG, tag + " " + s + "; " + fileMessage, here);
}
Also used : IOException(java.io.IOException) Application(android.app.Application) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 55 with Application

use of android.app.Application in project android-testing-templates by googlesamples.

the class LocalUnitTest method mockFinalMethod.

@Test
public void mockFinalMethod() {
    Activity activity = mock(Activity.class);
    Application app = mock(Application.class);
    when(activity.getApplication()).thenReturn(app);
    assertThat(app, is(equalTo(activity.getApplication())));
    verify(activity).getApplication();
    verifyNoMoreInteractions(activity);
}
Also used : Activity(android.app.Activity) Application(android.app.Application) Test(org.junit.Test)

Aggregations

Application (android.app.Application)125 Test (org.junit.Test)27 Context (android.content.Context)24 Activity (android.app.Activity)23 ApplicationInfo (android.content.pm.ApplicationInfo)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 File (java.io.File)10 MvpPresenter (com.hannesdorfmann.mosby3.mvp.MvpPresenter)8 Before (org.junit.Before)8 ShadowApplication (org.robolectric.shadows.ShadowApplication)8 MvpView (com.hannesdorfmann.mosby3.mvp.MvpView)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)6 PrintWriter (java.io.PrintWriter)6 Date (java.util.Date)6 SuppressLint (android.annotation.SuppressLint)5 PackageInfo (android.content.pm.PackageInfo)5 PackageManager (android.content.pm.PackageManager)5 RemoteException (android.os.RemoteException)5 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)5 AndroidRuntimeException (android.util.AndroidRuntimeException)5