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