1/*
2 * Copyright 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 */
16package com.android.managedprovisioning.preprovisioning.terms;
17
18import static org.hamcrest.CoreMatchers.sameInstance;
19import static org.junit.Assert.assertThat;
20
21import android.graphics.Color;
22import android.support.test.InstrumentationRegistry;
23import android.support.test.filters.SmallTest;
24import android.view.LayoutInflater;
25
26import com.android.managedprovisioning.common.ClickableSpanFactory;
27import com.android.managedprovisioning.common.AccessibilityContextMenuMaker;
28import com.android.managedprovisioning.common.HtmlToSpannedParser;
29import com.android.managedprovisioning.preprovisioning.WebActivity;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35
36import java.util.Arrays;
37import java.util.List;
38
39@SmallTest
40public class TermsListAdapterTest {
41    private @Mock LayoutInflater mLayoutInflater;
42    private @Mock AccessibilityContextMenuMaker mContextMenuMaker;
43
44    private List<TermsDocument> mDocs;
45    private HtmlToSpannedParser mHtmlToSpannedParser;
46    private TermsListAdapter.GroupExpandedInfo mGroupInfoAlwaysCollapsed = i -> false;
47
48    @Before
49    public void setUp() throws Exception {
50        MockitoAnnotations.initMocks(this);
51
52        TermsDocument doc1 = TermsDocument.createInstance("h1", "c1");
53        TermsDocument doc2 = TermsDocument.createInstance("h2", "c2");
54        TermsDocument doc3 = TermsDocument.createInstance("h3", "c3");
55        mDocs = Arrays.asList(doc1, doc2, doc3);
56
57        mHtmlToSpannedParser = new HtmlToSpannedParser(new ClickableSpanFactory(Color.MAGENTA),
58                url -> WebActivity.createIntent(InstrumentationRegistry.getTargetContext(), url,
59                        Color.BLUE));
60    }
61
62    @Test
63    public void returnsCorrectDocument() {
64        // given: an adapter
65        TermsListAdapter adapter = new TermsListAdapter(mDocs, mLayoutInflater, mContextMenuMaker,
66                mHtmlToSpannedParser, mGroupInfoAlwaysCollapsed);
67
68        // when: asked for a document from the initially passed-in list
69        for (int i = 0; i < mDocs.size(); i++) {
70            // then: elements from that list are returned
71            assertThat(adapter.getChild(i, 0), sameInstance(mDocs.get(i)));
72            assertThat(adapter.getGroup(i), sameInstance(mDocs.get(i)));
73        }
74    }
75}