summaryrefslogtreecommitdiff
path: root/tests/tests/mediacujtest/common/src/android/media/cujcommon/cts/DeviceLockTestPlayerListener.java
blob: ceb7ecd88d27cd9502c2b15c17f9c3d5f5c6f1d0 (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
/**
 * Copyright (C) 2024 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.media.cujcommon.cts;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.os.Looper;
import android.server.wm.UiDeviceUtils;
import android.view.Display;

import androidx.annotation.NonNull;
import androidx.media3.common.Player;

import java.util.Timer;
import java.util.TimerTask;

public class DeviceLockTestPlayerListener extends PlayerListener {

  private static final int LOCK_DURATION_MS = 5000;
  private static final int DELAY_MS = 2000;

  private final boolean mIsAudioOnlyClip;

  private Display mDisplay;
  private boolean mIsPlayerPlaying;

  public DeviceLockTestPlayerListener(long sendMessagePosition, boolean isAudioOnlyClip) {
    super();
    this.mSendMessagePosition = sendMessagePosition;
    this.mIsAudioOnlyClip = isAudioOnlyClip;
  }

  @Override
  public void onIsPlayingChanged(boolean isPlaying) {
    super.onIsPlayingChanged(isPlaying);
    mIsPlayerPlaying = isPlaying;
  }

  @Override
  public TestType getTestType() {
    return TestType.DEVICE_LOCK_TEST;
  }

  @Override
  public void onEventsPlaybackStateChanged(@NonNull Player player) {
    if (player.getPlaybackState() == Player.STATE_READY) {
      // At the first media transition player is not ready. So, add duration of
      // first clip when player is ready
      mExpectedTotalTime += player.getDuration() + LOCK_DURATION_MS;
      // Register the screen receiver to listen for screen on and off events
      mDisplay = mActivity.getDisplay();
    }
  }

  @Override
  public void onEventsMediaItemTransition(@NonNull Player player) {
    mActivity.mPlayer.createMessage((messageType, payload) -> {
          // Lock the device
          UiDeviceUtils.pressSleepButton();
          // Unlock the device after LOCK_DURATION
          Timer timer = new Timer();
          timer.schedule(new Task(), LOCK_DURATION_MS);
        }).setLooper(Looper.getMainLooper()).setPosition(mSendMessagePosition)
        .setDeleteAfterDelivery(true)
        .send();
    mActivity.mPlayer.createMessage((messageType, payload) -> {
          // Verify that the screen is on and player is playing while device is in unlocked state
          assertTrue(isDisplayOn());
          assertTrue(mIsPlayerPlaying);
        }).setLooper(Looper.getMainLooper())
        .setPosition(mIsAudioOnlyClip ? (mSendMessagePosition + LOCK_DURATION_MS + DELAY_MS)
            : (mSendMessagePosition + DELAY_MS))
        .setDeleteAfterDelivery(true)
        .send();
  }

  private boolean isDisplayOn() {
    return mDisplay != null && mDisplay.getState() == Display.STATE_ON;
  }

  class Task extends TimerTask {

    @Override
    public void run() {
      unlockPhone();
    }

    private void unlockPhone() {
      // Verify that the screen is off when device is in locked state
      assertFalse(isDisplayOn());
      if (mIsAudioOnlyClip) {
        // In case of audio only clip, verify that the player is playing while device is in
        // locked state
        assertTrue(mIsPlayerPlaying);
      } else {
        // Otherwise verify that the player is not playing while device is in locked state
        assertFalse(mIsPlayerPlaying);
      }
      // Unlock the device
      UiDeviceUtils.pressWakeupButton();
      UiDeviceUtils.pressUnlockButton();
    }
  }
}