13885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam/*
23885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * Copyright (C) 2015 The Android Open Source Project
33885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam *
43885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
53885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * you may not use this file except in compliance with the License.
63885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * You may obtain a copy of the License at
73885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam *
83885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
93885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam *
103885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * Unless required by applicable law or agreed to in writing, software
113885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
123885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * See the License for the specific language governing permissions and
143885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam * limitations under the License.
153885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam */
163885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
173885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lampackage com.android.setupwizardlib.test;
183885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
193885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.content.Context;
203885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.graphics.drawable.Drawable;
213885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.graphics.drawable.InsetDrawable;
223885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.os.Build;
233885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.support.v7.widget.RecyclerView;
243885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.test.InstrumentationTestCase;
253885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.test.suitebuilder.annotation.SmallTest;
263885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.view.ContextThemeWrapper;
273885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.view.LayoutInflater;
283885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.view.View;
293885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport android.view.ViewGroup;
303885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
313885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lamimport com.android.setupwizardlib.SetupWizardPreferenceLayout;
323885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
333885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lampublic class SetupWizardPreferenceLayoutTest extends InstrumentationTestCase {
343885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
353885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    private Context mContext;
363885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
373885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    @Override
383885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    protected void setUp() throws Exception {
393885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        super.setUp();
403885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        mContext = new ContextThemeWrapper(getInstrumentation().getContext(),
413885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam                R.style.SuwThemeMaterial_Light);
423885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    }
433885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
443885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    @SmallTest
453885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    public void testDefaultTemplate() {
463885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        SetupWizardPreferenceLayout layout = new TestLayout(mContext);
473885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertPreferenceTemplateInflated(layout);
483885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    }
493885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
503885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    @SmallTest
513885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    public void testGetRecyclerView() {
523885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        SetupWizardPreferenceLayout layout = new TestLayout(mContext);
533885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertPreferenceTemplateInflated(layout);
543885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertNotNull("getRecyclerView should not be null", layout.getRecyclerView());
553885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    }
563885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
573885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    @SmallTest
583885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    public void testOnCreateRecyclerView() {
593885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        SetupWizardPreferenceLayout layout = new TestLayout(mContext);
603885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertPreferenceTemplateInflated(layout);
613885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        final RecyclerView recyclerView = layout.onCreateRecyclerView(LayoutInflater.from(mContext),
623885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam                layout, null /* savedInstanceState */);
633885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertNotNull("RecyclerView created should not be null", recyclerView);
643885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    }
653885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
663885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    @SmallTest
673885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    public void testDividerInset() {
683885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        SetupWizardPreferenceLayout layout = new TestLayout(mContext);
693885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
703885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam            layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
713885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        }
723885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertPreferenceTemplateInflated(layout);
733885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
743885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        layout.addView(layout.onCreateRecyclerView(LayoutInflater.from(mContext), layout,
753885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam                null /* savedInstanceState */));
763885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
773885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        layout.setDividerInset(10);
783885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertEquals("Divider inset should be 10", 10, layout.getDividerInset());
793885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
803885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        final Drawable divider = layout.getDivider();
813885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable);
823885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    }
833885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
843885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    private void assertPreferenceTemplateInflated(SetupWizardPreferenceLayout layout) {
853885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        View contentContainer = layout.findViewById(R.id.suw_layout_content);
863885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        assertTrue("@id/suw_layout_content should be a ViewGroup",
873885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam                contentContainer instanceof ViewGroup);
883885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
893885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        if (layout instanceof TestLayout) {
903885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam            assertNotNull("Header text view should not be null",
91ec9d8c8aa8fdeb6640fb0037b062bf10ca1c016bMaurice Lam                    ((TestLayout) layout).findManagedViewById(R.id.suw_layout_title));
923885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam            assertNotNull("Decoration view should not be null",
93ec9d8c8aa8fdeb6640fb0037b062bf10ca1c016bMaurice Lam                    ((TestLayout) layout).findManagedViewById(R.id.suw_layout_decor));
943885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        }
953885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    }
963885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
973885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    // Make some methods public for testing
983885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    public static class TestLayout extends SetupWizardPreferenceLayout {
993885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
1003885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        public TestLayout(Context context) {
1013885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam            super(context);
1023885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        }
1033885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam
1043885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        @Override
105ec9d8c8aa8fdeb6640fb0037b062bf10ca1c016bMaurice Lam        public View findManagedViewById(int id) {
106ec9d8c8aa8fdeb6640fb0037b062bf10ca1c016bMaurice Lam            return super.findManagedViewById(id);
1073885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam        }
1083885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam    }
1093885d8491a008281d18ddbbabc5b8e26848fadccMaurice Lam}
110