blob: 7eeae67c9fdfb2708cf638a861cb77f201977b97 [file] [log] [blame]
Selim Cineka7679b62017-05-10 16:33:25 -07001/*
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
17package com.android.systemui.statusbar.notification;
18
Robert Snoebergerfb1e9262019-05-01 11:39:09 -040019import static com.google.common.truth.Truth.assertThat;
20
Selim Cineka7679b62017-05-10 16:33:25 -070021import static org.junit.Assert.assertNotSame;
22import static org.mockito.ArgumentMatchers.any;
23import static org.mockito.ArgumentMatchers.anyBoolean;
24import static org.mockito.ArgumentMatchers.anyInt;
25import static org.mockito.Mockito.spy;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.verifyZeroInteractions;
28
Robert Snoebergerfb1e9262019-05-01 11:39:09 -040029import android.annotation.Nullable;
Selim Cineka7679b62017-05-10 16:33:25 -070030import android.app.Notification;
31import android.graphics.Bitmap;
Robert Snoebergerfb1e9262019-05-01 11:39:09 -040032import android.graphics.Canvas;
33import android.graphics.Color;
Selim Cineka7679b62017-05-10 16:33:25 -070034import android.graphics.drawable.Drawable;
Selim Cineka7679b62017-05-10 16:33:25 -070035import android.test.suitebuilder.annotation.SmallTest;
36import android.widget.RemoteViews;
37
Robert Snoebergerfb1e9262019-05-01 11:39:09 -040038import androidx.palette.graphics.Palette;
Brett Chabot84151d92019-02-27 15:37:59 -080039import androidx.test.runner.AndroidJUnit4;
40
Selim Cineka7679b62017-05-10 16:33:25 -070041import com.android.systemui.R;
42import com.android.systemui.SysuiTestCase;
43
Robert Snoebergerfb1e9262019-05-01 11:39:09 -040044import org.junit.After;
Selim Cineka7679b62017-05-10 16:33:25 -070045import org.junit.Before;
46import org.junit.Test;
47import org.junit.runner.RunWith;
48
49@SmallTest
50@RunWith(AndroidJUnit4.class)
51public class MediaNotificationProcessorTest extends SysuiTestCase {
52
Robert Snoebergerfb1e9262019-05-01 11:39:09 -040053 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 Cineka7679b62017-05-10 16:33:25 -070061 private MediaNotificationProcessor mProcessor;
62 private Bitmap mBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
63 private ImageGradientColorizer mColorizer;
Robert Snoebergerfb1e9262019-05-01 11:39:09 -040064 @Nullable private Bitmap mArtwork;
Selim Cineka7679b62017-05-10 16:33:25 -070065
66 @Before
67 public void setUp() {
68 mColorizer = spy(new TestableColorizer(mBitmap));
69 mProcessor = new MediaNotificationProcessor(getContext(), getContext(), mColorizer);
70 }
71
Robert Snoebergerfb1e9262019-05-01 11:39:09 -040072 @After
73 public void tearDown() {
74 if (mArtwork != null) {
75 mArtwork.recycle();
76 mArtwork = null;
77 }
78 }
79
Selim Cineka7679b62017-05-10 16:33:25 -070080 @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 Snoebergerfb1e9262019-05-01 11:39:09 -0400127 @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 Cineka7679b62017-05-10 16:33:25 -0700157 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}