summaryrefslogtreecommitdiff
path: root/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPowerStatusTest.java
blob: 3ca4ee0e277069390c7405c856b8f7f0a78c28b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hdmicec.cts.common;

import static com.google.common.truth.Truth.assertThat;

import android.hdmicec.cts.BaseHdmiCecCtsTest;
import android.hdmicec.cts.CecMessage;
import android.hdmicec.cts.CecOperand;
import android.hdmicec.cts.HdmiCecConstants;
import android.hdmicec.cts.LogicalAddress;
import android.hdmicec.cts.WakeLockHelper;

import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * HDMI CEC tests verifying power status related messages of the device (CEC 2.0 CTS Section 7.6)
 */
@RunWith(DeviceJUnit4ClassRunner.class)
public final class HdmiCecPowerStatusTest extends BaseHdmiCecCtsTest {

    private static final int ON = 0x0;
    private static final int OFF = 0x1;
    private static final int IN_TRANSITION_TO_STANDBY = 0x3;

    private static final int SLEEP_TIMESTEP_SECONDS = 1;

    @Rule
    public RuleChain mRuleChain =
            RuleChain
                    .outerRule(CecRules.requiresCec(this))
                    .around(CecRules.requiresLeanback(this))
                    .around(hdmiCecClient);

    /**
     * Test HF4-6-20
     *
     * Verifies that {@code <Report Power Status>} message is broadcast when the device transitions
     * from standby to on.
     */
    @Test
    public void cect_hf4_6_20_broadcastsWhenTurningOn() throws Exception {
        ITestDevice device = getDevice();
        setCec20();

        // Move device to standby
        sendDeviceToSleep();

        // Turn device on
        wakeUpDeviceWithoutWait();

        String reportPowerStatus = hdmiCecClient.checkExpectedOutput(LogicalAddress.BROADCAST,
                CecOperand.REPORT_POWER_STATUS);

        if (CecMessage.getParams(reportPowerStatus) == HdmiCecConstants.CEC_POWER_STATUS_STANDBY) {
            // Received the "turning off" broadcast, check for the next broadcast message
            reportPowerStatus = hdmiCecClient.checkExpectedOutput(LogicalAddress.BROADCAST,
                    CecOperand.REPORT_POWER_STATUS);
        }

        assertThat(CecMessage.getParams(reportPowerStatus)).isEqualTo(
                HdmiCecConstants.CEC_POWER_STATUS_ON);
    }

    /**
     * Test HF4-6-21
     *
     * Verifies that {@code <Report Power Status>} message is broadcast when the device transitions
     * from on to standby.
     */
    @Test
    public void cect_hf4_6_21_broadcastsWhenGoingToStandby() throws Exception {
        ITestDevice device = getDevice();
        setCec20();

        try {
            // Turn device on
            wakeUpDevice();

            // Move device to standby
            sendDeviceToSleepWithoutWait();

            String reportPowerStatus =
                    hdmiCecClient.checkExpectedOutput(
                            LogicalAddress.BROADCAST, CecOperand.REPORT_POWER_STATUS);

            if (CecMessage.getParams(reportPowerStatus) == HdmiCecConstants.CEC_POWER_STATUS_ON) {
                // Received the "wake up" broadcast, check for the next broadcast message
                reportPowerStatus =
                        hdmiCecClient.checkExpectedOutput(
                                LogicalAddress.BROADCAST, CecOperand.REPORT_POWER_STATUS);
            }

            assertThat(CecMessage.getParams(reportPowerStatus))
                    .isEqualTo(HdmiCecConstants.CEC_POWER_STATUS_STANDBY);
        } finally {
            wakeUpDevice();
        }
    }

