1 /* 2 * Copyright (C) 2010 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.email; 18 19 import com.android.emailcommon.Logging; 20 21 import android.app.KeyguardManager; 22 import android.content.Context; 23 import android.os.PowerManager; 24 import android.test.MoreAsserts; 25 import android.test.suitebuilder.annotation.LargeTest; 26 import android.view.View; 27 import android.view.ViewParent; 28 29 import junit.framework.AssertionFailedError; 30 import junit.framework.TestCase; 31 32 /** 33 * Utility methods used only by tests. 34 */ 35 @LargeTest 36 public class TestUtils extends TestCase /* It tests itself */ { 37 public interface Condition { 38 public boolean isMet(); 39 } 40 41 /** Shortcut to create byte array */ 42 public static byte[] b(int... array) { 43 if (array == null) { 44 return null; 45 } 46 byte[] ret = new byte[array.length]; 47 for (int i = 0; i < ret.length; i++) { 48 ret[i] = (byte) array[i]; 49 } 50 return ret; 51 } 52 53 public void testB() { 54 assertNull(b(null)); 55 MoreAsserts.assertEquals(new byte[] {}, b()); 56 MoreAsserts.assertEquals(new byte[] {1, 2, (byte) 0xff}, b(1, 2, 0xff)); 57 } 58 59 /** 60 * Run {@code runnable} and fails if it doesn't throw a {@code expectedThrowable} or a subclass 61 * of it. 62 */ 63 public static void expectThrowable(Runnable runnable, 64 Class<? extends Throwable> expectedThrowable) { 65 try { 66 runnable.run(); 67 fail("Expected throwable not thrown."); 68 } catch (Throwable th) { 69 if (expectedThrowable.isAssignableFrom(th.getClass())) { 70 return; // Expcted. OK 71 } 72 fail("Cought unexpected throwable " + th.getClass().getName()); 73 } 74 } 75 76 public void testExpectThrowable() { 77 try { 78 expectThrowable(new Runnable() { 79 @Override public void run() { 80 // Throwing no exception 81 } 82 }, Throwable.class); 83 fail(); 84 } catch (AssertionFailedError ok) { 85 } 86 87 try { 88 expectThrowable(new Runnable() { 89 @Override public void run() { 90 // Throw RuntimeException, which is not a subclass of Error. 91 throw new RuntimeException(); 92 } 93 }, Error.class); 94 fail(); 95 } catch (AssertionFailedError ok) { 96 } 97 98 expectThrowable(new Runnable() { 99 @Override public void run() { 100 throw new RuntimeException(); 101 } 102 }, Exception.class); 103 } 104 105 /** 106 * Wait until a {@code Condition} is met. 107 */ 108 public static void waitUntil(Condition condition, int timeoutSeconds) { 109 waitUntil("", condition, timeoutSeconds); 110 } 111 112 /** 113 * Wait until a {@code Condition} is met. 114 */ 115 public static void waitUntil(String message, Condition condition, int timeoutSeconds) { 116 LogUtils.d(Logging.LOG_TAG, message + ": Waiting..."); 117 final long timeout = System.currentTimeMillis() + timeoutSeconds * 1000; 118 while (System.currentTimeMillis() < timeout) { 119 if (condition.isMet()) { 120 return; 121 } 122 try { 123 Thread.sleep(500); 124 } catch (InterruptedException ignore) { 125 } 126 } 127 fail(message + ": Timeout"); 128 } 129 130 public void testWaitUntil() { 131 // Shouldn't fail. 132 waitUntil("message", new Condition() { 133 @Override public boolean isMet() { 134 return true; 135 } 136 }, 1000000); 137 138 expectThrowable(new Runnable() { 139 @Override public void run() { 140 // Condition never meets, should fail. 141 waitUntil("message", new Condition() { 142 @Override public boolean isMet() { 143 return false; 144 } 145 }, 0); 146 } 147 }, AssertionFailedError.class); 148 } 149 150 /** 151 * @return true if the screen is on and not locked; false otherwise, in which case tests that 152 * send key events will fail. 153 */ 154 public static boolean isScreenOnAndNotLocked(Context context) { 155 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 156 if (!pm.isScreenOn()) { 157 return false; 158 } 159 KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); 160 if (km.inKeyguardRestrictedInputMode()) { 161 return false; 162 } 163 return true; 164 } 165 166 public static void assertViewVisible(View v) { 167 if (v == null) { 168 throw new NullPointerException(); 169 } 170 for (;;) { 171 assertTrue("visibility for " + v, View.VISIBLE == v.getVisibility()); 172 ViewParent parent = v.getParent(); 173 if (parent == null || !(parent instanceof View)) { 174 break; 175 } 176 v = (View) parent; 177 } 178 } 179 } 180