1 /* 2 * Copyright (C) 2009 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.view.cts; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.content.Context; 22 import android.hardware.Sensor; 23 import android.hardware.SensorManager; 24 import android.support.test.InstrumentationRegistry; 25 import android.support.test.filters.SmallTest; 26 import android.support.test.runner.AndroidJUnit4; 27 import android.view.OrientationEventListener; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 /** 34 * Test {@link OrientationEventListener}. 35 */ 36 @SmallTest 37 @RunWith(AndroidJUnit4.class) 38 public class OrientationEventListenerTest { 39 private Context mContext; 40 41 @Before 42 public void setup() { 43 mContext = InstrumentationRegistry.getTargetContext(); 44 } 45 46 @Test 47 public void testConstructor() { 48 new MyOrientationEventListener(mContext); 49 50 new MyOrientationEventListener(mContext, SensorManager.SENSOR_DELAY_UI); 51 } 52 53 @Test 54 public void testEnableAndDisable() { 55 MyOrientationEventListener listener = new MyOrientationEventListener(mContext); 56 listener.enable(); 57 listener.disable(); 58 } 59 60 @Test 61 public void testCanDetectOrientation() { 62 SensorManager sm = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE); 63 // Orientation can only be detected if there is an accelerometer 64 boolean hasSensor = (sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null); 65 66 MyOrientationEventListener listener = new MyOrientationEventListener(mContext); 67 assertEquals(hasSensor, listener.canDetectOrientation()); 68 } 69 70 private static class MyOrientationEventListener extends OrientationEventListener { 71 public MyOrientationEventListener(Context context) { 72 super(context); 73 } 74 75 public MyOrientationEventListener(Context context, int rate) { 76 super(context, rate); 77 } 78 79 @Override 80 public void onOrientationChanged(int orientation) { 81 } 82 } 83 } 84