OSDN Git Service

Merge "Allow setup apps to colorize notifications." into oc-dr1-dev
[android-x86/frameworks-base.git] / core / tests / coretests / src / android / app / NotificationTest.java
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 android.app;
18
19 import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
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;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33
34 @RunWith(AndroidJUnit4.class)
35 @SmallTest
36 public class NotificationTest {
37
38     private Context mContext;
39
40     @Before
41     public void setUp() {
42         mContext = InstrumentationRegistry.getContext();
43     }
44
45     @Test
46     public void testColorizedByPermission() {
47         Notification n = new Notification.Builder(mContext, "test")
48                 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
49                 .setColorized(true)
50                 .build();
51         assertTrue(n.isColorized());
52
53         n = new Notification.Builder(mContext, "test")
54                 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
55                 .build();
56         assertFalse(n.isColorized());
57
58         n = new Notification.Builder(mContext, "test")
59                 .setFlag(Notification.FLAG_CAN_COLORIZE, false)
60                 .setColorized(true)
61                 .build();
62         assertFalse(n.isColorized());
63     }
64
65     @Test
66     public void testColorizedByForeground() {
67         Notification n = new Notification.Builder(mContext, "test")
68                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
69                 .setColorized(true)
70                 .build();
71         assertTrue(n.isColorized());
72
73         n = new Notification.Builder(mContext, "test")
74                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
75                 .build();
76         assertFalse(n.isColorized());
77
78         n = new Notification.Builder(mContext, "test")
79                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, false)
80                 .setColorized(true)
81                 .build();
82         assertFalse(n.isColorized());
83     }
84
85     @Test
86     public void testColorSatisfiedWhenBgDarkTextDarker() {
87         Notification.Builder builder = getMediaNotification();
88         Notification n = builder.build();
89
90         assertTrue(n.isColorized());
91
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));
100     }
101
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()));
109     }
110 }