Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2018 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.os;
     18 
     19 import static junit.framework.Assert.assertEquals;
     20 import static junit.framework.Assert.assertNotNull;
     21 import static junit.framework.Assert.assertNull;
     22 
     23 import static org.mockito.Mockito.mock;
     24 import static org.mockito.Mockito.when;
     25 
     26 import android.content.Context;
     27 import android.content.res.Resources;
     28 import android.net.Uri;
     29 
     30 import com.android.internal.R;
     31 
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.mockito.junit.MockitoJUnitRunner;
     35 
     36 @RunWith(MockitoJUnitRunner.class)
     37 public class VibrationEffectTest {
     38     private static final String RINGTONE_URI_1 = "content://test/system/ringtone_1";
     39     private static final String RINGTONE_URI_2 = "content://test/system/ringtone_2";
     40     private static final String RINGTONE_URI_3 = "content://test/system/ringtone_3";
     41     private static final String UNKNOWN_URI = "content://test/system/other_audio";
     42 
     43     @Test
     44     public void getRingtones_noPrebakedRingtones() {
     45         Resources r = mockRingtoneResources(new String[0]);
     46         Context context = mockContext(r);
     47         VibrationEffect effect = VibrationEffect.get(Uri.parse(RINGTONE_URI_1), context);
     48         assertNull(effect);
     49     }
     50 
     51     @Test
     52     public void getRingtones_noPrebakedRingtoneForUri() {
     53         Resources r = mockRingtoneResources();
     54         Context context = mockContext(r);
     55         VibrationEffect effect = VibrationEffect.get(Uri.parse(UNKNOWN_URI), context);
     56         assertNull(effect);
     57     }
     58 
     59     @Test
     60     public void getRingtones_getPrebakedRingtone() {
     61         Resources r = mockRingtoneResources();
     62         Context context = mockContext(r);
     63         VibrationEffect effect = VibrationEffect.get(Uri.parse(RINGTONE_URI_2), context);
     64         VibrationEffect expectedEffect = VibrationEffect.get(VibrationEffect.RINGTONES[1]);
     65         assertNotNull(expectedEffect);
     66         assertEquals(expectedEffect, effect);
     67     }
     68 
     69 
     70     private Resources mockRingtoneResources() {
     71         return mockRingtoneResources(new String[] {
     72                 RINGTONE_URI_1,
     73                 RINGTONE_URI_2,
     74                 RINGTONE_URI_3
     75         });
     76     }
     77 
     78     private Resources mockRingtoneResources(String[] ringtoneUris) {
     79         Resources mockResources = mock(Resources.class);
     80         when(mockResources.getStringArray(R.array.config_ringtoneEffectUris))
     81                 .thenReturn(ringtoneUris);
     82         return mockResources;
     83     }
     84 
     85     private Context mockContext(Resources r) {
     86         Context ctx = mock(Context.class);
     87         when(ctx.getResources()).thenReturn(r);
     88         return ctx;
     89     }
     90 }
     91