Skip to content

Commit

Permalink
merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
rijogeorge7 committed Jun 8, 2018
2 parents 044a755 + ccee756 commit c0fee61
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 7 deletions.
9 changes: 7 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ext {
supportLibVersion = '26.1.0'
okhttpVersion = '3.9.0'
daggerVersion = '2.5'
espressoVersion = '3.0.1'
}

dependencies {
Expand All @@ -67,6 +68,7 @@ dependencies {
implementation "com.android.support:support-annotations:${supportLibVersion}"
implementation "com.android.support:palette-v7:${supportLibVersion}"
implementation "com.android.support:recyclerview-v7:${supportLibVersion}"
implementation "com.android.support.test.espresso:espresso-idling-resource:${espressoVersion}"

// Rx
implementation 'io.reactivex.rxjava2:rxjava:2.1.2'
Expand All @@ -82,6 +84,7 @@ dependencies {

// Other
implementation 'net.jcip:jcip-annotations:1.0'
implementation 'com.google.guava:guava:18.0'
implementation 'com.squareup.moshi:moshi:1.1.0'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
Expand All @@ -99,13 +102,15 @@ dependencies {
// Tests
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.9.0'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
androidTestCompile("com.android.support.test.espresso:espresso-core:${espressoVersion}", {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
androidTestCompile("com.android.support.test.espresso:espresso-contrib:${espressoVersion}") {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test:rules:1.0.1'
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.esoxjem.movieguide.listing;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.esoxjem.movieguide.R;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -21,11 +26,20 @@
* @author arunsasidharan
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MoviesListingActivityTest
{
@Rule
public final ActivityTestRule<MoviesListingActivity> activityTestRule = new ActivityTestRule<>(MoviesListingActivity.class);

private IdlingResource idlingResource;

@Before
public void registerIdlingResource() {
idlingResource = activityTestRule.getActivity().getCountingIdlingResource();
Espresso.registerIdlingResources(idlingResource);
}

@Test
public void shouldBeAbleToLaunchMainScreen()
{
Expand All @@ -34,17 +48,22 @@ public void shouldBeAbleToLaunchMainScreen()
}

@Test
public void shouldBeAbleToLoadMovies() throws InterruptedException
public void shouldBeAbleToLoadMovies()
{
Thread.sleep(3000);
onView(withId(R.id.movies_listing)).check(matches(isDisplayed()));
}

@Test
public void shouldBeAbleToScrollViewMovieDetails() throws InterruptedException
public void shouldBeAbleToScrollViewMovieDetails()
{
Thread.sleep(3000);
onView(withId(R.id.movies_listing)).perform(RecyclerViewActions.actionOnItemAtPosition(10, click()));
onView(withText("Summary")).check(matches(isDisplayed()));
}
}

@After
public void unregisterIdlingResource() {
if (idlingResource != null) {
Espresso.unregisterIdlingResources(idlingResource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.test.espresso.IdlingResource;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
Expand All @@ -15,6 +18,7 @@
import com.esoxjem.movieguide.details.MovieDetailsFragment;
import com.esoxjem.movieguide.Movie;
import com.esoxjem.movieguide.util.SoftKeyboardUtils;

import com.jakewharton.rxbinding2.support.v7.widget.RxSearchView;

import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -115,4 +119,10 @@ private void loadMovieFragment(Movie movie) {
.replace(R.id.movie_details_container, movieDetailsFragment, DETAILS_FRAGMENT)
.commit();
}

@VisibleForTesting
@NonNull
public IdlingResource getCountingIdlingResource() {
return EspressoIdlingResource.getIdlingResource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.support.annotation.NonNull;

import com.esoxjem.movieguide.Movie;
import com.esoxjem.movieguide.util.EspressoIdlingResource;
import com.esoxjem.movieguide.util.RxUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -43,6 +44,7 @@ public void destroy() {
}

private void displayMovies() {
EspressoIdlingResource.increment();
showLoading();
fetchSubscription = moviesInteractor.fetchMovies(currentPage)
.subscribeOn(Schedulers.io())
Expand Down Expand Up @@ -102,6 +104,7 @@ private void showLoading() {
}

private void onMovieFetchSuccess(List<Movie> movies) {
EspressoIdlingResource.decrement();
if (moviesInteractor.isPaginationSupported()) {
loadedMovies.addAll(movies);
} else {
Expand All @@ -113,6 +116,7 @@ private void onMovieFetchSuccess(List<Movie> movies) {
}

private void onMovieFetchFailed(Throwable e) {
EspressoIdlingResource.decrement();
view.loadingFailed(e.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2015, 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.esoxjem.movieguide.util;

import android.support.test.espresso.IdlingResource;

/**
* Contains a static reference to {@link IdlingResource}, only available in the 'mock' build type.
*/
public class EspressoIdlingResource {

private static final String RESOURCE = "GLOBAL";

private static SimpleCountingIdlingResource mCountingIdlingResource =
new SimpleCountingIdlingResource(RESOURCE);

public static void increment() {
mCountingIdlingResource.increment();
}

public static void decrement() {
mCountingIdlingResource.decrement();
}

public static IdlingResource getIdlingResource() {
return mCountingIdlingResource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2015 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.esoxjem.movieguide.util;

import android.support.test.espresso.IdlingResource;

import java.util.concurrent.atomic.AtomicInteger;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* A simple counter implementation of {@link IdlingResource} that determines idleness by
* maintaining an internal counter. When the counter is 0 - it is considered to be idle, when it is
* non-zero it is not idle. This is very similar to the way a {@link java.util.concurrent.Semaphore}
* behaves.
* <p>
* This class can then be used to wrap up operations that while in progress should block tests from
* accessing the UI.
*/
public final class SimpleCountingIdlingResource implements IdlingResource {

private final String mResourceName;

private final AtomicInteger counter = new AtomicInteger(0);

// written from main thread, read from any thread.
private volatile ResourceCallback resourceCallback;

/**
* Creates a SimpleCountingIdlingResource
*
* @param resourceName the resource name this resource should report to Espresso.
*/
public SimpleCountingIdlingResource(String resourceName) {
mResourceName = checkNotNull(resourceName);
}

@Override
public String getName() {
return mResourceName;
}

@Override
public boolean isIdleNow() {
return counter.get() == 0;
}

@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}

/**
* Increments the count of in-flight transactions to the resource being monitored.
*/
public void increment() {
counter.getAndIncrement();
}

/**
* Decrements the count of in-flight transactions to the resource being monitored.
*
* If this operation results in the counter falling below 0 - an exception is raised.
*
* @throws IllegalStateException if the counter is below 0.
*/
public void decrement() {
int counterVal = counter.decrementAndGet();
if (counterVal == 0) {
// we've gone from non-zero to zero. That means we're idle now! Tell espresso.
if (null != resourceCallback) {
resourceCallback.onTransitionToIdle();
}
}

if (counterVal < 0) {
throw new IllegalArgumentException("Counter has been corrupted!");
}
}
}

0 comments on commit c0fee61

Please sign in to comment.