OLD | NEW |
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 "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "content/public/common/manifest.h" | 8 #include "content/public/common/manifest.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 EXPECT_EQ("Manifest parsing error: Line: 1, column: 1, Unexpected token.", | 71 EXPECT_EQ("Manifest parsing error: Line: 1, column: 1, Unexpected token.", |
72 errors()[0]); | 72 errors()[0]); |
73 | 73 |
74 // A parsing error is equivalent to an empty manifest. | 74 // A parsing error is equivalent to an empty manifest. |
75 ASSERT_TRUE(manifest.IsEmpty()); | 75 ASSERT_TRUE(manifest.IsEmpty()); |
76 ASSERT_TRUE(manifest.name.is_null()); | 76 ASSERT_TRUE(manifest.name.is_null()); |
77 ASSERT_TRUE(manifest.short_name.is_null()); | 77 ASSERT_TRUE(manifest.short_name.is_null()); |
78 ASSERT_TRUE(manifest.start_url.is_empty()); | 78 ASSERT_TRUE(manifest.start_url.is_empty()); |
79 ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED); | 79 ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED); |
80 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); | 80 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); |
| 81 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
81 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); | 82 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); |
82 } | 83 } |
83 | 84 |
84 TEST_F(ManifestParserTest, ValidNoContentParses) { | 85 TEST_F(ManifestParserTest, ValidNoContentParses) { |
85 Manifest manifest = ParseManifest("{}"); | 86 Manifest manifest = ParseManifest("{}"); |
86 | 87 |
87 // Empty Manifest is not a parsing error. | 88 // Empty Manifest is not a parsing error. |
88 EXPECT_EQ(0u, GetErrorCount()); | 89 EXPECT_EQ(0u, GetErrorCount()); |
89 | 90 |
90 // Check that all the fields are null in that case. | 91 // Check that all the fields are null in that case. |
91 ASSERT_TRUE(manifest.IsEmpty()); | 92 ASSERT_TRUE(manifest.IsEmpty()); |
92 ASSERT_TRUE(manifest.name.is_null()); | 93 ASSERT_TRUE(manifest.name.is_null()); |
93 ASSERT_TRUE(manifest.short_name.is_null()); | 94 ASSERT_TRUE(manifest.short_name.is_null()); |
94 ASSERT_TRUE(manifest.start_url.is_empty()); | 95 ASSERT_TRUE(manifest.start_url.is_empty()); |
95 ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED); | 96 ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED); |
96 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); | 97 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); |
| 98 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
97 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); | 99 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); |
98 } | 100 } |
99 | 101 |
100 TEST_F(ManifestParserTest, MultipleErrorsReporting) { | 102 TEST_F(ManifestParserTest, MultipleErrorsReporting) { |
101 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4," | 103 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4," |
102 "\"orientation\": {}, \"display\": \"foo\", \"start_url\": null," | 104 "\"orientation\": {}, \"display\": \"foo\"," |
103 "\"icons\": {} }"); | 105 "\"start_url\": null, \"icons\": {}, \"theme_color\": 42 }"); |
104 | 106 |
105 EXPECT_EQ(6u, GetErrorCount()); | 107 EXPECT_EQ(7u, GetErrorCount()); |
106 | 108 |
107 EXPECT_EQ("Manifest parsing error: property 'name' ignored," | 109 EXPECT_EQ("Manifest parsing error: property 'name' ignored," |
108 " type string expected.", | 110 " type string expected.", |
109 errors()[0]); | 111 errors()[0]); |
110 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored," | 112 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored," |
111 " type string expected.", | 113 " type string expected.", |
112 errors()[1]); | 114 errors()[1]); |
113 EXPECT_EQ("Manifest parsing error: property 'start_url' ignored," | 115 EXPECT_EQ("Manifest parsing error: property 'start_url' ignored," |
114 " type string expected.", | 116 " type string expected.", |
115 errors()[2]); | 117 errors()[2]); |
116 EXPECT_EQ("Manifest parsing error: unknown 'display' value ignored.", | 118 EXPECT_EQ("Manifest parsing error: unknown 'display' value ignored.", |
117 errors()[3]); | 119 errors()[3]); |
118 EXPECT_EQ("Manifest parsing error: property 'orientation' ignored," | 120 EXPECT_EQ("Manifest parsing error: property 'orientation' ignored," |
119 " type string expected.", | 121 " type string expected.", |
120 errors()[4]); | 122 errors()[4]); |
121 EXPECT_EQ("Manifest parsing error: property 'icons' ignored, " | 123 EXPECT_EQ("Manifest parsing error: property 'icons' ignored, " |
122 "type array expected.", | 124 "type array expected.", |
123 errors()[5]); | 125 errors()[5]); |
| 126 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 127 " type string expected.", |
| 128 errors()[6]); |
124 } | 129 } |
125 | 130 |
126 TEST_F(ManifestParserTest, NameParseRules) { | 131 TEST_F(ManifestParserTest, NameParseRules) { |
127 // Smoke test. | 132 // Smoke test. |
128 { | 133 { |
129 Manifest manifest = ParseManifest("{ \"name\": \"foo\" }"); | 134 Manifest manifest = ParseManifest("{ \"name\": \"foo\" }"); |
130 ASSERT_TRUE(base::EqualsASCII(manifest.name.string(), "foo")); | 135 ASSERT_TRUE(base::EqualsASCII(manifest.name.string(), "foo")); |
131 ASSERT_FALSE(manifest.IsEmpty()); | 136 ASSERT_FALSE(manifest.IsEmpty()); |
132 EXPECT_EQ(0u, GetErrorCount()); | 137 EXPECT_EQ(0u, GetErrorCount()); |
133 } | 138 } |
(...skipping 838 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
972 | 977 |
973 // "False" should set the boolean false without throwing errors. | 978 // "False" should set the boolean false without throwing errors. |
974 { | 979 { |
975 Manifest manifest = | 980 Manifest manifest = |
976 ParseManifest("{ \"prefer_related_applications\": false }"); | 981 ParseManifest("{ \"prefer_related_applications\": false }"); |
977 EXPECT_FALSE(manifest.prefer_related_applications); | 982 EXPECT_FALSE(manifest.prefer_related_applications); |
978 EXPECT_EQ(0u, GetErrorCount()); | 983 EXPECT_EQ(0u, GetErrorCount()); |
979 } | 984 } |
980 } | 985 } |
981 | 986 |
| 987 TEST_F(ManifestParserTest, ThemeColorParserRules) { |
| 988 // Smoke test. |
| 989 { |
| 990 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }"); |
| 991 EXPECT_EQ(manifest.theme_color, 0xFFFF0000); |
| 992 EXPECT_FALSE(manifest.IsEmpty()); |
| 993 EXPECT_EQ(0u, GetErrorCount()); |
| 994 } |
| 995 |
| 996 // Trim whitespaces. |
| 997 { |
| 998 Manifest manifest = ParseManifest("{ \"theme_color\": \" blue \" }"); |
| 999 EXPECT_EQ(manifest.theme_color, 0xFF0000FF); |
| 1000 EXPECT_EQ(0u, GetErrorCount()); |
| 1001 } |
| 1002 |
| 1003 // Don't parse if theme_color isn't a string. |
| 1004 { |
| 1005 Manifest manifest = ParseManifest("{ \"theme_color\": {} }"); |
| 1006 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1007 EXPECT_EQ(1u, GetErrorCount()); |
| 1008 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 1009 " type string expected.", |
| 1010 errors()[0]); |
| 1011 } |
| 1012 |
| 1013 // Don't parse if theme_color isn't a string. |
| 1014 { |
| 1015 Manifest manifest = ParseManifest("{ \"theme_color\": false }"); |
| 1016 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1017 EXPECT_EQ(1u, GetErrorCount()); |
| 1018 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 1019 " type string expected.", |
| 1020 errors()[0]); |
| 1021 } |
| 1022 |
| 1023 // Don't parse if theme_color isn't a string. |
| 1024 { |
| 1025 Manifest manifest = ParseManifest("{ \"theme_color\": null }"); |
| 1026 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1027 EXPECT_EQ(1u, GetErrorCount()); |
| 1028 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 1029 " type string expected.", |
| 1030 errors()[0]); |
| 1031 } |
| 1032 |
| 1033 // Don't parse if theme_color isn't a string. |
| 1034 { |
| 1035 Manifest manifest = ParseManifest("{ \"theme_color\": [] }"); |
| 1036 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1037 EXPECT_EQ(1u, GetErrorCount()); |
| 1038 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 1039 " type string expected.", |
| 1040 errors()[0]); |
| 1041 } |
| 1042 |
| 1043 // Don't parse if theme_color isn't a string. |
| 1044 { |
| 1045 Manifest manifest = ParseManifest("{ \"theme_color\": 42 }"); |
| 1046 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1047 EXPECT_EQ(1u, GetErrorCount()); |
| 1048 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 1049 " type string expected.", |
| 1050 errors()[0]); |
| 1051 } |
| 1052 |
| 1053 // Parse fails if string is not in a known format. |
| 1054 { |
| 1055 Manifest manifest = ParseManifest("{ \"theme_color\": \"foo(bar)\" }"); |
| 1056 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1057 EXPECT_EQ(1u, GetErrorCount()); |
| 1058 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 1059 " 'foo(bar)' is not a valid color.", |
| 1060 errors()[0]); |
| 1061 } |
| 1062 |
| 1063 // Parse fails if string is not in a known format. |
| 1064 { |
| 1065 Manifest manifest = ParseManifest("{ \"theme_color\": \"bleu\" }"); |
| 1066 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1067 EXPECT_EQ(1u, GetErrorCount()); |
| 1068 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, 'bleu'" |
| 1069 " is not a valid color.", |
| 1070 errors()[0]); |
| 1071 } |
| 1072 |
| 1073 // Parse fails if string is not in a known format. |
| 1074 { |
| 1075 Manifest manifest = ParseManifest("{ \"theme_color\": \"FF00FF\" }"); |
| 1076 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1077 EXPECT_EQ(1u, GetErrorCount()); |
| 1078 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, 'FF00FF'" |
| 1079 " is not a valid color.", |
| 1080 errors()[0]); |
| 1081 } |
| 1082 |
| 1083 // Parse fails if multiple values for theme_color are given. |
| 1084 { |
| 1085 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC #DEF\" }"); |
| 1086 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1087 EXPECT_EQ(1u, GetErrorCount()); |
| 1088 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, " |
| 1089 "'#ABC #DEF' is not a valid color.", |
| 1090 errors()[0]); |
| 1091 } |
| 1092 |
| 1093 // Parse fails if multiple values for theme_color are given. |
| 1094 { |
| 1095 Manifest manifest = ParseManifest( |
| 1096 "{ \"theme_color\": \"#AABBCC #DDEEFF\" }"); |
| 1097 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 1098 EXPECT_EQ(1u, GetErrorCount()); |
| 1099 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, " |
| 1100 "'#AABBCC #DDEEFF' is not a valid color.", |
| 1101 errors()[0]); |
| 1102 } |
| 1103 |
| 1104 // Accept CSS color keyword format. |
| 1105 { |
| 1106 Manifest manifest = ParseManifest("{ \"theme_color\": \"blue\" }"); |
| 1107 EXPECT_EQ(manifest.theme_color, 0xFF0000FF); |
| 1108 EXPECT_EQ(0u, GetErrorCount()); |
| 1109 } |
| 1110 |
| 1111 // Accept CSS color keyword format. |
| 1112 { |
| 1113 Manifest manifest = ParseManifest("{ \"theme_color\": \"chartreuse\" }"); |
| 1114 EXPECT_EQ(manifest.theme_color, 0xFF7FFF00); |
| 1115 EXPECT_EQ(0u, GetErrorCount()); |
| 1116 } |
| 1117 |
| 1118 // Accept CSS RGB format. |
| 1119 { |
| 1120 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FFF\" }"); |
| 1121 EXPECT_EQ(manifest.theme_color, 0xFFFFFFFF); |
| 1122 EXPECT_EQ(0u, GetErrorCount()); |
| 1123 } |
| 1124 |
| 1125 // Accept CSS RGB format. |
| 1126 { |
| 1127 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC\" }"); |
| 1128 EXPECT_EQ(manifest.theme_color, 0xFFAABBCC); |
| 1129 EXPECT_EQ(0u, GetErrorCount()); |
| 1130 } |
| 1131 |
| 1132 // Accept CSS RRGGBB format. |
| 1133 { |
| 1134 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }"); |
| 1135 EXPECT_EQ(manifest.theme_color, 0xFFFF0000); |
| 1136 EXPECT_EQ(0u, GetErrorCount()); |
| 1137 } |
| 1138 } |
| 1139 |
982 TEST_F(ManifestParserTest, GCMSenderIDParseRules) { | 1140 TEST_F(ManifestParserTest, GCMSenderIDParseRules) { |
983 // Smoke test. | 1141 // Smoke test. |
984 { | 1142 { |
985 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); | 1143 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); |
986 EXPECT_TRUE(base::EqualsASCII(manifest.gcm_sender_id.string(), "foo")); | 1144 EXPECT_TRUE(base::EqualsASCII(manifest.gcm_sender_id.string(), "foo")); |
987 EXPECT_EQ(0u, GetErrorCount()); | 1145 EXPECT_EQ(0u, GetErrorCount()); |
988 } | 1146 } |
989 | 1147 |
990 // Trim whitespaces. | 1148 // Trim whitespaces. |
991 { | 1149 { |
(...skipping 15 matching lines...) Expand all Loading... |
1007 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); | 1165 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); |
1008 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); | 1166 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
1009 EXPECT_EQ(1u, GetErrorCount()); | 1167 EXPECT_EQ(1u, GetErrorCount()); |
1010 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," | 1168 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," |
1011 " type string expected.", | 1169 " type string expected.", |
1012 errors()[0]); | 1170 errors()[0]); |
1013 } | 1171 } |
1014 } | 1172 } |
1015 | 1173 |
1016 } // namespace content | 1174 } // namespace content |
OLD | NEW |