1 /*
2  * Copyright (C) 2013 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 android.hardware.consumerir.cts;
18 
19 import android.content.Context;
20 import android.hardware.ConsumerIrManager;
21 import android.util.Log;
22 import android.content.pm.PackageManager;
23 import android.os.SystemClock;
24 import android.test.AndroidTestCase;
25 import java.io.IOException;
26 
27 /**
28  * Very basic test, just of the static methods of {@link
29  * android.hardware.ConsumerIrManager}.
30  */
31 public class ConsumerIrTest extends AndroidTestCase {
32     private boolean mHasConsumerIr;
33     private ConsumerIrManager mCIR;
34 
35     @Override
setUp()36     public void setUp() throws Exception {
37         super.setUp();
38 
39         mHasConsumerIr = getContext().getPackageManager().hasSystemFeature(
40                 PackageManager.FEATURE_CONSUMER_IR);
41         if (mHasConsumerIr) {
42             mCIR = (ConsumerIrManager)getContext().getSystemService(
43                     Context.CONSUMER_IR_SERVICE);
44         }
45     }
46 
test_hasIrEmitter()47     public void test_hasIrEmitter() {
48         if (!mHasConsumerIr) {
49             // Skip the test if consumer IR is not present.
50             return;
51         }
52         assertTrue(mCIR.hasIrEmitter());
53     }
54 
test_getCarrierFrequencies()55     public void test_getCarrierFrequencies() {
56         if (!mHasConsumerIr) {
57             // Skip the test if consumer IR is not present.
58             return;
59         }
60 
61         ConsumerIrManager.CarrierFrequencyRange[] freqs = mCIR.getCarrierFrequencies();
62 
63         assertTrue(freqs.length > 0);
64         for (ConsumerIrManager.CarrierFrequencyRange range : freqs) {
65             // Each range must be valid
66             assertTrue(range.getMinFrequency() > 0);
67             assertTrue(range.getMaxFrequency() > 0);
68             assertTrue(range.getMinFrequency() <= range.getMaxFrequency());
69         }
70     }
71 
test_timing()72     public void test_timing() {
73         if (!mHasConsumerIr) {
74             // Skip the test if consumer IR is not present.
75             return;
76         }
77 
78         ConsumerIrManager.CarrierFrequencyRange[] freqs = mCIR.getCarrierFrequencies();
79         // Transmit two seconds for min and max for each frequency range
80         int[] pattern = {11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999};
81         long totalXmitTimeNanos = 0; // get the length of the pattern
82         for (int slice : pattern) {
83             totalXmitTimeNanos += slice * 1000; // add the time in nanoseconds
84         }
85         double margin = 0.5; // max fraction xmit is allowed to be off timing
86 
87         for (ConsumerIrManager.CarrierFrequencyRange range : freqs) {
88             // test min freq
89             long currentTime = SystemClock.elapsedRealtimeNanos();
90             mCIR.transmit(range.getMinFrequency(), pattern);
91             long newTime = SystemClock.elapsedRealtimeNanos();
92             String msg = String.format("Pattern length pattern:%d, actual:%d",
93                     totalXmitTimeNanos, newTime - currentTime);
94             assertTrue(msg, newTime - currentTime >= totalXmitTimeNanos * (1.0 - margin));
95             assertTrue(msg, newTime - currentTime <= totalXmitTimeNanos * (1.0 + margin));
96 
97             // test max freq
98             currentTime = SystemClock.elapsedRealtimeNanos();
99             mCIR.transmit(range.getMaxFrequency(), pattern);
100             newTime = SystemClock.elapsedRealtimeNanos();
101             msg = String.format("Pattern length pattern:%d, actual:%d",
102                     totalXmitTimeNanos, newTime - currentTime);
103             assertTrue(msg, newTime - currentTime >= totalXmitTimeNanos * (1.0 - margin));
104             assertTrue(msg, newTime - currentTime <= totalXmitTimeNanos * (1.0 + margin));
105         }
106     }
107 
test_transmit()108     public void test_transmit() {
109         if (!mHasConsumerIr) {
110             // Skip the test if consumer IR is not present.
111             return;
112         }
113 
114         ConsumerIrManager.CarrierFrequencyRange[] freqs = mCIR.getCarrierFrequencies();
115 
116         int[] pattern = {1901, 4453, 625, 1614, 625, 1588, 625, 1614, 625, 442, 625, 442, 625,
117             468, 625, 442, 625, 494, 572, 1614, 625, 1588, 625, 1614, 625, 494, 572, 442, 651,
118             442, 625, 442, 625, 442, 625, 1614, 625, 1588, 651, 1588, 625, 442, 625, 494, 598,
119             442, 625, 442, 625, 520, 572, 442, 625, 442, 625, 442, 651, 1588, 625, 1614, 625,
120             1588, 625, 1614, 625, 1588, 625, 48958};
121 
122         // just use the first frequency in the range
123         mCIR.transmit(freqs[0].getMinFrequency(), pattern);
124     }
125 }
126