1/*
2 * Copyright (C) 2016 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.design.widget;
18
19import static org.junit.Assert.assertTrue;
20
21import android.app.Activity;
22import android.support.design.test.R;
23import android.support.test.InstrumentationRegistry;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import org.junit.Test;
27
28public class TabLayoutPoolingTest extends BaseInstrumentationTestCase<TabLayoutPoolingActivity> {
29
30    public TabLayoutPoolingTest() {
31        super(TabLayoutPoolingActivity.class);
32    }
33
34    @SmallTest
35    @Test
36    public void testUsingTabsFromOtherInstance() {
37        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
38            @Override
39            public void run() {
40                final Activity activity = mActivityTestRule.getActivity();
41
42                // TabLayout1 has items added via the layout, so we'll just check they're
43                // there first
44                final TabLayout tabLayout1 = (TabLayout) activity.findViewById(R.id.tabs_1);
45                assertTrue(tabLayout1.getTabCount() > 0);
46
47                // Now remove all tabs. TabLayout will pool the Tab instances...
48                tabLayout1.removeAllTabs();
49
50                // Now add some tabs to the second TabLayout and make sure that we don't crash
51                final TabLayout tabLayout2 = (TabLayout) activity.findViewById(R.id.tabs_2);
52                tabLayout2.addTab(tabLayout2.newTab());
53                tabLayout2.addTab(tabLayout2.newTab());
54                tabLayout2.addTab(tabLayout2.newTab());
55            }
56        });
57    }
58
59}
60