1 /* 2 * Copyright (C) 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 17 package androidx.car.drawer; 18 19 import static android.support.test.espresso.Espresso.onView; 20 import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition; 21 import static android.support.test.espresso.matcher.ViewMatchers.withId; 22 23 import static org.hamcrest.CoreMatchers.is; 24 import static org.hamcrest.Matchers.lessThan; 25 import static org.junit.Assume.assumeThat; 26 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.pm.PackageManager; 30 import android.support.test.InstrumentationRegistry; 31 import android.support.test.espresso.contrib.DrawerActions; 32 import android.support.test.filters.MediumTest; 33 import android.support.test.rule.ActivityTestRule; 34 import android.support.test.runner.AndroidJUnit4; 35 36 import org.junit.After; 37 import org.junit.Assume; 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 import androidx.car.test.R; 44 import androidx.car.utils.CarUxRestrictionsTestUtils; 45 import androidx.car.widget.PagedListView; 46 47 /** 48 * Unit tests for classes under {@link androidx.car.drawer}. 49 * 50 * <p>{@code mActivity} is a subclass of {@link CarDrawerActivity}. To set content of drawer, use 51 * {@link CarDrawerController#setRootAdapter(CarDrawerAdapter)}. 52 */ 53 @RunWith(AndroidJUnit4.class) 54 @MediumTest 55 public final class CarDrawerTest { 56 // Note that launchActivity is passed "false" here because we only want to create the 57 // Activity after we checked that the test is being run on an auto device. Otherwise, this will 58 // cause an error due to classes not being found. 59 @Rule 60 public ActivityTestRule<CarDrawerTestActivity> mActivityRule = new ActivityTestRule<>( 61 CarDrawerTestActivity.class, 62 false /* initialTouchMode */, 63 false /* launchActivity */); 64 65 private CarDrawerTestActivity mActivity; 66 private PagedListView mDrawerList; 67 68 /** Returns {@code true} if the testing device has the automotive feature flag. */ 69 private boolean isAutoDevice() { 70 PackageManager packageManager = InstrumentationRegistry.getContext().getPackageManager(); 71 return packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); 72 } 73 74 @Before 75 public void setUp() { 76 Assume.assumeTrue(isAutoDevice()); 77 78 // Retrieve the activity after the isAutoDevice() check because this class depends on 79 // car-related classes (android.car.Car). These classes will not be available on non-auto 80 // devices. 81 mActivityRule.launchActivity(new Intent()); 82 mActivity = mActivityRule.getActivity(); 83 84 mDrawerList = mActivity.findViewById(R.id.drawer_list); 85 } 86 87 @After 88 public void tearDown() { 89 // The Activity is only launched if the test was run on an auto device. If it's been 90 // launched, then explicitly finish it here since it was also explicitly launched. 91 if (isAutoDevice()) { 92 mActivityRule.finishActivity(); 93 } 94 } 95 96 private DrawerItemViewHolder getViewHolderAtPositionInDrawer(int position) { 97 onView(withId(R.id.drawer_layout)).perform(DrawerActions.open()); 98 return (DrawerItemViewHolder) mDrawerList.getRecyclerView() 99 .findViewHolderForAdapterPosition(position); 100 } 101 102 private void refreshUi() { 103 try { 104 mActivityRule.runOnUiThread(() -> mDrawerList.getAdapter().notifyDataSetChanged()); 105 } catch (Throwable throwable) { 106 throwable.printStackTrace(); 107 throw new RuntimeException(throwable); 108 } 109 // Wait for PagedListView to layout by using espresso to scroll to a position. 110 onView(withId(R.id.recycler_view)).perform(scrollToPosition(0)); 111 } 112 113 @Test 114 public void testUxRestrictionsChange() throws Throwable { 115 String longText = mActivity.getResources().getString(R.string.over_uxr_text_length_limit); 116 CarDrawerAdapter adapter = new TextDrawerAdapter(mActivity, 5, longText); 117 mActivityRule.runOnUiThread(() -> mActivity.getDrawerController().setRootAdapter(adapter)); 118 119 DrawerItemViewHolder vh = getViewHolderAtPositionInDrawer(0); 120 final String originalText = (String) vh.getText().getText(); 121 122 vh.complyWithUxRestrictions(CarUxRestrictionsTestUtils.getFullyRestricted()); 123 refreshUi(); 124 125 assumeThat(vh.getText().getText().length(), is(lessThan(originalText.length()))); 126 } 127 128 /** 129 * Drawer adapter that populates {@itemCount} items, each with text set to {@cod text}. 130 */ 131 private static class TextDrawerAdapter extends CarDrawerAdapter { 132 private int mItemCount; 133 private String mText; 134 135 TextDrawerAdapter(Context context, int itemCount, String text) { 136 super(context, true); 137 mItemCount = itemCount; 138 mText = text; 139 140 setTitle("title"); 141 } 142 143 @Override 144 protected int getActualItemCount() { 145 return mItemCount; 146 } 147 148 @Override 149 protected void populateViewHolder(DrawerItemViewHolder holder, int position) { 150 holder.getText().setText(mText); 151 } 152 153 @Override 154 public void onItemClick(int position) { 155 // Do nothing. 156 } 157 158 @Override 159 protected boolean usesSmallLayout(int position) { 160 return false; 161 } 162 } 163 164 } 165