• 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.tv.testing;
18 
19 import android.content.ComponentName;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.pm.ServiceInfo;
23 import android.content.res.Resources;
24 import android.media.tv.TvContentRating;
25 import android.media.tv.TvInputInfo;
26 import android.media.tv.TvInputManager;
27 import android.net.Uri;
28 import android.os.Looper;
29 import android.text.TextUtils;
30 import android.util.Log;
31 
32 import com.android.tv.common.TvCommonUtils;
33 import com.android.tv.util.MainThreadExecutor;
34 
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.text.SimpleDateFormat;
39 import java.util.Date;
40 import java.util.Locale;
41 import java.util.Random;
42 import java.util.concurrent.ExecutionException;
43 import java.util.concurrent.Future;
44 
45 /**
46  * An utility class for testing.
47  *
48  * <p>This class is also used to check whether TV app is running in tests or not.
49  *
50  * @see TvCommonUtils#isRunningInTest
51  */
52 public final class Utils {
53     private static final String TAG ="Utils";
54 
55     private static final long DEFAULT_RANDOM_SEED = getSeed();
56 
getUriStringForResource(Context context, int resId)57     public static String getUriStringForResource(Context context, int resId) {
58         if (resId == 0) {
59             return "";
60         }
61         Resources res = context.getResources();
62         return new Uri.Builder()
63             .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
64             .authority(res.getResourcePackageName(resId))
65             .path(res.getResourceTypeName(resId))
66             .appendPath(res.getResourceEntryName(resId)).build().toString();
67     }
68 
copy(InputStream is, OutputStream os)69     public static void copy(InputStream is, OutputStream os) throws IOException {
70         byte[] buffer = new byte[1024];
71         int len;
72         while ((len = is.read(buffer)) != -1) {
73             os.write(buffer, 0, len);
74         }
75     }
76 
getServiceNameFromInputId(Context context, String inputId)77     public static String getServiceNameFromInputId(Context context, String inputId) {
78         TvInputManager tim = (TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE);
79         for (TvInputInfo info : tim.getTvInputList()) {
80             if (info.getId().equals(inputId)) {
81                 return info.getServiceInfo().name;
82             }
83         }
84         return null;
85     }
86 
getInputIdFromComponentName(Context context, ComponentName name)87     public static String getInputIdFromComponentName(Context context, ComponentName name) {
88         TvInputManager tim = (TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE);
89         for (TvInputInfo info : tim.getTvInputList()) {
90             ServiceInfo si = info.getServiceInfo();
91             if (new ComponentName(si.packageName, si.name).equals(name)) {
92                 return info.getId();
93             }
94         }
95         return null;
96     }
97 
98     /**
99      * Return the Random class which is needed to make random data for testing.
100      * Default seed of the random is today's date.
101      */
createTestRandom()102     public static Random createTestRandom() {
103         return new Random(DEFAULT_RANDOM_SEED);
104     }
105 
106     /**
107      * Executes a call on the main thread, blocking until it is completed.
108      *
109      * <p>Useful for doing things that are not thread-safe, such as looking at or modifying the view
110      * hierarchy.
111      *
112      * @param runnable The code to run on the main thread.
113      */
runOnMainSync(Runnable runnable)114     public static void runOnMainSync(Runnable runnable) {
115         if (Looper.myLooper() == Looper.getMainLooper()) {
116             runnable.run();
117         } else {
118             Future<?> temp = MainThreadExecutor.getInstance().submit(runnable);
119             try {
120                 temp.get();
121             } catch (InterruptedException e) {
122                 throw new RuntimeException(e);
123             } catch (ExecutionException e) {
124                 throw new RuntimeException(e);
125             }
126         }
127     }
128 
getSeed()129     private static long getSeed() {
130         // Set random seed as the date to track failed test data easily.
131         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
132         String today = dateFormat.format(new Date());
133         Log.d(TAG, "Today's random seed is " + today);
134         return Long.valueOf(today);
135     }
136 
Utils()137     private Utils() {}
138 }
139