1 /* 2 * Copyright 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.swiperefreshmultipleviews.tests; 18 19 import com.example.android.swiperefreshmultipleviews.MainActivity; 20 import com.example.android.swiperefreshmultipleviews.R; 21 import com.example.android.swiperefreshmultipleviews.SwipeRefreshMultipleViewsFragment; 22 23 import android.support.v4.widget.SwipeRefreshLayout; 24 import android.test.ActivityInstrumentationTestCase2; 25 import android.test.TouchUtils; 26 import android.view.Gravity; 27 28 /** 29 * Tests for SwipeRefreshMultipleViews sample. 30 */ 31 public class SampleTests extends ActivityInstrumentationTestCase2<MainActivity> { 32 33 private MainActivity mTestActivity; 34 private SwipeRefreshMultipleViewsFragment mTestFragment; 35 36 private SwipeRefreshLayout mSwipeRefreshLayout; 37 SampleTests()38 public SampleTests() { 39 super(MainActivity.class); 40 } 41 42 @Override setUp()43 protected void setUp() throws Exception { 44 super.setUp(); 45 46 // Starts the activity under test using the default Intent with: 47 // action = {@link Intent#ACTION_MAIN} 48 // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK} 49 // All other fields are null or empty. 50 mTestActivity = getActivity(); 51 mTestFragment = (SwipeRefreshMultipleViewsFragment) 52 mTestActivity.getSupportFragmentManager().getFragments().get(1); 53 mSwipeRefreshLayout = (SwipeRefreshLayout) 54 mTestFragment.getView().findViewById(R.id.swiperefresh); 55 } 56 57 /** 58 * Test if the test fixture has been set up correctly. 59 */ testPreconditions()60 public void testPreconditions() { 61 //Try to add a message to add context to your assertions. These messages will be shown if 62 //a tests fails and make it easy to understand why a test failed 63 assertNotNull("mTestActivity is null", mTestActivity); 64 assertNotNull("mTestFragment is null", mTestFragment); 65 assertNotNull("mSwipeRefreshLayout is null", mSwipeRefreshLayout); 66 } 67 68 /** 69 * Test that swiping on the empty view triggers a refresh. 70 */ testSwipingEmptyView()71 public void testSwipingEmptyView() { 72 // Given a SwipeRefreshLayout which is displaying the empty view 73 74 // When the swipe refresh layout is dragged 75 TouchUtils.dragViewBy(this, 76 mSwipeRefreshLayout, 77 Gravity.CENTER, 78 0, 79 Math.round(mSwipeRefreshLayout.getHeight() * 0.4f)); 80 81 // Then the SwipeRefreshLayout should be refreshing 82 getInstrumentation().runOnMainSync(new Runnable() { 83 @Override 84 public void run() { 85 assertTrue(mSwipeRefreshLayout.isRefreshing()); 86 } 87 }); 88 } 89 90 /** 91 * Test that swiping on the populated list triggers a refresh. 92 */ testSwipingListView()93 public void testSwipingListView() { 94 // Given a SwipeRefreshLayout which is displaying a populated list 95 populateList(); 96 97 // When the swipe refresh layout is dragged 98 TouchUtils.dragViewBy(this, 99 mSwipeRefreshLayout, 100 Gravity.CENTER, 101 0, 102 Math.round(mSwipeRefreshLayout.getHeight() * 0.4f)); 103 104 // Then the SwipeRefreshLayout should be refreshing 105 // We need to use runOnMainSync here as fake dragging uses waitForIdleSync() 106 getInstrumentation().runOnMainSync(new Runnable() { 107 @Override 108 public void run() { 109 assertTrue(mSwipeRefreshLayout.isRefreshing()); 110 } 111 }); 112 } 113 114 /** 115 * Test that selecting the refresh menu item triggers a refresh. 116 */ testRefreshMenuItem()117 public void testRefreshMenuItem() { 118 // When the refresh menu item is selected 119 getInstrumentation().invokeMenuActionSync(mTestActivity, R.id.menu_refresh, 0); 120 121 // Then the SwipeRefreshLayout should be refreshing 122 assertTrue(mSwipeRefreshLayout.isRefreshing()); 123 } 124 populateList()125 private void populateList() { 126 // Given a SwipeRefreshLayout which is displaying a populated list 127 getInstrumentation().invokeMenuActionSync(mTestActivity, R.id.menu_refresh, 0); 128 while (mSwipeRefreshLayout.isRefreshing()) { 129 // Waiting for the manual refresh to finish 130 } 131 } 132 133 }