Merge "Adds unit tests for WOL." into sc-dev

This commit is contained in:
Yi Jiang
2021-05-19 17:04:44 +00:00
committed by Android (Google) Code Review

View File

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