Highlight row (instead of ripple) when come from search.

- Moved HighlightableAdapter to its own class
- Replaced ripple with blue background
- Bluebackground only stays for 5 seconds. After that it's replaced by
  ?attr/selectableBackground

Misc fixes.

- Fix NPE in UserSettings
- Update char limit on a new string

Change-Id: I4687e54e71fd7b9243f520b7630563df58be23d4
Fixes: 71715698
Fixes: 72761974
Test: robotests
This commit is contained in:
Fan Zhang
2018-01-31 14:14:41 -08:00
parent 12fd732b55
commit 3d516e7607
8 changed files with 273 additions and 177 deletions

View File

@@ -0,0 +1,127 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.widget;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceViewHolder;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class HighlightablePreferenceGroupAdapterTest {
private static final String TEST_KEY = "key";
@Mock
private View mRoot;
@Mock
private PreferenceCategory mPreferenceCatetory;
private Context mContext;
private HighlightablePreferenceGroupAdapter mAdapter;
private PreferenceViewHolder mViewHolder;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
when(mPreferenceCatetory.getContext()).thenReturn(mContext);
mAdapter = new HighlightablePreferenceGroupAdapter(mPreferenceCatetory, TEST_KEY,
false /* highlighted*/);
mViewHolder = PreferenceViewHolder.createInstanceForTests(
View.inflate(mContext, R.layout.app_preference_item, null));
}
@Test
public void requestHighlight_hasKey_notHighlightedBefore_shouldRequest() {
mAdapter.requestHighlight(mRoot, mock(RecyclerView.class));
verify(mRoot).postDelayed(any(),
eq(HighlightablePreferenceGroupAdapter.DELAY_HIGHLIGHT_DURATION_MILLIS));
}
@Test
public void requestHighlight_noKey_highlightedBefore_noRecyclerView_shouldNotRequest() {
ReflectionHelpers.setField(mAdapter, "mHighlightKey", null);
ReflectionHelpers.setField(mAdapter, "mHighlightRequested", false);
mAdapter.requestHighlight(mRoot, mock(RecyclerView.class));
ReflectionHelpers.setField(mAdapter, "mHighlightKey", TEST_KEY);
ReflectionHelpers.setField(mAdapter, "mHighlightRequested", true);
mAdapter.requestHighlight(mRoot, mock(RecyclerView.class));
ReflectionHelpers.setField(mAdapter, "mHighlightKey", TEST_KEY);
ReflectionHelpers.setField(mAdapter, "mHighlightRequested", false);
mAdapter.requestHighlight(mRoot, null /* recyclerView */);
verifyZeroInteractions(mRoot);
}
@Test
public void updateBackground_notHighlightedRow_shouldNotSetHighlightedTag() {
ReflectionHelpers.setField(mAdapter, "mHighlightPosition", 10);
mAdapter.updateBackground(mViewHolder, 0);
assertThat(mViewHolder.itemView.getTag(R.id.preference_highlighted)).isNull();
}
@Test
public void updateBackground_highlight_shouldChangeBackgroundAndSetHighlightedTag() {
ReflectionHelpers.setField(mAdapter, "mHighlightPosition", 10);
mAdapter.updateBackground(mViewHolder, 10);
assertThat(mViewHolder.itemView.getBackground()).isInstanceOf(ColorDrawable.class);
assertThat(mViewHolder.itemView.getTag(R.id.preference_highlighted)).isEqualTo(true);
}
@Test
public void updateBackground_reuseHightlightedRowForNormalRow_shouldResetBackgroundAndTag() {
ReflectionHelpers.setField(mAdapter, "mHighlightPosition", 10);
mViewHolder.itemView.setTag(R.id.preference_highlighted, true);
mAdapter.updateBackground(mViewHolder, 0);
assertThat(mViewHolder.itemView.getBackground()).isNotInstanceOf(ColorDrawable.class);
assertThat(mViewHolder.itemView.getTag(R.id.preference_highlighted)).isEqualTo(false);
}
}

View File

@@ -1,70 +0,0 @@
package com.android.settings;
import static com.google.common.truth.Truth.assertThat;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroupAdapter;
import com.android.settings.accessibility.AccessibilitySettings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class SettingsPreferenceFragmentTest {
private Instrumentation mInstrumentation;
private Context mTargetContext;
@Before
public void setUp() throws Exception {
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mTargetContext = mInstrumentation.getTargetContext();
}
@Test
public void testHighlightCaptions() throws InterruptedException {
final String prefKey = "captioning_preference_screen";
Bundle args = new Bundle();
args.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, prefKey);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(mTargetContext, SubSettings.class);
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT,
"com.android.settings.accessibility.AccessibilitySettings");
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
SettingsActivity activity = (SettingsActivity) mInstrumentation.startActivitySync(intent);
AccessibilitySettings fragment = (AccessibilitySettings)
activity.getFragmentManager().getFragments().get(0);
// Allow time for highlight from post-delay.
Thread.sleep(SettingsPreferenceFragment.DELAY_HIGHLIGHT_DURATION_MILLIS);
if (!fragment.mPreferenceHighlighted) {
Thread.sleep(SettingsPreferenceFragment.DELAY_HIGHLIGHT_DURATION_MILLIS);
}
int prefPosition = -1;
PreferenceGroupAdapter adapter = (PreferenceGroupAdapter)
fragment.getListView().getAdapter();
for (int n = 0, count = adapter.getItemCount(); n < count; n++) {
final Preference preference = adapter.getItem(n);
final String preferenceKey = preference.getKey();
if (preferenceKey.equals(prefKey)) {
prefPosition = n;
break;
}
}
assertThat(fragment.mAdapter.initialHighlightedPosition).isEqualTo(prefPosition);
}
}