    /**
     * Test HF4-6-22 (CEC 2.0)
     *
     * <p>Verifies that the DUT notifies the transition back to Standby state if an ongoing
     * transition from standby state to power on state is interrupted.
     */
    @Test
    public void cect_hf4_6_22_interruptedPowerOn() throws Exception {
        ITestDevice device = getDevice();
        setCec20();

        try {
            // Turn device off
            sendDeviceToSleep();

            List<Integer> keycodes = new ArrayList<>();
            keycodes.add(HdmiCecConstants.CEC_KEYCODE_POWER_ON_FUNCTION);
            keycodes.add(HdmiCecConstants.CEC_KEYCODE_POWER_OFF_FUNCTION);

            // Send a <UCP>[Power On] immediately followed by a <UCP>[Power Off]
            hdmiCecClient.sendMultipleUserControlPressAndRelease(
                    hdmiCecClient.getSelfDevice(), keycodes);

            String reportPowerStatus =
                    hdmiCecClient.checkExpectedOutput(CecOperand.REPORT_POWER_STATUS);

            switch (CecMessage.getParams(reportPowerStatus)) {
                case HdmiCecConstants.CEC_POWER_STATUS_STANDBY:
                    // No further messages are expected, check for 5s outside the switch case.
                    break;
                case HdmiCecConstants.CEC_POWER_STATUS_IN_TRANSITION_TO_STANDBY:
                    reportPowerStatus =
                            hdmiCecClient.checkExpectedOutput(CecOperand.REPORT_POWER_STATUS);
                    assertThat(CecMessage.getParams(reportPowerStatus))
                            .isEqualTo(HdmiCecConstants.CEC_POWER_STATUS_STANDBY);
                    break;
                case HdmiCecConstants.CEC_POWER_STATUS_IN_TRANSITION_TO_ON:
                case HdmiCecConstants.CEC_POWER_STATUS_ON:
                    reportPowerStatus =
                            hdmiCecClient.checkExpectedOutput(CecOperand.REPORT_POWER_STATUS);
                    int powerState = CecMessage.getParams(reportPowerStatus);
                    if (powerState == HdmiCecConstants.CEC_POWER_STATUS_IN_TRANSITION_TO_STANDBY) {
                        // If it is in transition, wait for another <Report Power Status>[Power Off]
                        reportPowerStatus =
                                hdmiCecClient.checkExpectedOutput(CecOperand.REPORT_POWER_STATUS);
                        powerState = CecMessage.getParams(reportPowerStatus);
                    }
                    // If no <Report Power Status>[Power Off] is received, fail the test
                    assertThat(powerState).isEqualTo(HdmiCecConstants.CEC_POWER_STATUS_STANDBY);
                    break;
            }
            // Make sure there are no further <Report Power Status> for 5s
            hdmiCecClient.checkOutputDoesNotContainMessage(
                    LogicalAddress.BROADCAST, CecOperand.REPORT_POWER_STATUS, 5000);
        } finally {
            wakeUpDevice();
        }
    }

