Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright 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 androidx.mediarouter.app;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import android.content.Context;
     22 import android.content.res.TypedArray;
     23 import android.support.test.annotation.UiThreadTest;
     24 import android.support.test.filters.LargeTest;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.rule.ActivityTestRule;
     27 import android.support.test.runner.AndroidJUnit4;
     28 
     29 import androidx.mediarouter.media.MediaRouter.RouteInfo;
     30 import androidx.mediarouter.media.TestUtils;
     31 import androidx.mediarouter.test.R;
     32 
     33 import org.junit.Before;
     34 import org.junit.Rule;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 
     38 @LargeTest
     39 @RunWith(AndroidJUnit4.class)
     40 public class MediaRouteChooserDialogTest {
     41 
     42     @Rule
     43     public final ActivityTestRule<MediaRouteChooserDialogTestActivity> mActivityTestRule;
     44     private MediaRouteChooserDialog.RouteComparator mComparator;
     45 
     46     public MediaRouteChooserDialogTest() {
     47         mActivityTestRule = new ActivityTestRule<>(MediaRouteChooserDialogTestActivity.class);
     48     }
     49 
     50     @Before
     51     public void setup() {
     52         mComparator = new MediaRouteChooserDialog.RouteComparator();
     53     }
     54 
     55     @Test
     56     @SmallTest
     57     @UiThreadTest
     58     public void testWindowNoTitle() {
     59         final Context context = mActivityTestRule.getActivity();
     60         TypedArray typedArray;
     61 
     62         // Without any base theme or customized theme
     63         MediaRouteChooserDialog dialog = new MediaRouteChooserDialog(context);
     64         typedArray = dialog.getContext().obtainStyledAttributes(R.styleable.AppCompatTheme);
     65         assertTrue(typedArray.getBoolean(R.styleable.AppCompatTheme_windowNoTitle, false));
     66         typedArray.recycle();
     67 
     68         // No base theme, with a customized theme (has window title)
     69         dialog = new MediaRouteChooserDialog(context, R.style.HasWindowTitle);
     70         typedArray = dialog.getContext().obtainStyledAttributes(R.styleable.AppCompatTheme);
     71         assertTrue(typedArray.getBoolean(R.styleable.AppCompatTheme_windowNoTitle, false));
     72         typedArray.recycle();
     73 
     74         // With base theme (has window title), no customized theme
     75         context.setTheme(R.style.HasWindowTitle);
     76         dialog = new MediaRouteChooserDialog(context);
     77         typedArray = dialog.getContext().obtainStyledAttributes(R.styleable.AppCompatTheme);
     78         assertTrue(typedArray.getBoolean(R.styleable.AppCompatTheme_windowNoTitle, false));
     79         typedArray.recycle();
     80 
     81         // With base theme and a customized theme (both has window title)
     82         dialog = new MediaRouteChooserDialog(context, R.style.HasWindowTitle);
     83         typedArray = dialog.getContext().obtainStyledAttributes(R.styleable.AppCompatTheme);
     84         assertTrue(typedArray.getBoolean(R.styleable.AppCompatTheme_windowNoTitle, false));
     85         typedArray.recycle();
     86 
     87         context.setTheme(0);
     88     }
     89 
     90     @Test
     91     public void testRouteComparatorWithSameRouteName() {
     92         RouteInfo routeInfo1 = TestUtils.createRouteInfo("ROUTE_ID_1", "ROUTE_NAME_1");
     93         RouteInfo routeInfo2 = TestUtils.createRouteInfo("ROUTE_ID_2", "ROUTE_NAME_1");
     94         int result = mComparator.compare(routeInfo1, routeInfo2);
     95         assertTrue(result == 0);
     96     }
     97 
     98     @Test
     99     public void testRouteComparatorWithCommonComparison() {
    100         RouteInfo routeInfo1 = TestUtils.createRouteInfo("ROUTE_ID_1", "Route ABC");
    101         RouteInfo routeInfo2 = TestUtils.createRouteInfo("ROUTE_ID_2", "Route XYZ");
    102         int result = mComparator.compare(routeInfo1, routeInfo2);
    103         assertTrue(result < 0);
    104     }
    105 
    106     @Test
    107     public void testRouteComparatorWithCaseInsensitiveComparison() {
    108         RouteInfo routeInfo1 = TestUtils.createRouteInfo("ROUTE_ID_1", "living room abc");
    109         RouteInfo routeInfo2 = TestUtils.createRouteInfo("ROUTE_ID_2", "LIVING ROOM XYZ");
    110         int result1 = mComparator.compare(routeInfo1, routeInfo2);
    111         assertTrue(result1 < 0);
    112 
    113         RouteInfo routeInfo3 = TestUtils.createRouteInfo("ROUTE_ID_3", "LIVING ROOM ABC");
    114         RouteInfo routeInfo4 = TestUtils.createRouteInfo("ROUTE_ID_4", "living room xyz");
    115         int result2 = mComparator.compare(routeInfo3, routeInfo4);
    116         assertTrue(result2 < 0);
    117     }
    118 }
    119