<commit_before>/*
 * Copyright 2016, The Android Open Source Project
 *
 * 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.example.android.architecture.blueprints.todoapp.tasks

import android.support.test.InstrumentationRegistry
import android.support.test.InstrumentationRegistry.getTargetContext
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.action.ViewActions.closeSoftKeyboard
import android.support.test.espresso.action.ViewActions.replaceText
import android.support.test.espresso.action.ViewActions.typeText
import android.support.test.espresso.assertion.ViewAssertions.doesNotExist
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.hasSibling
import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom
import android.support.test.espresso.matcher.ViewMatchers.isChecked
import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
import android.support.test.espresso.matcher.ViewMatchers.withContentDescription
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.test.espresso.matcher.ViewMatchers.withText
import android.support.test.filters.LargeTest
import android.support.test.filters.SdkSuppress
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import android.view.View
import android.widget.ListView
import com.example.android.architecture.blueprints.todoapp.Injection
import com.example.android.architecture.blueprints.todoapp.R
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource
import com.example.android.architecture.blueprints.todoapp.getCurrentActivity
import com.example.android.architecture.blueprints.todoapp.getToolbarNavigationContentDescription
import com.example.android.architecture.blueprints.todoapp.rotateOrientation
import com.google.common.base.Preconditions.checkArgument
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.core.IsNot.not
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Tests for the tasks screen, the main screen which contains a list of all tasks.
 */
@RunWith(AndroidJUnit4::class)
@LargeTest
class TasksScreenTest {

  /**
   * [ActivityTestRule] is a JUnit [Rule] to launch your activity under test.
   *
   *
   * Rules are interceptors which are executed for each test method and are important building
   * blocks of Junit tests.
   */
  @Suppress("MemberVisibilityCanPrivate")
  @get:Rule
  var tasksActivityTestRule: ActivityTestRule<TasksActivity> =
      object : ActivityTestRule<TasksActivity>(TasksActivity::class.java) {

        /**
         * To avoid a long list of tasks and the need to scroll through the list to find a
         * task, we call [TasksDataSource.deleteAllTasks] before each test.
         */
        override fun beforeActivityLaunched() {
          super.beforeActivityLaunched()
          // Doing this in @Before generates a race condition.
          Injection
              .provideTasksRepository(InstrumentationRegistry.getTargetContext())
              .deleteAllTasks()
        }
      }

  private val toolbarNavigationContentDescription: String
    get() = getToolbarNavigationContentDescription(tasksActivityTestRule.activity, R.id.toolbar)

  /**
   * A custom [Matcher] which matches an item in a [ListView] by its text.
   *
   *
   * View constraints:
   *
   *  * View must be a child of a [ListView]
   *
   *
   * @param itemText the text to match
   * @return Matcher that matches text in the given view
   */
  private fun withItemText(itemText: String): Matcher<View> {
    checkArgument(itemText.isEmpty(), "itemText cannot be null or empty")
    return object : TypeSafeMatcher<View>() {
      public override fun matchesSafely(item: View): Boolean {
        return allOf(isDescendantOfA(isAssignableFrom(ListView::class.java)), withText(itemText))
            .matches(item)
      }

      override fun describeTo(description: Description) {
        description.appendText("is isDescendantOfA LV with text " + itemText)<commit_msg>Fix helper functions logic<commit_after>/*
 * Copyright 2016, The Android Open Source Project
 *
 * 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.example.android.architecture.blueprints.todoapp.tasks

import android.support.test.InstrumentationRegistry
import android.support.test.InstrumentationRegistry.getTargetContext
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.action.ViewActions.closeSoftKeyboard
import android.support.test.espresso.action.ViewActions.replaceText
import android.support.test.espresso.action.ViewActions.typeText
import android.support.test.espresso.assertion.ViewAssertions.doesNotExist
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.hasSibling
import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom
import android.support.test.espresso.matcher.ViewMatchers.isChecked
import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
import android.support.test.espresso.matcher.ViewMatchers.withContentDescription
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.test.espresso.matcher.ViewMatchers.withText
import android.support.test.filters.LargeTest
import android.support.test.filters.SdkSuppress
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import android.view.View
import android.widget.ListView
import com.example.android.architecture.blueprints.todoapp.Injection
import com.example.android.architecture.blueprints.todoapp.R
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource
import com.example.android.architecture.blueprints.todoapp.getCurrentActivity
import com.example.android.architecture.blueprints.todoapp.getToolbarNavigationContentDescription
import com.example.android.architecture.blueprints.todoapp.rotateOrientation
import com.google.common.base.Preconditions.checkArgument
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.core.IsNot.not
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Tests for the tasks screen, the main screen which contains a list of all tasks.
 */
@RunWith(AndroidJUnit4::class)
@LargeTest
class TasksScreenTest {

  /**
   * [ActivityTestRule] is a JUnit [Rule] to launch your activity under test.
   *
   *
   * Rules are interceptors which are executed for each test method and are important building
   * blocks of Junit tests.
   */
  @Suppress("MemberVisibilityCanPrivate")
  @get:Rule
  var tasksActivityTestRule: ActivityTestRule<TasksActivity> =
      object : ActivityTestRule<TasksActivity>(TasksActivity::class.java) {

        /**
         * To avoid a long list of tasks and the need to scroll through the list to find a
         * task, we call [TasksDataSource.deleteAllTasks] before each test.
         */
        override fun beforeActivityLaunched() {
          super.beforeActivityLaunched()
          // Doing this in @Before generates a race condition.
          Injection
              .provideTasksRepository(InstrumentationRegistry.getTargetContext())
              .deleteAllTasks()
        }
      }

  private val toolbarNavigationContentDescription: String
    get() = getToolbarNavigationContentDescription(tasksActivityTestRule.activity, R.id.toolbar)

  /**
   * A custom [Matcher] which matches an item in a [ListView] by its text.
   *
   *
   * View constraints:
   *
   *  * View must be a child of a [ListView]
   *
   *
   * @param itemText the text to match
   * @return Matcher that matches text in the given view
   */
  private fun withItemText(itemText: String): Matcher<View> {
    checkArgument(itemText.isNotEmpty(), "itemText cannot be null or empty")
    return object : TypeSafeMatcher<View>() {
      public override fun matchesSafely(item: View): Boolean {
        return allOf(isDescendantOfA(isAssignableFrom(ListView::class.java)), withText(itemText))
            .matches(item)
      }

      override fun describeTo(description: Description) {
        description.appendText("is isDescendantOfA LV with text " + itemText)
{'fraction_non_alphanumeric': 0.08148148148148149, 'fraction_numerical': 0.002002002002002002, 'mean_word_length': 4.5077177508269015, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3528952', 'n_tokens_mistral': 2768, 'n_tokens_neox': 2730, 'n_words': 686}