2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
19 import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
24 import android.content.Context;
25 import android.media.session.MediaSession;
26 import android.support.test.InstrumentationRegistry;
27 import android.support.test.filters.SmallTest;
28 import android.support.test.runner.AndroidJUnit4;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
34 @RunWith(AndroidJUnit4.class)
36 public class NotificationTest {
38 private Context mContext;
42 mContext = InstrumentationRegistry.getContext();
46 public void testColorizedByPermission() {
47 Notification n = new Notification.Builder(mContext, "test")
48 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
51 assertTrue(n.isColorized());
53 n = new Notification.Builder(mContext, "test")
54 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
56 assertFalse(n.isColorized());
58 n = new Notification.Builder(mContext, "test")
59 .setFlag(Notification.FLAG_CAN_COLORIZE, false)
62 assertFalse(n.isColorized());
66 public void testColorizedByForeground() {
67 Notification n = new Notification.Builder(mContext, "test")
68 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
71 assertTrue(n.isColorized());
73 n = new Notification.Builder(mContext, "test")
74 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
76 assertFalse(n.isColorized());
78 n = new Notification.Builder(mContext, "test")
79 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, false)
82 assertFalse(n.isColorized());
86 public void testColorSatisfiedWhenBgDarkTextDarker() {
87 Notification.Builder builder = getMediaNotification();
88 Notification n = builder.build();
90 assertTrue(n.isColorized());
92 // An initial guess where the foreground color is actually darker than an already dark bg
93 int backgroundColor = 0xff585868;
94 int initialForegroundColor = 0xff505868;
95 builder.setColorPalette(backgroundColor, initialForegroundColor);
96 int primaryTextColor = builder.getPrimaryTextColor();
97 assertTrue(satisfiesTextContrast(primaryTextColor, backgroundColor));
98 int secondaryTextColor = builder.getSecondaryTextColor();
99 assertTrue(satisfiesTextContrast(secondaryTextColor, backgroundColor));
102 private Notification.Builder getMediaNotification() {
103 MediaSession session = new MediaSession(mContext, "test");
104 return new Notification.Builder(mContext, "color")
105 .setSmallIcon(com.android.internal.R.drawable.emergency_icon)
106 .setContentTitle("Title")
107 .setContentText("Text")
108 .setStyle(new Notification.MediaStyle().setMediaSession(session.getSessionToken()));