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