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