isabellekim 3dcbac3255 Adds a method to RippleUtils that should be called before passing a CSL
to RippleDrawable. It will:
1. return the original non-null CSL, unmodified. If null, it will return
a transparent CSL
2. log a warning if the original CSL has a non-transparent pressed color
and a transparent default color for API levels 22-27 (this will cause
ripples to end early).

PiperOrigin-RevId: 254431342
2019-06-24 17:14:09 -04:00

106 lines
4.1 KiB
Java

/*
* Copyright (C) 2019 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.google.android.material.ripple;
import static com.google.common.truth.Truth.assertThat;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build.VERSION_CODES;
import android.util.Log;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.junit.rules.ExpectedLogMessagesRule;
import org.robolectric.shadows.ShadowLog;
/** Unit tests for {@link RippleUtils}. */
@RunWith(RobolectricTestRunner.class)
public class RippleUtilsTest {
@Rule
public final ExpectedLogMessagesRule logged = new ExpectedLogMessagesRule();
@Test
public void testValidateColor_null_returnsTransparent() {
assertThat(RippleUtils.sanitizeRippleDrawableColor(null))
.isEqualTo(ColorStateList.valueOf(Color.TRANSPARENT));
}
@Test
public void testValidateColor_transparent_returnsSelf_noWarning() {
assertThat(RippleUtils.sanitizeRippleDrawableColor(ColorStateList.valueOf(Color.TRANSPARENT)))
.isEqualTo(ColorStateList.valueOf(Color.TRANSPARENT));
}
@Test
public void testValidateColor_solidColor_returnsSelf_noWarning() {
ColorStateList rippleColor = ColorStateList.valueOf(Color.BLUE);
assertThat(RippleUtils.sanitizeRippleDrawableColor(rippleColor)).isEqualTo(rippleColor);
}
@Test
@Config(sdk = VERSION_CODES.KITKAT)
public void testValidateColor_transparentDefaultColor_returnsSelf_kitkat_noWarning() {
ColorStateList rippleColor = createTransparentDefaultColor();
assertThat(RippleUtils.sanitizeRippleDrawableColor(rippleColor)).isEqualTo(rippleColor);
ShadowLog.getLogsForTag(RippleUtils.LOG_TAG).isEmpty();
}
@Test
@Config(sdk = VERSION_CODES.LOLLIPOP)
public void testValidateColor_transparentDefaultColor_returnsSelf_lollipop_noWarning() {
ColorStateList rippleColor = createTransparentDefaultColor();
assertThat(RippleUtils.sanitizeRippleDrawableColor(rippleColor)).isEqualTo(rippleColor);
ShadowLog.getLogsForTag(RippleUtils.LOG_TAG).isEmpty();
}
@Test
@Config(sdk = VERSION_CODES.LOLLIPOP_MR1)
public void testValidateColor_transparentDefaultColor_returnsSelf_lollipopMr1_logsWarning() {
ColorStateList rippleColor = createTransparentDefaultColor();
assertThat(RippleUtils.sanitizeRippleDrawableColor(rippleColor)).isEqualTo(rippleColor);
logged.expectLogMessage(
Log.WARN, RippleUtils.LOG_TAG, RippleUtils.TRANSPARENT_DEFAULT_COLOR_WARNING);
}
@Test
@Config(sdk = VERSION_CODES.O_MR1)
public void testValidateColor_transparentDefaultColor_returnsSelf_oreoMr1_logsWarning() {
ColorStateList rippleColor = createTransparentDefaultColor();
assertThat(RippleUtils.sanitizeRippleDrawableColor(rippleColor)).isEqualTo(rippleColor);
logged.expectLogMessage(
Log.WARN, RippleUtils.LOG_TAG, RippleUtils.TRANSPARENT_DEFAULT_COLOR_WARNING);
}
@Test
@Config(sdk = VERSION_CODES.P)
public void testValidateColor_transparentDefaultColor_returnsSelf_pie_noWarning() {
ColorStateList rippleColor = createTransparentDefaultColor();
assertThat(RippleUtils.sanitizeRippleDrawableColor(rippleColor)).isEqualTo(rippleColor);
ShadowLog.getLogsForTag(RippleUtils.LOG_TAG).isEmpty();
}
private ColorStateList createTransparentDefaultColor() {
return new ColorStateList(
new int[][] {new int[] {android.R.attr.state_pressed}, new int[]{}},
new int[] {Color.BLUE, Color.TRANSPARENT});
}
}