1a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek/*
2a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * Copyright (C) 2017 The Android Open Source Project
3a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek *
4a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * Licensed under the Apache License, Version 2.0 (the "License");
5a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * you may not use this file except in compliance with the License.
6a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * You may obtain a copy of the License at
7a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek *
8a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek *      http://www.apache.org/licenses/LICENSE-2.0
9a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek *
10a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * Unless required by applicable law or agreed to in writing, software
11a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * distributed under the License is distributed on an "AS IS" BASIS,
12a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * See the License for the specific language governing permissions and
14a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * limitations under the License
15a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek */
16a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
17a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekpackage com.android.systemui.statusbar.notification;
18a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
19a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport static org.junit.Assert.assertNotSame;
20a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport static org.mockito.ArgumentMatchers.any;
21a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport static org.mockito.ArgumentMatchers.anyBoolean;
22a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport static org.mockito.ArgumentMatchers.anyInt;
23a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport static org.mockito.Mockito.spy;
24a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport static org.mockito.Mockito.verify;
25a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport static org.mockito.Mockito.verifyZeroInteractions;
26a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
27a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport android.app.Notification;
28a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport android.graphics.Bitmap;
29a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport android.graphics.drawable.Drawable;
30a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport android.support.test.runner.AndroidJUnit4;
31a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport android.test.suitebuilder.annotation.SmallTest;
32a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport android.widget.RemoteViews;
33a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
34a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport com.android.systemui.R;
35a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport com.android.systemui.SysuiTestCase;
36a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
37a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport org.junit.Before;
38a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport org.junit.Test;
39a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekimport org.junit.runner.RunWith;
40a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
41a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek@SmallTest
42a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek@RunWith(AndroidJUnit4.class)
43a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekpublic class MediaNotificationProcessorTest extends SysuiTestCase {
44a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
45a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    private MediaNotificationProcessor mProcessor;
46a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    private Bitmap mBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
47a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    private ImageGradientColorizer mColorizer;
48a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
49a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    @Before
50a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    public void setUp() {
51a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        mColorizer = spy(new TestableColorizer(mBitmap));
52a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        mProcessor = new MediaNotificationProcessor(getContext(), getContext(), mColorizer);
53a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    }
54a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
55a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    @Test
56a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    public void testColorizedWithLargeIcon() {
57a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon(
58a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                R.drawable.ic_person)
59a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setContentTitle("Title")
60a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setLargeIcon(mBitmap)
61a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setContentText("Text");
62a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        Notification notification = builder.build();
63a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        mProcessor.processNotification(notification, builder);
64a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        verify(mColorizer).colorize(any(), anyInt(), anyBoolean());
65a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    }
66a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
67a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    @Test
68a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    public void testNotColorizedWithoutLargeIcon() {
69a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon(
70a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                R.drawable.ic_person)
71a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setContentTitle("Title")
72a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setContentText("Text");
73a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        Notification notification = builder.build();
74a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        mProcessor.processNotification(notification, builder);
75a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        verifyZeroInteractions(mColorizer);
76a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    }
77a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
78a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    @Test
79a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    public void testRemoteViewsReset() {
80a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon(
81a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                R.drawable.ic_person)
82a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setContentTitle("Title")
83a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setStyle(new Notification.MediaStyle())
84a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setLargeIcon(mBitmap)
85a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                .setContentText("Text");
86a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        Notification notification = builder.build();
87a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        RemoteViews remoteViews = new RemoteViews(getContext().getPackageName(),
88a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek                R.layout.custom_view_dark);
89a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        notification.contentView = remoteViews;
90a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        notification.bigContentView = remoteViews;
91a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        notification.headsUpContentView = remoteViews;
92a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        mProcessor.processNotification(notification, builder);
93a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        verify(mColorizer).colorize(any(), anyInt(), anyBoolean());
94a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        RemoteViews contentView = builder.createContentView();
95a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        assertNotSame(contentView, remoteViews);
96a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        contentView = builder.createBigContentView();
97a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        assertNotSame(contentView, remoteViews);
98a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        contentView = builder.createHeadsUpContentView();
99a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        assertNotSame(contentView, remoteViews);
100a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    }
101a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
102a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    public static class TestableColorizer extends ImageGradientColorizer {
103a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        private final Bitmap mBitmap;
104a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
105a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        private TestableColorizer(Bitmap bitmap) {
106a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek            mBitmap = bitmap;
107a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        }
108a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek
109a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        @Override
110a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        public Bitmap colorize(Drawable drawable, int backgroundColor, boolean isRtl) {
111a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek            return mBitmap;
112a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek        }
113a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek    }
114a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek}
115