    /**
     * Test HF4-6-23 (CEC 2.0)
     *
     * <p>Verifies that the DUT notifies the transition back to On state if an ongoing transition
     * from On state to Standby state is interrupted.
     */
    @Test
    public void cect_hf4_6_23_interruptedStandby() throws Exception {
        ITestDevice device = getDevice();
        setCec20();

        try {
            // Turn device off
            wakeUpDevice();
            WakeLockHelper.acquirePartialWakeLock(getDevice());

            List<Integer> keycodes = new ArrayList<>();
            keycodes.add(HdmiCecConstants.CEC_KEYCODE_POWER_OFF_FUNCTION);
            keycodes.add(HdmiCecConstants.CEC_KEYCODE_POWER_ON_FUNCTION);

            // Send a <UCP>[Power Off] immediately followed by a <UCP>[Power On]
            hdmiCecClient.sendMultipleUserControlPressAndRelease(
                    hdmiCecClient.getSelfDevice(), keycodes);

            String reportPowerStatus =
                    hdmiCecClient.checkExpectedOutput(CecOperand.REPORT_POWER_STATUS);

            switch (CecMessage.getParams(reportPowerStatus)) {
                case HdmiCecConstants.CEC_POWER_STATUS_ON:
                    // No further messages are expected, check for 5s outside the switch case.
                    break;
                case HdmiCecConstants.CEC_POWER_STATUS_IN_TRANSITION_TO_ON:
                    reportPowerStatus =
                            hdmiCecClient.checkExpectedOutput(CecOperand.REPORT_POWER_STATUS);
                    assertThat(CecMessage.getParams(reportPowerStatus))
                            .isEqualTo(HdmiCecConstants.CEC_POWER_STATUS_ON);
                    break;
                case HdmiCecConstants.CEC_POWER_STATUS_IN_TRANSITION_TO_STANDBY:
                case HdmiCecConstants.CEC_POWER_STATUS_STANDBY:
                    reportPowerStatus =
                            hdmiCecClient.checkExpectedOutput(CecOperand.REPORT_POWER_STATUS);
                    int powerState = CecMessage.getParams(reportPowerStatus);
                    if (powerState == HdmiCecConstants.CEC_POWER_STATUS_IN_TRANSITION_TO_ON) {
                        // If it is in transition, wait for another <Report Power Status>[Power On]
                        reportPowerStatus =
                                hdmiCecClient.checkExpectedOutput(CecOperand.REPORT_POWER_STATUS);
                        powerState = CecMessage.getParams(reportPowerStatus);
                    }
                    // If no <Report Power Status>[Power On] is received, fail the test
                    assertThat(powerState).isEqualTo(HdmiCecConstants.CEC_POWER_STATUS_ON);
                    break;
            }
            // Make sure there are no further <Report Power Status> for 5s
            hdmiCecClient.checkOutputDoesNotContainMessage(
                    LogicalAddress.BROADCAST, CecOperand.REPORT_POWER_STATUS, 5000);
        } finally {
            wakeUpDevice();
        }
    }

    /**
     * Test 11.1.14-1, 11.2.14-1
     *
     * <p>Tests that the device sends a {@code <REPORT_POWER_STATUS>} with params 0x0 when the
     * device is powered on.
     */
    @Test
    public void cect_PowerStatusWhenOn() throws Exception {
        ITestDevice device = getDevice();
        /* Make sure the device is not booting up/in standby */
        device.waitForBootComplete(HdmiCecConstants.REBOOT_TIMEOUT);
        wakeUpDevice();

        LogicalAddress cecClientDevice = hdmiCecClient.getSelfDevice();
        hdmiCecClient.sendCecMessage(cecClientDevice, CecOperand.GIVE_POWER_STATUS);
        String message =
                hdmiCecClient.checkExpectedOutput(cecClientDevice, CecOperand.REPORT_POWER_STATUS);
        assertThat(CecMessage.getParams(message)).isEqualTo(ON);
    }

    /**
     * Test 11.2.14-1, 11.2.14-2
     *
     * <p>Tests that the device sends a {@code <REPORT_POWER_STATUS>} with params 0x1 when the
     * device is powered off.
     */
    @Test
    public void cect_PowerStatusWhenOff() throws Exception {
        ITestDevice device = getDevice();
        try {
            /* Make sure the device is not booting up/in standby */
            device.waitForBootComplete(HdmiCecConstants.REBOOT_TIMEOUT);
            /* The sleep below could send some devices into a deep suspend state. */
            sendDeviceToSleep();
            int waitTimeSeconds = HdmiCecConstants.DEVICE_WAIT_TIME_SECONDS;
            int powerStatus;
            LogicalAddress cecClientDevice = hdmiCecClient.getSelfDevice();
            do {
                TimeUnit.SECONDS.sleep(SLEEP_TIMESTEP_SECONDS);
                waitTimeSeconds += SLEEP_TIMESTEP_SECONDS;
                hdmiCecClient.sendCecMessage(cecClientDevice, CecOperand.GIVE_POWER_STATUS);
                powerStatus =
                        CecMessage.getParams(
                                hdmiCecClient.checkExpectedOutput(
                                        cecClientDevice, CecOperand.REPORT_POWER_STATUS));
            } while (powerStatus == IN_TRANSITION_TO_STANDBY &&
                    waitTimeSeconds <= HdmiCecConstants.MAX_SLEEP_TIME_SECONDS);
            assertThat(powerStatus).isEqualTo(OFF);
        } finally {
            /* Wake up the device */
            wakeUpDevice();
        }
    }

