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 17package androidx.car.widget; 18 19import static android.support.test.espresso.Espresso.onView; 20import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition; 21import static android.support.test.espresso.matcher.ViewMatchers.withId; 22 23import static org.hamcrest.core.Is.is; 24import static org.hamcrest.core.IsEqual.equalTo; 25import static org.junit.Assert.assertThat; 26 27import android.content.pm.PackageManager; 28import android.support.test.filters.SmallTest; 29import android.support.test.rule.ActivityTestRule; 30import android.support.test.runner.AndroidJUnit4; 31 32import org.junit.Assume; 33import org.junit.Before; 34import org.junit.Rule; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37 38import java.util.ArrayList; 39import java.util.Arrays; 40import java.util.List; 41 42import androidx.car.test.R; 43 44/** 45 * Tests the layout configuration in {@link SubheaderListItem}. 46 */ 47@RunWith(AndroidJUnit4.class) 48@SmallTest 49public class SubheaderListItemTest { 50 51 @Rule 52 public ActivityTestRule<PagedListViewTestActivity> mActivityRule = 53 new ActivityTestRule<>(PagedListViewTestActivity.class); 54 55 private PagedListViewTestActivity mActivity; 56 private PagedListView mPagedListView; 57 58 private boolean isAutoDevice() { 59 PackageManager packageManager = mActivityRule.getActivity().getPackageManager(); 60 return packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); 61 } 62 63 @Before 64 public void setUp() { 65 Assume.assumeTrue(isAutoDevice()); 66 mActivity = mActivityRule.getActivity(); 67 mPagedListView = mActivity.findViewById(R.id.paged_list_view); 68 } 69 70 private void setupPagedListView(List<? extends ListItem> items) { 71 ListItemProvider provider = new ListItemProvider.ListProvider( 72 new ArrayList<>(items)); 73 try { 74 mActivityRule.runOnUiThread(() -> { 75 mPagedListView.setAdapter(new ListItemAdapter(mActivity, provider)); 76 }); 77 } catch (Throwable throwable) { 78 throwable.printStackTrace(); 79 throw new RuntimeException(throwable); 80 } 81 // Wait for paged list view to layout by using espresso to scroll to a position. 82 onView(withId(R.id.recycler_view)).perform(scrollToPosition(0)); 83 } 84 85 private SubheaderListItem.ViewHolder getViewHolderAtPosition(int position) { 86 return (SubheaderListItem.ViewHolder) mPagedListView.getRecyclerView() 87 .findViewHolderForAdapterPosition(position); 88 } 89 90 private TextListItem.ViewHolder getTextViewHolderAtPosition(int position) { 91 return (TextListItem.ViewHolder) mPagedListView.getRecyclerView() 92 .findViewHolderForAdapterPosition(position); 93 } 94 95 @Test 96 public void testEmptyStartMargin() { 97 SubheaderListItem subheader = new SubheaderListItem(mActivity, "text"); 98 subheader.setTextStartMarginType(SubheaderListItem.TEXT_START_MARGIN_TYPE_NONE); 99 100 TextListItem item = new TextListItem(mActivity); 101 item.setTitle("title"); 102 item.setPrimaryActionNoIcon(); 103 104 setupPagedListView(Arrays.asList(subheader, item)); 105 106 assertThat(getViewHolderAtPosition(0).getText().getLeft(), 107 is(equalTo(getTextViewHolderAtPosition(1).getTitle().getLeft()))); 108 } 109 110 @Test 111 public void testStartMarginMatchesSmallIcon() { 112 SubheaderListItem subheader = new SubheaderListItem(mActivity, "text"); 113 subheader.setTextStartMarginType(SubheaderListItem.TEXT_START_MARGIN_TYPE_SMALL); 114 115 TextListItem item = new TextListItem(mActivity); 116 item.setTitle("title"); 117 item.setPrimaryActionIcon(android.R.drawable.sym_def_app_icon, /* useLargeIcon= */ false); 118 119 setupPagedListView(Arrays.asList(subheader, item)); 120 121 assertThat(getViewHolderAtPosition(0).getText().getLeft(), 122 is(equalTo(getTextViewHolderAtPosition(1).getTitle().getLeft()))); 123 } 124 125 @Test 126 public void testStartMarginMatchesLargeIcon() { 127 SubheaderListItem subheader = new SubheaderListItem(mActivity, "text"); 128 subheader.setTextStartMarginType(SubheaderListItem.TEXT_START_MARGIN_TYPE_LARGE); 129 130 TextListItem item = new TextListItem(mActivity); 131 item.setTitle("title"); 132 item.setPrimaryActionIcon(android.R.drawable.sym_def_app_icon, /* useLargeIcon= */ true); 133 134 setupPagedListView(Arrays.asList(subheader, item)); 135 136 assertThat(getViewHolderAtPosition(0).getText().getLeft(), 137 is(equalTo(getTextViewHolderAtPosition(1).getTitle().getLeft()))); 138 } 139} 140