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.assertTrue; 22 23import android.content.Context; 24import android.media.session.MediaSession; 25import android.support.test.InstrumentationRegistry; 26import android.support.test.filters.SmallTest; 27import android.support.test.runner.AndroidJUnit4; 28 29import com.android.internal.util.NotificationColorUtil; 30 31import org.junit.Before; 32import org.junit.Test; 33import org.junit.runner.RunWith; 34 35@RunWith(AndroidJUnit4.class) 36@SmallTest 37public class NotificationTest { 38 39 private Context mContext; 40 41 @Before 42 public void setUp() { 43 mContext = InstrumentationRegistry.getContext(); 44 } 45 46 @Test 47 public void testColorSatisfiedWhenBgDarkTextDarker() { 48 Notification.Builder builder = getMediaNotification(); 49 builder.build(); 50 51 // An initial guess where the foreground color is actually darker than an already dark bg 52 int backgroundColor = 0xff585868; 53 int initialForegroundColor = 0xff505868; 54 builder.setColorPalette(backgroundColor, initialForegroundColor); 55 int primaryTextColor = builder.getPrimaryTextColor(); 56 assertTrue(satisfiesTextContrast(primaryTextColor, backgroundColor)); 57 int secondaryTextColor = builder.getSecondaryTextColor(); 58 assertTrue(satisfiesTextContrast(secondaryTextColor, backgroundColor)); 59 } 60 61 private Notification.Builder getMediaNotification() { 62 MediaSession session = new MediaSession(mContext, "test"); 63 return new Notification.Builder(mContext, "color") 64 .setSmallIcon(com.android.internal.R.drawable.emergency_icon) 65 .setContentTitle("Title") 66 .setContentText("Text") 67 .setStyle(new Notification.MediaStyle().setMediaSession(session.getSessionToken())); 68 } 69} 70