Home | History | Annotate | Download | only in tests
      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.swiperefreshlistfragment.tests;
     18 
     19 import com.example.android.swiperefreshlistfragment.MainActivity;
     20 import com.example.android.swiperefreshlistfragment.R;
     21 import com.example.android.swiperefreshlistfragment.SwipeRefreshListFragmentFragment;
     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 SwipeRefreshListFragment sample.
     30  */
     31 public class SampleTests extends ActivityInstrumentationTestCase2<MainActivity> {
     32 
     33     private MainActivity mTestActivity;
     34     private SwipeRefreshListFragmentFragment mTestFragment;
     35 
     36     private SwipeRefreshLayout mSwipeRefreshLayout;
     37 
     38     public SampleTests() {
     39         super(MainActivity.class);
     40     }
     41 
     42     @Override
     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 = (SwipeRefreshListFragmentFragment)
     52                 mTestActivity.getSupportFragmentManager().getFragments().get(1);
     53         mSwipeRefreshLayout = mTestFragment.getSwipeRefreshLayout();
     54     }
     55 
     56     /**
     57      * Test if the test fixture has been set up correctly.
     58      */
     59     public void testPreconditions() {
     60         //Try to add a message to add context to your assertions. These messages will be shown if
     61         //a tests fails and make it easy to understand why a test failed
     62         assertNotNull("mTestActivity is null", mTestActivity);
     63         assertNotNull("mTestFragment is null", mTestFragment);
     64         assertNotNull("mSwipeRefreshLayout is null", mSwipeRefreshLayout);
     65     }
     66 
     67     /**
     68      * Test that swiping on the populated list triggers a refresh.
     69      */
     70     public void testSwipingListView() {
     71         // Given a SwipeRefreshLayout which is displaying a populated list
     72 
     73         // When the swipe refresh layout is dragged
     74         TouchUtils.dragViewBy(this,
     75                 mSwipeRefreshLayout,
     76                 Gravity.TOP | Gravity.CENTER_HORIZONTAL,
     77                 0,
     78                 Math.round(mSwipeRefreshLayout.getHeight() * 0.4f));
     79 
     80         // Then the SwipeRefreshLayout should be refreshing
     81         // We need to use runOnMainSync here as fake dragging uses waitForIdleSync()
     82         getInstrumentation().runOnMainSync(new Runnable() {
     83             @Override
     84             public void run() {
     85                 assertTrue(mSwipeRefreshLayout.isRefreshing());
     86             }
     87         });
     88     }
     89 
     90     /**
     91      * Test that selecting the refresh menu item triggers a refresh.
     92      */
     93     public void testRefreshMenuItem() {
     94         // When the refresh menu item is selected
     95         getInstrumentation().invokeMenuActionSync(mTestActivity, R.id.menu_refresh, 0);
     96 
     97         // Then the SwipeRefreshLayout should be refreshing
     98         assertTrue(mSwipeRefreshLayout.isRefreshing());
     99     }
    100 
    101 }