1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/* 2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project 3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License"); 5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License. 6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at 7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * http://www.apache.org/licenses/LICENSE-2.0 9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software 11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS, 12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and 14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License. 15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging; 18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.appwidget.AppWidgetManager; 20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.ComponentName; 21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context; 22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.PowerManager; 23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.test.InstrumentationTestCase; 24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil; 26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.widget.BugleWidgetProvider; 27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.widget.WidgetConversationProvider; 28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport junit.framework.TestCase; 30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport org.mockito.MockitoAnnotations; 32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/** 34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Helpers that can be called from all test base classes to 'reset' state and prevent as much as 35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * possible having side effects leak from one test to another. 36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class TestUtil { 38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static void testSetup(final Context context, final TestCase testCase) { 39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd haltIfTestsAreNotAbleToRun(context); 40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Workaround to get mockito to work. 42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // See https://code.google.com/p/dexmaker/issues/detail?id=2. TODO: Apparently 43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // solvable by using a different runner. 44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd System.setProperty("dexmaker.dexcache", 45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd context.getCacheDir().getPath()); 46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Initialize @Mock objects. 48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd MockitoAnnotations.initMocks(testCase); 49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Tests have to explicitly override this 51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd Factory.setInstance(null); 52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static void testTeardown(final TestCase testCase) { 55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (testCase instanceof InstrumentationTestCase) { 56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Make sure the test case is finished running or we'll get NPEs when accessing 57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Fragment.get() 58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd ((InstrumentationTestCase) testCase).getInstrumentation().waitForIdleSync(); 59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd Factory.setInstance(null); 61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private static void haltIfTestsAreNotAbleToRun(final Context context) { 64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (!pm.isScreenOn()) { 66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Ideally we could turn it on for you using the WindowManager, but we currently run 67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // the tests independently of the activity life cycle. 68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.wtf(LogUtil.BUGLE_TAG, "You need to turn on your screen to run tests!"); 69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int [] conversationWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, 73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd WidgetConversationProvider.class)); 74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int [] conversationListWidgetIds = appWidgetManager.getAppWidgetIds(new 75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd ComponentName(context, BugleWidgetProvider.class)); 76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if ((conversationWidgetIds.length > 0) || (conversationListWidgetIds.length > 0)) { 78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Currently widgets asynchronously access our content providers and singletons which 79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // interacts badly with our test setup and tear down. 80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.wtf(LogUtil.BUGLE_TAG, "You currently can't reliably run unit tests" + 81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd " with a Messaging widget on your desktop!"); 82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd} 85