<commit_before>package com.google.android.apps.common.testing.ui.espresso.matcher;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;

import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitor;
import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitorRegistry;
import com.google.android.apps.common.testing.testrunner.Stage;
import com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException;
import com.google.android.apps.common.testing.ui.espresso.Root;
import com.google.common.collect.Lists;

import android.app.Activity;
import android.os.IBinder;
import android.view.View;
import android.view.WindowManager;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.Collection;
import java.util.List;

/**
 * A collection of matchers for {@link Root} objects.
 */
public final class RootMatchers {

  private RootMatchers() {}

  /**
   * Espresso's default {@link Root} matcher.
   */
  @SuppressWarnings("unchecked")
  public static final Matcher<Root> DEFAULT =
      allOf(
        hasWindowLayoutParams(),
        allOf(
              anyOf(
                  allOf(isDialog(), withDecorView(hasWindowFocus())),
                  isSubwindowOfCurrentActivity()),
              anyOf(isFocusable(), isTouchable())));

  /**
   * Matches {@link Root}s that can take window focus.
   */
  public static Matcher<Root> isFocusable() {
    return new TypeSafeMatcher<Root>() {

      @Override
      public void describeTo(Description description) {
        description.appendText("is focusable");
      }

      @Override
      public boolean matchesSafely(Root root) {
        int flags = root.getWindowLayoutParams().get().flags;
        boolean r = !((flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 1);
        return r;
      }
    };
  }

  /**
   * Matches {@link Root}s that can receive touch events.
   */
  public static Matcher<Root> isTouchable() {
    return new TypeSafeMatcher<Root>() {

      @Override
      public void describeTo(Description description) {
        description.appendText("is touchable");
      }

      @Override
      public boolean matchesSafely(Root root) {
        int flags = root.getWindowLayoutParams().get().flags;
        boolean r = !((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) == 1);
        return r;
      }
    };
  }

  /**
   * Matches {@link Root}s that are dialogs (i.e. is not a window of the currently resumed
   * activity).<commit_msg>This check was wrong and always declaring windows to be focusable and touchable.<commit_after>package com.google.android.apps.common.testing.ui.espresso.matcher;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;

import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitor;
import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitorRegistry;
import com.google.android.apps.common.testing.testrunner.Stage;
import com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException;
import com.google.android.apps.common.testing.ui.espresso.Root;
import com.google.common.collect.Lists;

import android.app.Activity;
import android.os.IBinder;
import android.view.View;
import android.view.WindowManager;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.Collection;
import java.util.List;

/**
 * A collection of matchers for {@link Root} objects.
 */
public final class RootMatchers {

  private RootMatchers() {}

  /**
   * Espresso's default {@link Root} matcher.
   */
  @SuppressWarnings("unchecked")
  public static final Matcher<Root> DEFAULT =
      allOf(
        hasWindowLayoutParams(),
        allOf(
             anyOf(
                  allOf(isDialog(), withDecorView(hasWindowFocus())),
                  isSubwindowOfCurrentActivity()),
             isFocusable()));


  /**
   * Matches {@link Root}s that can take window focus.
   */
  public static Matcher<Root> isFocusable() {
    return new TypeSafeMatcher<Root>() {

      @Override
      public void describeTo(Description description) {
        description.appendText("is focusable");
      }

      @Override
      public boolean matchesSafely(Root root) {
        int flags = root.getWindowLayoutParams().get().flags;
        boolean r = !((flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0);
        return r;
      }
    };
  }

  /**
   * Matches {@link Root}s that can receive touch events.
   */
  public static Matcher<Root> isTouchable() {
    return new TypeSafeMatcher<Root>() {

      @Override
      public void describeTo(Description description) {
        description.appendText("is touchable");
      }

      @Override
      public boolean matchesSafely(Root root) {
        int flags = root.getWindowLayoutParams().get().flags;
        boolean r = !((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0);
        return r;
      }
    };
  }

  /**
   * Matches {@link Root}s that are dialogs (i.e. is not a window of the currently resumed
   * activity).
{'fraction_non_alphanumeric': 0.11119430924747285, 'fraction_numerical': 0.0007487832272557095, 'mean_word_length': 3.3158319870759287, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '1225420', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1536, 'n_words': 340}