├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── vgrec │ │ └── espressoexamples │ │ ├── ActionBarExampleTest.java │ │ ├── CustomListTest.java │ │ ├── DateTimePickerTest.java │ │ ├── DialogTests.java │ │ ├── EnterNameTest.java │ │ ├── RecyclerViewTest.java │ │ ├── SearchViewTest.java │ │ ├── SpinnerSelectionTest.java │ │ ├── ViewPagerTest.java │ │ └── matchers │ │ └── CustomMatchers.java │ └── main │ ├── AndroidManifest.xml │ ├── ic_about-web.png │ ├── ic_settings-web.png │ ├── java │ └── com │ │ └── vgrec │ │ └── espressoexamples │ │ ├── Database.java │ │ ├── activities │ │ ├── ActionBarExampleActivity.java │ │ ├── BookDetailsActivity.java │ │ ├── CustomListActivity.java │ │ ├── DateTimePickerActivity.java │ │ ├── DialogExampleActivity.java │ │ ├── DisplayNameActivity.java │ │ ├── EnterNameActivity.java │ │ ├── MainActivity.java │ │ ├── RecyclerViewActivity.java │ │ ├── SearchViewActivity.java │ │ ├── SearchableActivity.java │ │ ├── SpinnerSelectionActivity.java │ │ └── ViewPagerActivity.java │ │ ├── adapter │ │ ├── BooksAdapter.java │ │ └── RecyclerBooksAdapter.java │ │ ├── models │ │ └── Book.java │ │ └── provider │ │ └── AppRecentSearchesProvider.java │ └── res │ ├── drawable-xhdpi │ ├── book.png │ └── ic_action_search.png │ ├── layout │ ├── activity_action_bar_example.xml │ ├── activity_book_details.xml │ ├── activity_custom_list.xml │ ├── activity_date_time_picker.xml │ ├── activity_dialog_example.xml │ ├── activity_display_name.xml │ ├── activity_enter_name.xml │ ├── activity_main.xml │ ├── activity_recycler_view.xml │ ├── activity_search_view.xml │ ├── activity_searchable.xml │ ├── activity_spinner_selection.xml │ ├── activity_view_pager.xml │ ├── fragment_books.xml │ └── item_book.xml │ ├── menu │ ├── context_menu.xml │ ├── menu_action_bar_example.xml │ └── menu_search_view.xml │ ├── mipmap-hdpi │ ├── ic_about.png │ ├── ic_launcher.png │ └── ic_settings.png │ ├── mipmap-mdpi │ ├── ic_about.png │ ├── ic_launcher.png │ └── ic_settings.png │ ├── mipmap-xhdpi │ ├── ic_about.png │ ├── ic_launcher.png │ └── ic_settings.png │ ├── mipmap-xxhdpi │ ├── ic_about.png │ ├── ic_launcher.png │ └── ic_settings.png │ ├── mipmap-xxxhdpi │ ├── ic_about.png │ └── ic_settings.png │ ├── values-w820dp │ └── dimens.xml │ ├── values │ ├── arrays.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml │ └── xml │ └── searchable.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screens └── main_activity.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | .idea 3 | *.iml 4 | /app/*.iml 5 | /local.properties 6 | /.idea/workspace.xml 7 | /.idea/libraries 8 | .DS_Store 9 | /build 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EspressoExamples 2 | A collection of examples demonstrating different techniques for automated testing with Espresso. 3 | 4 | ![alt tag](screens/main_activity.png) 5 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 21 5 | buildToolsVersion "21.1.2" 6 | 7 | defaultConfig { 8 | applicationId "com.vgrec.espressoexamples" 9 | minSdkVersion 15 10 | targetSdkVersion 21 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | compile 'com.android.support:appcompat-v7:22.1.1' 26 | compile 'com.jpardogo.materialtabstrip:library:1.0.9' 27 | compile 'com.android.support:recyclerview-v7:21.0.3' 28 | 29 | // Testing dependencies 30 | androidTestCompile 'com.android.support.test:testing-support-lib:0.1' 31 | androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0' 32 | androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') { 33 | exclude group: 'com.android.support', module: 'appcompat' 34 | exclude group: 'com.android.support', module: 'support-v4' 35 | exclude module: 'recyclerview-v7' 36 | } 37 | } 38 | 39 | android { 40 | packagingOptions { 41 | exclude 'LICENSE.txt' 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/vgrec/Android/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/ActionBarExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.test.ActivityInstrumentationTestCase2; 4 | 5 | import com.vgrec.espressoexamples.activities.ActionBarExampleActivity; 6 | 7 | import static android.support.test.espresso.Espresso.onView; 8 | import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; 9 | import static android.support.test.espresso.action.ViewActions.click; 10 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 11 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 12 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 13 | 14 | /** 15 | * @author vgrec, created on 3/20/15. 16 | */ 17 | public class ActionBarExampleTest extends ActivityInstrumentationTestCase2 { 18 | 19 | 20 | public ActionBarExampleTest() { 21 | super(ActionBarExampleActivity.class); 22 | } 23 | 24 | @Override 25 | protected void setUp() throws Exception { 26 | super.setUp(); 27 | getActivity(); 28 | } 29 | 30 | public void testClickOnMenuItem() { 31 | // Click on an item from ActionBar 32 | onView(withId(R.id.action_settings)).perform(click()); 33 | 34 | // Verify the correct item was clicked by checking the content of the status TextView 35 | onView(withId(R.id.status)).check(matches(withText("Settings"))); 36 | } 37 | 38 | public void testOverflowMenuOrOptionsMenu() { 39 | // Open the action bar overflow or options menu (depending if the device has or not a hardware menu button.) 40 | openActionBarOverflowOrOptionsMenu(getInstrumentation().getContext()); 41 | 42 | // Find the menu item with text "About" and click on it 43 | onView(withText("About")).perform(click()); 44 | 45 | // Verify the correct item was clicked by checking the content of the status TextView 46 | onView(withId(R.id.status)).check(matches(withText("About"))); 47 | } 48 | 49 | public void testActionMode() { 50 | // Show the contextual ActionBar 51 | onView(withId(R.id.toggle_action_mode)).perform(click()); 52 | 53 | // Click on a context item 54 | onView(withId(R.id.action_one)).perform(click()); 55 | 56 | // Verify the correct item was clicked by checking the content of the status TextView 57 | onView(withId(R.id.status)).check(matches(withText("ActionMode1"))); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/CustomListTest.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.test.ActivityInstrumentationTestCase2; 4 | 5 | import com.vgrec.espressoexamples.activities.CustomListActivity; 6 | 7 | import static android.support.test.espresso.Espresso.onData; 8 | import static android.support.test.espresso.Espresso.onView; 9 | import static android.support.test.espresso.action.ViewActions.click; 10 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 11 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 12 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 13 | import static com.vgrec.espressoexamples.matchers.CustomMatchers.withBookAuthor; 14 | import static com.vgrec.espressoexamples.matchers.CustomMatchers.withBookId; 15 | import static com.vgrec.espressoexamples.matchers.CustomMatchers.withBookTitle; 16 | import static org.hamcrest.CoreMatchers.allOf; 17 | import static org.hamcrest.CoreMatchers.anything; 18 | 19 | 20 | /** 21 | * @author vgrec, created on 3/18/15. 22 | */ 23 | public class CustomListTest extends ActivityInstrumentationTestCase2 { 24 | 25 | private static final String BOOK_TITLE = "Java Concurrency in Practice"; 26 | private static final String BOOK_AUTHOR = "Brian Goetz"; 27 | 28 | public CustomListTest() { 29 | super(CustomListActivity.class); 30 | } 31 | 32 | @Override 33 | protected void setUp() throws Exception { 34 | super.setUp(); 35 | getActivity(); 36 | } 37 | 38 | public void testOpenBookById() { 39 | // Click on the Book with ID 5 40 | onData(withBookId(5)).perform(click()); 41 | 42 | // Check the correct book title is displayed 43 | onView(withId(R.id.book_title)).check(matches(withText(BOOK_TITLE))); 44 | 45 | // Check the correct author is displayed 46 | onView(withId(R.id.book_author)).check(matches(withText(BOOK_AUTHOR))); 47 | } 48 | 49 | public void testOpenBookByTitleAndAuthor() { 50 | // Match a book with a specific title and author name 51 | onData(allOf(withBookTitle(BOOK_TITLE), withBookAuthor(BOOK_AUTHOR))).perform(click()); 52 | 53 | // Check the correct book title is displayed 54 | onView(withId(R.id.book_title)).check(matches(withText(BOOK_TITLE))); 55 | 56 | // Check the correct author is displayed 57 | onView(withId(R.id.book_author)).check(matches(withText(BOOK_AUTHOR))); 58 | } 59 | 60 | public void testClickOnBookByPosition(){ 61 | onData(anything()).atPosition(5).perform(click()); 62 | onView(withId(R.id.book_title)).check(matches(withText(BOOK_TITLE))); 63 | onView(withId(R.id.book_author)).check(matches(withText(BOOK_AUTHOR))); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/DateTimePickerTest.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.support.test.espresso.contrib.PickerActions; 4 | import android.test.ActivityInstrumentationTestCase2; 5 | import android.widget.DatePicker; 6 | import android.widget.TimePicker; 7 | 8 | import com.vgrec.espressoexamples.activities.DateTimePickerActivity; 9 | 10 | import org.hamcrest.Matchers; 11 | 12 | import static android.support.test.espresso.Espresso.onView; 13 | import static android.support.test.espresso.action.ViewActions.click; 14 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 15 | import static android.support.test.espresso.matcher.ViewMatchers.withClassName; 16 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 17 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 18 | 19 | /** 20 | * @author vgrec, created on 4/2/15. 21 | */ 22 | public class DateTimePickerTest extends ActivityInstrumentationTestCase2 { 23 | public DateTimePickerTest() { 24 | super(DateTimePickerActivity.class); 25 | } 26 | 27 | @Override 28 | protected void setUp() throws Exception { 29 | super.setUp(); 30 | getActivity(); 31 | } 32 | 33 | public void testSetDate() { 34 | int year = 2020; 35 | int month = 11; 36 | int day = 15; 37 | 38 | onView(withId(R.id.date_picker_button)).perform(click()); 39 | onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, month + 1, day)); 40 | onView(withId(android.R.id.button1)).perform(click()); 41 | 42 | onView(withId(R.id.status)).check(matches(withText(year + "/" + month + "/" + day))); 43 | } 44 | 45 | public void testSetTime() { 46 | int hour = 10; 47 | int minutes = 59; 48 | 49 | onView(withId(R.id.time_picker_button)).perform(click()); 50 | onView(withClassName(Matchers.equalTo(TimePicker.class.getName()))).perform(PickerActions.setTime(hour, minutes)); 51 | onView(withId(android.R.id.button1)).perform(click()); 52 | 53 | onView(withId(R.id.status)).check(matches(withText(hour + ":" + minutes))); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/DialogTests.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.test.ActivityInstrumentationTestCase2; 4 | 5 | import com.vgrec.espressoexamples.activities.DialogExampleActivity; 6 | 7 | import static android.support.test.espresso.Espresso.onView; 8 | import static android.support.test.espresso.action.ViewActions.click; 9 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 10 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 11 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 12 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 13 | 14 | /** 15 | * @author vgrec, created on 3/24/15. 16 | */ 17 | public class DialogTests extends ActivityInstrumentationTestCase2 { 18 | 19 | public DialogTests() { 20 | super(DialogExampleActivity.class); 21 | } 22 | 23 | @Override 24 | protected void setUp() throws Exception { 25 | super.setUp(); 26 | getActivity(); 27 | } 28 | 29 | public void testCheckDialogDisplayed() { 30 | // Click on the button that shows the dialog 31 | onView(withId(R.id.confirm_dialog_button)).perform(click()); 32 | 33 | // Check the dialog title text is displayed 34 | onView(withText(R.string.dialog_title)).check(matches(isDisplayed())); 35 | } 36 | 37 | public void testClickOkButton() { 38 | onView(withId(R.id.confirm_dialog_button)).perform(click()); 39 | 40 | // android.R.id.button1 = positive button 41 | onView(withId(android.R.id.button1)).perform(click()); 42 | 43 | onView(withId(R.id.status_text)).check(matches(withText(R.string.ok))); 44 | } 45 | 46 | public void testClickCancelButton() { 47 | onView(withId(R.id.confirm_dialog_button)).perform(click()); 48 | 49 | // android.R.id.button2 = negative button 50 | onView(withId(android.R.id.button2)).perform(click()); 51 | 52 | onView(withId(R.id.status_text)).check(matches(withText(R.string.cancel))); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/EnterNameTest.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.test.ActivityInstrumentationTestCase2; 4 | 5 | import com.vgrec.espressoexamples.activities.EnterNameActivity; 6 | 7 | import static android.support.test.espresso.Espresso.onView; 8 | import static android.support.test.espresso.action.ViewActions.click; 9 | import static android.support.test.espresso.action.ViewActions.typeText; 10 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 11 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 12 | import static android.support.test.espresso.matcher.ViewMatchers.withHint; 13 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 14 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 15 | import static org.hamcrest.CoreMatchers.not; 16 | 17 | /** 18 | * @author vgrec, created on 3/17/15. 19 | */ 20 | public class EnterNameTest extends ActivityInstrumentationTestCase2 { 21 | 22 | public static final String USER_NAME = "John"; 23 | public static final String GREETING_MESSAGE = "Hello " + USER_NAME + "!"; 24 | 25 | public EnterNameTest() { 26 | super(EnterNameActivity.class); 27 | } 28 | 29 | @Override 30 | protected void setUp() throws Exception { 31 | super.setUp(); 32 | getActivity(); 33 | } 34 | 35 | public void testHintDisplayed() { 36 | onView(withId(R.id.name_edittext)).check(matches(withHint(R.string.enter_name))); 37 | } 38 | 39 | public void testErrorMessageDisplayed() { 40 | // Making sure the error message is not displayed by default 41 | onView(withId(R.id.error_text)).check(matches(not(isDisplayed()))); 42 | 43 | // Click on "Next" button 44 | onView(withId(R.id.next_button)).perform(click()); 45 | 46 | // Now check the error message is displayed 47 | onView(withId(R.id.error_text)).check(matches(isDisplayed())); 48 | } 49 | 50 | public void testGreetingMessageWithNameDisplayed() { 51 | onView(withId(R.id.name_edittext)).perform(typeText(USER_NAME)); 52 | onView(withId(R.id.next_button)).perform(click()); 53 | onView(withId(R.id.greeting_message)).check(matches(withText(GREETING_MESSAGE))); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/RecyclerViewTest.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.support.test.espresso.contrib.RecyclerViewActions; 4 | import android.test.ActivityInstrumentationTestCase2; 5 | 6 | import com.vgrec.espressoexamples.activities.RecyclerViewActivity; 7 | 8 | import static android.support.test.espresso.Espresso.onView; 9 | import static android.support.test.espresso.action.ViewActions.click; 10 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 11 | import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; 12 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 13 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 14 | 15 | /** 16 | * @author vgrec, created on 3/30/15. 17 | */ 18 | public class RecyclerViewTest extends ActivityInstrumentationTestCase2 { 19 | 20 | private static final String BOOK_TITLE = "Clean Code"; 21 | private static final String BOOK_AUTHOR = "Robert C. Martin"; 22 | 23 | public RecyclerViewTest() { 24 | super(RecyclerViewActivity.class); 25 | } 26 | 27 | @Override 28 | protected void setUp() throws Exception { 29 | super.setUp(); 30 | getActivity(); 31 | } 32 | 33 | public void testClickAtPosition() { 34 | // Perform a click on first element in the RecyclerView 35 | onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); 36 | 37 | onView(withId(R.id.book_title)).check(matches(withText(BOOK_TITLE))); 38 | onView(withId(R.id.book_author)).check(matches(withText(BOOK_AUTHOR))); 39 | } 40 | 41 | public void testClickOnViewInRow() { 42 | // Perform click on an element with a specific text 43 | onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.actionOnItem( 44 | hasDescendant(withText(BOOK_TITLE)), click())); 45 | 46 | onView(withId(R.id.book_title)).check(matches(withText(BOOK_TITLE))); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/SearchViewTest.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.test.ActivityInstrumentationTestCase2; 4 | import android.widget.EditText; 5 | 6 | import com.vgrec.espressoexamples.activities.SearchViewActivity; 7 | 8 | import org.hamcrest.Matchers; 9 | 10 | import static android.support.test.espresso.Espresso.onData; 11 | import static android.support.test.espresso.Espresso.onView; 12 | import static android.support.test.espresso.Espresso.pressBack; 13 | import static android.support.test.espresso.action.ViewActions.clearText; 14 | import static android.support.test.espresso.action.ViewActions.click; 15 | import static android.support.test.espresso.action.ViewActions.pressImeActionButton; 16 | import static android.support.test.espresso.action.ViewActions.typeText; 17 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 18 | import static android.support.test.espresso.matcher.RootMatchers.withDecorView; 19 | import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 20 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 21 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 22 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 23 | import static com.vgrec.espressoexamples.matchers.CustomMatchers.withItemContent; 24 | import static org.hamcrest.CoreMatchers.is; 25 | import static org.hamcrest.Matchers.allOf; 26 | import static org.hamcrest.Matchers.instanceOf; 27 | import static org.hamcrest.Matchers.not; 28 | 29 | /** 30 | * @author vgrec, created on 3/19/15. 31 | */ 32 | public class SearchViewTest extends ActivityInstrumentationTestCase2 { 33 | 34 | public static final String HELSINKI = "Helsinki"; 35 | 36 | public SearchViewTest() { 37 | super(SearchViewActivity.class); 38 | } 39 | 40 | @Override 41 | protected void setUp() throws Exception { 42 | super.setUp(); 43 | getActivity(); 44 | } 45 | 46 | public void testItemNotFound() { 47 | // Click on the search icon 48 | onView(withId(R.id.action_search)).perform(click()); 49 | 50 | // Type the text in the search field and submit the query 51 | onView(isAssignableFrom(EditText.class)).perform(typeText("No such item"), pressImeActionButton()); 52 | 53 | // Check the empty view is displayed 54 | onView(withId(R.id.empty_view)).check(matches(isDisplayed())); 55 | } 56 | 57 | public void testItemFound() { 58 | onView(withId(R.id.action_search)).perform(click()); 59 | onView(isAssignableFrom(EditText.class)).perform(typeText(HELSINKI), pressImeActionButton()); 60 | 61 | // Check empty view is not displayed 62 | onView(withId(R.id.empty_view)).check(matches(not(isDisplayed()))); 63 | 64 | // Check the item we are looking for is in the search result list. 65 | onData(allOf(is(instanceOf(String.class)), withItemContent(HELSINKI))).check(matches(isDisplayed())); 66 | } 67 | 68 | public void testSearchSuggestionDisplayed() { 69 | onView(withId(R.id.action_search)).perform(click()); 70 | onView(isAssignableFrom(EditText.class)).perform(typeText(HELSINKI), pressImeActionButton()); 71 | 72 | // Go back to previous screen 73 | pressBack(); 74 | 75 | // Clear the text in search field 76 | onView(isAssignableFrom(EditText.class)).perform(clearText()); 77 | 78 | // Enter the first letter of the previously searched word 79 | onView(isAssignableFrom(EditText.class)).perform(typeText("He")); 80 | 81 | // Check the search suggestions appear 82 | onView(withText(HELSINKI)) 83 | .inRoot(withDecorView(not(Matchers.is(getActivity().getWindow().getDecorView())))) 84 | .check(matches(isDisplayed())); 85 | } 86 | 87 | public void testClickOnSearchSuggestion() { 88 | onView(withId(R.id.action_search)).perform(click()); 89 | onView(isAssignableFrom(EditText.class)).perform(typeText(HELSINKI), pressImeActionButton()); 90 | 91 | // Go back to previous screen 92 | pressBack(); 93 | 94 | // Clear the text in search field 95 | onView(isAssignableFrom(EditText.class)).perform(clearText()); 96 | 97 | // Enter the first letter of the previously searched word 98 | onView(isAssignableFrom(EditText.class)).perform(typeText("He")); 99 | 100 | 101 | // Click on the "Java" item from the suggestions list 102 | onView(withText(HELSINKI)) 103 | .inRoot(withDecorView(not(Matchers.is(getActivity().getWindow().getDecorView())))) 104 | .perform(click()); 105 | 106 | // Check the item appears in search results list. 107 | onData(allOf(is(instanceOf(String.class)), withItemContent(HELSINKI))).check(matches(isDisplayed())); 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/SpinnerSelectionTest.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.test.ActivityInstrumentationTestCase2; 4 | 5 | import com.vgrec.espressoexamples.activities.SpinnerSelectionActivity; 6 | 7 | import static android.support.test.espresso.Espresso.onData; 8 | import static android.support.test.espresso.Espresso.onView; 9 | import static android.support.test.espresso.action.ViewActions.click; 10 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 11 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 12 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 13 | import static com.vgrec.espressoexamples.matchers.CustomMatchers.withAdaptedData; 14 | import static com.vgrec.espressoexamples.matchers.CustomMatchers.withItemContent; 15 | import static org.hamcrest.CoreMatchers.is; 16 | import static org.hamcrest.Matchers.allOf; 17 | import static org.hamcrest.Matchers.instanceOf; 18 | import static org.hamcrest.Matchers.not; 19 | 20 | 21 | /** 22 | * @author vgrec, created on 3/17/15. 23 | */ 24 | public class SpinnerSelectionTest extends ActivityInstrumentationTestCase2 { 25 | 26 | public static final String INVALID_COUNTRY_NAME = "NoSuchCountry"; 27 | public static final String VALID_COUNTRY_NAME = "Moldova"; 28 | public static final String FIRST_ITEM_TEXT = "Select your country"; 29 | 30 | public SpinnerSelectionTest() { 31 | super(SpinnerSelectionActivity.class); 32 | } 33 | 34 | @Override 35 | protected void setUp() throws Exception { 36 | super.setUp(); 37 | getActivity(); 38 | } 39 | 40 | public void testCountryNotInList() { 41 | onView(withId(R.id.countries_spinner)).check(matches(not(withAdaptedData(withItemContent(INVALID_COUNTRY_NAME))))); 42 | } 43 | 44 | public void testLabelDoesNotChangeIfFirstItemSelected() { 45 | // Click on the Spinner 46 | onView(withId(R.id.countries_spinner)).perform(click()); 47 | 48 | // Click on the first item from the list, which is a marker string: "Select your country" 49 | onData(allOf(is(instanceOf(String.class)))).atPosition(0).perform(click()); 50 | 51 | // Check that the country label is not updated. 52 | onView(withId(R.id.country_label)).check(matches(not(withText(FIRST_ITEM_TEXT)))); 53 | } 54 | 55 | public void testLabelUpdatesIfValidCountrySelected() { 56 | // Click on the Spinner 57 | onView(withId(R.id.countries_spinner)).perform(click()); 58 | 59 | // Select a country from the list 60 | onData(allOf(is(instanceOf(String.class)), is(VALID_COUNTRY_NAME))).perform(click()); 61 | 62 | // Check that the country label is updated with selected country 63 | onView(withId(R.id.country_label)).check(matches(withText(VALID_COUNTRY_NAME))); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/ViewPagerTest.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import android.test.ActivityInstrumentationTestCase2; 4 | import android.widget.AdapterView; 5 | 6 | import com.vgrec.espressoexamples.activities.ViewPagerActivity; 7 | 8 | import static android.support.test.espresso.Espresso.onData; 9 | import static android.support.test.espresso.Espresso.onView; 10 | import static android.support.test.espresso.action.ViewActions.click; 11 | import static android.support.test.espresso.action.ViewActions.swipeLeft; 12 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 13 | import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 14 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 15 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 16 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 17 | import static com.vgrec.espressoexamples.matchers.CustomMatchers.withBookAuthor; 18 | import static com.vgrec.espressoexamples.matchers.CustomMatchers.withBookTitle; 19 | import static org.hamcrest.CoreMatchers.allOf; 20 | 21 | 22 | /** 23 | * @author vgrec, created on 3/23/15. 24 | */ 25 | public class ViewPagerTest extends ActivityInstrumentationTestCase2 { 26 | 27 | private static final String BOOK_TITLE = "Clean Code"; 28 | private static final String BOOK_AUTHOR = "Robert C. Martin"; 29 | 30 | public ViewPagerTest() { 31 | super(ViewPagerActivity.class); 32 | } 33 | 34 | @Override 35 | protected void setUp() throws Exception { 36 | super.setUp(); 37 | getActivity(); 38 | } 39 | 40 | public void testAllTabDisplayedOnSwipe() { 41 | // Locate the ViewPager and perform a swipe left action 42 | onView(withId(R.id.pager)).perform(swipeLeft()); 43 | 44 | // Check the "ALL BOOKS" text is displayed 45 | onView(allOf(withId(R.id.header_text), isDisplayed())).check(matches(withText("ALL BOOKS"))); 46 | } 47 | 48 | public void testClickOnBookFromNewTab() { 49 | // The below commented out line will fail with AmbiguousViewMatcherException because the same ListView is used in both pages of ViewPager. 50 | // onData(allOf(withBookTitle(BOOK_TITLE), withBookAuthor(BOOK_AUTHOR))).perform(click()); 51 | 52 | // We have to refine the query specifying that we are looking for an AdapterView that is currently visible. 53 | onData(allOf(withBookTitle(BOOK_TITLE), withBookAuthor(BOOK_AUTHOR))) 54 | .inAdapterView(allOf(isAssignableFrom(AdapterView.class), isDisplayed())) 55 | .perform(click()); 56 | 57 | // Check the correct book title is displayed 58 | onView(withId(R.id.book_title)).check(matches(withText(BOOK_TITLE))); 59 | 60 | // Check the correct author is displayed 61 | onView(withId(R.id.book_author)).check(matches(withText(BOOK_AUTHOR))); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vgrec/espressoexamples/matchers/CustomMatchers.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.matchers; 2 | 3 | import android.support.test.espresso.matcher.BoundedMatcher; 4 | import android.view.View; 5 | import android.widget.Adapter; 6 | import android.widget.AdapterView; 7 | 8 | import com.vgrec.espressoexamples.models.Book; 9 | 10 | import org.hamcrest.Description; 11 | import org.hamcrest.Matcher; 12 | import org.hamcrest.TypeSafeMatcher; 13 | 14 | import static com.android.support.test.deps.guava.base.Preconditions.checkNotNull; 15 | import static org.hamcrest.Matchers.equalTo; 16 | 17 | /** 18 | * @author vgrec, created on 3/17/15. 19 | */ 20 | public class CustomMatchers { 21 | 22 | 23 | /** 24 | * Finds the AdapterView and let another Matcher interrogate the data within it. 25 | */ 26 | public static Matcher withAdaptedData(final Matcher dataMatcher) { 27 | return new TypeSafeMatcher() { 28 | 29 | @Override 30 | public void describeTo(Description description) { 31 | description.appendText("with class name: "); 32 | dataMatcher.describeTo(description); 33 | } 34 | 35 | @Override 36 | public boolean matchesSafely(View view) { 37 | if (!(view instanceof AdapterView)) { 38 | return false; 39 | } 40 | @SuppressWarnings("rawtypes") 41 | Adapter adapter = ((AdapterView) view).getAdapter(); 42 | for (int i = 0; i < adapter.getCount(); i++) { 43 | if (dataMatcher.matches(adapter.getItem(i))) { 44 | return true; 45 | } 46 | } 47 | return false; 48 | } 49 | }; 50 | } 51 | 52 | /** 53 | * Matches an item from an AdapterView with a specific String. 54 | * (The items in AdapterView should be strings) 55 | */ 56 | public static Matcher withItemContent(String expectedText) { 57 | checkNotNull(expectedText); 58 | return withItemContent(equalTo(expectedText)); 59 | } 60 | 61 | public static Matcher withItemContent(final Matcher itemTextMatcher) { 62 | checkNotNull(itemTextMatcher); 63 | return new BoundedMatcher(String.class) { 64 | @Override 65 | public boolean matchesSafely(String text) { 66 | return itemTextMatcher.matches(text); 67 | } 68 | 69 | @Override 70 | public void describeTo(Description description) { 71 | description.appendText("with item content: "); 72 | itemTextMatcher.describeTo(description); 73 | } 74 | }; 75 | } 76 | 77 | 78 | /** 79 | * Matches a Book with a specific ID 80 | */ 81 | public static Matcher withBookId(final int bookId) { 82 | return new BoundedMatcher(Book.class) { 83 | @Override 84 | protected boolean matchesSafely(Book book) { 85 | return bookId == book.getId(); 86 | } 87 | 88 | @Override 89 | public void describeTo(Description description) { 90 | description.appendText("with id: " + bookId); 91 | } 92 | }; 93 | } 94 | 95 | /** 96 | * Matches a Book with a specific title 97 | */ 98 | public static Matcher withBookTitle(final String bookTitle) { 99 | return new BoundedMatcher(Book.class) { 100 | @Override 101 | protected boolean matchesSafely(Book book) { 102 | return bookTitle.equals(book.getTitle()); 103 | } 104 | 105 | @Override 106 | public void describeTo(Description description) { 107 | description.appendText("with id: " + bookTitle); 108 | } 109 | }; 110 | } 111 | 112 | /** 113 | * Matches a Book with a specific author name 114 | */ 115 | public static Matcher withBookAuthor(final String bookAuthor) { 116 | return new BoundedMatcher(Book.class) { 117 | @Override 118 | protected boolean matchesSafely(Book book) { 119 | return bookAuthor.equals(book.getAuthor()); 120 | } 121 | 122 | @Override 123 | public void describeTo(Description description) { 124 | description.appendText("with id: " + bookAuthor); 125 | } 126 | }; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 25 | 28 | 31 | 34 | 37 | 38 | 39 | 40 | 41 | 44 | 45 | 48 | 49 | 53 | 54 | 57 | 58 | 61 | 62 | 65 | 66 | 69 | 70 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /app/src/main/ic_about-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vgrec/EspressoExamples/2f73307db37bda8283d366ebee9e9977218eda86/app/src/main/ic_about-web.png -------------------------------------------------------------------------------- /app/src/main/ic_settings-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vgrec/EspressoExamples/2f73307db37bda8283d366ebee9e9977218eda86/app/src/main/ic_settings-web.png -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/Database.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples; 2 | 3 | import com.vgrec.espressoexamples.models.Book; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | 8 | /** 9 | * This class holds constants with some data for testing. 10 | * 11 | * @author vgrec, created on 3/23/15. 12 | */ 13 | public class Database { 14 | 15 | public static final ArrayList NEW_BOOKS = new ArrayList<>(Arrays.asList( 16 | new Book(1, "Clean Code", "Robert C. Martin"), 17 | new Book(2, "The Clean Coder", "Robert C. Martin"), 18 | new Book(3, "Code Complete 2", "Steve McConnell"), 19 | new Book(4, "Effective Java ", "Joshua Bloch"), 20 | new Book(5, "Java Concurrency in Practice", "Brian Goetz") 21 | )); 22 | 23 | public static final ArrayList ALL_BOOKS = new ArrayList<>(Arrays.asList( 24 | new Book(1, "Clean Code", "Robert C. Martin"), 25 | new Book(2, "The Clean Coder", "Robert C. Martin"), 26 | new Book(3, "Code Complete 2", "Steve McConnell"), 27 | new Book(4, "Effective Java ", "Joshua Bloch"), 28 | new Book(9, "Refactoring: Improving the Design of Existing Code", "Martin Fowler"), 29 | new Book(5, "Java Concurrency in Practice", "Brian Goetz"), 30 | new Book(6, "Essential Algorithms", "Rod Stephens"), 31 | new Book(7, "Algorithms", "Robert Sedgewick"), 32 | new Book(8, "The Passionate Programmer", "Chad Fowler"), 33 | new Book(10, "The Pragmatic Programmer", "Andrew Hunt"), 34 | new Book(11, "Seven Languages in Seven Weeks", "Bruce A. Tate"), 35 | new Book(12, "The Ruby Programming Language", "David Flanagan"), 36 | new Book(13, "Agile Web Development with Rails 4 ", "Sam Ruby") 37 | )); 38 | 39 | public static final String[] CITIES = { 40 | "Berlin", 41 | "Havana", 42 | "Helsinki", 43 | "Jakarta", 44 | "Paris", 45 | "Prague", 46 | "Sofia" 47 | }; 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/ActionBarExampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.support.v7.view.ActionMode; 6 | import android.view.Menu; 7 | import android.view.MenuItem; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import android.widget.TextView; 11 | 12 | import com.vgrec.espressoexamples.R; 13 | 14 | 15 | public class ActionBarExampleActivity extends ActionBarActivity { 16 | 17 | private TextView statusTextView; 18 | private ActionMode actionMode; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_action_bar_example); 24 | 25 | statusTextView = (TextView) findViewById(R.id.status); 26 | 27 | Button toggleActionModeButton = (Button) findViewById(R.id.toggle_action_mode); 28 | toggleActionModeButton.setOnClickListener(new View.OnClickListener() { 29 | @Override 30 | public void onClick(View v) { 31 | startActionMode(); 32 | } 33 | }); 34 | } 35 | 36 | private void startActionMode() { 37 | if (actionMode == null) { 38 | actionMode = startSupportActionMode(actionModeCallbacks); 39 | } 40 | } 41 | 42 | private ActionMode.Callback actionModeCallbacks = new ActionMode.Callback() { 43 | @Override 44 | public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { 45 | actionMode.getMenuInflater().inflate(R.menu.context_menu, menu); 46 | return true; 47 | } 48 | 49 | @Override 50 | public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { 51 | return true; 52 | } 53 | 54 | @Override 55 | public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { 56 | switch (menuItem.getItemId()) { 57 | case R.id.action_one: 58 | updateStatusText(R.string.action_mode_one); 59 | return true; 60 | } 61 | return false; 62 | } 63 | 64 | @Override 65 | public void onDestroyActionMode(ActionMode actionMode) { 66 | ActionBarExampleActivity.this.actionMode = null; 67 | } 68 | }; 69 | 70 | 71 | @Override 72 | public boolean onCreateOptionsMenu(Menu menu) { 73 | getMenuInflater().inflate(R.menu.menu_action_bar_example, menu); 74 | return true; 75 | } 76 | 77 | @Override 78 | public boolean onOptionsItemSelected(MenuItem item) { 79 | switch (item.getItemId()) { 80 | case R.id.action_settings: 81 | updateStatusText(R.string.action_settings); 82 | break; 83 | case R.id.action_about: 84 | updateStatusText(R.string.action_about); 85 | break; 86 | } 87 | 88 | return super.onOptionsItemSelected(item); 89 | } 90 | 91 | private void updateStatusText(int statusResId) { 92 | statusTextView.setText(statusResId); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/BookDetailsActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.widget.TextView; 6 | 7 | import com.vgrec.espressoexamples.R; 8 | 9 | 10 | public class BookDetailsActivity extends ActionBarActivity { 11 | 12 | public static final String TITLE = "title"; 13 | public static final String AUTHOR = "author"; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_book_details); 19 | 20 | TextView bookTitle = (TextView) findViewById(R.id.book_title); 21 | TextView bookAuthor = (TextView) findViewById(R.id.book_author); 22 | 23 | bookTitle.setText(getIntent().getStringExtra(TITLE)); 24 | bookAuthor.setText(getIntent().getStringExtra(AUTHOR)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/CustomListActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.ActionBarActivity; 6 | import android.view.View; 7 | import android.widget.AdapterView; 8 | import android.widget.ListView; 9 | 10 | import com.vgrec.espressoexamples.Database; 11 | import com.vgrec.espressoexamples.R; 12 | import com.vgrec.espressoexamples.adapter.BooksAdapter; 13 | import com.vgrec.espressoexamples.models.Book; 14 | 15 | 16 | public class CustomListActivity extends ActionBarActivity { 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_custom_list); 22 | 23 | ListView list = (ListView) findViewById(R.id.list); 24 | list.setAdapter(new BooksAdapter(this, Database.ALL_BOOKS)); 25 | list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 26 | @Override 27 | public void onItemClick(AdapterView parent, View view, int position, long id) { 28 | Book book = (Book) parent.getItemAtPosition(position); 29 | Intent intent = new Intent(CustomListActivity.this, BookDetailsActivity.class); 30 | intent.putExtra(BookDetailsActivity.TITLE, book.getTitle()); 31 | intent.putExtra(BookDetailsActivity.AUTHOR, book.getAuthor()); 32 | startActivity(intent); 33 | } 34 | }); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/DateTimePickerActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.app.DatePickerDialog; 4 | import android.app.TimePickerDialog; 5 | import android.os.Bundle; 6 | import android.support.v7.app.ActionBarActivity; 7 | import android.view.View; 8 | import android.widget.DatePicker; 9 | import android.widget.TextView; 10 | import android.widget.TimePicker; 11 | 12 | import com.vgrec.espressoexamples.R; 13 | 14 | import java.util.Calendar; 15 | 16 | public class DateTimePickerActivity extends ActionBarActivity { 17 | 18 | private TextView statusTextView; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_date_time_picker); 24 | 25 | statusTextView = (TextView) findViewById(R.id.status); 26 | 27 | findViewById(R.id.date_picker_button).setOnClickListener(new View.OnClickListener() { 28 | @Override 29 | public void onClick(View v) { 30 | showDatePickerDialog(); 31 | } 32 | }); 33 | 34 | findViewById(R.id.time_picker_button).setOnClickListener(new View.OnClickListener() { 35 | @Override 36 | public void onClick(View v) { 37 | showTimePickerDialog(); 38 | } 39 | }); 40 | 41 | } 42 | 43 | private void showTimePickerDialog() { 44 | Calendar calendar = Calendar.getInstance(); 45 | int hour = calendar.get(Calendar.HOUR_OF_DAY); 46 | int minute = calendar.get(Calendar.MINUTE); 47 | 48 | TimePickerDialog timePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { 49 | @Override 50 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 51 | updateStatusView(hourOfDay + ":" + minute); 52 | } 53 | }, hour, minute, true); 54 | timePicker.setTitle("Pick a time"); 55 | timePicker.show(); 56 | } 57 | 58 | private void showDatePickerDialog() { 59 | DatePickerDialog datePicker = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 60 | @Override 61 | public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 62 | updateStatusView(year + "/" + monthOfYear + "/" + dayOfMonth); 63 | } 64 | }, 2015, 0, 1); 65 | datePicker.setTitle("Pick a date"); 66 | datePicker.show(); 67 | } 68 | 69 | private void updateStatusView(String date) { 70 | statusTextView.setText(date); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/DialogExampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.app.AlertDialog; 4 | import android.content.DialogInterface; 5 | import android.os.Bundle; 6 | import android.support.v7.app.ActionBarActivity; 7 | import android.view.View; 8 | import android.widget.TextView; 9 | 10 | import com.vgrec.espressoexamples.R; 11 | 12 | public class DialogExampleActivity extends ActionBarActivity { 13 | 14 | private TextView statusTextView; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_dialog_example); 20 | 21 | statusTextView = (TextView) findViewById(R.id.status_text); 22 | 23 | findViewById(R.id.confirm_dialog_button).setOnClickListener(new View.OnClickListener() { 24 | @Override 25 | public void onClick(View v) { 26 | showAlertDialog(); 27 | } 28 | }); 29 | } 30 | 31 | private void showAlertDialog() { 32 | AlertDialog.Builder builder = new AlertDialog.Builder(this); 33 | builder.setTitle(R.string.dialog_title); 34 | builder.setMessage(R.string.dialog_message); 35 | builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 36 | @Override 37 | public void onClick(DialogInterface dialog, int which) { 38 | statusTextView.setText(getString(R.string.cancel)); 39 | } 40 | }); 41 | builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 42 | @Override 43 | public void onClick(DialogInterface dialog, int which) { 44 | statusTextView.setText(getString(R.string.ok)); 45 | } 46 | }); 47 | builder.create().show(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/DisplayNameActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.widget.TextView; 6 | 7 | import com.vgrec.espressoexamples.R; 8 | 9 | 10 | public class DisplayNameActivity extends ActionBarActivity { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_display_name); 16 | 17 | String text = getIntent().getStringExtra(EnterNameActivity.NAME); 18 | TextView greetingTextView = (TextView) findViewById(R.id.greeting_message); 19 | greetingTextView.setText("Hello " + text + "!"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/EnterNameActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.ActionBarActivity; 6 | import android.view.View; 7 | import android.widget.EditText; 8 | import android.widget.TextView; 9 | 10 | import com.vgrec.espressoexamples.R; 11 | 12 | 13 | public class EnterNameActivity extends ActionBarActivity { 14 | 15 | public static final String NAME = "text"; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_enter_name); 21 | 22 | final EditText nameEditText = (EditText) findViewById(R.id.name_edittext); 23 | final TextView errorTextView = (TextView) findViewById(R.id.error_text); 24 | 25 | findViewById(R.id.next_button).setOnClickListener(new View.OnClickListener() { 26 | @Override 27 | public void onClick(View v) { 28 | String name = nameEditText.getText().toString().trim(); 29 | if (name.length() > 0) { 30 | errorTextView.setVisibility(View.INVISIBLE); 31 | Intent intent = new Intent(EnterNameActivity.this, DisplayNameActivity.class); 32 | intent.putExtra(NAME, name); 33 | startActivity(intent); 34 | } else { 35 | errorTextView.setVisibility(View.VISIBLE); 36 | } 37 | } 38 | }); 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v7.app.ActionBarActivity; 7 | import android.view.View; 8 | 9 | import com.vgrec.espressoexamples.R; 10 | 11 | 12 | public class MainActivity extends ActionBarActivity { 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_main); 18 | 19 | setListener(R.id.type_text_button, EnterNameActivity.class); 20 | setListener(R.id.spinner_selection_button, SpinnerSelectionActivity.class); 21 | setListener(R.id.custom_list_adapter_button, CustomListActivity.class); 22 | setListener(R.id.search_view_button, SearchViewActivity.class); 23 | setListener(R.id.action_bar_button, ActionBarExampleActivity.class); 24 | setListener(R.id.viewpager_button, ViewPagerActivity.class); 25 | setListener(R.id.dialogs_button, DialogExampleActivity.class); 26 | setListener(R.id.recycler_view_button, RecyclerViewActivity.class); 27 | setListener(R.id.pickers_button, DateTimePickerActivity.class); 28 | } 29 | 30 | private void setListener(int buttonResId, final Class activity) { 31 | findViewById(buttonResId).setOnClickListener(new View.OnClickListener() { 32 | @Override 33 | public void onClick(View v) { 34 | startActivity(new Intent(MainActivity.this, activity)); 35 | } 36 | }); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/RecyclerViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | 8 | import com.vgrec.espressoexamples.R; 9 | import com.vgrec.espressoexamples.adapter.RecyclerBooksAdapter; 10 | 11 | public class RecyclerViewActivity extends ActionBarActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_recycler_view); 17 | RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 18 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 19 | recyclerView.setAdapter(new RecyclerBooksAdapter(this)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/SearchViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.app.SearchManager; 4 | import android.content.ComponentName; 5 | import android.content.Context; 6 | import android.os.Bundle; 7 | import android.support.v7.app.ActionBarActivity; 8 | import android.support.v7.widget.SearchView; 9 | import android.view.Menu; 10 | 11 | import com.vgrec.espressoexamples.R; 12 | 13 | 14 | public class SearchViewActivity extends ActionBarActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_search_view); 20 | } 21 | 22 | @Override 23 | public boolean onCreateOptionsMenu(Menu menu) { 24 | getMenuInflater().inflate(R.menu.menu_search_view, menu); 25 | 26 | SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 27 | SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 28 | searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchableActivity.class))); 29 | 30 | return true; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/SearchableActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.app.SearchManager; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.provider.SearchRecentSuggestions; 7 | import android.support.v7.app.ActionBarActivity; 8 | import android.widget.ArrayAdapter; 9 | import android.widget.ListView; 10 | 11 | import com.vgrec.espressoexamples.Database; 12 | import com.vgrec.espressoexamples.R; 13 | import com.vgrec.espressoexamples.provider.AppRecentSearchesProvider; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | 19 | public class SearchableActivity extends ActionBarActivity { 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_searchable); 25 | 26 | ListView listView = (ListView) findViewById(R.id.list); 27 | listView.setEmptyView(findViewById(R.id.empty_view)); 28 | 29 | Intent intent = getIntent(); 30 | if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 31 | String query = intent.getStringExtra(SearchManager.QUERY); 32 | List results = search(query); 33 | listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, results)); 34 | saveRecentQuery(query); 35 | } 36 | } 37 | 38 | private void saveRecentQuery(String query) { 39 | SearchRecentSuggestions suggestions = new SearchRecentSuggestions( 40 | this, 41 | AppRecentSearchesProvider.AUTHORITY, 42 | AppRecentSearchesProvider.MODE); 43 | suggestions.saveRecentQuery(query, null); 44 | } 45 | 46 | private List search(String query) { 47 | List results = new ArrayList<>(); 48 | 49 | for (String record : Database.CITIES) { 50 | if (record.toLowerCase().startsWith(query.toLowerCase())) { 51 | results.add(record); 52 | } 53 | } 54 | 55 | return results; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/SpinnerSelectionActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.view.View; 6 | import android.widget.AdapterView; 7 | import android.widget.ArrayAdapter; 8 | import android.widget.Spinner; 9 | import android.widget.TextView; 10 | 11 | import com.vgrec.espressoexamples.R; 12 | 13 | 14 | public class SpinnerSelectionActivity extends ActionBarActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_spinner_selection); 20 | 21 | final TextView labelTextView = (TextView) findViewById(R.id.country_label); 22 | Spinner spinner = (Spinner) findViewById(R.id.countries_spinner); 23 | 24 | spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.europe_countries))); 25 | spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 26 | @Override 27 | public void onItemSelected(AdapterView parent, View view, int position, long id) { 28 | if (position > 0) { 29 | labelTextView.setText((CharSequence) parent.getSelectedItem()); 30 | } 31 | } 32 | 33 | @Override 34 | public void onNothingSelected(AdapterView parent) { 35 | 36 | } 37 | }); 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/activities/ViewPagerActivity.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.activities; 2 | 3 | 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.app.Fragment; 8 | import android.support.v4.app.FragmentManager; 9 | import android.support.v4.app.FragmentPagerAdapter; 10 | import android.support.v4.view.ViewPager; 11 | import android.support.v7.app.ActionBarActivity; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | import android.widget.AdapterView; 16 | import android.widget.ListView; 17 | import android.widget.TextView; 18 | 19 | import com.astuetz.PagerSlidingTabStrip; 20 | import com.vgrec.espressoexamples.Database; 21 | import com.vgrec.espressoexamples.R; 22 | import com.vgrec.espressoexamples.adapter.BooksAdapter; 23 | import com.vgrec.espressoexamples.models.Book; 24 | 25 | import java.util.ArrayList; 26 | 27 | public class ViewPagerActivity extends ActionBarActivity { 28 | 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.activity_view_pager); 33 | 34 | ViewPager pager = (ViewPager) findViewById(R.id.pager); 35 | pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); 36 | 37 | PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); 38 | tabs.setViewPager(pager); 39 | } 40 | 41 | 42 | private class ViewPagerAdapter extends FragmentPagerAdapter { 43 | private String[] tabs = {"New", "All"}; 44 | 45 | public ViewPagerAdapter(FragmentManager fragmentManager) { 46 | super(fragmentManager); 47 | } 48 | 49 | @Override 50 | public Fragment getItem(int position) { 51 | return newBooksFragment(position); 52 | } 53 | 54 | @Override 55 | public int getCount() { 56 | return tabs.length; 57 | } 58 | 59 | @Override 60 | public CharSequence getPageTitle(int position) { 61 | return tabs[position]; 62 | } 63 | 64 | private Fragment newBooksFragment(int position) { 65 | Bundle args = new Bundle(); 66 | args.putInt("tab_index", position); 67 | BooksFragment fragment = new BooksFragment(); 68 | fragment.setArguments(args); 69 | return fragment; 70 | } 71 | } 72 | 73 | public static class BooksFragment extends Fragment { 74 | private static final int TAB_NEW = 0; 75 | private static final int TAB_ALL = 1; 76 | private ArrayList books = new ArrayList<>(); 77 | private String headerText; 78 | 79 | @Override 80 | public void onCreate(Bundle savedInstanceState) { 81 | super.onCreate(savedInstanceState); 82 | int currentTab = getArguments().getInt("tab_index"); 83 | if (currentTab == TAB_NEW) { 84 | books.addAll(Database.NEW_BOOKS); 85 | headerText = "NEW BOOKS"; 86 | } else if (currentTab == TAB_ALL) { 87 | books.addAll(Database.ALL_BOOKS); 88 | headerText = "ALL BOOKS"; 89 | } 90 | } 91 | 92 | @Override 93 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 94 | View view = inflater.inflate(R.layout.fragment_books, container, false); 95 | ListView listView = (ListView) view.findViewById(R.id.list); 96 | listView.setAdapter(new BooksAdapter(getActivity(), books)); 97 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 98 | @Override 99 | public void onItemClick(AdapterView parent, View view, int position, long id) { 100 | Book book = (Book) parent.getItemAtPosition(position); 101 | Intent intent = new Intent(getActivity(), BookDetailsActivity.class); 102 | intent.putExtra(BookDetailsActivity.TITLE, book.getTitle()); 103 | intent.putExtra(BookDetailsActivity.AUTHOR, book.getAuthor()); 104 | startActivity(intent); 105 | } 106 | }); 107 | 108 | TextView headerTextView = (TextView) view.findViewById(R.id.header_text); 109 | headerTextView.setText(headerText); 110 | return view; 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/adapter/BooksAdapter.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.adapter; 2 | 3 | import android.content.Context; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseAdapter; 8 | import android.widget.TextView; 9 | 10 | import com.vgrec.espressoexamples.R; 11 | import com.vgrec.espressoexamples.models.Book; 12 | 13 | import java.util.ArrayList; 14 | 15 | /** 16 | * Adapter that provides views for the list 17 | */ 18 | public class BooksAdapter extends BaseAdapter { 19 | private LayoutInflater inflater; 20 | private ArrayList items; 21 | 22 | 23 | public BooksAdapter(Context context, ArrayList items) { 24 | this.items = items; 25 | inflater = LayoutInflater.from(context); 26 | } 27 | 28 | @Override 29 | public int getCount() { 30 | return items.size(); 31 | } 32 | 33 | @Override 34 | public Object getItem(int position) { 35 | return items.get(position); 36 | } 37 | 38 | @Override 39 | public long getItemId(int position) { 40 | return position; 41 | } 42 | 43 | @Override 44 | public View getView(int position, View convertView, ViewGroup parent) { 45 | if (convertView == null) { 46 | convertView = inflater.inflate(R.layout.item_book, parent, false); 47 | } 48 | 49 | TextView bookTitle = (TextView) convertView.findViewById(R.id.book_title); 50 | bookTitle.setText(items.get(position).getTitle()); 51 | 52 | TextView bookAuthor = (TextView) convertView.findViewById(R.id.book_author); 53 | bookAuthor.setText("by " + items.get(position).getAuthor()); 54 | 55 | return convertView; 56 | } 57 | } -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/adapter/RecyclerBooksAdapter.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.adapter; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.TextView; 10 | 11 | import com.vgrec.espressoexamples.Database; 12 | import com.vgrec.espressoexamples.R; 13 | import com.vgrec.espressoexamples.activities.BookDetailsActivity; 14 | import com.vgrec.espressoexamples.models.Book; 15 | 16 | /** 17 | * The adapter used by RecyclerView to display books. 18 | * 19 | * @author vgrec, created on 3/30/15. 20 | */ 21 | public class RecyclerBooksAdapter extends RecyclerView.Adapter { 22 | 23 | private LayoutInflater inflater; 24 | 25 | public RecyclerBooksAdapter(Context context) { 26 | inflater = LayoutInflater.from(context); 27 | } 28 | 29 | @Override 30 | public RowHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 31 | return new RowHolder(inflater.inflate(R.layout.item_book, viewGroup, false)); 32 | } 33 | 34 | @Override 35 | public void onBindViewHolder(RowHolder rowHolder, int i) { 36 | rowHolder.bindModel(Database.ALL_BOOKS.get(i)); 37 | } 38 | 39 | @Override 40 | public int getItemCount() { 41 | return Database.ALL_BOOKS.size(); 42 | } 43 | 44 | public static class RowHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 45 | 46 | TextView titleTextView; 47 | TextView authorTextView; 48 | 49 | public RowHolder(View row) { 50 | super(row); 51 | titleTextView = (TextView) row.findViewById(R.id.book_title); 52 | authorTextView = (TextView) row.findViewById(R.id.book_author); 53 | row.setOnClickListener(this); 54 | } 55 | 56 | public void bindModel(Book book) { 57 | titleTextView.setText(book.getTitle()); 58 | authorTextView.setText(book.getAuthor()); 59 | } 60 | 61 | @Override 62 | public void onClick(View v) { 63 | Book book = Database.ALL_BOOKS.get(getPosition()); 64 | 65 | Intent intent = new Intent(v.getContext(), BookDetailsActivity.class); 66 | intent.putExtra(BookDetailsActivity.TITLE, book.getTitle()); 67 | intent.putExtra(BookDetailsActivity.AUTHOR, book.getAuthor()); 68 | v.getContext().startActivity(intent); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/models/Book.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.models; 2 | 3 | /** 4 | * @author vgrec, created on 3/18/15. 5 | */ 6 | public class Book { 7 | private int id; 8 | private String title; 9 | private String author; 10 | 11 | public Book(int id, String title, String author) { 12 | this.id = id; 13 | this.title = title; 14 | this.author = author; 15 | } 16 | 17 | public String getTitle() { 18 | return title; 19 | } 20 | 21 | public String getAuthor() { 22 | return author; 23 | } 24 | 25 | public int getId() { 26 | return id; 27 | } 28 | } -------------------------------------------------------------------------------- /app/src/main/java/com/vgrec/espressoexamples/provider/AppRecentSearchesProvider.java: -------------------------------------------------------------------------------- 1 | package com.vgrec.espressoexamples.provider; 2 | 3 | import android.content.SearchRecentSuggestionsProvider; 4 | 5 | /** 6 | * @author vgrec, created on 3/18/15. 7 | */ 8 | public class AppRecentSearchesProvider extends SearchRecentSuggestionsProvider { 9 | 10 | public final static String AUTHORITY = "com.vgrec.espressoexamples"; 11 | public final static int MODE = DATABASE_MODE_QUERIES; 12 | 13 | public AppRecentSearchesProvider() { 14 | setupSuggestions(AUTHORITY, MODE); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/book.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vgrec/EspressoExamples/2f73307db37bda8283d366ebee9e9977218eda86/app/src/main/res/drawable-xhdpi/book.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_action_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vgrec/EspressoExamples/2f73307db37bda8283d366ebee9e9977218eda86/app/src/main/res/drawable-xhdpi/ic_action_search.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_action_bar_example.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 |