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, with a ringer tone
     74         a1.mFlags = Account.FLAGS_VIBRATE;
     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         // No VIBRATE flags, with a ringer tone
     87         a1.mFlags = 0;
     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         // === Ringer mode change ===
    100         mRingerMode = AudioManager.RINGER_MODE_VIBRATE;
    101 
    102         // VIBRATE, with a ringer tone
    103         a1.mFlags = Account.FLAGS_VIBRATE;
    104 
    105         nb.setDefaults(0);
    106         nb.setSound(null);
    107         mTarget.setupSoundAndVibration(nb, a1);
    108         n = nb.getNotification();
    109 
    110         assertEquals(expectedRingtone, n.sound);
    111         assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
    112         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    113         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    114 
    115         // No VIBRATE flags, with a ringer tone
    116         a1.mFlags = 0;
    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         assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
    125         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    126         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    127 
    128         // === Ringer mode change ===
    129         mRingerMode = AudioManager.RINGER_MODE_SILENT;
    130 
    131         // VIBRATE, with a ringer tone
    132         a1.mFlags = Account.FLAGS_VIBRATE;
    133 
    134         nb.setDefaults(0);
    135         nb.setSound(null);
    136         mTarget.setupSoundAndVibration(nb, a1);
    137         n = nb.getNotification();
    138 
    139         assertEquals(expectedRingtone, n.sound);
    140         assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
    141         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    142         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    143 
    144         // No VIBRATE flags, with a ringer tone
    145         a1.mFlags = 0;
    146 
    147         nb.setDefaults(0);
    148         nb.setSound(null);
    149         mTarget.setupSoundAndVibration(nb, a1);
    150         n = nb.getNotification();
    151 
    152         assertEquals(expectedRingtone, n.sound);
    153         assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
    154         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
    155         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
    156 
    157         // No ringer tone
    158         a1.mRingtoneUri = null;
    159 
    160         nb.setDefaults(0);
    161         nb.setSound(null);
    162         mTarget.setupSoundAndVibration(nb, a1);
    163         n = nb.getNotification();
    164 
    165         assertNull(n.sound);
    166     }
    167 
    168     public void testCreateNewMessageNotification() {
    169         final Context c = mProviderContext;
    170         Notification n;
    171 
    172         // Case 1: 1 account, 1 unseen message
    173         Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
    174         Mailbox b1 = ProviderTestUtils.setupMailbox("inbox", a1.mId, true, c, Mailbox.TYPE_INBOX);
    175         Message m1 = ProviderTestUtils.setupMessage("message", a1.mId, b1.mId, true, true, c);
    176 
    177         n = mTarget.createNewMessageNotification(a1.mId, b1.mId, m1.mId, 1, 1);
    178 
    179         assertEquals(R.drawable.stat_notify_email_generic, n.icon);
    180         assertEquals(mMockClock.mTime, n.when);
    181         assertNotNull(n.largeIcon);
    182         assertEquals(0, n.number);
    183 
    184         // TODO Check content -- how?
    185 
    186         // Case 2: 1 account, 2 unseen message
    187         n = mTarget.createNewMessageNotification(a1.mId, b1.mId, m1.mId, 2, 2);
    188 
    189         assertEquals(R.drawable.stat_notify_email_generic, n.icon);
    190         assertEquals(mMockClock.mTime, n.when);
    191         assertNotNull(n.largeIcon);
    192         assertEquals(2, n.number);
    193 
    194         // TODO Check content -- how?
    195 
    196         // TODO Add 2 account test, if we find a way to check content
    197     }
    198 
    199     public void testCreateNewMessageNotificationWithEmptyFrom() {
    200         final Context c = mProviderContext;
    201         Notification n;
    202 
    203         // Message with no from fields.
    204         Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
    205         Mailbox b1 = ProviderTestUtils.setupMailbox("inbox", a1.mId, true, c, Mailbox.TYPE_INBOX);
    206         Message m1 = ProviderTestUtils.setupMessage("message", a1.mId, b1.mId, true, false, c);
    207         m1.mFrom = null;
    208         m1.save(c);
    209 
    210         // This shouldn't crash.
    211         n = mTarget.createNewMessageNotification(a1.mId, b1.mId, m1.mId, 1, 1);
    212 
    213         // Minimum test for the result
    214         assertEquals(R.drawable.stat_notify_email_generic, n.icon);
    215     }
    216 }
    217