aboutsummaryrefslogtreecommitdiff
path: root/tests/common/src/com/android/tv/testing/Utils.java
diff options
context:
space:
mode:
Diffstat (limited to 'tests/common/src/com/android/tv/testing/Utils.java')
-rw-r--r--tests/common/src/com/android/tv/testing/Utils.java55
1 files changed, 54 insertions, 1 deletions
diff --git a/tests/common/src/com/android/tv/testing/Utils.java b/tests/common/src/com/android/tv/testing/Utils.java
index 33fe4c54..c1a7506f 100644
--- a/tests/common/src/com/android/tv/testing/Utils.java
+++ b/tests/common/src/com/android/tv/testing/Utils.java
@@ -25,11 +25,21 @@ import android.media.tv.TvContentRating;
import android.media.tv.TvInputInfo;
import android.media.tv.TvInputManager;
import android.net.Uri;
+import android.os.Looper;
import android.text.TextUtils;
+import android.util.Log;
+
+import com.android.tv.util.MainThreadExecutor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.Random;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
/**
* An utility class for testing.
@@ -38,7 +48,11 @@ import java.io.OutputStream;
*
* @see com.android.tv.util.Utils#isRunningInTest
*/
-public class Utils {
+public final class Utils {
+ private static final String TAG ="Utils";
+
+ private static final long DEFAULT_RANDOM_SEED = getSeed();
+
public static String getUriStringForResource(Context context, int resId) {
if (resId == 0) {
return "";
@@ -105,5 +119,44 @@ public class Utils {
return ratings.toString();
}
+ /**
+ * Return the Random class which is needed to make random data for testing.
+ * Default seed of the random is today's date.
+ */
+ public static Random createTestRandom() {
+ return new Random(DEFAULT_RANDOM_SEED);
+ }
+
+ /**
+ * Executes a call on the main thread, blocking until it is completed.
+ *
+ * <p>Useful for doing things that are not thread-safe, such as looking at or modifying the view
+ * hierarchy.
+ *
+ * @param runnable The code to run on the main thread.
+ */
+ public static void runOnMainSync(Runnable runnable) {
+ if (Looper.myLooper() == Looper.getMainLooper()) {
+ runnable.run();
+ } else {
+ Future<?> temp = MainThreadExecutor.getInstance().submit(runnable);
+ try {
+ temp.get();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ } catch (ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private static long getSeed() {
+ // Set random seed as the date to track failed test data easily.
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
+ String today = dateFormat.format(new Date());
+ Log.d(TAG, "Today's random seed is " + today);
+ return Long.valueOf(today);
+ }
+
private Utils() {}
}