Adds unit tests for WOL.

Test: atest WindowOrientationListenerTest
Bug: 187852366
Change-Id: I0ef2b64a800efad9f8919abb7397eff441fda7e7
This commit is contained in:
Yi Jiang
2021-05-18 13:05:28 -07:00
parent c1c3ab17ea
commit 645decb3fa

View File

@@ -31,6 +31,7 @@ import android.hardware.input.InputSensorInfo;
import android.os.CancellationSignal;
import android.os.Handler;
import android.rotationresolver.RotationResolverInternal;
import android.service.rotationresolver.RotationResolverService;
import android.view.Surface;
import org.junit.Before;
@@ -42,37 +43,37 @@ import org.mockito.MockitoAnnotations;
* Tests for {@link com.android.server.wm.WindowOrientationListener}
*/
public class WindowOrientationListenerTest {
private static final int DEFAULT_SENSOR_ROTATION = Surface.ROTATION_90;
@Mock
private Context mMockContext;
@Mock
private Handler mMockHandler;
@Mock
private InputSensorInfo mMockInputSensorInfo;
@Mock
private SensorManager mMockSensorManager;
private TestableRotationResolver mFakeRotationResolverInternal;
private com.android.server.wm.WindowOrientationListener mWindowOrientationListener;
private TestableWindowOrientationListener mWindowOrientationListener;
private int mFinalizedRotation;
private boolean mRotationResolverEnabled;
private SensorEvent mFakeSensorEvent;
private Sensor mFakeSensor;
private Handler mHandler;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mRotationResolverEnabled = true;
mHandler = Handler.getMain();
mFakeRotationResolverInternal = new TestableRotationResolver();
doReturn(mMockSensorManager).when(mMockContext).getSystemService(Context.SENSOR_SERVICE);
mWindowOrientationListener = new TestableWindowOrientationListener(mMockContext,
mMockHandler);
mHandler);
mWindowOrientationListener.mRotationResolverService = mFakeRotationResolverInternal;
mFakeSensor = new Sensor(mMockInputSensorInfo);
mFakeSensorEvent = new SensorEvent(mFakeSensor, /* accuracy */ 1, /* timestamp */ 1L,
new float[]{(float) Surface.ROTATION_90});
new float[]{(float) DEFAULT_SENSOR_ROTATION});
}
@Test
@@ -81,21 +82,49 @@ public class WindowOrientationListenerTest {
mWindowOrientationListener.mOrientationJudge.onSensorChanged(mFakeSensorEvent);
assertThat(mFinalizedRotation).isEqualTo(Surface.ROTATION_90);
assertThat(mFinalizedRotation).isEqualTo(DEFAULT_SENSOR_ROTATION);
}
@Test
public void testOnSensorChanged_normalCase() {
mFakeRotationResolverInternal.mResult = Surface.ROTATION_180;
public void testOnSensorChanged_callbackNotTheLatest_IgnoreResult() {
mWindowOrientationListener.mOrientationJudge.onSensorChanged(mFakeSensorEvent);
final RotationResolverInternal.RotationResolverCallbackInternal callback1 =
mFakeRotationResolverInternal.getCallback();
mWindowOrientationListener.mOrientationJudge.onSensorChanged(mFakeSensorEvent);
final RotationResolverInternal.RotationResolverCallbackInternal callback2 =
mFakeRotationResolverInternal.getCallback();
callback1.onSuccess(Surface.ROTATION_180);
assertThat(mWindowOrientationListener.mIsOnProposedRotationChangedCalled).isFalse();
callback2.onSuccess(Surface.ROTATION_270);
assertThat(mWindowOrientationListener.mIsOnProposedRotationChangedCalled).isTrue();
assertThat(mFinalizedRotation).isEqualTo(Surface.ROTATION_270);
}
@Test
public void testOnSensorChanged_normalCase1() {
mWindowOrientationListener.mOrientationJudge.onSensorChanged(mFakeSensorEvent);
mFakeRotationResolverInternal.callbackWithSuccessResult(Surface.ROTATION_180);
assertThat(mFinalizedRotation).isEqualTo(Surface.ROTATION_180);
}
final class TestableRotationResolver extends RotationResolverInternal {
@Test
public void testSensorChanged_normalCase2() {
mWindowOrientationListener.mOrientationJudge.onSensorChanged(mFakeSensorEvent);
mFakeRotationResolverInternal.callbackWithFailureResult(
RotationResolverService.ROTATION_RESULT_FAILURE_CANCELLED);
assertThat(mFinalizedRotation).isEqualTo(DEFAULT_SENSOR_ROTATION);
}
static final class TestableRotationResolver extends RotationResolverInternal {
@Surface.Rotation
int mResult;
RotationResolverCallbackInternal mCallback;
@Override
public boolean isRotationResolverSupported() {
@@ -107,11 +136,28 @@ public class WindowOrientationListenerTest {
String packageName, @Surface.Rotation int proposedRotation,
@Surface.Rotation int currentRotation, @DurationMillisLong long timeoutMillis,
@NonNull CancellationSignal cancellationSignal) {
callback.onSuccess(mResult);
mCallback = callback;
}
public RotationResolverCallbackInternal getCallback() {
return mCallback;
}
public void callbackWithSuccessResult(int result) {
if (mCallback != null) {
mCallback.onSuccess(result);
}
}
public void callbackWithFailureResult(int error) {
if (mCallback != null) {
mCallback.onFailure(error);
}
}
}
final class TestableWindowOrientationListener extends WindowOrientationListener {
boolean mIsOnProposedRotationChangedCalled = false;
TestableWindowOrientationListener(Context context, Handler handler) {
super(context, handler);
@@ -121,6 +167,7 @@ public class WindowOrientationListenerTest {
@Override
public void onProposedRotationChanged(int rotation) {
mFinalizedRotation = rotation;
mIsOnProposedRotationChangedCalled = true;
}
@Override