summaryrefslogtreecommitdiff
path: root/chromium/components/gcm_driver/android/junit/src/org/chromium/components/gcm_driver/GCMMessageTest.java
blob: f39d2b5dca7b2c3644f284c9d5adad9e85617f6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.components.gcm_driver;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import android.os.Bundle;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

import org.chromium.base.test.BaseRobolectricTestRunner;

/**
 * Unit tests for GCMMessage.
 */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class GCMMessageTest {
    private void assertMessageEquals(GCMMessage m1, GCMMessage m2) {
        assertEquals(m1.getSenderId(), m2.getSenderId());
        assertEquals(m1.getAppId(), m2.getAppId());
        assertEquals(m1.getCollapseKey(), m2.getCollapseKey());
        assertArrayEquals(m1.getDataKeysAndValuesArray(), m2.getDataKeysAndValuesArray());
    }

    /**
     * Tests that a message object can be created based on data received from GCM. Note that the raw
     * data field is tested separately.
     */
    @Test
    public void testCreateMessageFromGCM() {
        Bundle extras = new Bundle();

        // Compose a simple message that lacks all optional fields.
        extras.putString("subtype", "MyAppId");

        {
            GCMMessage message = new GCMMessage("MySenderId", extras);

            assertEquals("MySenderId", message.getSenderId());
            assertEquals("MyAppId", message.getAppId());
            assertEquals(null, message.getCollapseKey());
            assertArrayEquals(new String[] {}, message.getDataKeysAndValuesArray());
        }

        // Add the optional fields: collapse key, raw binary data, a custom property and an original
        // priority.
        extras.putString("collapse_key", "MyCollapseKey");
        extras.putByteArray("rawData", new byte[] {0x00, 0x15, 0x30, 0x45});
        extras.putString("property", "value");
        extras.putString("google.original_priority", "normal");

        {
            GCMMessage message = new GCMMessage("MySenderId", extras);

            assertEquals("MySenderId", message.getSenderId());
            assertEquals("MyAppId", message.getAppId());
            assertEquals("MyCollapseKey", message.getCollapseKey());
            assertArrayEquals(
                    new String[] {"property", "value"}, message.getDataKeysAndValuesArray());
            assertEquals(GCMMessage.Priority.NORMAL, message.getOriginalPriority());
        }
    }

    /**
     * Tests that the bundle containing extras from GCM will be filtered for values that we either
     * pass through by other means, or that we know to be irrelevant to regular GCM messages.
     */
    @Test
    public void testFiltersExtraBundle() {
        Bundle extras = new Bundle();

        // These should be filtered by full key.
        extras.putString("collapse_key", "collapseKey");
        extras.putString("rawData", "rawData");
        extras.putString("from", "from");
        extras.putString("subtype", "subtype");

        // These should be filtered by prefix matching.
        extras.putString("com.google.ipc.invalidation.gcmmplex.foo", "bar");
        extras.putString("com.google.ipc.invalidation.gcmmplex.bar", "baz");

        // These should be filtered because they're not strings.
        extras.putBoolean("myBoolean", true);
        extras.putInt("myInteger", 42);

        // These should not be filtered.
        extras.putString("collapse_key2", "secondCollapseKey");
        extras.putString("myString", "foobar");

        GCMMessage message = new GCMMessage("senderId", extras);

        assertArrayEquals(new String[] {"collapse_key2", "secondCollapseKey", "myString", "foobar"},
                message.getDataKeysAndValuesArray());
    }

    /**
     * Tests that a GCMMessage object can be serialized to and deserialized from a persistable
     * bundle. Note that the raw data field is tested separately. Only run on Android L and beyond
     * because it depends on PersistableBundle.
     */
    @Test
    public void testSerializationToPersistableBundle() {
        Bundle extras = new Bundle();

        // Compose a simple message that lacks all optional fields.
        extras.putString("subtype", "MyAppId");

        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromBundle(message.toBundle());
            assertMessageEquals(message, copiedMessage);
        }

        // Add the optional fields: collapse key, raw binary data and a custom property.
        extras.putString("collapse_key", "MyCollapseKey");
        extras.putString("property", "value");
        extras.putString("google.original_priority", "normal");

        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromBundle(message.toBundle());
            assertMessageEquals(message, copiedMessage);
        }
    }

    /**
     * Tests that the raw data field can be serialized and deserialized as expected. It should be
     * NULL when undefined, an empty byte array when defined but empty, and a regular, filled
     * byte array when data has been provided. Only run on Android L and beyond because it depends
     * on PersistableBundle.
     */
    @Test
    public void testRawDataSerializationBehaviour() {
        Bundle extras = new Bundle();
        extras.putString("subtype", "MyAppId");

        // Case 1: No raw data supplied. Should be NULL.
        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromBundle(message.toBundle());

            assertArrayEquals(null, message.getRawData());
            assertArrayEquals(null, copiedMessage.getRawData());
        }

        extras.putByteArray("rawData", new byte[] {});

        // Case 2: Empty byte array of raw data supplied. Should be just that.
        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromBundle(message.toBundle());

            assertArrayEquals(new byte[] {}, message.getRawData());
            assertArrayEquals(new byte[] {}, copiedMessage.getRawData());
        }

        extras.putByteArray("rawData", new byte[] {0x00, 0x15, 0x30, 0x45});

        // Case 3: Byte array with data supplied.
        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromBundle(message.toBundle());

            assertArrayEquals(new byte[] {0x00, 0x15, 0x30, 0x45}, message.getRawData());
            assertArrayEquals(new byte[] {0x00, 0x15, 0x30, 0x45}, copiedMessage.getRawData());
        }
    }

    /**
     * Tests that a GCMMessage object can be serialized to and deserialized from
     * a JSONObject. Note that the raw data field is tested separately.
     */
    @Test
    public void testSerializationToJSON() throws JSONException {
        Bundle extras = new Bundle();

        // Compose a simple message that lacks all optional fields.
        extras.putString("subtype", "MyAppId");

        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            JSONObject messageJSON = message.toJSON();

            // Version must be written to JSON.
            assertEquals(messageJSON.get("version"), GCMMessage.VERSION);
            GCMMessage copiedMessage = GCMMessage.createFromJSON(messageJSON);

            assertMessageEquals(message, copiedMessage);
        }

        // Add the optional fields: collapse key, raw binary data and a custom property.
        extras.putString("collapse_key", "MyCollapseKey");
        extras.putString("property", "value");
        extras.putString("google.original_priority", "normal");

        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromJSON(message.toJSON());

            assertMessageEquals(message, copiedMessage);
        }
    }

    /**
     * Tests that the raw data field can be serialized and deserialized as expected from JSONObject.
     * It should be NULL when undefined, an empty byte array when defined but empty, and a regular,
     * filled byte array when data has been provided.
     */
    @Test
    public void testRawDataSerializationToJSON() {
        Bundle extras = new Bundle();
        extras.putString("subtype", "MyAppId");

        // Case 1: No raw data supplied. Should be NULL.
        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromJSON(message.toJSON());

            assertArrayEquals(null, message.getRawData());
            assertArrayEquals(null, copiedMessage.getRawData());
        }

        extras.putByteArray("rawData", new byte[] {});

        // Case 2: Empty byte array of raw data supplied. Should be just that.
        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromJSON(message.toJSON());

            assertArrayEquals(new byte[] {}, message.getRawData());
            assertArrayEquals(new byte[] {}, copiedMessage.getRawData());
        }
        final byte[] rawData = {0x00, 0x15, 0x30, 0x45};
        extras.putByteArray("rawData", rawData);

        // Case 3: Byte array with data supplied.
        {
            GCMMessage message = new GCMMessage("MySenderId", extras);
            GCMMessage copiedMessage = GCMMessage.createFromJSON(message.toJSON());

            assertArrayEquals(rawData, message.getRawData());
            assertArrayEquals(rawData, copiedMessage.getRawData());
        }
    }

    /**
     * Tests that getOriginalPriority returns Priority.NONE if it was not set in the bundle.
     */
    @Test
    public void testNullOriginalPriority() {
        Bundle extras = new Bundle();

        // Compose a simple message that lacks all optional fields.
        extras.putString("subtype", "MyAppId");
        GCMMessage message = new GCMMessage("MySenderId", extras);

        assertEquals(GCMMessage.Priority.NONE, message.getOriginalPriority());
    }
}