• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 android.support.v7.util;
18 
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Rule;
22 
23 import android.app.Instrumentation;
24 import android.support.annotation.UiThread;
25 import android.support.test.InstrumentationRegistry;
26 import android.support.test.rule.ActivityTestRule;
27 import android.support.v7.widget.TestActivity;
28 import android.test.ActivityInstrumentationTestCase2;
29 
30 abstract public class BaseThreadedTest {
31     @Rule
32     public ActivityTestRule<TestActivity> mActivityRule = new ActivityTestRule<>(
33             TestActivity.class);
34 
setUp()35     public final void setUp() throws Exception{
36         try {
37             getInstrumentation().runOnMainSync(new Runnable() {
38                 @Override
39                 public void run() {
40                     setUpUi();
41                 }
42             });
43         } catch (Throwable throwable) {
44             Assert.fail(throwable.getMessage());
45         }
46     }
47 
getInstrumentation()48     public Instrumentation getInstrumentation() {
49         return InstrumentationRegistry.getInstrumentation();
50     }
51 
runTestOnUiThread(final Runnable test)52     public void runTestOnUiThread(final Runnable test) {
53         final Throwable[] error = new Throwable[1];
54         getInstrumentation().runOnMainSync(new Runnable() {
55             @Override
56             public void run() {
57                 try {
58                     test.run();
59                 } catch (Throwable t) {
60                     error[0] = t;
61                 }
62             }
63         });
64         Assert.assertNull(error[0]);
65     }
66 
67     @UiThread
setUpUi()68     protected abstract void setUpUi();
69 }
70