Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 com.android.systemui.statusbar.notification; |
| 18 | |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.assertNotSame; |
| 22 | import static org.mockito.ArgumentMatchers.any; |
| 23 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.Mockito.spy; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.verifyZeroInteractions; |
| 28 | |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 29 | import android.annotation.Nullable; |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 30 | import android.app.Notification; |
| 31 | import android.graphics.Bitmap; |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 32 | import android.graphics.Canvas; |
| 33 | import android.graphics.Color; |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 35 | import android.test.suitebuilder.annotation.SmallTest; |
| 36 | import android.widget.RemoteViews; |
| 37 | |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 38 | import androidx.palette.graphics.Palette; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 39 | import androidx.test.runner.AndroidJUnit4; |
| 40 | |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 41 | import com.android.systemui.R; |
| 42 | import com.android.systemui.SysuiTestCase; |
| 43 | |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 44 | import org.junit.After; |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | |
| 49 | @SmallTest |
| 50 | @RunWith(AndroidJUnit4.class) |
| 51 | public class MediaNotificationProcessorTest extends SysuiTestCase { |
| 52 | |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 53 | private static final int BITMAP_WIDTH = 10; |
| 54 | private static final int BITMAP_HEIGHT = 10; |
| 55 | |
| 56 | /** |
| 57 | * Color tolerance is borrowed from the AndroidX test utilities for Palette. |
| 58 | */ |
| 59 | private static final int COLOR_TOLERANCE = 8; |
| 60 | |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 61 | private MediaNotificationProcessor mProcessor; |
| 62 | private Bitmap mBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
| 63 | private ImageGradientColorizer mColorizer; |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 64 | @Nullable private Bitmap mArtwork; |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 65 | |
| 66 | @Before |
| 67 | public void setUp() { |
| 68 | mColorizer = spy(new TestableColorizer(mBitmap)); |
| 69 | mProcessor = new MediaNotificationProcessor(getContext(), getContext(), mColorizer); |
| 70 | } |
| 71 | |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 72 | @After |
| 73 | public void tearDown() { |
| 74 | if (mArtwork != null) { |
| 75 | mArtwork.recycle(); |
| 76 | mArtwork = null; |
| 77 | } |
| 78 | } |
| 79 | |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 80 | @Test |
| 81 | public void testColorizedWithLargeIcon() { |
| 82 | Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon( |
| 83 | R.drawable.ic_person) |
| 84 | .setContentTitle("Title") |
| 85 | .setLargeIcon(mBitmap) |
| 86 | .setContentText("Text"); |
| 87 | Notification notification = builder.build(); |
| 88 | mProcessor.processNotification(notification, builder); |
| 89 | verify(mColorizer).colorize(any(), anyInt(), anyBoolean()); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testNotColorizedWithoutLargeIcon() { |
| 94 | Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon( |
| 95 | R.drawable.ic_person) |
| 96 | .setContentTitle("Title") |
| 97 | .setContentText("Text"); |
| 98 | Notification notification = builder.build(); |
| 99 | mProcessor.processNotification(notification, builder); |
| 100 | verifyZeroInteractions(mColorizer); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testRemoteViewsReset() { |
| 105 | Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon( |
| 106 | R.drawable.ic_person) |
| 107 | .setContentTitle("Title") |
| 108 | .setStyle(new Notification.MediaStyle()) |
| 109 | .setLargeIcon(mBitmap) |
| 110 | .setContentText("Text"); |
| 111 | Notification notification = builder.build(); |
| 112 | RemoteViews remoteViews = new RemoteViews(getContext().getPackageName(), |
| 113 | R.layout.custom_view_dark); |
| 114 | notification.contentView = remoteViews; |
| 115 | notification.bigContentView = remoteViews; |
| 116 | notification.headsUpContentView = remoteViews; |
| 117 | mProcessor.processNotification(notification, builder); |
| 118 | verify(mColorizer).colorize(any(), anyInt(), anyBoolean()); |
| 119 | RemoteViews contentView = builder.createContentView(); |
| 120 | assertNotSame(contentView, remoteViews); |
| 121 | contentView = builder.createBigContentView(); |
| 122 | assertNotSame(contentView, remoteViews); |
| 123 | contentView = builder.createHeadsUpContentView(); |
| 124 | assertNotSame(contentView, remoteViews); |
| 125 | } |
| 126 | |
Robert Snoeberger | fb1e926 | 2019-05-01 11:39:09 -0400 | [diff] [blame] | 127 | @Test |
| 128 | public void findBackgroundSwatch_white() { |
| 129 | // Given artwork that is completely white. |
| 130 | mArtwork = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888); |
| 131 | Canvas canvas = new Canvas(mArtwork); |
| 132 | canvas.drawColor(Color.WHITE); |
| 133 | // WHEN the background swatch is computed |
| 134 | Palette.Swatch swatch = MediaNotificationProcessor.findBackgroundSwatch(mArtwork); |
| 135 | // THEN the swatch color is white |
| 136 | assertCloseColors(swatch.getRgb(), Color.WHITE); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void findBackgroundSwatch_red() { |
| 141 | // Given artwork that is completely red. |
| 142 | mArtwork = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888); |
| 143 | Canvas canvas = new Canvas(mArtwork); |
| 144 | canvas.drawColor(Color.RED); |
| 145 | // WHEN the background swatch is computed |
| 146 | Palette.Swatch swatch = MediaNotificationProcessor.findBackgroundSwatch(mArtwork); |
| 147 | // THEN the swatch color is red |
| 148 | assertCloseColors(swatch.getRgb(), Color.RED); |
| 149 | } |
| 150 | |
| 151 | static void assertCloseColors(int expected, int actual) { |
| 152 | assertThat((float) Color.red(expected)).isWithin(COLOR_TOLERANCE).of(Color.red(actual)); |
| 153 | assertThat((float) Color.green(expected)).isWithin(COLOR_TOLERANCE).of(Color.green(actual)); |
| 154 | assertThat((float) Color.blue(expected)).isWithin(COLOR_TOLERANCE).of(Color.blue(actual)); |
| 155 | } |
| 156 | |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 157 | public static class TestableColorizer extends ImageGradientColorizer { |
| 158 | private final Bitmap mBitmap; |
| 159 | |
| 160 | private TestableColorizer(Bitmap bitmap) { |
| 161 | mBitmap = bitmap; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Bitmap colorize(Drawable drawable, int backgroundColor, boolean isRtl) { |
| 166 | return mBitmap; |
| 167 | } |
| 168 | } |
| 169 | } |