• 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 com.android.messaging;
18 
19 import android.appwidget.AppWidgetManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.os.PowerManager;
23 import android.test.InstrumentationTestCase;
24 
25 import com.android.messaging.util.LogUtil;
26 import com.android.messaging.widget.BugleWidgetProvider;
27 import com.android.messaging.widget.WidgetConversationProvider;
28 
29 import junit.framework.TestCase;
30 
31 import org.mockito.MockitoAnnotations;
32 
33 /**
34  * Helpers that can be called from all test base classes to 'reset' state and prevent as much as
35  * possible having side effects leak from one test to another.
36  */
37 public class TestUtil {
testSetup(final Context context, final TestCase testCase)38     public static void testSetup(final Context context, final TestCase testCase) {
39         haltIfTestsAreNotAbleToRun(context);
40 
41         // Workaround to get mockito to work.
42         // See https://code.google.com/p/dexmaker/issues/detail?id=2. TODO: Apparently
43         // solvable by using a different runner.
44         System.setProperty("dexmaker.dexcache",
45                 context.getCacheDir().getPath());
46 
47         // Initialize @Mock objects.
48         MockitoAnnotations.initMocks(testCase);
49 
50         // Tests have to explicitly override this
51         Factory.setInstance(null);
52     }
53 
testTeardown(final TestCase testCase)54     public static void testTeardown(final TestCase testCase) {
55         if (testCase instanceof InstrumentationTestCase) {
56             // Make sure the test case is finished running or we'll get NPEs when accessing
57             // Fragment.get()
58             ((InstrumentationTestCase) testCase).getInstrumentation().waitForIdleSync();
59         }
60         Factory.setInstance(null);
61     }
62 
haltIfTestsAreNotAbleToRun(final Context context)63     private static void haltIfTestsAreNotAbleToRun(final Context context) {
64         final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
65         if (!pm.isScreenOn()) {
66             // Ideally we could turn it on for you using the WindowManager, but we currently run
67             // the tests independently of the activity life cycle.
68             LogUtil.wtf(LogUtil.BUGLE_TAG, "You need to turn on your screen to run tests!");
69         }
70 
71         final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
72         int [] conversationWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
73                 WidgetConversationProvider.class));
74         int [] conversationListWidgetIds = appWidgetManager.getAppWidgetIds(new
75                 ComponentName(context, BugleWidgetProvider.class));
76 
77         if ((conversationWidgetIds.length > 0) || (conversationListWidgetIds.length > 0)) {
78             // Currently widgets asynchronously access our content providers and singletons which
79             // interacts badly with our test setup and tear down.
80             LogUtil.wtf(LogUtil.BUGLE_TAG, "You currently can't reliably run unit tests" +
81                     " with a Messaging widget on your desktop!");
82         }
83     }
84 }
85