    /**
     * Test HF4-6-8
     *
     * Tests that a device comes out of the standby state when it receives a {@code <User Control
     * Pressed>} message with power related operands.
     */
    @Test
    public void cect_hf4_6_8_userControlPressed_powerOn() throws Exception {
        ITestDevice device = getDevice();
        List<Integer> powerControlOperands = Arrays.asList(HdmiCecConstants.CEC_KEYCODE_POWER,
                HdmiCecConstants.CEC_KEYCODE_POWER_ON_FUNCTION,
                HdmiCecConstants.CEC_KEYCODE_POWER_TOGGLE_FUNCTION);

        LogicalAddress source = hasDeviceType(HdmiCecConstants.CEC_DEVICE_TYPE_TV)
                ? LogicalAddress.PLAYBACK_1
                : LogicalAddress.TV;

        for (Integer operand : powerControlOperands) {
            try {
                sendDeviceToSleep();
                hdmiCecClient.sendUserControlPressAndRelease(source, operand, false);
                TimeUnit.SECONDS.sleep(HdmiCecConstants.DEVICE_WAIT_TIME_SECONDS);
                assertDeviceWakefulness(HdmiCecConstants.WAKEFULNESS_AWAKE);
            } finally {
                wakeUpDevice();
            }
        }
    }

    /**
     * Test HF4-6-10
     *
     * Tests that a device comes enters the standby state when it receives a {@code <User Control
     * Pressed>} message with power related operands.
     */
    @Test
    public void cect_hf4_6_10_userControlPressed_powerOff() throws Exception {
        ITestDevice device = getDevice();
        List<Integer> powerControlOperands = Arrays.asList(
                HdmiCecConstants.CEC_KEYCODE_POWER_OFF_FUNCTION,
                HdmiCecConstants.CEC_KEYCODE_POWER_TOGGLE_FUNCTION);

        LogicalAddress source = hasDeviceType(HdmiCecConstants.CEC_DEVICE_TYPE_TV)
                ? LogicalAddress.PLAYBACK_1
                : LogicalAddress.TV;

        for (Integer operand : powerControlOperands) {
            try {
                wakeUpDevice();
                WakeLockHelper.acquirePartialWakeLock(device);
                hdmiCecClient.sendUserControlPressAndRelease(source, operand, false);
                TimeUnit.SECONDS.sleep(HdmiCecConstants.DEVICE_WAIT_TIME_SECONDS);
                assertDeviceWakefulness(HdmiCecConstants.WAKEFULNESS_ASLEEP);
            } finally {
                wakeUpDevice();
            }
        }
    }

    /**
     * Test HF4-6-26
     *
     * <p> Verify that, when a Source device is put to Standby by the user, it does not broadcast a
     * system {@code <Standby>} message unless explicitly requested by the user.
     */
    @Test
    public void cect_hf4_6_26_standby_noBroadcast_20() throws Exception {
        ITestDevice device = getDevice();
        setCec20();
        String previousPowerControlMode =
                setPowerControlMode(HdmiCecConstants.POWER_CONTROL_MODE_NONE);
        try {
            sendDeviceToSleep();
            hdmiCecClient.checkOutputDoesNotContainMessage(
                    LogicalAddress.BROADCAST, CecOperand.STANDBY);
        } finally {
            wakeUpDevice();
            setPowerControlMode(previousPowerControlMode);
        }
    }

