Android: Only the original thread that created a view hierarchy can touch its views - UnitTest

前端 未结 4 1033
别跟我提以往
别跟我提以往 2021-01-13 02:03

I\'m designing UnitTest for little app. This app cout what numerology number You are using birth date. I have a problem, I what to invoke method which check number and put v

4条回答
  •  终归单人心
    2021-01-13 02:36

    getActivity() implementation is as below:

       /**
             * @return The activity under test.
             */
            public T getActivity() {
                if (mActivity == null) {
                    Log.w(TAG, "Activity wasn't created yet");
                }
                return mActivity;
            }
    

    Since when getActivity() returns Activity instance, it means, it must create Activity. When Activity gets created, it must create Activity's View. Activity's View can only be created via UI Thread.

    @Before
        public void setUp() throws Exception {
            mainActivity = intentsTestRule.getActivity(); / *now Activity's view gets created */
            /* Because Activity's view creation happens in UI Thread, so all the test cases, here, are used by UI Thread */
        }
    

    Now, every test cases are inside the memory of UI Thread. Hence, you should explicitly use @UiThreadTest for every test.

提交回复
热议问题