Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: content/renderer/manifest/manifest_parser_unittest.cc

Issue 2036803002: [WIP] Use Optional<SkColor> instead of int64 for colors in Manifest. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: with unit tests Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/renderer/manifest/manifest_parser.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/manifest/manifest_parser.h" 5 #include "content/renderer/manifest/manifest_parser.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "content/public/common/manifest.h" 13 #include "content/public/common/manifest.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace content { 16 namespace content {
17 17
18 namespace {
19
20 uint32_t ExtractColor(int64_t color) {
21 return reinterpret_cast<uint32_t&>(color);
22 }
23
24 } // anonymous namespace
25
26 class ManifestParserTest : public testing::Test { 18 class ManifestParserTest : public testing::Test {
27 protected: 19 protected:
28 ManifestParserTest() {} 20 ManifestParserTest() {}
29 ~ManifestParserTest() override {} 21 ~ManifestParserTest() override {}
30 22
31 Manifest ParseManifestWithURLs(const base::StringPiece& data, 23 Manifest ParseManifestWithURLs(const base::StringPiece& data,
32 const GURL& manifest_url, 24 const GURL& manifest_url,
33 const GURL& document_url) { 25 const GURL& document_url) {
34 ManifestParser parser(data, manifest_url, document_url); 26 ManifestParser parser(data, manifest_url, document_url);
35 parser.Parse(); 27 parser.Parse();
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 EXPECT_EQ("Line: 1, column: 1, Unexpected token.", 83 EXPECT_EQ("Line: 1, column: 1, Unexpected token.",
92 errors()[0]); 84 errors()[0]);
93 85
94 // A parsing error is equivalent to an empty manifest. 86 // A parsing error is equivalent to an empty manifest.
95 ASSERT_TRUE(manifest.IsEmpty()); 87 ASSERT_TRUE(manifest.IsEmpty());
96 ASSERT_TRUE(manifest.name.is_null()); 88 ASSERT_TRUE(manifest.name.is_null());
97 ASSERT_TRUE(manifest.short_name.is_null()); 89 ASSERT_TRUE(manifest.short_name.is_null());
98 ASSERT_TRUE(manifest.start_url.is_empty()); 90 ASSERT_TRUE(manifest.start_url.is_empty());
99 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined); 91 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined);
100 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); 92 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
101 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 93 ASSERT_FALSE(manifest.theme_color.has_value());
102 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 94 ASSERT_FALSE(manifest.background_color.has_value());
103 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); 95 ASSERT_TRUE(manifest.gcm_sender_id.is_null());
104 ASSERT_TRUE(manifest.scope.is_empty()); 96 ASSERT_TRUE(manifest.scope.is_empty());
105 } 97 }
106 98
107 TEST_F(ManifestParserTest, ValidNoContentParses) { 99 TEST_F(ManifestParserTest, ValidNoContentParses) {
108 Manifest manifest = ParseManifest("{}"); 100 Manifest manifest = ParseManifest("{}");
109 101
110 // Empty Manifest is not a parsing error. 102 // Empty Manifest is not a parsing error.
111 EXPECT_EQ(0u, GetErrorCount()); 103 EXPECT_EQ(0u, GetErrorCount());
112 104
113 // Check that all the fields are null in that case. 105 // Check that all the fields are null in that case.
114 ASSERT_TRUE(manifest.IsEmpty()); 106 ASSERT_TRUE(manifest.IsEmpty());
115 ASSERT_TRUE(manifest.name.is_null()); 107 ASSERT_TRUE(manifest.name.is_null());
116 ASSERT_TRUE(manifest.short_name.is_null()); 108 ASSERT_TRUE(manifest.short_name.is_null());
117 ASSERT_TRUE(manifest.start_url.is_empty()); 109 ASSERT_TRUE(manifest.start_url.is_empty());
118 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined); 110 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined);
119 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); 111 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
120 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 112 ASSERT_FALSE(manifest.theme_color.has_value());
121 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 113 ASSERT_FALSE(manifest.background_color.has_value());
122 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); 114 ASSERT_TRUE(manifest.gcm_sender_id.is_null());
123 ASSERT_TRUE(manifest.scope.is_empty()); 115 ASSERT_TRUE(manifest.scope.is_empty());
124 } 116 }
125 117
126 TEST_F(ManifestParserTest, MultipleErrorsReporting) { 118 TEST_F(ManifestParserTest, MultipleErrorsReporting) {
127 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4," 119 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4,"
128 "\"orientation\": {}, \"display\": \"foo\"," 120 "\"orientation\": {}, \"display\": \"foo\","
129 "\"start_url\": null, \"icons\": {}, \"theme_color\": 42," 121 "\"start_url\": null, \"icons\": {}, \"theme_color\": 42,"
130 "\"background_color\": 42 }"); 122 "\"background_color\": 42 }");
131 123
(...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 ParseManifest("{ \"prefer_related_applications\": false }"); 1048 ParseManifest("{ \"prefer_related_applications\": false }");
1057 EXPECT_FALSE(manifest.prefer_related_applications); 1049 EXPECT_FALSE(manifest.prefer_related_applications);
1058 EXPECT_EQ(0u, GetErrorCount()); 1050 EXPECT_EQ(0u, GetErrorCount());
1059 } 1051 }
1060 } 1052 }
1061 1053
1062 TEST_F(ManifestParserTest, ThemeColorParserRules) { 1054 TEST_F(ManifestParserTest, ThemeColorParserRules) {
1063 // Smoke test. 1055 // Smoke test.
1064 { 1056 {
1065 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }"); 1057 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }");
1066 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFFFF0000u); 1058 EXPECT_EQ(0xFFFF0000, manifest.theme_color);
1067 EXPECT_FALSE(manifest.IsEmpty()); 1059 EXPECT_FALSE(manifest.IsEmpty());
1068 EXPECT_EQ(0u, GetErrorCount()); 1060 EXPECT_EQ(0u, GetErrorCount());
1069 } 1061 }
1070 1062
1071 // Trim whitespaces. 1063 // Trim whitespaces.
1072 { 1064 {
1073 Manifest manifest = ParseManifest("{ \"theme_color\": \" blue \" }"); 1065 Manifest manifest = ParseManifest("{ \"theme_color\": \" blue \" }");
1074 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFF0000FFu); 1066 EXPECT_EQ(0xFF0000FFu, manifest.theme_color);
1075 EXPECT_EQ(0u, GetErrorCount()); 1067 EXPECT_EQ(0u, GetErrorCount());
1076 } 1068 }
1077 1069
1078 // Don't parse if theme_color isn't a string. 1070 // Don't parse if theme_color isn't a string.
1079 { 1071 {
1080 Manifest manifest = ParseManifest("{ \"theme_color\": {} }"); 1072 Manifest manifest = ParseManifest("{ \"theme_color\": {} }");
1081 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1073 EXPECT_FALSE(manifest.theme_color.has_value());
1082 EXPECT_EQ(1u, GetErrorCount()); 1074 EXPECT_EQ(1u, GetErrorCount());
1083 EXPECT_EQ("property 'theme_color' ignored, type string expected.", 1075 EXPECT_EQ("property 'theme_color' ignored, type string expected.",
1084 errors()[0]); 1076 errors()[0]);
1085 } 1077 }
1086 1078
1087 // Don't parse if theme_color isn't a string. 1079 // Don't parse if theme_color isn't a string.
1088 { 1080 {
1089 Manifest manifest = ParseManifest("{ \"theme_color\": false }"); 1081 Manifest manifest = ParseManifest("{ \"theme_color\": false }");
1090 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1082 EXPECT_FALSE(manifest.theme_color.has_value());
1091 EXPECT_EQ(1u, GetErrorCount()); 1083 EXPECT_EQ(1u, GetErrorCount());
1092 EXPECT_EQ("property 'theme_color' ignored, type string expected.", 1084 EXPECT_EQ("property 'theme_color' ignored, type string expected.",
1093 errors()[0]); 1085 errors()[0]);
1094 } 1086 }
1095 1087
1096 // Don't parse if theme_color isn't a string. 1088 // Don't parse if theme_color isn't a string.
1097 { 1089 {
1098 Manifest manifest = ParseManifest("{ \"theme_color\": null }"); 1090 Manifest manifest = ParseManifest("{ \"theme_color\": null }");
1099 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1091 EXPECT_FALSE(manifest.theme_color.has_value());
1100 EXPECT_EQ(1u, GetErrorCount()); 1092 EXPECT_EQ(1u, GetErrorCount());
1101 EXPECT_EQ("property 'theme_color' ignored, type string expected.", 1093 EXPECT_EQ("property 'theme_color' ignored, type string expected.",
1102 errors()[0]); 1094 errors()[0]);
1103 } 1095 }
1104 1096
1105 // Don't parse if theme_color isn't a string. 1097 // Don't parse if theme_color isn't a string.
1106 { 1098 {
1107 Manifest manifest = ParseManifest("{ \"theme_color\": [] }"); 1099 Manifest manifest = ParseManifest("{ \"theme_color\": [] }");
1108 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1100 EXPECT_FALSE(manifest.theme_color.has_value());
1109 EXPECT_EQ(1u, GetErrorCount()); 1101 EXPECT_EQ(1u, GetErrorCount());
1110 EXPECT_EQ("property 'theme_color' ignored, type string expected.", 1102 EXPECT_EQ("property 'theme_color' ignored, type string expected.",
1111 errors()[0]); 1103 errors()[0]);
1112 } 1104 }
1113 1105
1114 // Don't parse if theme_color isn't a string. 1106 // Don't parse if theme_color isn't a string.
1115 { 1107 {
1116 Manifest manifest = ParseManifest("{ \"theme_color\": 42 }"); 1108 Manifest manifest = ParseManifest("{ \"theme_color\": 42 }");
1117 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1109 EXPECT_FALSE(manifest.theme_color.has_value());
1118 EXPECT_EQ(1u, GetErrorCount()); 1110 EXPECT_EQ(1u, GetErrorCount());
1119 EXPECT_EQ("property 'theme_color' ignored, type string expected.", 1111 EXPECT_EQ("property 'theme_color' ignored, type string expected.",
1120 errors()[0]); 1112 errors()[0]);
1121 } 1113 }
1122 1114
1123 // Parse fails if string is not in a known format. 1115 // Parse fails if string is not in a known format.
1124 { 1116 {
1125 Manifest manifest = ParseManifest("{ \"theme_color\": \"foo(bar)\" }"); 1117 Manifest manifest = ParseManifest("{ \"theme_color\": \"foo(bar)\" }");
1126 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1118 EXPECT_FALSE(manifest.theme_color.has_value());
1127 EXPECT_EQ(1u, GetErrorCount()); 1119 EXPECT_EQ(1u, GetErrorCount());
1128 EXPECT_EQ("property 'theme_color' ignored," 1120 EXPECT_EQ("property 'theme_color' ignored,"
1129 " 'foo(bar)' is not a valid color.", 1121 " 'foo(bar)' is not a valid color.",
1130 errors()[0]); 1122 errors()[0]);
1131 } 1123 }
1132 1124
1133 // Parse fails if string is not in a known format. 1125 // Parse fails if string is not in a known format.
1134 { 1126 {
1135 Manifest manifest = ParseManifest("{ \"theme_color\": \"bleu\" }"); 1127 Manifest manifest = ParseManifest("{ \"theme_color\": \"bleu\" }");
1136 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1128 EXPECT_FALSE(manifest.theme_color.has_value());
1137 EXPECT_EQ(1u, GetErrorCount()); 1129 EXPECT_EQ(1u, GetErrorCount());
1138 EXPECT_EQ("property 'theme_color' ignored, 'bleu' is not a valid color.", 1130 EXPECT_EQ("property 'theme_color' ignored, 'bleu' is not a valid color.",
1139 errors()[0]); 1131 errors()[0]);
1140 } 1132 }
1141 1133
1142 // Parse fails if string is not in a known format. 1134 // Parse fails if string is not in a known format.
1143 { 1135 {
1144 Manifest manifest = ParseManifest("{ \"theme_color\": \"FF00FF\" }"); 1136 Manifest manifest = ParseManifest("{ \"theme_color\": \"FF00FF\" }");
1145 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1137 EXPECT_FALSE(manifest.theme_color.has_value());
1146 EXPECT_EQ(1u, GetErrorCount()); 1138 EXPECT_EQ(1u, GetErrorCount());
1147 EXPECT_EQ("property 'theme_color' ignored, 'FF00FF'" 1139 EXPECT_EQ("property 'theme_color' ignored, 'FF00FF'"
1148 " is not a valid color.", 1140 " is not a valid color.",
1149 errors()[0]); 1141 errors()[0]);
1150 } 1142 }
1151 1143
1152 // Parse fails if multiple values for theme_color are given. 1144 // Parse fails if multiple values for theme_color are given.
1153 { 1145 {
1154 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC #DEF\" }"); 1146 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC #DEF\" }");
1155 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1147 EXPECT_FALSE(manifest.theme_color.has_value());
1156 EXPECT_EQ(1u, GetErrorCount()); 1148 EXPECT_EQ(1u, GetErrorCount());
1157 EXPECT_EQ("property 'theme_color' ignored, " 1149 EXPECT_EQ("property 'theme_color' ignored, "
1158 "'#ABC #DEF' is not a valid color.", 1150 "'#ABC #DEF' is not a valid color.",
1159 errors()[0]); 1151 errors()[0]);
1160 } 1152 }
1161 1153
1162 // Parse fails if multiple values for theme_color are given. 1154 // Parse fails if multiple values for theme_color are given.
1163 { 1155 {
1164 Manifest manifest = ParseManifest( 1156 Manifest manifest = ParseManifest(
1165 "{ \"theme_color\": \"#AABBCC #DDEEFF\" }"); 1157 "{ \"theme_color\": \"#AABBCC #DDEEFF\" }");
1166 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 1158 EXPECT_FALSE(manifest.theme_color.has_value());
1167 EXPECT_EQ(1u, GetErrorCount()); 1159 EXPECT_EQ(1u, GetErrorCount());
1168 EXPECT_EQ("property 'theme_color' ignored, " 1160 EXPECT_EQ("property 'theme_color' ignored, "
1169 "'#AABBCC #DDEEFF' is not a valid color.", 1161 "'#AABBCC #DDEEFF' is not a valid color.",
1170 errors()[0]); 1162 errors()[0]);
1171 } 1163 }
1172 1164
1173 // Accept CSS color keyword format. 1165 // Accept CSS color keyword format.
1174 { 1166 {
1175 Manifest manifest = ParseManifest("{ \"theme_color\": \"blue\" }"); 1167 Manifest manifest = ParseManifest("{ \"theme_color\": \"blue\" }");
1176 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFF0000FFu); 1168 EXPECT_EQ(0xFF0000FFu, manifest.theme_color);
1177 EXPECT_EQ(0u, GetErrorCount()); 1169 EXPECT_EQ(0u, GetErrorCount());
1178 } 1170 }
1179 1171
1180 // Accept CSS color keyword format. 1172 // Accept CSS color keyword format.
1181 { 1173 {
1182 Manifest manifest = ParseManifest("{ \"theme_color\": \"chartreuse\" }"); 1174 Manifest manifest = ParseManifest("{ \"theme_color\": \"chartreuse\" }");
1183 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFF7FFF00u); 1175 EXPECT_EQ(0xFF7FFF00u, manifest.theme_color);
1184 EXPECT_EQ(0u, GetErrorCount()); 1176 EXPECT_EQ(0u, GetErrorCount());
1185 } 1177 }
1186 1178
1187 // Accept CSS RGB format. 1179 // Accept CSS RGB format.
1188 { 1180 {
1189 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FFF\" }"); 1181 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FFF\" }");
1190 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFFFFFFFFu); 1182 EXPECT_EQ(0xFFFFFFFFu, manifest.theme_color);
1191 EXPECT_EQ(0u, GetErrorCount()); 1183 EXPECT_EQ(0u, GetErrorCount());
1192 } 1184 }
1193 1185
1194 // Accept CSS RGB format. 1186 // Accept CSS RGB format.
1195 { 1187 {
1196 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC\" }"); 1188 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC\" }");
1197 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFFAABBCCu); 1189 EXPECT_EQ(0xFFAABBCCu, manifest.theme_color);
1198 EXPECT_EQ(0u, GetErrorCount()); 1190 EXPECT_EQ(0u, GetErrorCount());
1199 } 1191 }
1200 1192
1201 // Accept CSS RRGGBB format. 1193 // Accept CSS RRGGBB format.
1202 { 1194 {
1203 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }"); 1195 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }");
1204 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFFFF0000u); 1196 EXPECT_EQ(0xFFFF0000u, manifest.theme_color);
1205 EXPECT_EQ(0u, GetErrorCount()); 1197 EXPECT_EQ(0u, GetErrorCount());
1206 } 1198 }
1207 1199
1208 // Accept translucent colors. 1200 // Accept translucent colors.
1209 { 1201 {
1210 Manifest manifest = ParseManifest("{ \"theme_color\": \"rgba(255,0,0," 1202 Manifest manifest = ParseManifest("{ \"theme_color\": \"rgba(255,0,0,"
1211 "0.4)\" }"); 1203 "0.4)\" }");
1212 EXPECT_EQ(ExtractColor(manifest.theme_color), 0x66FF0000u); 1204 EXPECT_EQ(0x66FF0000u, manifest.theme_color);
1213 EXPECT_EQ(0u, GetErrorCount()); 1205 EXPECT_EQ(0u, GetErrorCount());
1214 } 1206 }
1215 1207
1216 // Accept transparent colors. 1208 // Accept transparent colors.
1217 { 1209 {
1218 Manifest manifest = ParseManifest("{ \"theme_color\": \"rgba(0,0,0,0)\" }"); 1210 Manifest manifest = ParseManifest("{ \"theme_color\": \"rgba(0,0,0,0)\" }");
1219 EXPECT_EQ(ExtractColor(manifest.theme_color), 0x00000000u); 1211 EXPECT_EQ(0x00000000u, manifest.theme_color);
1220 EXPECT_EQ(0u, GetErrorCount()); 1212 EXPECT_EQ(0u, GetErrorCount());
1221 } 1213 }
1222 } 1214 }
1223 1215
1224 TEST_F(ManifestParserTest, BackgroundColorParserRules) { 1216 TEST_F(ManifestParserTest, BackgroundColorParserRules) {
1225 // Smoke test. 1217 // Smoke test.
1226 { 1218 {
1227 Manifest manifest = ParseManifest("{ \"background_color\": \"#FF0000\" }"); 1219 Manifest manifest = ParseManifest("{ \"background_color\": \"#FF0000\" }");
1228 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFFFF0000u); 1220 EXPECT_EQ(0xFFFF0000u, manifest.background_color);
1229 EXPECT_FALSE(manifest.IsEmpty()); 1221 EXPECT_FALSE(manifest.IsEmpty());
1230 EXPECT_EQ(0u, GetErrorCount()); 1222 EXPECT_EQ(0u, GetErrorCount());
1231 } 1223 }
1232 1224
1233 // Trim whitespaces. 1225 // Trim whitespaces.
1234 { 1226 {
1235 Manifest manifest = ParseManifest( 1227 Manifest manifest = ParseManifest(
1236 "{ \"background_color\": \" blue \" }"); 1228 "{ \"background_color\": \" blue \" }");
1237 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFF0000FFu); 1229 EXPECT_EQ(0xFF0000FFu, manifest.background_color);
1238 EXPECT_EQ(0u, GetErrorCount()); 1230 EXPECT_EQ(0u, GetErrorCount());
1239 } 1231 }
1240 1232
1241 // Don't parse if background_color isn't a string. 1233 // Don't parse if background_color isn't a string.
1242 { 1234 {
1243 Manifest manifest = ParseManifest("{ \"background_color\": {} }"); 1235 Manifest manifest = ParseManifest("{ \"background_color\": {} }");
1244 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1236 EXPECT_FALSE(manifest.background_color.has_value());
1245 EXPECT_EQ(1u, GetErrorCount()); 1237 EXPECT_EQ(1u, GetErrorCount());
1246 EXPECT_EQ("property 'background_color' ignored, type string expected.", 1238 EXPECT_EQ("property 'background_color' ignored, type string expected.",
1247 errors()[0]); 1239 errors()[0]);
1248 } 1240 }
1249 1241
1250 // Don't parse if background_color isn't a string. 1242 // Don't parse if background_color isn't a string.
1251 { 1243 {
1252 Manifest manifest = ParseManifest("{ \"background_color\": false }"); 1244 Manifest manifest = ParseManifest("{ \"background_color\": false }");
1253 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1245 EXPECT_FALSE(manifest.background_color.has_value());
1254 EXPECT_EQ(1u, GetErrorCount()); 1246 EXPECT_EQ(1u, GetErrorCount());
1255 EXPECT_EQ("property 'background_color' ignored, type string expected.", 1247 EXPECT_EQ("property 'background_color' ignored, type string expected.",
1256 errors()[0]); 1248 errors()[0]);
1257 } 1249 }
1258 1250
1259 // Don't parse if background_color isn't a string. 1251 // Don't parse if background_color isn't a string.
1260 { 1252 {
1261 Manifest manifest = ParseManifest("{ \"background_color\": null }"); 1253 Manifest manifest = ParseManifest("{ \"background_color\": null }");
1262 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1254 EXPECT_FALSE(manifest.background_color.has_value());
1263 EXPECT_EQ(1u, GetErrorCount()); 1255 EXPECT_EQ(1u, GetErrorCount());
1264 EXPECT_EQ("property 'background_color' ignored, type string expected.", 1256 EXPECT_EQ("property 'background_color' ignored, type string expected.",
1265 errors()[0]); 1257 errors()[0]);
1266 } 1258 }
1267 1259
1268 // Don't parse if background_color isn't a string. 1260 // Don't parse if background_color isn't a string.
1269 { 1261 {
1270 Manifest manifest = ParseManifest("{ \"background_color\": [] }"); 1262 Manifest manifest = ParseManifest("{ \"background_color\": [] }");
1271 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1263 EXPECT_FALSE(manifest.background_color.has_value());
1272 EXPECT_EQ(1u, GetErrorCount()); 1264 EXPECT_EQ(1u, GetErrorCount());
1273 EXPECT_EQ("property 'background_color' ignored, type string expected.", 1265 EXPECT_EQ("property 'background_color' ignored, type string expected.",
1274 errors()[0]); 1266 errors()[0]);
1275 } 1267 }
1276 1268
1277 // Don't parse if background_color isn't a string. 1269 // Don't parse if background_color isn't a string.
1278 { 1270 {
1279 Manifest manifest = ParseManifest("{ \"background_color\": 42 }"); 1271 Manifest manifest = ParseManifest("{ \"background_color\": 42 }");
1280 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1272 EXPECT_FALSE(manifest.background_color.has_value());
1281 EXPECT_EQ(1u, GetErrorCount()); 1273 EXPECT_EQ(1u, GetErrorCount());
1282 EXPECT_EQ("property 'background_color' ignored, type string expected.", 1274 EXPECT_EQ("property 'background_color' ignored, type string expected.",
1283 errors()[0]); 1275 errors()[0]);
1284 } 1276 }
1285 1277
1286 // Parse fails if string is not in a known format. 1278 // Parse fails if string is not in a known format.
1287 { 1279 {
1288 Manifest manifest = ParseManifest("{ \"background_color\": \"foo(bar)\" }"); 1280 Manifest manifest = ParseManifest("{ \"background_color\": \"foo(bar)\" }");
1289 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1281 EXPECT_FALSE(manifest.background_color.has_value());
1290 EXPECT_EQ(1u, GetErrorCount()); 1282 EXPECT_EQ(1u, GetErrorCount());
1291 EXPECT_EQ("property 'background_color' ignored," 1283 EXPECT_EQ("property 'background_color' ignored,"
1292 " 'foo(bar)' is not a valid color.", 1284 " 'foo(bar)' is not a valid color.",
1293 errors()[0]); 1285 errors()[0]);
1294 } 1286 }
1295 1287
1296 // Parse fails if string is not in a known format. 1288 // Parse fails if string is not in a known format.
1297 { 1289 {
1298 Manifest manifest = ParseManifest("{ \"background_color\": \"bleu\" }"); 1290 Manifest manifest = ParseManifest("{ \"background_color\": \"bleu\" }");
1299 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1291 EXPECT_FALSE(manifest.background_color.has_value());
1300 EXPECT_EQ(1u, GetErrorCount()); 1292 EXPECT_EQ(1u, GetErrorCount());
1301 EXPECT_EQ("property 'background_color' ignored," 1293 EXPECT_EQ("property 'background_color' ignored,"
1302 " 'bleu' is not a valid color.", 1294 " 'bleu' is not a valid color.",
1303 errors()[0]); 1295 errors()[0]);
1304 } 1296 }
1305 1297
1306 // Parse fails if string is not in a known format. 1298 // Parse fails if string is not in a known format.
1307 { 1299 {
1308 Manifest manifest = ParseManifest("{ \"background_color\": \"FF00FF\" }"); 1300 Manifest manifest = ParseManifest("{ \"background_color\": \"FF00FF\" }");
1309 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1301 EXPECT_FALSE(manifest.background_color.has_value());
1310 EXPECT_EQ(1u, GetErrorCount()); 1302 EXPECT_EQ(1u, GetErrorCount());
1311 EXPECT_EQ("property 'background_color' ignored," 1303 EXPECT_EQ("property 'background_color' ignored,"
1312 " 'FF00FF' is not a valid color.", 1304 " 'FF00FF' is not a valid color.",
1313 errors()[0]); 1305 errors()[0]);
1314 } 1306 }
1315 1307
1316 // Parse fails if multiple values for background_color are given. 1308 // Parse fails if multiple values for background_color are given.
1317 { 1309 {
1318 Manifest manifest = ParseManifest( 1310 Manifest manifest = ParseManifest(
1319 "{ \"background_color\": \"#ABC #DEF\" }"); 1311 "{ \"background_color\": \"#ABC #DEF\" }");
1320 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1312 EXPECT_FALSE(manifest.background_color.has_value());
1321 EXPECT_EQ(1u, GetErrorCount()); 1313 EXPECT_EQ(1u, GetErrorCount());
1322 EXPECT_EQ("property 'background_color' ignored, " 1314 EXPECT_EQ("property 'background_color' ignored, "
1323 "'#ABC #DEF' is not a valid color.", 1315 "'#ABC #DEF' is not a valid color.",
1324 errors()[0]); 1316 errors()[0]);
1325 } 1317 }
1326 1318
1327 // Parse fails if multiple values for background_color are given. 1319 // Parse fails if multiple values for background_color are given.
1328 { 1320 {
1329 Manifest manifest = ParseManifest( 1321 Manifest manifest = ParseManifest(
1330 "{ \"background_color\": \"#AABBCC #DDEEFF\" }"); 1322 "{ \"background_color\": \"#AABBCC #DDEEFF\" }");
1331 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 1323 EXPECT_FALSE(manifest.background_color.has_value());
1332 EXPECT_EQ(1u, GetErrorCount()); 1324 EXPECT_EQ(1u, GetErrorCount());
1333 EXPECT_EQ("property 'background_color' ignored, " 1325 EXPECT_EQ("property 'background_color' ignored, "
1334 "'#AABBCC #DDEEFF' is not a valid color.", 1326 "'#AABBCC #DDEEFF' is not a valid color.",
1335 errors()[0]); 1327 errors()[0]);
1336 } 1328 }
1337 1329
1338 // Accept CSS color keyword format. 1330 // Accept CSS color keyword format.
1339 { 1331 {
1340 Manifest manifest = ParseManifest("{ \"background_color\": \"blue\" }"); 1332 Manifest manifest = ParseManifest("{ \"background_color\": \"blue\" }");
1341 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFF0000FFu); 1333 EXPECT_EQ(0xFF0000FFu, manifest.background_color);
1342 EXPECT_EQ(0u, GetErrorCount()); 1334 EXPECT_EQ(0u, GetErrorCount());
1343 } 1335 }
1344 1336
1345 // Accept CSS color keyword format. 1337 // Accept CSS color keyword format.
1346 { 1338 {
1347 Manifest manifest = ParseManifest( 1339 Manifest manifest = ParseManifest(
1348 "{ \"background_color\": \"chartreuse\" }"); 1340 "{ \"background_color\": \"chartreuse\" }");
1349 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFF7FFF00u); 1341 EXPECT_EQ(0xFF7FFF00u, manifest.background_color);
1350 EXPECT_EQ(0u, GetErrorCount()); 1342 EXPECT_EQ(0u, GetErrorCount());
1351 } 1343 }
1352 1344
1353 // Accept CSS RGB format. 1345 // Accept CSS RGB format.
1354 { 1346 {
1355 Manifest manifest = ParseManifest("{ \"background_color\": \"#FFF\" }"); 1347 Manifest manifest = ParseManifest("{ \"background_color\": \"#FFF\" }");
1356 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFFFFFFFFu); 1348 EXPECT_EQ(0xFFFFFFFFu, manifest.background_color);
1357 EXPECT_EQ(0u, GetErrorCount()); 1349 EXPECT_EQ(0u, GetErrorCount());
1358 } 1350 }
1359 1351
1360 // Accept CSS RGB format. 1352 // Accept CSS RGB format.
1361 { 1353 {
1362 Manifest manifest = ParseManifest("{ \"background_color\": \"#ABC\" }"); 1354 Manifest manifest = ParseManifest("{ \"background_color\": \"#ABC\" }");
1363 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFFAABBCCu); 1355 EXPECT_EQ(0xFFAABBCCu, manifest.background_color);
1364 EXPECT_EQ(0u, GetErrorCount()); 1356 EXPECT_EQ(0u, GetErrorCount());
1365 } 1357 }
1366 1358
1367 // Accept CSS RRGGBB format. 1359 // Accept CSS RRGGBB format.
1368 { 1360 {
1369 Manifest manifest = ParseManifest("{ \"background_color\": \"#FF0000\" }"); 1361 Manifest manifest = ParseManifest("{ \"background_color\": \"#FF0000\" }");
1370 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFFFF0000u); 1362 EXPECT_EQ(0xFFFF0000, manifest.background_color);
1371 EXPECT_EQ(0u, GetErrorCount()); 1363 EXPECT_EQ(0u, GetErrorCount());
1372 } 1364 }
1373 1365
1374 // Accept translucent colors. 1366 // Accept translucent colors.
1375 { 1367 {
1376 Manifest manifest = ParseManifest("{ \"background_color\": \"rgba(255,0,0," 1368 Manifest manifest = ParseManifest("{ \"background_color\": \"rgba(255,0,0,"
1377 "0.4)\" }"); 1369 "0.4)\" }");
1378 EXPECT_EQ(ExtractColor(manifest.background_color), 0x66FF0000u); 1370 EXPECT_EQ(0x66FF0000u, manifest.background_color);
1379 EXPECT_EQ(0u, GetErrorCount()); 1371 EXPECT_EQ(0u, GetErrorCount());
1380 } 1372 }
1381 1373
1382 // Accept transparent colors. 1374 // Accept transparent colors.
1383 { 1375 {
1384 Manifest manifest = ParseManifest("{ \"background_color\": \"rgba(0,0,0," 1376 Manifest manifest = ParseManifest("{ \"background_color\": \"rgba(0,0,0,"
1385 "0)\" }"); 1377 "0)\" }");
1386 EXPECT_EQ(ExtractColor(manifest.background_color), 0x00000000u); 1378 EXPECT_EQ(0x00000000u, manifest.background_color);
1387 EXPECT_EQ(0u, GetErrorCount()); 1379 EXPECT_EQ(0u, GetErrorCount());
1388 } 1380 }
1389 } 1381 }
1390 1382
1391 TEST_F(ManifestParserTest, GCMSenderIDParseRules) { 1383 TEST_F(ManifestParserTest, GCMSenderIDParseRules) {
1392 // Smoke test. 1384 // Smoke test.
1393 { 1385 {
1394 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); 1386 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }");
1395 EXPECT_TRUE(base::EqualsASCII(manifest.gcm_sender_id.string(), "foo")); 1387 EXPECT_TRUE(base::EqualsASCII(manifest.gcm_sender_id.string(), "foo"));
1396 EXPECT_EQ(0u, GetErrorCount()); 1388 EXPECT_EQ(0u, GetErrorCount());
(...skipping 17 matching lines...) Expand all
1414 { 1406 {
1415 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); 1407 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }");
1416 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); 1408 EXPECT_TRUE(manifest.gcm_sender_id.is_null());
1417 EXPECT_EQ(1u, GetErrorCount()); 1409 EXPECT_EQ(1u, GetErrorCount());
1418 EXPECT_EQ("property 'gcm_sender_id' ignored, type string expected.", 1410 EXPECT_EQ("property 'gcm_sender_id' ignored, type string expected.",
1419 errors()[0]); 1411 errors()[0]);
1420 } 1412 }
1421 } 1413 }
1422 1414
1423 } // namespace content 1415 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/manifest/manifest_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698