NotificationControllerTest.java revision 74e094834c6efd3260c717090a89232a892c411b
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
17package com.android.email;
18
19import com.android.email.provider.EmailContent.Account;
20import com.android.email.provider.EmailContent.Mailbox;
21import com.android.email.provider.EmailContent.Message;
22import com.android.email.provider.ProviderTestUtils;
23
24import android.app.Notification;
25import android.content.Context;
26import android.media.AudioManager;
27import android.net.Uri;
28import android.test.AndroidTestCase;
29
30/**
31 * Test for {@link NotificationController}.
32 *
33 * TODO Add tests for all methods.
34 */
35public 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 testSetupNotificationSoundAndVibrationFromAccount() {
64        final Notification n = new Notification();
65
66        final Context c = mProviderContext;
67        final Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
68
69        // === Ringer mode change ===
70        mRingerMode = AudioManager.RINGER_MODE_NORMAL;
71
72        // VIBRATE_ALWAYS, with a ringer tone
73        a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
74
75        n.defaults = 0;
76        n.flags = 0;
77        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
78
79        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
80        assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
81        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
82        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
83
84        // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
85        a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
86
87        n.defaults = 0;
88        n.flags = 0;
89        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
90
91        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
92        assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
93        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
94        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
95
96        // No VIBRATE flags, with a ringer tone
97        a1.mFlags = 0;
98
99        n.defaults = 0;
100        n.flags = 0;
101        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
102
103        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
104        assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
105        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
106        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
107
108        // === Ringer mode change ===
109        mRingerMode = AudioManager.RINGER_MODE_VIBRATE;
110
111        // VIBRATE_ALWAYS, with a ringer tone
112        a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
113
114        n.defaults = 0;
115        n.flags = 0;
116        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
117
118        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
119        assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
120        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
121        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
122
123        // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
124        a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
125
126        n.defaults = 0;
127        n.flags = 0;
128        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
129
130        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
131        assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
132        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
133        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
134
135        // No VIBRATE flags, with a ringer tone
136        a1.mFlags = 0;
137
138        n.defaults = 0;
139        n.flags = 0;
140        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
141
142        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
143        assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
144        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
145        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
146
147        // === Ringer mode change ===
148        mRingerMode = AudioManager.RINGER_MODE_SILENT;
149
150        // VIBRATE_ALWAYS, with a ringer tone
151        a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
152
153        n.defaults = 0;
154        n.flags = 0;
155        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
156
157        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
158        assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
159        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
160        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
161
162        // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
163        a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
164
165        n.defaults = 0;
166        n.flags = 0;
167        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
168
169        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
170        assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
171        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
172        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
173
174        // No VIBRATE flags, with a ringer tone
175        a1.mFlags = 0;
176
177        n.defaults = 0;
178        n.flags = 0;
179        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
180
181        assertEquals(Uri.parse(a1.mRingtoneUri), n.sound);
182        assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
183        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
184        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
185
186        // No ringer tone
187        a1.mRingtoneUri = null;
188
189        n.defaults = 0;
190        n.flags = 0;
191        mTarget.setupNotificationSoundAndVibrationFromAccount(n, a1);
192
193        assertNull(n.sound);
194    }
195
196    public void testCreateNewMessageNotification() {
197        final Context c = mProviderContext;
198        Notification n;
199
200        // Case 1: 1 account, 1 unseen message
201        Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
202        Mailbox b1 = ProviderTestUtils.setupMailbox("inbox", a1.mId, true, c, Mailbox.TYPE_INBOX);
203        Message m1 = ProviderTestUtils.setupMessage("message", a1.mId, b1.mId, true, true, c);
204
205        n = mTarget.createNewMessageNotification(a1.mId, 1);
206
207        assertEquals(R.drawable.stat_notify_email_generic, n.icon);
208        assertEquals(mMockClock.mTime, n.when);
209        assertNotNull(n.largeIcon);
210        assertEquals(0, n.number);
211
212        // TODO Check content -- how?
213
214        // Case 2: 1 account, 2 unseen message
215        n = mTarget.createNewMessageNotification(a1.mId, 2);
216
217        assertEquals(R.drawable.stat_notify_email_generic, n.icon);
218        assertEquals(mMockClock.mTime, n.when);
219        assertNotNull(n.largeIcon);
220        assertEquals(2, n.number);
221
222        // TODO Check content -- how?
223
224        // TODO Add 2 account test, if we find a way to check content
225    }
226
227    public void testGetNotificationTitle() {
228        final Context c = mProviderContext;
229
230        // Case 1: 1 account
231        Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
232
233        // Just check the content.  Ignore the spans.
234        String title = mTarget.getNotificationTitle("*sender*", "*receiver*").toString();
235        assertEquals("*sender*", title);
236
237        // Case 1: 2 account
238        Account a2 = ProviderTestUtils.setupAccount("a1", true, c);
239
240        // Just check the content.  Ignore the spans.
241        title = mTarget.getNotificationTitle("*sender*", "*receiver*").toString();
242        assertEquals("*sender* to *receiver*", title);
243    }
244}
245