Home | History | Annotate | Download | only in email
      1 /*
      2  * Copyright (C) 2010 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.email;
     18 
     19 import android.app.Notification;
     20 import android.content.Context;
     21 import android.media.AudioManager;
     22 import android.net.Uri;
     23 import android.test.AndroidTestCase;
     24 
     25 import com.android.email.provider.ProviderTestUtils;
     26 import com.android.emailcommon.provider.Account;
     27 import com.android.emailcommon.provider.EmailContent.Message;
     28 import com.android.emailcommon.provider.Mailbox;
     29 
     30 /**
     31  * Test for {@link NotificationController}.
     32  *
     33  * TODO Add tests for all methods.
     34  */
     35 public class NotificationControllerTest extends AndroidTestCase {
     36     private Context mProviderContext;
     37     private NotificationController mTarget;
     38 
     39     private final MockClock mMockClock = new MockClock();
     40     private int mRingerMode;
     41 
     42     /**
     43      * Subclass {@link NotificationController} to override un-mockable operations.
     44      */
     45     private class NotificationControllerForTest extends NotificationController {
     46         NotificationControllerForTest(Context context) {
     47             super(context, mMockClock);
     48         }
     49 
     50         @Override
     51         int getRingerMode() {
     52             return mRingerMode;
     53         }
     54     }
     55 
     56     @Override
     57     protected void setUp() throws Exception {
     58         super.setUp();
     59         mProviderContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(mContext);
     60         mTarget = new NotificationControllerForTest(mProviderContext);
     61     }
     62 
     63     public void testSetupSoundAndVibration() {
     64         final Context c = mProviderContext;
     65         final Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
     66         final Notification.Builder nb = new Notification.Builder(c);
     67         final Uri expectedRingtone = Uri.parse(a1.mRingtoneUri);
     68         Notification n;
     69 
     70         // === Ringer mode change ===
     71         mRingerMode = AudioManager.RINGER_MODE_NORMAL;
     72 
     73         // VIBRATE_ALWAYS, with a ringer tone
     74         a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
     75 
     76         nb.setDefaults(0);
     77         nb.setSound(null);
     78         mTarget.setupSoundAndVibration(nb, a1);
     79         n = nb.getNotification();
     80 
     81         assertEquals(expectedRingtone, n.sound);
     82         assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
     83         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
     84         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
     85 
     86         // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
     87         a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
     88 
     89         nb.setDefaults(0);
     90         nb.setSound(null);
     91         mTarget.setupSoundAndVibration(nb, a1);
     92         n = nb.getNotification();
     93 
     94         assertEquals(expectedRingtone, n.sound);
     95         assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
     96         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
     97         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
     98 
     99         // No VIBRATE flags, with a ringer tone
    100         a1.mFlags = 0;
    101 
    102         nb.setDefaults(0);
    103         nb.setSound(null);
    104         mTarget.setupSoundAndVibration(nb, a1);
    105         n = nb.getNotification();
    106 
    107         assertEquals(expectedRingtone, n.sound);
    108         assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
    109         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    110         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    111 
    112         // === Ringer mode change ===
    113         mRingerMode = AudioManager.RINGER_MODE_VIBRATE;
    114 
    115         // VIBRATE_ALWAYS, with a ringer tone
    116         a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
    117 
    118         nb.setDefaults(0);
    119         nb.setSound(null);
    120         mTarget.setupSoundAndVibration(nb, a1);
    121         n = nb.getNotification();
    122 
    123         assertEquals(expectedRingtone, n.sound);
    124         assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
    125         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    126         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    127 
    128         // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
    129         a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
    130 
    131         nb.setDefaults(0);
    132         nb.setSound(null);
    133         mTarget.setupSoundAndVibration(nb, a1);
    134         n = nb.getNotification();
    135 
    136         assertEquals(expectedRingtone, n.sound);
    137         assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
    138         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    139         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    140 
    141         // No VIBRATE flags, with a ringer tone
    142         a1.mFlags = 0;
    143 
    144         nb.setDefaults(0);
    145         nb.setSound(null);
    146         mTarget.setupSoundAndVibration(nb, a1);
    147         n = nb.getNotification();
    148 
    149         assertEquals(expectedRingtone, n.sound);
    150         assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
    151         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    152         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    153 
    154         // === Ringer mode change ===
    155         mRingerMode = AudioManager.RINGER_MODE_SILENT;
    156 
    157         // VIBRATE_ALWAYS, with a ringer tone
    158         a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
    159 
    160         nb.setDefaults(0);
    161         nb.setSound(null);
    162         mTarget.setupSoundAndVibration(nb, a1);
    163         n = nb.getNotification();
    164 
    165         assertEquals(expectedRingtone, n.sound);
    166         assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
    167         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    168         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    169 
    170         // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
    171         a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
    172 
    173         nb.setDefaults(0);
    174         nb.setSound(null);
    175         mTarget.setupSoundAndVibration(nb, a1);
    176         n = nb.getNotification();
    177 
    178         assertEquals(expectedRingtone, n.sound);
    179         assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
    180         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    181         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    182 
    183         // No VIBRATE flags, with a ringer tone
    184         a1.mFlags = 0;
    185 
    186         nb.setDefaults(0);
    187         nb.setSound(null);
    188         mTarget.setupSoundAndVibration(nb, a1);
    189         n = nb.getNotification();
    190 
    191         assertEquals(expectedRingtone, n.sound);
    192         assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
    193         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    194         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    195 
    196         // No ringer tone
    197         a1.mRingtoneUri = null;
    198 
    199         nb.setDefaults(0);
    200         nb.setSound(null);
    201         mTarget.setupSoundAndVibration(nb, a1);
    202         n = nb.getNotification();
    203 
    204         assertNull(n.sound);
    205     }
    206 
    207     public void testCreateNewMessageNotification() {
    208         final Context c = mProviderContext;
    209         Notification n;
    210 
    211         // Case 1: 1 account, 1 unseen message
    212         Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
    213         Mailbox b1 = ProviderTestUtils.setupMailbox("inbox", a1.mId, true, c, Mailbox.TYPE_INBOX);
    214         Message m1 = ProviderTestUtils.setupMessage("message", a1.mId, b1.mId, true, true, c);
    215 
    216         n = mTarget.createNewMessageNotification(a1.mId, b1.mId, m1.mId, 1, 1);
    217 
    218         assertEquals(R.drawable.stat_notify_email_generic, n.icon);
    219         assertEquals(mMockClock.mTime, n.when);
    220         assertNotNull(n.largeIcon);
    221         assertEquals(0, n.number);
    222 
    223         // TODO Check content -- how?
    224 
    225         // Case 2: 1 account, 2 unseen message
    226         n = mTarget.createNewMessageNotification(a1.mId, b1.mId, m1.mId, 2, 2);
    227 
    228         assertEquals(R.drawable.stat_notify_email_generic, n.icon);
    229         assertEquals(mMockClock.mTime, n.when);
    230         assertNotNull(n.largeIcon);
    231         assertEquals(2, n.number);
    232 
    233         // TODO Check content -- how?
    234 
    235         // TODO Add 2 account test, if we find a way to check content
    236     }
    237 
    238     public void testCreateNewMessageNotificationWithEmptyFrom() {
    239         final Context c = mProviderContext;
    240         Notification n;
    241 
    242         // Message with no from fields.
    243         Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
    244         Mailbox b1 = ProviderTestUtils.setupMailbox("inbox", a1.mId, true, c, Mailbox.TYPE_INBOX);
    245         Message m1 = ProviderTestUtils.setupMessage("message", a1.mId, b1.mId, true, false, c);
    246         m1.mFrom = null;
    247         m1.save(c);
    248 
    249         // This shouldn't crash.
    250         n = mTarget.createNewMessageNotification(a1.mId, b1.mId, m1.mId, 1, 1);
    251 
    252         // Minimum test for the result
    253         assertEquals(R.drawable.stat_notify_email_generic, n.icon);
    254     }
    255 }
    256