    /**
     * Test HF4-6-27 (CEC 2.0)
     *
     * <p>Verify that action of a {@code <Standby>} message can only be used to send a device to
     * the standby state.
     */
    @Test
    public void cect_hf4_6_27_standby_action_20() throws Exception {
        ITestDevice device = getDevice();
        /* Make sure the device is not booting up/in standby */
        device.waitForBootComplete(HdmiCecConstants.REBOOT_TIMEOUT);
        setCec20();
        try {
            // Directly addressed standby message to DUT.
            sendDeviceToSleepAndValidateUsingStandbyMessage(true);
            // Resending Standby message and validate device stays in standby mode
            sendDeviceToSleepAndValidateUsingStandbyMessage(true);
            wakeUpDevice();

            // Broadcast standby message.
            sendDeviceToSleepAndValidateUsingStandbyMessage(false);
            // Resending Standby message and validate device stays in standby mode
            sendDeviceToSleepAndValidateUsingStandbyMessage(false);
        } finally {
            wakeUpDevice();
        }
    }

    /*
     * Test HF4-6-28
     *
     * <p>Tests that the DUT handles {@code <User Control Pressed>} of any power related buttons
     * correctly
     */
    @Test
    public void cect_hf_4_6_28_testPowerUcpHandling() throws Exception {
        setCec20();
        int waitSeconds = 10;
        ITestDevice device = getDevice();
        // Ensure device is awake.
        wakeUpDevice();

        // Acquire the wakelock.
        WakeLockHelper.acquirePartialWakeLock(device);
        try {
            // All <UCP> commands will be sent from cec client device.
            LogicalAddress source = hdmiCecClient.getSelfDevice();
            hdmiCecClient.sendUserControlPressAndRelease(
                    source, HdmiCecConstants.CEC_KEYCODE_POWER_TOGGLE_FUNCTION, false);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_STANDBY);

            // Toggle power again, DUT should wakeup.
            hdmiCecClient.sendUserControlPressAndRelease(
                    source, HdmiCecConstants.CEC_KEYCODE_POWER_TOGGLE_FUNCTION, false);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_ON);

            // Send <UCP>[Power On]. DUT should remain in ON state, check for 10s.
            hdmiCecClient.sendUserControlPressAndRelease(
                    source, HdmiCecConstants.CEC_KEYCODE_POWER_ON_FUNCTION, false);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_ON);
            TimeUnit.SECONDS.sleep(waitSeconds);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_ON);

            // Send <UCP>[Power Off]. DUT should got to OFF state, check for 10s.
            hdmiCecClient.sendUserControlPressAndRelease(
                    source, HdmiCecConstants.CEC_KEYCODE_POWER_OFF_FUNCTION, false);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_STANDBY);
            TimeUnit.SECONDS.sleep(waitSeconds);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_STANDBY);

            // Send <UCP>[Power Off] again. DUT should stay in OFF state, check for 10s.
            hdmiCecClient.sendUserControlPressAndRelease(
                    source, HdmiCecConstants.CEC_KEYCODE_POWER_OFF_FUNCTION, false);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_STANDBY);
            TimeUnit.SECONDS.sleep(waitSeconds);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_STANDBY);

            // Send <UCP>[Power On]. DUT should go to ON state, check for 10s.
            hdmiCecClient.sendUserControlPressAndRelease(
                    source, HdmiCecConstants.CEC_KEYCODE_POWER_ON_FUNCTION, false);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_ON);
            TimeUnit.SECONDS.sleep(waitSeconds);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_ON);

            // Send <UCP> [Power]. DUT should remain in ON state.
            hdmiCecClient.sendUserControlPressAndRelease(
                    source, HdmiCecConstants.CEC_KEYCODE_POWER, false);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_ON);

            sendDeviceToSleep();

            // Send <UCP> [Power]. DUT should wakeup.
            hdmiCecClient.sendUserControlPressAndRelease(
                    source, HdmiCecConstants.CEC_KEYCODE_POWER, false);
            waitForTransitionTo(HdmiCecConstants.CEC_POWER_STATUS_ON);
        } finally {
            // Wake up the device. This will also release the wakelock.
            wakeUpDevice();
        }
    }
}