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 com.android.server.notification;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertNotNull;
21import static junit.framework.Assert.assertNull;
22
23import static org.junit.Assert.assertTrue;
24import static org.mockito.Mockito.spy;
25import static org.mockito.Mockito.when;
26
27import android.app.ActivityManager;
28import android.app.Notification;
29import android.content.Context;
30import android.content.pm.ApplicationInfo;
31import android.graphics.Color;
32import android.os.Build;
33import android.support.test.filters.SmallTest;
34import android.support.test.runner.AndroidJUnit4;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.MockitoAnnotations;
41
42@RunWith(AndroidJUnit4.class)
43@SmallTest
44public class NotificationTest extends NotificationTestCase {
45
46    @Mock
47    ActivityManager mAm;
48
49    @Before
50    public void setUp() {
51        MockitoAnnotations.initMocks(this);
52    }
53
54    @Test
55    public void testStripsExtendersInLowRamMode() {
56        Notification.Builder nb = new Notification.Builder(mContext, "channel");
57        nb.extend(new Notification.CarExtender().setColor(Color.RED));
58        nb.extend(new Notification.TvExtender().setChannelId("different channel"));
59        nb.extend(new Notification.WearableExtender().setDismissalId("dismiss"));
60        Notification before = nb.build();
61
62        Notification after = Notification.Builder.maybeCloneStrippedForDelivery(before, true);
63
64        assertEquals("different channel", new Notification.TvExtender(before).getChannelId());
65        assertNull(new Notification.TvExtender(after).getChannelId());
66
67        assertEquals(Color.RED, new Notification.CarExtender(before).getColor());
68        assertEquals(Notification.COLOR_DEFAULT, new Notification.CarExtender(after).getColor());
69
70        assertEquals("dismiss", new Notification.WearableExtender(before).getDismissalId());
71        assertNull(new Notification.WearableExtender(after).getDismissalId());
72    }
73
74    @Test
75    public void testStripsRemoteViewsInLowRamMode() {
76        Context context = spy(getContext());
77        ApplicationInfo ai = new ApplicationInfo();
78        ai.targetSdkVersion = Build.VERSION_CODES.M;
79        when(context.getApplicationInfo()).thenReturn(ai);
80
81        final Notification.BigTextStyle style = new Notification.BigTextStyle()
82                .bigText("hello")
83                .setSummaryText("And the summary");
84        Notification before = new Notification.Builder(context, "channel")
85                .setContentText("hi")
86                .setStyle(style)
87                .build();
88
89        Notification after = Notification.Builder.maybeCloneStrippedForDelivery(before, true);
90        assertNotNull(before.contentView);
91        assertNotNull(before.bigContentView);
92        assertNotNull(before.headsUpContentView);
93        assertNull(after.contentView);
94        assertNull(after.bigContentView);
95        assertNull(after.headsUpContentView);
96    }
97
98    @Test
99    public void testDoesNotStripsExtendersInNormalRamMode() {
100        Notification.Builder nb = new Notification.Builder(mContext, "channel");
101        nb.extend(new Notification.CarExtender().setColor(Color.RED));
102        nb.extend(new Notification.TvExtender().setChannelId("different channel"));
103        nb.extend(new Notification.WearableExtender().setDismissalId("dismiss"));
104        Notification before = nb.build();
105        Notification after = Notification.Builder.maybeCloneStrippedForDelivery(before, false);
106
107        assertTrue(before == after);
108
109        assertEquals("different channel", new Notification.TvExtender(before).getChannelId());
110        assertEquals(Color.RED, new Notification.CarExtender(before).getColor());
111        assertEquals("dismiss", new Notification.WearableExtender(before).getDismissalId());
112    }
113}
114
115