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.browser.customtabs; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertTrue; 24 25 import android.content.Intent; 26 import android.graphics.Color; 27 import android.os.Build; 28 import android.support.test.InstrumentationRegistry; 29 import android.support.test.filters.SmallTest; 30 import android.support.test.runner.AndroidJUnit4; 31 32 import androidx.annotation.ColorRes; 33 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 /** 38 * Tests for CustomTabsIntent. 39 */ 40 @RunWith(AndroidJUnit4.class) 41 @SmallTest 42 public class CustomTabsIntentTest { 43 44 @Test 45 public void testBareboneCustomTabIntent() { 46 CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build(); 47 Intent intent = customTabsIntent.intent; 48 assertNotNull(intent); 49 assertNull(customTabsIntent.startAnimationBundle); 50 51 assertEquals(Intent.ACTION_VIEW, intent.getAction()); 52 assertTrue(intent.hasExtra(CustomTabsIntent.EXTRA_SESSION)); 53 if (Build.VERSION.SDK_INT >= 18) { 54 assertNull(intent.getExtras().getBinder(CustomTabsIntent.EXTRA_SESSION)); 55 } 56 assertNull(intent.getComponent()); 57 } 58 59 @Test 60 public void testToolbarColor() { 61 int color = Color.RED; 62 Intent intent = new CustomTabsIntent.Builder().setToolbarColor(color).build().intent; 63 assertTrue(intent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR)); 64 assertEquals(color, intent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0)); 65 } 66 67 @Test 68 public void testToolbarColorIsNotAResource() { 69 @ColorRes int colorId = android.R.color.background_dark; 70 int color = InstrumentationRegistry.getContext().getResources().getColor(colorId); 71 Intent intent = new CustomTabsIntent.Builder().setToolbarColor(colorId).build().intent; 72 assertFalse("The color should not be a resource ID", 73 color == intent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0)); 74 intent = new CustomTabsIntent.Builder().setToolbarColor(color).build().intent; 75 assertEquals(color, intent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0)); 76 } 77 } 78