From d36b7acceded332bc7701695c4d5096937f403ca Mon Sep 17 00:00:00 2001 From: Beverly Date: Wed, 17 Feb 2021 10:13:23 -0500 Subject: [PATCH] Add test to allow icon-only messages For the KeyguardIndication Test: atest KeyguardIndicationTest Fixes: 180101407 Change-Id: Ie46d630d87f2a566d3b480efe173b15109b4d7ca --- .../systemui/keyguard/KeyguardIndication.java | 24 +++- ...ardIndicationRotateTextViewController.java | 8 +- .../keyguard/KeyguardIndicationTest.java | 110 ++++++++++++++++++ 3 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 packages/SystemUI/tests/src/com/android/systemui/keyguard/KeyguardIndicationTest.java diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardIndication.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardIndication.java index d65d169511918..2873cd36409db 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardIndication.java +++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardIndication.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.content.res.ColorStateList; import android.graphics.drawable.Drawable; +import android.text.TextUtils; import android.view.View; /** @@ -56,7 +57,7 @@ public class KeyguardIndication { /** * Message to display */ - public @NonNull CharSequence getMessage() { + public @Nullable CharSequence getMessage() { return mMessage; } @@ -88,6 +89,17 @@ public class KeyguardIndication { return mBackground; } + @Override + public String toString() { + String str = "KeyguardIndication{"; + if (!TextUtils.isEmpty(mMessage)) str += "mMessage=" + mMessage; + if (mIcon != null) str += " mIcon=" + mIcon; + if (mOnClickListener != null) str += " mOnClickListener=" + mOnClickListener; + if (mBackground != null) str += " mBackground=" + mBackground; + str += "}"; + return str; + } + /** * KeyguardIndication Builder */ @@ -101,7 +113,7 @@ public class KeyguardIndication { public Builder() { } /** - * Required field. Message to display. + * Message to display. Indication requires a non-null message or icon. */ public Builder setMessage(@NonNull CharSequence message) { this.mMessage = message; @@ -117,9 +129,9 @@ public class KeyguardIndication { } /** - * Optional. Icon to show next to the text. Icon location changes based on language - * display direction. For LTR, icon shows to the left of the message. For RTL, icon shows - * to the right of the message. + * Icon to show next to the text. Indication requires a non-null icon or message. + * Icon location changes based on language display direction. For LTR, icon shows to the + * left of the message. For RTL, icon shows to the right of the message. */ public Builder setIcon(Drawable icon) { this.mIcon = icon; @@ -146,7 +158,7 @@ public class KeyguardIndication { * Build the KeyguardIndication. */ public KeyguardIndication build() { - if (mMessage == null && mIcon == null) { + if (TextUtils.isEmpty(mMessage) && mIcon == null) { throw new IllegalStateException("message or icon must be set"); } if (mTextColor == null) { diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardIndicationRotateTextViewController.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardIndicationRotateTextViewController.java index 2e599de1970d6..d4678f39e404e 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardIndicationRotateTextViewController.java +++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardIndicationRotateTextViewController.java @@ -19,7 +19,6 @@ package com.android.systemui.keyguard; import android.annotation.Nullable; import android.content.res.ColorStateList; import android.graphics.Color; -import android.text.TextUtils; import android.view.View; import androidx.annotation.IntDef; @@ -105,9 +104,7 @@ public class KeyguardIndicationRotateTextViewController extends public void updateIndication(@IndicationType int type, KeyguardIndication newIndication, boolean showImmediately) { final boolean hasPreviousIndication = mIndicationMessages.get(type) != null; - final boolean hasNewIndication = newIndication != null - && (!TextUtils.isEmpty(newIndication.getMessage()) - || newIndication.getIcon() != null); + final boolean hasNewIndication = newIndication != null; if (!hasNewIndication) { mIndicationMessages.remove(type); mIndicationQueue.removeIf(x -> x == type); @@ -289,8 +286,7 @@ public class KeyguardIndicationRotateTextViewController extends if (hasIndications()) { pw.println(" All messages:"); for (int type : mIndicationMessages.keySet()) { - pw.println(" type=" + type - + " message=" + mIndicationMessages.get(type).getMessage()); + pw.println(" type=" + type + " " + mIndicationMessages.get(type)); } } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/KeyguardIndicationTest.java b/packages/SystemUI/tests/src/com/android/systemui/keyguard/KeyguardIndicationTest.java new file mode 100644 index 0000000000000..b44fb8e61c32d --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/KeyguardIndicationTest.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2021 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.systemui.keyguard; + +import static android.graphics.Color.WHITE; + +import static org.junit.Assert.assertEquals; + +import android.content.res.ColorStateList; +import android.graphics.Canvas; +import android.graphics.ColorFilter; +import android.graphics.drawable.Drawable; +import android.testing.AndroidTestingRunner; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.test.filters.SmallTest; + +import com.android.systemui.SysuiTestCase; + +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidTestingRunner.class) +@SmallTest +public class KeyguardIndicationTest extends SysuiTestCase { + + @Test(expected = IllegalStateException.class) + public void testCannotCreateIndicationWithoutMessageOrIcon() { + new KeyguardIndication.Builder() + .setTextColor(ColorStateList.valueOf(WHITE)) + .build(); + } + + @Test(expected = IllegalStateException.class) + public void testCannotCreateIndicationWithoutColor() { + new KeyguardIndication.Builder() + .setMessage("message") + .build(); + } + + @Test(expected = IllegalStateException.class) + public void testCannotCreateIndicationWithEmptyMessage() { + new KeyguardIndication.Builder() + .setMessage("") + .setTextColor(ColorStateList.valueOf(WHITE)) + .build(); + } + + @Test + public void testCreateIndicationWithMessage() { + final String text = "regular indication"; + final KeyguardIndication indication = new KeyguardIndication.Builder() + .setMessage(text) + .setTextColor(ColorStateList.valueOf(WHITE)) + .build(); + assertEquals(text, indication.getMessage()); + } + + @Test + public void testCreateIndicationWithIcon() { + final KeyguardIndication indication = new KeyguardIndication.Builder() + .setIcon(mDrawable) + .setTextColor(ColorStateList.valueOf(WHITE)) + .build(); + assertEquals(mDrawable, indication.getIcon()); + } + + @Test + public void testCreateIndicationWithMessageAndIcon() { + final String text = "indication with msg and icon"; + final KeyguardIndication indication = new KeyguardIndication.Builder() + .setMessage(text) + .setIcon(mDrawable) + .setTextColor(ColorStateList.valueOf(WHITE)) + .build(); + assertEquals(text, indication.getMessage()); + assertEquals(mDrawable, indication.getIcon()); + } + + final Drawable mDrawable = new Drawable() { + @Override + public void draw(@NonNull Canvas canvas) { } + + @Override + public void setAlpha(int alpha) { } + + @Override + public void setColorFilter(@Nullable ColorFilter colorFilter) { } + + @Override + public int getOpacity() { + return 0; + } + }; +}