Home | History | Annotate | Download | only in notification
      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 com.android.systemui.statusbar.notification;
     18 
     19 import static org.junit.Assert.assertNotSame;
     20 import static org.mockito.ArgumentMatchers.any;
     21 import static org.mockito.ArgumentMatchers.anyBoolean;
     22 import static org.mockito.ArgumentMatchers.anyInt;
     23 import static org.mockito.Mockito.spy;
     24 import static org.mockito.Mockito.verify;
     25 import static org.mockito.Mockito.verifyZeroInteractions;
     26 
     27 import android.app.Notification;
     28 import android.graphics.Bitmap;
     29 import android.graphics.drawable.Drawable;
     30 import android.support.test.runner.AndroidJUnit4;
     31 import android.test.suitebuilder.annotation.SmallTest;
     32 import android.widget.RemoteViews;
     33 
     34 import com.android.systemui.R;
     35 import com.android.systemui.SysuiTestCase;
     36 
     37 import org.junit.Before;
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 
     41 @SmallTest
     42 @RunWith(AndroidJUnit4.class)
     43 public class MediaNotificationProcessorTest extends SysuiTestCase {
     44 
     45     private MediaNotificationProcessor mProcessor;
     46     private Bitmap mBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
     47     private ImageGradientColorizer mColorizer;
     48 
     49     @Before
     50     public void setUp() {
     51         mColorizer = spy(new TestableColorizer(mBitmap));
     52         mProcessor = new MediaNotificationProcessor(getContext(), getContext(), mColorizer);
     53     }
     54 
     55     @Test
     56     public void testColorizedWithLargeIcon() {
     57         Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon(
     58                 R.drawable.ic_person)
     59                 .setContentTitle("Title")
     60                 .setLargeIcon(mBitmap)
     61                 .setContentText("Text");
     62         Notification notification = builder.build();
     63         mProcessor.processNotification(notification, builder);
     64         verify(mColorizer).colorize(any(), anyInt(), anyBoolean());
     65     }
     66 
     67     @Test
     68     public void testNotColorizedWithoutLargeIcon() {
     69         Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon(
     70                 R.drawable.ic_person)
     71                 .setContentTitle("Title")
     72                 .setContentText("Text");
     73         Notification notification = builder.build();
     74         mProcessor.processNotification(notification, builder);
     75         verifyZeroInteractions(mColorizer);
     76     }
     77 
     78     @Test
     79     public void testRemoteViewsReset() {
     80         Notification.Builder builder = new Notification.Builder(getContext()).setSmallIcon(
     81                 R.drawable.ic_person)
     82                 .setContentTitle("Title")
     83                 .setStyle(new Notification.MediaStyle())
     84                 .setLargeIcon(mBitmap)
     85                 .setContentText("Text");
     86         Notification notification = builder.build();
     87         RemoteViews remoteViews = new RemoteViews(getContext().getPackageName(),
     88                 R.layout.custom_view_dark);
     89         notification.contentView = remoteViews;
     90         notification.bigContentView = remoteViews;
     91         notification.headsUpContentView = remoteViews;
     92         mProcessor.processNotification(notification, builder);
     93         verify(mColorizer).colorize(any(), anyInt(), anyBoolean());
     94         RemoteViews contentView = builder.createContentView();
     95         assertNotSame(contentView, remoteViews);
     96         contentView = builder.createBigContentView();
     97         assertNotSame(contentView, remoteViews);
     98         contentView = builder.createHeadsUpContentView();
     99         assertNotSame(contentView, remoteViews);
    100     }
    101 
    102     public static class TestableColorizer extends ImageGradientColorizer {
    103         private final Bitmap mBitmap;
    104 
    105         private TestableColorizer(Bitmap bitmap) {
    106             mBitmap = bitmap;
    107         }
    108 
    109         @Override
    110         public Bitmap colorize(Drawable drawable, int backgroundColor, boolean isRtl) {
    111             return mBitmap;
    112         }
    113     }
    114 }
    115