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
17package android.app;
18
19import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertTrue;
23
24import android.content.Context;
25import android.media.session.MediaSession;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34@RunWith(AndroidJUnit4.class)
35@SmallTest
36public 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    @Test
103    public void testHasCompletedProgress_noProgress() {
104        Notification n = new Notification.Builder(mContext).build();
105
106        assertFalse(n.hasCompletedProgress());
107    }
108
109    @Test
110    public void testHasCompletedProgress_complete() {
111        Notification n = new Notification.Builder(mContext)
112                .setProgress(100, 100, true)
113                .build();
114        Notification n2 = new Notification.Builder(mContext)
115                .setProgress(10, 10, false)
116                .build();
117        assertTrue(n.hasCompletedProgress());
118        assertTrue(n2.hasCompletedProgress());
119    }
120
121    @Test
122    public void testHasCompletedProgress_notComplete() {
123        Notification n = new Notification.Builder(mContext)
124                .setProgress(100, 99, true)
125                .build();
126        Notification n2 = new Notification.Builder(mContext)
127                .setProgress(10, 4, false)
128                .build();
129        assertFalse(n.hasCompletedProgress());
130        assertFalse(n2.hasCompletedProgress());
131    }
132
133    @Test
134    public void testHasCompletedProgress_zeroMax() {
135        Notification n = new Notification.Builder(mContext)
136                .setProgress(0, 0, true)
137                .build();
138        assertFalse(n.hasCompletedProgress());
139    }
140
141    private Notification.Builder getMediaNotification() {
142        MediaSession session = new MediaSession(mContext, "test");
143        return new Notification.Builder(mContext, "color")
144                .setSmallIcon(com.android.internal.R.drawable.emergency_icon)
145                .setContentTitle("Title")
146                .setContentText("Text")
147                .setStyle(new Notification.MediaStyle().setMediaSession(session.getSessionToken()));
148    }
149}
150