• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.car.notification;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.testng.Assert.assertThrows;
22 
23 import android.content.Context;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 
27 import androidx.annotation.LayoutRes;
28 import androidx.test.core.app.ApplicationProvider;
29 import androidx.test.ext.junit.runners.AndroidJUnit4;
30 
31 import com.android.car.notification.template.BasicNotificationViewHolder;
32 import com.android.car.notification.template.CallNotificationViewHolder;
33 import com.android.car.notification.template.CarNotificationBaseViewHolder;
34 import com.android.car.notification.template.GroupNotificationViewHolder;
35 import com.android.car.notification.template.GroupSummaryNotificationViewHolder;
36 import com.android.car.notification.template.InboxNotificationViewHolder;
37 import com.android.car.notification.template.MessageNotificationViewHolder;
38 import com.android.car.notification.template.NavigationNotificationViewHolder;
39 import com.android.car.notification.template.ProgressNotificationViewHolder;
40 
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 
45 @RunWith(AndroidJUnit4.class)
46 public class CarNotificationTypeItemTest {
47     // No template is represented by -1 in CarNotificationTypeItem.
48     private static final int NO_TEMPLATE = -1;
49 
50     private final Context mApplicationContext = ApplicationProvider.getApplicationContext();
51 
52     @Mock
53     private NotificationClickHandlerFactory mClickHandlerFactory;
54 
55     @Test
navigationNotificationType_shouldHaveCorrectValues()56     public void navigationNotificationType_shouldHaveCorrectValues() {
57         CarNotificationTypeItem navigation = CarNotificationTypeItem.of(
58                 NotificationViewType.NAVIGATION);
59         assertThat(navigation.getNotificationType()).isEqualTo(NotificationViewType.NAVIGATION);
60 
61         assertProperties(navigation, R.layout.navigation_headsup_notification_template,
62                 NO_TEMPLATE, NavigationNotificationViewHolder.class);
63     }
64 
65     @Test
callNotificationType_shouldHaveCorrectValues()66     public void callNotificationType_shouldHaveCorrectValues() {
67         CarNotificationTypeItem call = CarNotificationTypeItem.of(
68                 NotificationViewType.CALL);
69         assertThat(call.getNotificationType()).isEqualTo(NotificationViewType.CALL);
70 
71         assertProperties(call, R.layout.call_headsup_notification_template,
72                 R.layout.call_notification_template, CallNotificationViewHolder.class);
73     }
74 
75     @Test
messageNotificationType_shouldHaveCorrectValues()76     public void messageNotificationType_shouldHaveCorrectValues() {
77         CarNotificationTypeItem message = CarNotificationTypeItem.of(
78                 NotificationViewType.MESSAGE);
79         assertThat(message.getNotificationType()).isEqualTo(NotificationViewType.MESSAGE);
80 
81         assertProperties(message, R.layout.message_headsup_notification_template,
82                 R.layout.message_notification_template, MessageNotificationViewHolder.class);
83     }
84 
85     @Test
messageInGroupNotificationType_shouldHaveCorrectValues()86     public void messageInGroupNotificationType_shouldHaveCorrectValues() {
87         CarNotificationTypeItem messageInGroup = CarNotificationTypeItem.of(
88                 NotificationViewType.MESSAGE_IN_GROUP);
89         assertThat(messageInGroup.getNotificationType()).isEqualTo(
90                 NotificationViewType.MESSAGE_IN_GROUP);
91 
92         assertProperties(messageInGroup, NO_TEMPLATE, R.layout.message_notification_template_inner,
93                 MessageNotificationViewHolder.class);
94     }
95 
96     @Test
inboxNotificationType_shouldHaveCorrectValues()97     public void inboxNotificationType_shouldHaveCorrectValues() {
98         CarNotificationTypeItem inbox = CarNotificationTypeItem.of(
99                 NotificationViewType.INBOX);
100         assertThat(inbox.getNotificationType()).isEqualTo(NotificationViewType.INBOX);
101 
102         assertProperties(inbox, R.layout.inbox_headsup_notification_template,
103                 R.layout.inbox_notification_template, InboxNotificationViewHolder.class);
104     }
105 
106     @Test
inboxInGroupNotificationType_shouldHaveCorrectValues()107     public void inboxInGroupNotificationType_shouldHaveCorrectValues() {
108         CarNotificationTypeItem inboxInGroup = CarNotificationTypeItem.of(
109                 NotificationViewType.INBOX_IN_GROUP);
110         assertThat(inboxInGroup.getNotificationType()).isEqualTo(
111                 NotificationViewType.INBOX_IN_GROUP);
112 
113         assertProperties(inboxInGroup, NO_TEMPLATE, R.layout.inbox_notification_template_inner,
114                 InboxNotificationViewHolder.class);
115     }
116 
117     @Test
groupNotificationType_shouldHaveCorrectValues()118     public void groupNotificationType_shouldHaveCorrectValues() {
119         CarNotificationTypeItem groupExpanded = CarNotificationTypeItem.of(
120                 NotificationViewType.GROUP);
121         assertThat(groupExpanded.getNotificationType()).isEqualTo(
122                 NotificationViewType.GROUP);
123 
124         assertProperties(groupExpanded, NO_TEMPLATE, R.layout.group_notification_template,
125                 GroupNotificationViewHolder.class);
126     }
127 
128     @Test
groupSummaryNotificationType_shouldHaveCorrectValues()129     public void groupSummaryNotificationType_shouldHaveCorrectValues() {
130         CarNotificationTypeItem groupSummary = CarNotificationTypeItem.of(
131                 NotificationViewType.GROUP_SUMMARY);
132         assertThat(groupSummary.getNotificationType()).isEqualTo(
133                 NotificationViewType.GROUP_SUMMARY);
134 
135         assertProperties(groupSummary, NO_TEMPLATE, R.layout.group_summary_notification_template,
136                 GroupSummaryNotificationViewHolder.class);
137     }
138 
139     @Test
progressNotificationType_shouldHaveCorrectValues()140     public void progressNotificationType_shouldHaveCorrectValues() {
141         CarNotificationTypeItem progress = CarNotificationTypeItem.of(
142                 NotificationViewType.PROGRESS);
143         assertThat(progress.getNotificationType()).isEqualTo(NotificationViewType.PROGRESS);
144 
145         assertProperties(progress, NO_TEMPLATE, R.layout.progress_notification_template,
146                 ProgressNotificationViewHolder.class);
147     }
148 
149     @Test
progressInGroupNotificationType_shouldHaveCorrectValues()150     public void progressInGroupNotificationType_shouldHaveCorrectValues() {
151         CarNotificationTypeItem progressInGroup = CarNotificationTypeItem.of(
152                 NotificationViewType.PROGRESS_IN_GROUP);
153         assertThat(progressInGroup.getNotificationType()).isEqualTo(
154                 NotificationViewType.PROGRESS_IN_GROUP);
155 
156         assertProperties(progressInGroup, NO_TEMPLATE,
157                 R.layout.progress_notification_template_inner,
158                 ProgressNotificationViewHolder.class);
159     }
160 
161     @Test
basicNotificationType_shouldHaveCorrectValues()162     public void basicNotificationType_shouldHaveCorrectValues() {
163         CarNotificationTypeItem basic = CarNotificationTypeItem.of(
164                 NotificationViewType.BASIC);
165         assertThat(basic.getNotificationType()).isEqualTo(NotificationViewType.BASIC);
166 
167         assertProperties(basic, R.layout.basic_headsup_notification_template,
168                 R.layout.basic_notification_template, BasicNotificationViewHolder.class);
169     }
170 
171     @Test
basicInGroupNotificationType_shouldHaveCorrectValues()172     public void basicInGroupNotificationType_shouldHaveCorrectValues() {
173         CarNotificationTypeItem basicInGroup = CarNotificationTypeItem.of(
174                 NotificationViewType.BASIC_IN_GROUP);
175         assertThat(basicInGroup.getNotificationType()).isEqualTo(
176                 NotificationViewType.BASIC_IN_GROUP);
177 
178         assertProperties(basicInGroup, NO_TEMPLATE, R.layout.basic_notification_template_inner,
179                 BasicNotificationViewHolder.class);
180     }
181 
182     @Test
carWarningNotificationType_shouldHaveCorrectValues()183     public void carWarningNotificationType_shouldHaveCorrectValues() {
184         CarNotificationTypeItem carWarning = CarNotificationTypeItem.of(
185                 NotificationViewType.CAR_WARNING);
186         assertThat(carWarning.getNotificationType()).isEqualTo(
187                 NotificationViewType.CAR_WARNING);
188 
189         assertProperties(carWarning, R.layout.car_warning_headsup_notification_template,
190                 R.layout.car_warning_notification_template, BasicNotificationViewHolder.class);
191     }
192 
193     @Test
carInformationNotificationType_shouldHaveCorrectValues()194     public void carInformationNotificationType_shouldHaveCorrectValues() {
195         CarNotificationTypeItem carInformation = CarNotificationTypeItem.of(
196                 NotificationViewType.CAR_INFORMATION);
197         assertThat(carInformation.getNotificationType()).isEqualTo(
198                 NotificationViewType.CAR_INFORMATION);
199 
200         assertProperties(carInformation, R.layout.car_information_headsup_notification_template,
201                 R.layout.car_information_notification_template, BasicNotificationViewHolder.class);
202     }
203 
204     @Test
carInformationInGroupNotificationType_shouldHaveCorrectValues()205     public void carInformationInGroupNotificationType_shouldHaveCorrectValues() {
206         CarNotificationTypeItem carInfoInGroup = CarNotificationTypeItem.of(
207                 NotificationViewType.CAR_INFORMATION_IN_GROUP);
208         assertThat(carInfoInGroup.getNotificationType()).isEqualTo(
209                 NotificationViewType.CAR_INFORMATION_IN_GROUP);
210 
211         assertProperties(carInfoInGroup, NO_TEMPLATE,
212                 R.layout.car_information_notification_template_inner,
213                 BasicNotificationViewHolder.class);
214     }
215 
216     @Test
getCarNotificationTypeItem_invalidNotificationType_shouldThrowException()217     public void getCarNotificationTypeItem_invalidNotificationType_shouldThrowException() {
218         assertThrows(IllegalArgumentException.class, () -> CarNotificationTypeItem.of(123));
219     }
220 
assertProperties(CarNotificationTypeItem item, @LayoutRes int itemHeadsUpTemplate, @LayoutRes int itemNotificationCenterTemplate, Class expectedViewHolderClazz)221     private void assertProperties(CarNotificationTypeItem item, @LayoutRes int itemHeadsUpTemplate,
222             @LayoutRes int itemNotificationCenterTemplate, Class expectedViewHolderClazz) {
223         int headsUpTemplate = item.getHeadsUpTemplate();
224         assertThat(headsUpTemplate).isEqualTo(itemHeadsUpTemplate);
225 
226         int notificationCenterTemplate = item.getNotificationCenterTemplate();
227         assertThat(notificationCenterTemplate).isEqualTo(itemNotificationCenterTemplate);
228 
229         View viewTemplate = LayoutInflater.from(mApplicationContext).inflate(
230                 headsUpTemplate != NO_TEMPLATE ? headsUpTemplate : notificationCenterTemplate,
231                 null);
232 
233         CarNotificationBaseViewHolder viewHolder = item.getViewHolder(viewTemplate,
234                 mClickHandlerFactory);
235         assertThat(viewHolder).isInstanceOf(expectedViewHolderClazz);
236     }
237 }
238