<commit_before>package de.nenick.espressomacchiato.testbase;

import android.app.Activity;
import android.os.Build;
import android.support.annotation.IdRes;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import de.nenick.espressomacchiato.elements.EspApplicationIsNotRespondingDialog;

@RunWith(AndroidJUnit4.class)
abstract class EspressoTestBase<A extends Activity> {

    @Rule
    public ExpectedException exception = ExpectedException.none();

    public abstract A getActivity();

    /*
    private Activity currentActivity = null;
    public static Activity getCurrentActivity() {
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
                if (resumedActivities.iterator().hasNext()) {
                    currentActivity = (Activity) resumedActivities.iterator().next();
                }
            }
        });

        return currentActivity;
    }
    */

    @Before
    public void setupEspresso() {
        EspApplicationIsNotRespondingDialog.build().dismissIfShown();
        Espresso.setFailureHandler(new EspScreenshotFailureHandler(getActivity()));
        avoidLockScreen();

        // "java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong. Selected Root:
        // Root{application-window-token=android.view.ViewRootImpl$W@b2047918, window-token=<PASSWORD>.view.<PASSWORD>, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#100 ty=1 fl=#1810100 pfl=0x8 wanim=0x10302a1}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=480, height=800, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}"
        // https://groups.google.com/forum/#!msg/android-test-kit-discuss/yIEwus_hjeY/xI1qpehDa7MJ -> looks like there's a dialog that says "Launcher isn't responding. Do you want to close it?" with options Wait and OK
    }

    @After
    public void cleanupEspresso() throws Exception {
        EspCloseAllActivitiesFunction.apply(InstrumentationRegistry.getInstrumentation());
    }

    protected void addViewToActivity(final View view, @IdRes final int targetLayoutId) {
        performOnUiThread(new Runnable() {
            @Override
            public void run() {
                ViewGroup layout = (ViewGroup) getActivity().findViewById(targetLayoutId);
                layout.addView(view);
            }
        });
    }

    protected void performOnUiThread(Runnable runnable) {
        InstrumentationRegistry.getInstrumentation().runOnMainSync(runnable);
    }

    private void avoidLockScreen() {
        // sometimes tests failed on emulator because lock screen is shown<commit_msg>only close initial anr dialog<commit_after>package de.nenick.espressomacchiato.testbase;

import android.app.Activity;
import android.os.Build;
import android.support.annotation.IdRes;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import de.nenick.espressomacchiato.elements.EspApplicationIsNotRespondingDialog;

@RunWith(AndroidJUnit4.class)
abstract class EspressoTestBase<A extends Activity> {

    @Rule
    public ExpectedException exception = ExpectedException.none();

    private static boolean initialAnrDialogClosed = false;

    public abstract A getActivity();

    /*
    private Activity currentActivity = null;
    public static Activity getCurrentActivity() {
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
                if (resumedActivities.iterator().hasNext()) {
                    currentActivity = (Activity) resumedActivities.iterator().next();
                }
            }
        });

        return currentActivity;
    }
    */

    @Before
    public void setupEspresso() {
        if(!initialAnrDialogClosed) {
            EspApplicationIsNotRespondingDialog.build().dismissIfShown();
            initialAnrDialogClosed = true;
        }
        Espresso.setFailureHandler(new EspScreenshotFailureHandler(getActivity()));
        avoidLockScreen();

        // "java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong. Selected Root:
        // Root{application-window-token=android.view.ViewRootImpl$W@b2047918, window-token=android.view.ViewRootImpl$W@b2047918, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#100 ty=1 fl=#1810100 pfl=0x8 wanim=0x10302a1}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=480, height=800, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}"
        // https://groups.google.com/forum/#!msg/android-test-kit-discuss/yIEwus_hjeY/xI1qpehDa7MJ -> looks like there's a dialog that says "Launcher isn't responding. Do you want to close it?" with options Wait and OK
    }

    @After
    public void cleanupEspresso() throws Exception {
        EspCloseAllActivitiesFunction.apply(InstrumentationRegistry.getInstrumentation());
    }

    protected void addViewToActivity(final View view, @IdRes final int targetLayoutId) {
        performOnUiThread(new Runnable() {
            @Override
            public void run() {
                ViewGroup layout = (ViewGroup) getActivity().findViewById(targetLayoutId);
                layout.addView(view);
            }
        });
    }

    protected void performOnUiThread(Runnable runnable) {
        InstrumentationRegistry.getInstrumentation().runOnMainSync(runnable);
    }

    private void avoidLockScreen() {
        // sometimes tests failed on emulator because lock screen is shown
{'fraction_non_alphanumeric': 0.0997233566065077, 'fraction_numerical': 0.013568699776050587, 'mean_word_length': 3.8111533586818758, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5538283', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2120, 'n_words': 467}