久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Android Espresso:按住按鈕時進行斷言

Android Espresso: Make assertion while button is kept pressed(Android Espresso:按住按鈕時進行斷言)
本文介紹了Android Espresso:按住按鈕時進行斷言的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我對 Android 上的 Espresso 很陌生,我遇到了以下問題:我希望 Espresso 在按鈕上執行長按(或其他操作),并且在按住按鈕的同時,我想檢查不同視圖的狀態.

I'm quite new to Espresso on Android and I am running into the following problem: I want Espresso to perform a longclick(or something..) on a button, and while the button is kept pressed down, I want to check the state of a different View.

在(或多或少)偽代碼中:

In (more or less) pseudocode:

onView(withId(button_id)).perform(pressButtonDown());
onView(withId(textBox_id)).check(matches(withText("Button is pressed")));
onView(withId(button_id)).perform(releaseButton());

我嘗試使用 MotionEvents.sendDown() 和 .sendUp() 編寫 2 個自定義 Taps,PRESS 和 RELEASE,但沒有成功.如果這是正確的路徑,我可以發布到目前為止的代碼.

I tried writing 2 custom Taps, PRESS and RELEASE, with MotionEvents.sendDown() and .sendUp(), but did not get it to work. If this is the right path, I can post the code I've got so far.

public class PressViewActions
{
    public static ViewAction pressDown(){
        return new GeneralClickAction(HoldTap.DOWN, GeneralLocation.CENTER, Press.THUMB);
    }

    public static ViewAction release() {
        return new GeneralClickAction(HoldTap.UP, GeneralLocation.CENTER, Press.THUMB);
    }
}

以及 Tappers 的代碼:

And the code for the Tappers:

public enum HoldTap implements Tapper {

    DOWN {
        @Override
        public Tapper.Status sendTap(UiController uiController, float[] coordinates, float[] precision)
        {
            checkNotNull(uiController);
            checkNotNull(coordinates);
            checkNotNull(precision);

            DownResultHolder res = MotionEvents.sendDown(uiController, coordinates, precision);

            ResultHolder.setController(uiController);
            ResultHolder.setResult(res);

            return Status.SUCCESS;
        }
    },

    UP{
        @Override
        public Tapper.Status sendTap(UiController uiController, float[] coordinates, float[] precision)
        {
            DownResultHolder res = ResultHolder.getResult();
            UiController controller = ResultHolder.getController();

            try {
                if(!MotionEvents.sendUp(controller, res.down))
                {
                    MotionEvents.sendCancel(controller, res.down);
                    return Status.FAILURE;
                }

            }
            finally {
                //res.down.recycle();
            }

            return Status.SUCCESS;
        }
    }
}

我得到的錯誤如下:

android.support.test.espresso.PerformException: Error performing 'up click' on view 'with id: de.test.app:id/key_ptt'.
...
...
Caused by: java.lang.RuntimeException: Couldn't click at: 281.5,1117.5 precision: 25.0, 25.0 . Tapper: UP coordinate provider: CENTER precision describer: THUMB. Tried 3 times. With Rollback? false

我希望,這會有所幫助.

I hope, this helps.

我們將不勝感激任何幫助和想法!

Any help and ideas will be appreciated!

提前坦克很多!

推薦答案

Android 中的點擊由兩個事件組成,一個向下事件和一個向上/取消事件.如果您想將這兩者分開,則不能進行點擊",因為它們已經包含了兩者.

A tap in Android is made up of two events, a down event and a up/cancel event. If you want to have these two as separate you cannot make "taps" as they already encompass both.

也就是說,你的想法行得通,你只需要使用一個較低級別的 api,即 UiController 以及 MotionEvents 中的輔助方法.但請注意:由于釋放"視圖需要首先持有它,如果您不進行適當的清理,您的測試將相互依賴.

That said, your idea works, you just need to use a lower-level api, namely UiController along with the helper methods in MotionEvents. Be warned though: since "releasing" a view requires first holding on it, your tests will be dependent on each other if you don't do proper clean up.

