Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
MainActivityTest.java
/* * Copyright 2017, 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.persistence.ui; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; import static android.support.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.core.IsNot.not; import android.arch.core.executor.testing.CountingTaskExecutorRule; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.Observer; import android.support.annotation.Nullable; import android.support.test.InstrumentationRegistry; import android.support.test.espresso.contrib.RecyclerViewActions; import android.support.test.espresso.matcher.ViewMatchers; import android.support.test.rule.ActivityTestRule; import com.example.android.persistence.AppExecutors; import com.example.android.persistence.EspressoTestUtil; import com.example.android.persistence.R; import com.example.android.persistence.db.AppDatabase; import org.hamcrest.CoreMatchers; import org.hamcrest.MatcherAssert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class MainActivityTest { @Rule public ActivityTestRule
mActivityRule = new ActivityTestRule<>( MainActivity.class); @Rule public CountingTaskExecutorRule mCountingTaskExecutorRule = new CountingTaskExecutorRule(); public MainActivityTest() { // delete the database InstrumentationRegistry.getTargetContext().deleteDatabase(AppDatabase.DATABASE_NAME); } @Before public void disableRecyclerViewAnimations() { // Disable RecyclerView animations EspressoTestUtil.disableAnimations(mActivityRule); } @Before public void waitForDbCreation() throws Throwable { final CountDownLatch latch = new CountDownLatch(1); final LiveData
databaseCreated = AppDatabase.getInstance( InstrumentationRegistry.getTargetContext(), new AppExecutors()) .getDatabaseCreated(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { databaseCreated.observeForever(new Observer
() { @Override public void onChanged(@Nullable Boolean aBoolean) { if (Boolean.TRUE.equals(aBoolean)) { databaseCreated.removeObserver(this); latch.countDown(); } } }); } }); MatcherAssert.assertThat("database should've initialized", latch.await(1, TimeUnit.MINUTES), CoreMatchers.is(true)); } @Test public void clickOnFirstItem_opensComments() throws Throwable { drain(); // When clicking on the first product onView(ViewMatchers.withContentDescription(R.string.cd_products_list)) .perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); drain(); // Then the second screen with the comments should appear. onView(withContentDescription(R.string.cd_comments_list)) .check(matches(isDisplayed())); drain(); // Then the second screen with the comments should appear. onView(withContentDescription(R.string.cd_product_name)) .check(matches(not(withText("")))); } private void drain() throws TimeoutException, InterruptedException { mCountingTaskExecutorRule.drainTasks(1, TimeUnit.MINUTES); } }