Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.components.gcm_driver; | |
| 6 | |
| 7 import static org.junit.Assert.assertArrayEquals; | |
| 8 import static org.junit.Assert.assertEquals; | |
| 9 | |
| 10 import android.os.Build; | |
| 11 import android.os.Bundle; | |
| 12 | |
| 13 import org.junit.Test; | |
| 14 import org.junit.runner.RunWith; | |
| 15 | |
| 16 import org.robolectric.annotation.Config; | |
| 17 | |
| 18 import org.chromium.base.test.util.MinAndroidSdkLevel; | |
| 19 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
| 20 | |
| 21 /** | |
| 22 * Unit tests for GCMMessage. | |
| 23 */ | |
| 24 @RunWith(LocalRobolectricTestRunner.class) | |
| 25 @Config(manifest = Config.NONE) | |
| 26 public class GCMMessageTest { | |
| 27 /** | |
| 28 * Tests that a message object can be created based on data received from GC M. Note that the raw | |
| 29 * data field is tested separately. | |
| 30 */ | |
| 31 @Test | |
| 32 public void testCreateMessageFromGCM() { | |
| 33 Bundle extras = new Bundle(); | |
| 34 | |
| 35 // Compose a simple message that lacks all optional fields. | |
| 36 extras.putString("subtype", "MyAppId"); | |
| 37 | |
| 38 { | |
| 39 GCMMessage message = new GCMMessage("MySenderId", extras); | |
| 40 | |
| 41 assertEquals("MySenderId", message.getSenderId()); | |
| 42 assertEquals("MyAppId", message.getAppId()); | |
| 43 assertEquals(null, message.getCollapseKey()); | |
| 44 assertArrayEquals(new String[] {}, message.getDataKeysAndValuesArray ()); | |
| 45 } | |
| 46 | |
| 47 // Add the optional fields: collapse key, raw binary data and a custom p roperty. | |
| 48 extras.putString("collapse_key", "MyCollapseKey"); | |
| 49 extras.putByteArray("rawData", new byte[] {0x00, 0x15, 0x30, 0x45}); | |
| 50 extras.putString("property", "value"); | |
| 51 | |
| 52 { | |
| 53 GCMMessage message = new GCMMessage("MySenderId", extras); | |
| 54 | |
| 55 assertEquals("MySenderId", message.getSenderId()); | |
| 56 assertEquals("MyAppId", message.getAppId()); | |
| 57 assertEquals("MyCollapseKey", message.getCollapseKey()); | |
|
awdf
2017/02/16 21:26:48
check message.getRawData too?
Peter Beverloo
2017/02/17 15:43:59
I'm testing getRawData elsewhere because its seman
| |
| 58 assertArrayEquals( | |
| 59 new String[] {"property", "value"}, message.getDataKeysAndVa luesArray()); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * Tests that the bundle containing extras from GCM will be filtered for val ues that we either | |
| 65 * pass through by other means, or that we know to be irrelevant to regular GCM messages. | |
| 66 */ | |
| 67 @Test | |
| 68 public void testFiltersExtraBundle() { | |
| 69 Bundle extras = new Bundle(); | |
| 70 | |
| 71 // These should be filtered by full key. | |
| 72 extras.putString("collapse_key", "collapseKey"); | |
| 73 extras.putString("rawData", "rawData"); | |
| 74 extras.putString("from", "from"); | |
| 75 extras.putString("subtype", "subtype"); | |
| 76 | |
| 77 // These should be filtered by prefix matching. | |
| 78 extras.putString("com.google.ipc.invalidation.gcmmplex.foo", "bar"); | |
| 79 extras.putString("com.google.ipc.invalidation.gcmmplex.bar", "baz"); | |
| 80 | |
| 81 // These should be filtered because they're not strings. | |
| 82 extras.putBoolean("myBoolean", true); | |
| 83 extras.putInt("myInteger", 42); | |
| 84 | |
| 85 // These should not be filtered. | |
| 86 extras.putString("collapse_key2", "secondCollapseKey"); | |
| 87 extras.putString("myString", "foobar"); | |
| 88 | |
| 89 GCMMessage message = new GCMMessage("senderId", extras); | |
| 90 | |
| 91 assertArrayEquals(new String[] {"collapse_key2", "secondCollapseKey", "m yString", "foobar"}, | |
| 92 message.getDataKeysAndValuesArray()); | |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * Tests that a GCMMessage object can be serialized to and deserialized from a persistable | |
| 97 * bundle. Note that the raw data field is tested separately. Only run on An droid L and beyond | |
| 98 * because it depends on PersistableBundle. | |
| 99 */ | |
| 100 @Test | |
| 101 @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP) | |
| 102 public void testSerializationToPersistableBundle() { | |
| 103 Bundle extras = new Bundle(); | |
| 104 | |
| 105 // Compose a simple message that lacks all optional fields. | |
| 106 extras.putString("subtype", "MyAppId"); | |
| 107 | |
| 108 { | |
| 109 GCMMessage message = new GCMMessage("MySenderId", extras); | |
| 110 GCMMessage copiedMessage = new GCMMessage(message.toPersistableBundl e()); | |
| 111 | |
| 112 assertEquals(message.getSenderId(), copiedMessage.getSenderId()); | |
| 113 assertEquals(message.getAppId(), copiedMessage.getAppId()); | |
| 114 assertEquals(message.getCollapseKey(), copiedMessage.getCollapseKey( )); | |
| 115 assertArrayEquals( | |
| 116 message.getDataKeysAndValuesArray(), copiedMessage.getDataKe ysAndValuesArray()); | |
| 117 } | |
| 118 | |
| 119 // Add the optional fields: collapse key, raw binary data and a custom p roperty. | |
| 120 extras.putString("collapse_key", "MyCollapseKey"); | |
| 121 extras.putString("property", "value"); | |
| 122 | |
| 123 { | |
| 124 GCMMessage message = new GCMMessage("MySenderId", extras); | |
| 125 GCMMessage copiedMessage = new GCMMessage(message.toPersistableBundl e()); | |
| 126 | |
| 127 assertEquals(message.getSenderId(), copiedMessage.getSenderId()); | |
| 128 assertEquals(message.getAppId(), copiedMessage.getAppId()); | |
| 129 assertEquals(message.getCollapseKey(), copiedMessage.getCollapseKey( )); | |
| 130 assertArrayEquals( | |
| 131 message.getDataKeysAndValuesArray(), copiedMessage.getDataKe ysAndValuesArray()); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 /** | |
| 136 * Tests that the raw data field can be serialized and deserialized as expec ted. It should be | |
| 137 * NULL when undefined, an empty byte array when defined but empty, and a re gular, filled | |
| 138 * byte array when data has been provided. Only run on Android L and beyond because it depends | |
| 139 * on PersistableBundle. | |
| 140 */ | |
| 141 @Test | |
| 142 @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP) | |
| 143 public void testRawDataSerializationBehaviour() { | |
| 144 Bundle extras = new Bundle(); | |
| 145 extras.putString("subtype", "MyAppId"); | |
| 146 | |
| 147 // Case 1: No raw data supplied. Should be NULL. | |
| 148 { | |
| 149 GCMMessage message = new GCMMessage("MySenderId", extras); | |
| 150 GCMMessage copiedMessage = new GCMMessage(message.toPersistableBundl e()); | |
| 151 | |
| 152 assertArrayEquals(null, message.getRawData()); | |
| 153 assertArrayEquals(null, copiedMessage.getRawData()); | |
| 154 } | |
| 155 | |
| 156 extras.putByteArray("rawData", new byte[] {}); | |
| 157 | |
| 158 // Case 2: Empty byte array of raw data supplied. Should be just that. | |
| 159 { | |
| 160 GCMMessage message = new GCMMessage("MySenderId", extras); | |
| 161 GCMMessage copiedMessage = new GCMMessage(message.toPersistableBundl e()); | |
| 162 | |
| 163 assertArrayEquals(new byte[] {}, message.getRawData()); | |
| 164 assertArrayEquals(new byte[] {}, copiedMessage.getRawData()); | |
| 165 } | |
| 166 | |
| 167 extras.putByteArray("rawData", new byte[] {0x00, 0x15, 0x30, 0x45}); | |
| 168 | |
| 169 // Case 3: Byte array with data supplied. | |
| 170 { | |
| 171 GCMMessage message = new GCMMessage("MySenderId", extras); | |
| 172 GCMMessage copiedMessage = new GCMMessage(message.toPersistableBundl e()); | |
| 173 | |
| 174 assertArrayEquals(new byte[] {0x00, 0x15, 0x30, 0x45}, message.getRa wData()); | |
| 175 assertArrayEquals(new byte[] {0x00, 0x15, 0x30, 0x45}, copiedMessage .getRawData()); | |
| 176 } | |
| 177 } | |
| 178 } | |
| OLD | NEW |