NotificationInflaterTest.java revision 25a52b65b2cac1f49f37f4532cfa62282432957a
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.systemui.statusbar.notification;
18
19import static com.android.systemui.statusbar.notification.NotificationInflater.FLAG_REINFLATE_ALL;
20
21import static org.mockito.Mockito.spy;
22import static org.mockito.Mockito.times;
23import static org.mockito.Mockito.verify;
24
25import android.app.Notification;
26import android.content.Context;
27import android.service.notification.StatusBarNotification;
28import android.support.test.InstrumentationRegistry;
29import android.support.test.annotation.UiThreadTest;
30import android.support.test.filters.FlakyTest;
31import android.support.test.filters.SmallTest;
32import android.support.test.runner.AndroidJUnit4;
33import android.widget.RemoteViews;
34
35import com.android.systemui.R;
36import com.android.systemui.SysuiTestCase;
37import com.android.systemui.statusbar.ExpandableNotificationRow;
38import com.android.systemui.statusbar.NotificationData;
39import com.android.systemui.statusbar.NotificationTestHelper;
40
41import org.junit.Assert;
42import org.junit.Before;
43import org.junit.Ignore;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46
47import java.util.concurrent.CountDownLatch;
48
49@SmallTest
50@RunWith(AndroidJUnit4.class)
51public class NotificationInflaterTest extends SysuiTestCase {
52
53    private NotificationInflater mNotificationInflater;
54    private Notification.Builder mBuilder;
55    private ExpandableNotificationRow mRow;
56
57    @Before
58    public void setUp() throws Exception {
59        mBuilder = new Notification.Builder(mContext).setSmallIcon(
60                R.drawable.ic_person)
61                .setContentTitle("Title")
62                .setContentText("Text")
63                .setStyle(new Notification.BigTextStyle().bigText("big text"));
64        ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow(
65                mBuilder.build());
66        mRow = spy(row);
67        mNotificationInflater = new NotificationInflater(mRow);
68        mNotificationInflater.setInflationCallback(new NotificationInflater.InflationCallback() {
69            @Override
70            public void handleInflationException(StatusBarNotification notification,
71                    Exception e) {
72            }
73
74            @Override
75            public void onAsyncInflationFinished(NotificationData.Entry entry) {
76            }
77        });
78    }
79
80    @Test
81    @UiThreadTest
82    public void testIncreasedHeadsUpBeingUsed() {
83        mNotificationInflater.setUsesIncreasedHeadsUpHeight(true);
84        Notification.Builder builder = spy(mBuilder);
85        mNotificationInflater.inflateNotificationViews(FLAG_REINFLATE_ALL, builder, mContext);
86        verify(builder).createHeadsUpContentView(true);
87    }
88
89    @Test
90    @UiThreadTest
91    public void testIncreasedHeightBeingUsed() {
92        mNotificationInflater.setUsesIncreasedHeight(true);
93        Notification.Builder builder = spy(mBuilder);
94        mNotificationInflater.inflateNotificationViews(FLAG_REINFLATE_ALL, builder, mContext);
95        verify(builder).createContentView(true);
96    }
97
98    @Test
99    public void testInflationCallsUpdated() throws Exception {
100        runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
101                mNotificationInflater);
102        verify(mRow).onNotificationUpdated();
103    }
104
105    @Test
106    public void testInflationCallsOnlyRightMethod() throws Exception {
107        mRow.getPrivateLayout().removeAllViews();
108        mRow.getEntry().cachedBigContentView = null;
109        runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(
110                NotificationInflater.FLAG_REINFLATE_EXPANDED_VIEW), mNotificationInflater);
111        Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 1);
112        Assert.assertTrue(mRow.getPrivateLayout().getChildAt(0)
113                == mRow.getPrivateLayout().getExpandedChild());
114        verify(mRow).onNotificationUpdated();
115    }
116
117    @Test
118    public void testInflationThrowsErrorDoesntCallUpdated() throws Exception {
119        mRow.getPrivateLayout().removeAllViews();
120        mRow.getStatusBarNotification().getNotification().contentView
121                = new RemoteViews(mContext.getPackageName(), R.layout.status_bar);
122        runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
123                true /* expectingException */, mNotificationInflater);
124        Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 0);
125        verify(mRow, times(0)).onNotificationUpdated();
126    }
127
128    @Test
129    public void testAsyncTaskRemoved() throws Exception {
130        mRow.getEntry().abortTask();
131        runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
132                mNotificationInflater);
133        Assert.assertNull(mRow.getEntry().getRunningTask() );
134    }
135
136    public static void runThenWaitForInflation(Runnable block,
137            NotificationInflater inflater) throws Exception {
138        runThenWaitForInflation(block, false /* expectingException */, inflater);
139    }
140
141    private static void runThenWaitForInflation(Runnable block, boolean expectingException,
142            NotificationInflater inflater) throws Exception {
143        com.android.systemui.util.Assert.isNotMainThread();
144        CountDownLatch countDownLatch = new CountDownLatch(1);
145        final ExceptionHolder exceptionHolder = new ExceptionHolder();
146        inflater.setInflationCallback(new NotificationInflater.InflationCallback() {
147            @Override
148            public void handleInflationException(StatusBarNotification notification,
149                    Exception e) {
150                if (!expectingException) {
151                    exceptionHolder.setException(e);
152                }
153                countDownLatch.countDown();
154            }
155
156            @Override
157            public void onAsyncInflationFinished(NotificationData.Entry entry) {
158                if (expectingException) {
159                    exceptionHolder.setException(new RuntimeException(
160                            "Inflation finished even though there should be an error"));
161                }
162                countDownLatch.countDown();
163            }
164        });
165        block.run();
166        countDownLatch.await();
167        if (exceptionHolder.mException != null) {
168            throw exceptionHolder.mException;
169        }
170    }
171
172    private static class ExceptionHolder {
173        private Exception mException;
174
175        public void setException(Exception exception) {
176            mException = exception;
177        }
178    }
179}
180