1 /* 2 * Copyright (C) 2021 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.server.wm.jetpack.utils; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.fail; 22 23 import android.os.IBinder; 24 25 import androidx.annotation.NonNull; 26 import androidx.window.sidecar.SidecarDeviceState; 27 import androidx.window.sidecar.SidecarInterface.SidecarCallback; 28 import androidx.window.sidecar.SidecarWindowLayoutInfo; 29 30 import java.util.concurrent.CountDownLatch; 31 32 public class SidecarCallbackCounter implements SidecarCallback { 33 34 private final IBinder mWindowToken; 35 private final int mExpectedCallbackCount; 36 private final CountDownLatch mCountDownLatch; 37 SidecarCallbackCounter(IBinder windowToken, int expectedCallbackCount)38 public SidecarCallbackCounter(IBinder windowToken, int expectedCallbackCount) { 39 mWindowToken = windowToken; 40 mExpectedCallbackCount = expectedCallbackCount; 41 mCountDownLatch = new CountDownLatch(mExpectedCallbackCount); 42 } 43 44 @Override onDeviceStateChanged(@onNull SidecarDeviceState sidecarDeviceState)45 public void onDeviceStateChanged(@NonNull SidecarDeviceState sidecarDeviceState) { 46 } 47 48 @Override onWindowLayoutChanged(@onNull IBinder iBinder, @NonNull SidecarWindowLayoutInfo sidecarWindowLayoutInfo)49 public void onWindowLayoutChanged(@NonNull IBinder iBinder, 50 @NonNull SidecarWindowLayoutInfo sidecarWindowLayoutInfo) { 51 assertEquals(iBinder, mWindowToken); 52 assertNotNull(sidecarWindowLayoutInfo); 53 if (mCountDownLatch.getCount() == 0) { 54 fail("onWindowLayoutChanged callback count has exceeded expected amount of: " 55 + mExpectedCallbackCount); 56 } 57 mCountDownLatch.countDown(); 58 } 59 assertZeroCount()60 public void assertZeroCount() { 61 assertEquals("Callback count should be zero", 0, mCountDownLatch.getCount()); 62 } 63 64 } 65