示例:在一個測試中,您按下一個視圖,您的測試失敗,然后在另一個測試中您在一個您未單擊的視圖上發布:您的第二個測試會通過,而它不應該通過.

Example: in a test you press a View, your test fails, then in another test you release on a view you didn't click: your second test would pass while it shouldn't have.

我在 我的 github 上上傳了一個完整的示例.這里是關鍵點.首先是測試代碼:

I uploaded on my github a complete sample. Here the keypoints. First the test code:

@Before
public void setUp() throws Exception {
    super.setUp();
    injectInstrumentation(InstrumentationRegistry.getInstrumentation());
    getActivity();
}

@After
public void tearDown() throws Exception {
    super.tearDown();
    LowLevelActions.tearDown();
}

@Test
public void testAssertWhilePressed() {
    onView(withId(R.id.button)).perform(pressAndHold());
    onView(withId(R.id.text)).check(matches(withText("Button is held down")));
    onView(withId(R.id.button)).perform(release());
}

然后是 LowLevelActions:

Then LowLevelActions:

public class LowLevelActions {
    static MotionEvent sMotionEventDownHeldView = null;

    public static PressAndHoldAction pressAndHold() {
        return new PressAndHoldAction();
    }

    public static ReleaseAction release() {
        return new ReleaseAction();
    }

    public static void tearDown() {
        sMotionEventDownHeldView = null;
    }

    static class PressAndHoldAction implements ViewAction {
        @Override
        public Matcher<View> getConstraints() {
            return isDisplayingAtLeast(90); // Like GeneralClickAction
        }

        @Override
        public String getDescription() {
            return "Press and hold action";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            if (sMotionEventDownHeldView != null) {
                throw new AssertionError("Only one view can be held at a time");
            }

            float[] precision = Press.FINGER.describePrecision();
            float[] coords = GeneralLocation.CENTER.calculateCoordinates(view);
            sMotionEventDownHeldView = MotionEvents.sendDown(uiController, coords, precision).down;
            // TODO: save view information and make sure release() is on same view
        }
    }

    static class ReleaseAction implements ViewAction {
        @Override
        public Matcher<View> getConstraints() {
            return isDisplayingAtLeast(90);  // Like GeneralClickAction
        }

        @Override
        public String getDescription() {
            return "Release action";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            if (sMotionEventDownHeldView == null) {
                throw new AssertionError("Before calling release(), you must call pressAndHold() on a view");
            }

            float[] coords = GeneralLocation.CENTER.calculateCoordinates(view);
            MotionEvents.sendUp(uiController, sMotionEventDownHeldView, coords);
        }
    }
}

這篇關于Android Espresso:按住按鈕時進行斷言的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 美人の美乳で授乳プレイ | 国产高潮好爽受不了了夜色 | 成人3d动漫一区二区三区91 | www.成人在线视频 | 日本欧美在线 | 亚洲视频免费在线播放 | 国产精品久久av | 一区二区视频在线 | 中文成人在线 | 成人av电影在线观看 | 国产一级大片 | 成人在线免费观看 | 亚洲国产精品一区二区三区 | 国产高清在线精品一区二区三区 | 亚洲日本欧美日韩高观看 | 久久久久国产精品午夜一区 | 成人av播放 | 亚洲日本中文 | 亚洲人成人一区二区在线观看 | 亚洲少妇综合网 | 国产国产精品久久久久 | 91精品国产乱码久久久久久久 | 国产男女精品 | 国产视频线观看永久免费 | a级免费黄色片 | 国产线视频精品免费观看视频 | 国产一区欧美一区 | 91传媒在线播放 | av一区二区三区四区 | 69堂永久69tangcom | 欧美久久久久久 | 伊人春色在线 | 在线视频亚洲 | 婷婷精品| 欧美中文一区 | 免费国产一区二区 | 九九久久在线看 | 视频1区 | 一级片免费视频 | 久久精品欧美一区二区三区不卡 | 日韩欧美操 |