4

我正在尝试编写依赖于使 TCP/IP 网络连接到外部应用程序以成功通过的组件的 Espresso 单元测试。

测试失败的原因是 TCP/IP 网络花费的时间超过了 Espresso 允许的时间......

因此,我们需要让 TCP/IP 代码类 TCPConnectionTask 实现 IdlingResource:

但是,我得到了这个例外:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.app.Activity.<init>(Activity.java:786)
at com.sample.QuicksetSampleActivity.<init>(QuicksetSampleActivity.java:82)
at com.unitTests.QuicksetSampleActivityTest.<init>(QuicksetSampleActivityTest.java:52)

我附上了TCPConnectionTask并调用了Looper.prepare()& 也尝试Looper.prepareMainLooper()了,但没有成功,见下文(TCPConnectionTask)

/**
     * Async task to connect to create TCPIPDataComm and connect to external IRB.
     *
     */
    public class TCPConnectionTask extends AsyncTask<String, Void, Void > implements IdlingResource {

        String ip_user = null;
        int port_user;
        private ResourceCallback callback;
        private boolean flag = false;
        protected Void doInBackground(String... args) {
            try {

                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {

                                Looper.prepare();
                                //Looper.prepareMainLooper();

                                flag = true;
                                TCPIPDataComm tcp = new TCPIPDataComm(ip_user, port_user);
                                if(tcp != null){
                                    tcp.open();
                                    _TCPDataComm = tcp;

                                    // we can enable the DataComm interface for simulation in UI app
                                    int resultCode = 0;
                                    try {
                                        resultCode = QuicksetSampleApplication.getSetup().setDataCommInfo(
                                                getAuthKey(), _TCPDataComm.getHostName(),
                                                _TCPDataComm.getPortNumber());

                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    finally {
                                        //task completed
                                        flag = false;
                                    }
                                    Log.d(QuicksetSampleActivity.LOGTAG,
                                            "Setting DataComm Result = "
                                                    + resultCode
                                                    + " - "
                                                    + ResultCode
                                                    .getString(resultCode));
                                }

                            }
                        }
                );


            } catch (Exception e) {
                e.printStackTrace();

            }
            return null;
        }
        public void setInfo(String ipValue, int portNumber)
        {
            ip_user = ipValue;
            port_user = portNumber;
        }

        @Override
        public String getName() {
            return this.getClass().getName().toString();
        }

        @Override public boolean isIdleNow() {
            if (flag && callback != null) {
                callback.onTransitionToIdle();
            }
            return flag;
        }

        @Override public void registerIdleTransitionCallback(ResourceCallback callback) {
            this.callback = callback;
        }
    }

以下是单元测试类的相关片段QuicksetSampleActivityTest

@RunWith(AndroidJUnit4.class)
public class QuicksetSampleActivityTest  extends ActivityInstrumentationTestCase2<QuicksetSampleActivity> {

    private QuicksetSampleActivity newQuicksetSampleActivity = null;
    private final String ip = "192.168.43.139";
    private final int port = 9999;
    private final int timeOutTime = 1000;

    //This is the idling resource that takes time to complete due to network latency...
    private QuicksetSampleActivity.TCPConnectionTask taskIdlingResource = null;

    //const
    public QuicksetSampleActivityTest() {

        super(QuicksetSampleActivity.class);

        //instantiation of idling resource that is used for TCP connection 
        taskIdlingResource = new QuicksetSampleActivity().new TCPConnectionTask();
    }

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

        //open activity
        newQuicksetSampleActivity = getActivity();
        // Make sure Espresso does not time out
        IdlingPolicies.setMasterPolicyTimeout(timeOutTime * 10, TimeUnit.MILLISECONDS);
        IdlingPolicies.setIdlingResourceTimeout(timeOutTime * 10, TimeUnit.MILLISECONDS);
        //register idling resource
        Espresso.registerIdlingResources(taskIdlingResource);

    }


    @After
    public void unregisterIntentServiceIdlingResource() {
        //unregister idling resource
        Espresso.unregisterIdlingResources(taskIdlingResource);
    }

    //The EditText GUI with the port & Ip was noe found using espresso, we need to set teh ip & port programmatically
    public void setIpandPortToPcBridge() {
        // Use TCPCommunicatuonTask interface
        taskIdlingResource.setInfo(ip, port);
        taskIdlingResource.execute();
    }


    //after TCP connection is made and/or tested
    @Test
    public void testActionBarMenuItemsIrDevicesAfterTCPConnectionFunctions() {

        //we were not able to find the IP & Port fields so set them programmatically
        setIpandPortToPcBridge();
        //open action bar menu
        Espresso.openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
        //test IR Devices/Functions menu item
        Espresso.onData(Matchers.allOf(Matchers.instanceOf(MenuItem.class), MatcherUtility.menuItemWithTitle("IR Devices/Functions"))).perform(ViewActions.click());
        //add new device will connect the app
        Espresso.onView(ViewMatchers.withId(R.id.btAdd)).perform(ViewActions.click());

        //DeviceFunctionsActivity is rendered
        Espresso.onView(ViewMatchers.withText("IR Devices")).check(ViewAssertions.matches(ViewMatchers.withText("IR Devices")));
        //find the 3 required buttons for this UI
        //test START learning
        //Espresso.onView(ViewMatchers.withText("Start")).check(ViewAssertions.matches(ViewMatchers.withText("Start")));
        //click
        //test CANCEL learning
        //test TEST Learned IR
        //Espresso.onView(ViewMatchers.withText("Test Learned IR")).check(ViewAssertions.matches(ViewMatchers.withText("Test Learned IR")));
        //click
        //test Delete Learn Code
        // Espresso.onView(ViewMatchers.withText("Delete Learn Code")).check(ViewAssertions.matches(ViewMatchers.withText("Delete Learn Code")));
        //click
        //go back
        //ViewActions.pressBack();

    }
}
}   

如何解决此异常并IdlingResource成功运行 Espresso?

4

2 回答 2

12

尝试

 getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {

         // Your testActionBarMenuItemsIrDevicesAfterTCPConnectionFunctions() test body

        }
    });
于 2016-12-23T12:16:58.850 回答
0

ActivityTestRule 的使用示例:

 getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mMusicPlayerActivityTestRule.getActivity()
                        .getSupportMediaController().registerCallback(
                        new MediaControllerCompat.Callback() {
                            @Override
                            public void onPlaybackStateChanged(PlaybackStateCompat state) {
                                super.onPlaybackStateChanged(state);
                                if (state.getState() == STATE_PLAYING) {
                                    countDownLatch.countDown();
                                }
                            }
                        });
            }});
于 2020-09-10T11:21:35.677 回答