Espresso 测试SwipeRefreshLayout 组件,下拉问题记录

package com.umeng.community.example.feedlist;

import android.support.test.espresso.IdlingPolicies;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import com.umeng.comm.core.impl.FeedAPIImpl;
import com.umeng.comm.core.listeners.Listeners;
import com.umeng.comm.core.nets.responses.FeedsResponse;
import com.umeng.community.example.MainTestActivity;
import com.umeng.community.example.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.concurrent.TimeUnit;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.swipeDown;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.hasFocus;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.mockito.Mockito.verify;

/**
 * Created by admin on 16/6/15.
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class RealTimeFeedListTest {

   // UiDevice mDevice;
    @Mock
    protected FeedAPIImpl request;

    @Captor
    private ArgumentCaptor feedsResponseArgumentCaptor;

    @Rule
    public ActivityTestRule<MainTestActivity> mainActivityTestRule = new ActivityTestRule<MainTestActivity>(MainTestActivity.class){

        @Override
        protected void beforeActivityLaunched() {
            super.beforeActivityLaunched();

        }
    };

    @Before
    @SuppressWarnings("unchecked")
    public void setup(){
        // Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
        // inject the mocks in the test the initMocks method needs to be called.
        MockitoAnnotations.initMocks(this);
        //mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    }

    @Test
    public void  testLauchActivityFirtDisplayRealTimeFeedList() {


        onView(withId(R.id.umeng_comm_segment_view)).check(matches(isDisplayed()));  //
        String text[] = mainActivityTestRule.getActivity().getResources().getStringArray(R.array.umeng_simplify_feed_titles);
        onView(withText(text[0])).check(matches(isDisplayed())).check(matches(withParent(withId(R.id.umeng_comm_segment_view))));
        onView(withText(text[1])).check(matches(isDisplayed())).check(matches(withParent(withId(R.id.umeng_comm_segment_view))));

        //onView(withId(R.id.umeng_comm_ll)).check(matches(isDisplayed()));
        onView(withId(R.id.umeng_comm_feeds_frag_id)).check(matches(isDisplayed()));

        //onView(withId(R.id.umeng_comm_segment_view)).check(matches(withChild(withText(text[0]))));
       // onView(withId(R.id.umeng_comm_segment_view)).check(matches(withChild(withText(text[1]))));
       // onView(withText(text[1])).check(matches(isDisplayed()));

       // onView(withText(text[0])).inRoot(matcheswithId(R.id.umeng_comm_segment_view));


    }

    @Test
    public void testDispalyDataRightInRealTimeFeedList()  {
        onView(withId(R.id.umeng_comm_feeds_frag_id)).check(matches(isDisplayed()));
        //onView(withId(R.id.umeng_comm_feeds_frag_id)).perform(swipeLeft());
       // onView(withId(R.layout.umeng_simplify_feeds_frgm_layout)).check(matches(isDisplayed()));
       onView(allOf(withId(R.id.umeng_comm_swipe_layout),hasFocus())).check(matches(isDisplayed()));

      // onView(allOf(withId(R.id.umeng_comm_swipe_layout),hasFocus())).perform(swipeDown());
        onView(allOf(withId(R.id.umeng_comm_feed_listview),hasFocus())).check(matches(isDisplayed()));
        IdlingPolicies.setMasterPolicyTimeout(100, TimeUnit.SECONDS);

        onView(allOf(withId(R.id.umeng_comm_feed_listview),hasFocus())).perform(swipeDown());

       // viewInteraction.check(matches(isDisplayed()));
        //viewInteraction.perform(swipeDown());
//        UiObject myApp = mDevice.findObject(new UiSelector().className(com.umeng.common.ui.widgets.RefreshLvLayout.class));
//        myApp.swipeDown(10);

        String aa = "{\n" +
                "    \"navigator\": \"http://api.wsq.umeng.com/v2/feeds/stream?start=20&count=20&read_tag=2359797\",\n" +
                "    \"visit\": true,\n" +
                "    \"items\": [\n" +
                "        {\n" +
                "            \"status\": 0,\n" +
                "            \"liked\": false,\n" +
                "            \"related_user\": [],\n" +
                "            \"seq\": 2385157,\n" +
                "            \"creator\": {\n" +
                "                \"status\": 0,\n" +
                "                \"name\": \"Sofia\",\n" +
                "                \"source_uid\": \"gz@usc.edu\",\n" +
                "                \"level\": 0,\n" +
                "                \"gender\": 0,\n" +
                "                \"has_followed\": false,\n" +
                "                \"atype\": 0,\n" +
                "                \"custom\": null,\n" +
                "                \"medal_list\": [],\n" +
                "                \"icon_url\": {\n" +
                "                    \"240\": \"http://c-54d19014ee785020801f83c4.image.alimmdn.com/2016-06-17/5714a06b7019c91edf031dc8/c7c8495740c87749b0f85b6853b73f88@240w_240h_0e_1l_75Q\",\n" +
                "                    \"640\": \"http://c-54d19014ee785020801f83c4.image.alimmdn.com/2016-06-17/5714a06b7019c91edf031dc8/c7c8495740c87749b0f85b6853b73f88@640w_640h_0e_1l_75Q\",\n" +
                "                    \"origin\": \"http://c-54d19014ee785020801f83c4.image.alimmdn.com/2016-06-17/5714a06b7019c91edf031dc8/c7c8495740c87749b0f85b6853b73f88\"\n" +
                "                },\n" +
                "                \"id\": \"5763919fd014631f3b1ef68a\"\n" +
                "            },\n" +
                "            \"forward_count\": 0,\n" +
                "            \"ban_user\": false,\n" +
                "            \"is_topic_top\": null,\n" +
                "            \"image_urls\": [],\n" +
                "            \"is_top\": 0,\n" +
                "            \"create_time\": \"2016-06-17 14:11:55\",\n" +
                "            \"user_mark\": 0,\n" +
                "            \"media_type\": 0,\n" +
                "            \"id\": \"576394abee785022af4c2b05\",\n" +
                "            \"likes_count\": 0,\n" +
                "            \"has_collected\": false,\n" +
                "            \"title\": \"\",\n" +
                "            \"origin_feed\": [],\n" +
                "            \"permission\": 0,\n" +
                "            \"custom\": \"\",\n" +
                "            \"content\": \"挺好的呀啊\",\n" +
                "            \"rich_text\": \"\",\n" +
                "            \"rich_text_url\": \"http://api.wsq.umeng.com/v2/feed/rich_text?feed_id=576394abee785022af4c2b05\",\n" +
                "            \"comments_count\": 0,\n" +
                "            \"location\": null,\n" +
                "            \"parent_feed_id\": \"\",\n" +
                "            \"media_info\": [],\n" +
                "            \"is_recommended\": false,\n" +
                "            \"tag\": 0,\n" +
                "            \"type\": 0,\n" +
                "            \"topics\": [\n" +
                "                {\n" +
                "                    \"create_time\": \"2016-04-19 13:16:12\",\n" +
                "                    \"name\": \"运营讲堂\",\n" +
                "                    \"icon_url\": null,\n" +
                "                    \"description\": null,\n" +
                "                    \"id\": \"5715bf1c55c40008641d6c3d\",\n" +
                "                    \"image_urls\": [],\n" +
                "                    \"custom\": \"\"\n" +
                "                }\n" +
                "            ],\n" +
                "            \"share_link\": \"http://wsq.umeng.com/feeds/576394abee785022af4c2b05/\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        JSONObject js = null;
        try {
            js = new JSONObject(aa);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        FeedsResponse feedsResponse= new FeedsResponse(js);
        verify(request).fetchMyFollowedFeeds((Listeners.FetchListener<FeedsResponse>) feedsResponseArgumentCaptor.capture());



        ( (Listeners.FetchListener<FeedsResponse>)feedsResponseArgumentCaptor.getValue()).onComplete(feedsResponse);




    }



}

配置:

   dependencies {

        compile project(':umeng_community_library_simplify')
//            {
//            exclude(module:'umeng_community_core')
//        }
        compile project(':umeng_community_library_db')


        // Dependencies for local unit tests
        testCompile "junit:junit:4.12"
        testCompile "org.mockito:mockito-all:1.10.19"
        testCompile "org.hamcrest:hamcrest-all:1.3"

        //Dependencies for Anroid unit tests
        androidTestCompile ('com.android.support.test:runner:0.4.1')
        androidTestCompile ('com.android.support.test:rules:0.4.1'){
            exclude group:'com.android.support.test',module:'runner'
        }


        // Dependencies for Android unit tests
        //androidTestCompile "junit:junit:4.12"
        androidTestCompile "org.mockito:mockito-core:1.10.19"
        androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
        androidTestCompile ('com.google.dexmaker:dexmaker-mockito:1.2'){
            exclude group:'org.mockito:',module:'mockito-core'
        }

        //Espresso UI Testing
        androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.1'){
            exclude group:'com.android.support.test',module:'runner'
            exclude group:'com.android.support.test',module:'rules'
           // exclude group:'org.hamcrest:',module:'hamcrest-library'
        }
        androidTestCompile ("com.android.support.test.espresso:espresso-contrib:2.2.1"){
            exclude group:'com.android.support.test.espresso',module:'espresso-core'
            exclude group:'com.android.support',module:'support-v4'
        }
        androidTestCompile ("com.android.support.test.espresso:espresso-intents:2.2.1"){
            exclude group:'com.android.support.test.espresso',module:'espresso-core'
        }

android.support.test.espresso.PerformException: Error performing 'fast swipe' on view '(with id: com.umeng.community.example:id/umeng_comm_feed_listview and has focus on the screen to the user)'.

at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.umeng.community.example.feedlist.RealTimeFeedListTest.testDispalyDataRightInRealTimeFeedList(RealTimeFeedListTest.java:103)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 4370 iterations over 100 SECONDS. The following Idle Conditions failed .
at android.support.test.espresso.IdlingPolicy.handleTimeout(IdlingPolicy.java:61)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:477)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:408)
at android.support.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:226)
at android.support.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:138)
at android.support.test.espresso.action.Swipe.sendLinearSwipe(Swipe.java:104)
at android.support.test.espresso.action.Swipe.access$100(Swipe.java:31)
at android.support.test.espresso.action.Swipe$1.sendSwipe(Swipe.java:38)
at android.support.test.espresso.action.GeneralSwipeAction.perform(GeneralSwipeAction.java:70)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5432)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值