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