1 // Copyright 2014 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.
5 function createMessage() {
7 messageId: "message-id",
8 destinationId: "destination-id",
17 function successfulSend(message) {
18 chrome.gcm.send(message, function(messageId) {
19 chrome.test.assertEq(message.messageId, messageId);
20 chrome.test.succeed();
24 function unsuccessfulSend(message) {
26 chrome.gcm.send(message, function(messageId) {
27 chrome.test.fail(message);
30 chrome.test.succeed();
34 function scenario(messageMutations, send) {
35 var message = createMessage();
36 messageMutations.forEach(function(mutation) {
42 function expectSuccessWhen() {
43 scenario(Array.prototype.slice.call(arguments), successfulSend);
46 function expectFailureWhen() {
47 scenario(Array.prototype.slice.call(arguments), unsuccessfulSend);
50 chrome.test.runTests([
51 function successWhenHappyPath() {
52 expectSuccessWhen(/* no changes to message here */);
54 function successWhenTtlIsZero() {
55 expectSuccessWhen(function(message) { message.timeToLive = 0; });
57 function successWhenTtlIsMissing() {
58 expectSuccessWhen(function(message) { delete message.timeToLive; });
60 function failureWhenTtlIsNegative() {
61 expectFailureWhen(function(message) { message.timeToLive = -1; });
63 function failureWhenTtlIsTooLarge() {
64 expectFailureWhen(function(message) { message.timeToLive = 86401; });
66 function failureWhenMessageIdMissing() {
67 expectFailureWhen(function(message) { delete message.messageId; });
69 function failureWhenMessageIdIsEmpty() {
70 expectFailureWhen(function(message) { message.messageId = ""; });
72 function failureWhenDestinationIdMissing() {
73 expectFailureWhen(function(message) { delete message.destinationId; });
75 function failureWhenDestinationIdIsEmpty() {
76 expectFailureWhen(function(message) { message.destinationId = ""; });
78 function failureWhenDataIsMissing() {
79 expectFailureWhen(function(message) { delete message.data; });
81 function failureWhenDataIsEmpty() {
82 expectFailureWhen(function(message) { message.data = {}; });
84 function failureWhenDataKeyIsEmpty() {
85 expectFailureWhen(function(message) { message.data[""] = "value"; });
87 function successWhenDataKeyHasGoogDotInIt() {
88 expectSuccessWhen(function(message) {
89 message.data["something.goog."] = "value";
92 function failureWhenDataKeyIsGoogDot() {
93 expectFailureWhen(function(message) { message.data["goog."] = "value"; });
95 function failureWhenDataKeyIsGoogDotPrefixed() {
96 expectFailureWhen(function(message) {
97 message.data["goog.something"] = "value";
100 function failureWhenDataKeyIsGoogDotMixedCasedPrefixed() {
101 expectFailureWhen(function(message) {
102 message.data["GoOg.something"] = "value";
105 function successWhenDataKeyHasGoogleInIt() {
106 expectSuccessWhen(function(message) {
107 message.data["somthing.google"] = "value";
110 function failureWhenDataKeyIsGoogle() {
111 expectFailureWhen(function(message) {
112 message.data["google"] = "value";
115 function failureWhenDataKeyIsMixedCasedGoogle() {
116 expectFailureWhen(function(message) {
117 message.data["GoOgLe"] = "value";
120 function failureWhenDataKeyIsGooglePrefixed() {
121 expectFailureWhen(function(message) {
122 message.data["googleSomething"] = "value";
125 function failureWhenDataKeyIsCollapeKey() {
126 expectFailureWhen(function(message) {
127 message.data["collapse_key"] = "value";
130 function failureWhenMessageIsTooLarge() {
131 expectFailureWhen(function(message) {
132 function generateString(base, len) {
133 // Generates a string of size |len| by concatenating |base| multiple
134 // times and trimming to |len|.
135 while (base.length < len) base += base;
136 return base.substring(0, len);
139 var source = "abcdefghijklmnopqrstuvwxyz";
140 // Creates 8 * (256 + 256) == 4096 bytes of message data which together
141 // with data put in by default is more than allowed max.
143 while (entries > 0) {
144 var s = generateString(source + entries, 256);