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.util;
18 
19 import android.content.pm.ResolveInfo;
20 import android.content.pm.ServiceInfo;
21 import android.media.tv.TvInputInfo;
22 import android.os.Build;
23 import android.support.v4.os.BuildCompat;
24 
25 import java.lang.reflect.Constructor;
26 
27 /**
28  * A class that includes convenience methods for testing.
29  */
30 public class TestUtils {
createTvInputInfo(ResolveInfo service, String id, String parentId, int type, boolean isHardwareInput)31     public static TvInputInfo createTvInputInfo(ResolveInfo service, String id, String parentId,
32             int type, boolean isHardwareInput) throws Exception {
33         // Create a mock TvInputInfo by using private constructor
34         // TODO: Find better way to mock TvInputInfo.
35         // Note that mockito doesn't support mock/spy on final object.
36         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
37             return createTvInputInfoForLmp(service, id, parentId, type);
38         } else if (BuildCompat.isAtLeastN()) {
39             new RuntimeException("TOOD(dvr): implement");  // http://b/26903987
40         }
41         return createTvInputInfoForMnc(service, id, parentId, type, isHardwareInput);
42     }
43 
createTvInputInfoForLmp(ResolveInfo service, String id, String parentId, int type)44     private static TvInputInfo createTvInputInfoForLmp(ResolveInfo service, String id,
45             String parentId, int type) throws Exception {
46         Constructor<TvInputInfo> constructor = TvInputInfo.class.getDeclaredConstructor(new Class[]{
47                 ResolveInfo.class, String.class, String.class, int.class});
48         constructor.setAccessible(true);
49         return constructor.newInstance(service, id, parentId, type);
50     }
51 
createTvInputInfoForMnc(ResolveInfo service, String id, String parentId, int type, boolean isHardwareInput)52     private static TvInputInfo createTvInputInfoForMnc(ResolveInfo service, String id,
53             String parentId, int type, boolean isHardwareInput) throws Exception {
54         Constructor<TvInputInfo> constructor = TvInputInfo.class.getDeclaredConstructor(new Class[]{
55                 ResolveInfo.class, String.class, String.class, int.class, boolean.class});
56         constructor.setAccessible(true);
57         return constructor.newInstance(service, id, parentId, type, isHardwareInput);
58     }
59 
createTvInputInfoForNpreview(ResolveInfo service, String id, String parentId, int type)60     private static TvInputInfo createTvInputInfoForNpreview(ResolveInfo service, String id,
61             String parentId, int type) throws Exception {
62         Constructor<TvInputInfo> constructor = TvInputInfo.class.getDeclaredConstructor(
63                 new Class[]{ResolveInfo.class, String.class, String.class, int.class});
64         constructor.setAccessible(true);
65         return constructor.newInstance(service, id, parentId, type);
66     }
67 
createResolveInfo(String packageName, String name)68     public static ResolveInfo createResolveInfo(String packageName, String name) {
69         ResolveInfo resolveInfo = new ResolveInfo();
70         resolveInfo.serviceInfo = new ServiceInfo();
71         resolveInfo.serviceInfo.packageName = packageName;
72         resolveInfo.serviceInfo.name = name;
73         return resolveInfo;
74     }
75 }
76