| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/declarative_content/content_action.h" | 5 #include "chrome/browser/extensions/api/declarative_content/content_action.h" |
| 6 | 6 |
| 7 #include "base/test/values_test_util.h" | 7 #include "base/test/values_test_util.h" |
| 8 #include "chrome/browser/extensions/extension_action.h" | 8 #include "chrome/browser/extensions/extension_action.h" |
| 9 #include "chrome/browser/extensions/extension_action_manager.h" | 9 #include "chrome/browser/extensions/extension_action_manager.h" |
| 10 #include "chrome/browser/extensions/extension_tab_util.h" | 10 #include "chrome/browser/extensions/extension_tab_util.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 using testing::HasSubstr; | 22 using testing::HasSubstr; |
| 23 | 23 |
| 24 TEST(DeclarativeContentActionTest, InvalidCreation) { | 24 TEST(DeclarativeContentActionTest, InvalidCreation) { |
| 25 TestExtensionEnvironment env; | 25 TestExtensionEnvironment env; |
| 26 std::string error; | 26 std::string error; |
| 27 bool bad_message = false; | 27 bool bad_message = false; |
| 28 scoped_refptr<const ContentAction> result; | 28 scoped_refptr<const ContentAction> result; |
| 29 | 29 |
| 30 // Test wrong data type passed. | 30 // Test wrong data type passed. |
| 31 error.clear(); | 31 error.clear(); |
| 32 result = ContentAction::Create(NULL, *ParseJson("[]"), &error, &bad_message); | 32 result = ContentAction::Create( |
| 33 NULL, NULL, *ParseJson("[]"), &error, &bad_message); |
| 33 EXPECT_TRUE(bad_message); | 34 EXPECT_TRUE(bad_message); |
| 34 EXPECT_EQ("", error); | 35 EXPECT_EQ("", error); |
| 35 EXPECT_FALSE(result.get()); | 36 EXPECT_FALSE(result.get()); |
| 36 | 37 |
| 37 // Test missing instanceType element. | 38 // Test missing instanceType element. |
| 38 error.clear(); | 39 error.clear(); |
| 39 result = ContentAction::Create(NULL, *ParseJson("{}"), &error, &bad_message); | 40 result = ContentAction::Create( |
| 41 NULL, NULL, *ParseJson("{}"), &error, &bad_message); |
| 40 EXPECT_TRUE(bad_message); | 42 EXPECT_TRUE(bad_message); |
| 41 EXPECT_EQ("", error); | 43 EXPECT_EQ("", error); |
| 42 EXPECT_FALSE(result.get()); | 44 EXPECT_FALSE(result.get()); |
| 43 | 45 |
| 44 // Test wrong instanceType element. | 46 // Test wrong instanceType element. |
| 45 error.clear(); | 47 error.clear(); |
| 46 result = ContentAction::Create(NULL, *ParseJson( | 48 result = ContentAction::Create(NULL, NULL, *ParseJson( |
| 47 "{\n" | 49 "{\n" |
| 48 " \"instanceType\": \"declarativeContent.UnknownType\",\n" | 50 " \"instanceType\": \"declarativeContent.UnknownType\",\n" |
| 49 "}"), | 51 "}"), |
| 50 &error, &bad_message); | 52 &error, &bad_message); |
| 51 EXPECT_THAT(error, HasSubstr("invalid instanceType")); | 53 EXPECT_THAT(error, HasSubstr("invalid instanceType")); |
| 52 EXPECT_FALSE(result.get()); | 54 EXPECT_FALSE(result.get()); |
| 53 } | 55 } |
| 54 | 56 |
| 55 TEST(DeclarativeContentActionTest, ShowPageActionWithoutPageAction) { | 57 TEST(DeclarativeContentActionTest, ShowPageActionWithoutPageAction) { |
| 56 TestExtensionEnvironment env; | 58 TestExtensionEnvironment env; |
| 57 | 59 |
| 58 const Extension* extension = env.MakeExtension(base::DictionaryValue()); | 60 const Extension* extension = env.MakeExtension(base::DictionaryValue()); |
| 59 std::string error; | 61 std::string error; |
| 60 bool bad_message = false; | 62 bool bad_message = false; |
| 61 scoped_refptr<const ContentAction> result = ContentAction::Create( | 63 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 64 NULL, |
| 62 extension, | 65 extension, |
| 63 *ParseJson( | 66 *ParseJson( |
| 64 "{\n" | 67 "{\n" |
| 65 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" | 68 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" |
| 66 "}"), | 69 "}"), |
| 67 &error, | 70 &error, |
| 68 &bad_message); | 71 &bad_message); |
| 69 EXPECT_THAT(error, testing::HasSubstr("without a page action")); | 72 EXPECT_THAT(error, testing::HasSubstr("without a page action")); |
| 70 EXPECT_FALSE(bad_message); | 73 EXPECT_FALSE(bad_message); |
| 71 ASSERT_FALSE(result.get()); | 74 ASSERT_FALSE(result.get()); |
| 72 } | 75 } |
| 73 | 76 |
| 74 TEST(DeclarativeContentActionTest, ShowPageAction) { | 77 TEST(DeclarativeContentActionTest, ShowPageAction) { |
| 75 TestExtensionEnvironment env; | 78 TestExtensionEnvironment env; |
| 76 | 79 |
| 77 const Extension* extension = env.MakeExtension( | 80 const Extension* extension = env.MakeExtension( |
| 78 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }")); | 81 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }")); |
| 79 std::string error; | 82 std::string error; |
| 80 bool bad_message = false; | 83 bool bad_message = false; |
| 81 scoped_refptr<const ContentAction> result = ContentAction::Create( | 84 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 85 NULL, |
| 82 extension, | 86 extension, |
| 83 *ParseJson( | 87 *ParseJson( |
| 84 "{\n" | 88 "{\n" |
| 85 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" | 89 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" |
| 86 "}"), | 90 "}"), |
| 87 &error, | 91 &error, |
| 88 &bad_message); | 92 &bad_message); |
| 89 EXPECT_EQ("", error); | 93 EXPECT_EQ("", error); |
| 90 EXPECT_FALSE(bad_message); | 94 EXPECT_FALSE(bad_message); |
| 91 ASSERT_TRUE(result.get()); | 95 ASSERT_TRUE(result.get()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 109 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); | 113 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| 110 } | 114 } |
| 111 | 115 |
| 112 TEST(DeclarativeContentActionTest, RequestContentScriptMissingScripts) { | 116 TEST(DeclarativeContentActionTest, RequestContentScriptMissingScripts) { |
| 113 TestExtensionEnvironment env; | 117 TestExtensionEnvironment env; |
| 114 | 118 |
| 115 std::string error; | 119 std::string error; |
| 116 bool bad_message = false; | 120 bool bad_message = false; |
| 117 scoped_refptr<const ContentAction> result = ContentAction::Create( | 121 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 118 NULL, | 122 NULL, |
| 123 NULL, |
| 119 *ParseJson( | 124 *ParseJson( |
| 120 "{\n" | 125 "{\n" |
| 121 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 126 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 122 " \"allFrames\": true,\n" | 127 " \"allFrames\": true,\n" |
| 123 " \"matchAboutBlank\": true\n" | 128 " \"matchAboutBlank\": true\n" |
| 124 "}"), | 129 "}"), |
| 125 &error, | 130 &error, |
| 126 &bad_message); | 131 &bad_message); |
| 127 EXPECT_THAT(error, testing::HasSubstr("Missing parameter is required")); | 132 EXPECT_THAT(error, testing::HasSubstr("Missing parameter is required")); |
| 128 EXPECT_FALSE(bad_message); | 133 EXPECT_FALSE(bad_message); |
| 129 ASSERT_FALSE(result.get()); | 134 ASSERT_FALSE(result.get()); |
| 130 } | 135 } |
| 131 | 136 |
| 132 TEST(DeclarativeContentActionTest, RequestContentScriptCSS) { | 137 TEST(DeclarativeContentActionTest, RequestContentScriptCSS) { |
| 133 TestExtensionEnvironment env; | 138 TestExtensionEnvironment env; |
| 134 | 139 |
| 135 std::string error; | 140 std::string error; |
| 136 bool bad_message = false; | 141 bool bad_message = false; |
| 137 scoped_refptr<const ContentAction> result = ContentAction::Create( | 142 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 138 NULL, | 143 NULL, |
| 144 NULL, |
| 139 *ParseJson( | 145 *ParseJson( |
| 140 "{\n" | 146 "{\n" |
| 141 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 147 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 142 " \"css\": [\"style.css\"]\n" | 148 " \"css\": [\"style.css\"]\n" |
| 143 "}"), | 149 "}"), |
| 144 &error, | 150 &error, |
| 145 &bad_message); | 151 &bad_message); |
| 146 EXPECT_EQ("", error); | 152 EXPECT_EQ("", error); |
| 147 EXPECT_FALSE(bad_message); | 153 EXPECT_FALSE(bad_message); |
| 148 ASSERT_TRUE(result.get()); | 154 ASSERT_TRUE(result.get()); |
| 149 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); | 155 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); |
| 150 } | 156 } |
| 151 | 157 |
| 152 TEST(DeclarativeContentActionTest, RequestContentScriptJS) { | 158 TEST(DeclarativeContentActionTest, RequestContentScriptJS) { |
| 153 TestExtensionEnvironment env; | 159 TestExtensionEnvironment env; |
| 154 | 160 |
| 155 std::string error; | 161 std::string error; |
| 156 bool bad_message = false; | 162 bool bad_message = false; |
| 157 scoped_refptr<const ContentAction> result = ContentAction::Create( | 163 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 158 NULL, | 164 NULL, |
| 165 NULL, |
| 159 *ParseJson( | 166 *ParseJson( |
| 160 "{\n" | 167 "{\n" |
| 161 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 168 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 162 " \"js\": [\"script.js\"]\n" | 169 " \"js\": [\"script.js\"]\n" |
| 163 "}"), | 170 "}"), |
| 164 &error, | 171 &error, |
| 165 &bad_message); | 172 &bad_message); |
| 166 EXPECT_EQ("", error); | 173 EXPECT_EQ("", error); |
| 167 EXPECT_FALSE(bad_message); | 174 EXPECT_FALSE(bad_message); |
| 168 ASSERT_TRUE(result.get()); | 175 ASSERT_TRUE(result.get()); |
| 169 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); | 176 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); |
| 170 } | 177 } |
| 171 | 178 |
| 172 TEST(DeclarativeContentActionTest, RequestContentScriptCSSBadType) { | 179 TEST(DeclarativeContentActionTest, RequestContentScriptCSSBadType) { |
| 173 TestExtensionEnvironment env; | 180 TestExtensionEnvironment env; |
| 174 | 181 |
| 175 std::string error; | 182 std::string error; |
| 176 bool bad_message = false; | 183 bool bad_message = false; |
| 177 scoped_refptr<const ContentAction> result = ContentAction::Create( | 184 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 178 NULL, | 185 NULL, |
| 186 NULL, |
| 179 *ParseJson( | 187 *ParseJson( |
| 180 "{\n" | 188 "{\n" |
| 181 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 189 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 182 " \"css\": \"style.css\"\n" | 190 " \"css\": \"style.css\"\n" |
| 183 "}"), | 191 "}"), |
| 184 &error, | 192 &error, |
| 185 &bad_message); | 193 &bad_message); |
| 186 EXPECT_TRUE(bad_message); | 194 EXPECT_TRUE(bad_message); |
| 187 ASSERT_FALSE(result.get()); | 195 ASSERT_FALSE(result.get()); |
| 188 } | 196 } |
| 189 | 197 |
| 190 TEST(DeclarativeContentActionTest, RequestContentScriptJSBadType) { | 198 TEST(DeclarativeContentActionTest, RequestContentScriptJSBadType) { |
| 191 TestExtensionEnvironment env; | 199 TestExtensionEnvironment env; |
| 192 | 200 |
| 193 std::string error; | 201 std::string error; |
| 194 bool bad_message = false; | 202 bool bad_message = false; |
| 195 scoped_refptr<const ContentAction> result = ContentAction::Create( | 203 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 196 NULL, | 204 NULL, |
| 205 NULL, |
| 197 *ParseJson( | 206 *ParseJson( |
| 198 "{\n" | 207 "{\n" |
| 199 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 208 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 200 " \"js\": \"script.js\"\n" | 209 " \"js\": \"script.js\"\n" |
| 201 "}"), | 210 "}"), |
| 202 &error, | 211 &error, |
| 203 &bad_message); | 212 &bad_message); |
| 204 EXPECT_TRUE(bad_message); | 213 EXPECT_TRUE(bad_message); |
| 205 ASSERT_FALSE(result.get()); | 214 ASSERT_FALSE(result.get()); |
| 206 } | 215 } |
| 207 | 216 |
| 208 TEST(DeclarativeContentActionTest, RequestContentScriptAllFrames) { | 217 TEST(DeclarativeContentActionTest, RequestContentScriptAllFrames) { |
| 209 TestExtensionEnvironment env; | 218 TestExtensionEnvironment env; |
| 210 | 219 |
| 211 std::string error; | 220 std::string error; |
| 212 bool bad_message = false; | 221 bool bad_message = false; |
| 213 scoped_refptr<const ContentAction> result = ContentAction::Create( | 222 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 214 NULL, | 223 NULL, |
| 224 NULL, |
| 215 *ParseJson( | 225 *ParseJson( |
| 216 "{\n" | 226 "{\n" |
| 217 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 227 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 218 " \"js\": [\"script.js\"],\n" | 228 " \"js\": [\"script.js\"],\n" |
| 219 " \"allFrames\": true\n" | 229 " \"allFrames\": true\n" |
| 220 "}"), | 230 "}"), |
| 221 &error, | 231 &error, |
| 222 &bad_message); | 232 &bad_message); |
| 223 EXPECT_EQ("", error); | 233 EXPECT_EQ("", error); |
| 224 EXPECT_FALSE(bad_message); | 234 EXPECT_FALSE(bad_message); |
| 225 ASSERT_TRUE(result.get()); | 235 ASSERT_TRUE(result.get()); |
| 226 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); | 236 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); |
| 227 } | 237 } |
| 228 | 238 |
| 229 TEST(DeclarativeContentActionTest, RequestContentScriptMatchAboutBlank) { | 239 TEST(DeclarativeContentActionTest, RequestContentScriptMatchAboutBlank) { |
| 230 TestExtensionEnvironment env; | 240 TestExtensionEnvironment env; |
| 231 | 241 |
| 232 std::string error; | 242 std::string error; |
| 233 bool bad_message = false; | 243 bool bad_message = false; |
| 234 scoped_refptr<const ContentAction> result = ContentAction::Create( | 244 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 235 NULL, | 245 NULL, |
| 246 NULL, |
| 236 *ParseJson( | 247 *ParseJson( |
| 237 "{\n" | 248 "{\n" |
| 238 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 249 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 239 " \"js\": [\"script.js\"],\n" | 250 " \"js\": [\"script.js\"],\n" |
| 240 " \"matchAboutBlank\": true\n" | 251 " \"matchAboutBlank\": true\n" |
| 241 "}"), | 252 "}"), |
| 242 &error, | 253 &error, |
| 243 &bad_message); | 254 &bad_message); |
| 244 EXPECT_EQ("", error); | 255 EXPECT_EQ("", error); |
| 245 EXPECT_FALSE(bad_message); | 256 EXPECT_FALSE(bad_message); |
| 246 ASSERT_TRUE(result.get()); | 257 ASSERT_TRUE(result.get()); |
| 247 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); | 258 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); |
| 248 } | 259 } |
| 249 | 260 |
| 250 TEST(DeclarativeContentActionTest, RequestContentScriptAllFramesBadType) { | 261 TEST(DeclarativeContentActionTest, RequestContentScriptAllFramesBadType) { |
| 251 TestExtensionEnvironment env; | 262 TestExtensionEnvironment env; |
| 252 | 263 |
| 253 std::string error; | 264 std::string error; |
| 254 bool bad_message = false; | 265 bool bad_message = false; |
| 255 scoped_refptr<const ContentAction> result = ContentAction::Create( | 266 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 256 NULL, | 267 NULL, |
| 268 NULL, |
| 257 *ParseJson( | 269 *ParseJson( |
| 258 "{\n" | 270 "{\n" |
| 259 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 271 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 260 " \"js\": [\"script.js\"],\n" | 272 " \"js\": [\"script.js\"],\n" |
| 261 " \"allFrames\": null\n" | 273 " \"allFrames\": null\n" |
| 262 "}"), | 274 "}"), |
| 263 &error, | 275 &error, |
| 264 &bad_message); | 276 &bad_message); |
| 265 EXPECT_TRUE(bad_message); | 277 EXPECT_TRUE(bad_message); |
| 266 ASSERT_FALSE(result.get()); | 278 ASSERT_FALSE(result.get()); |
| 267 } | 279 } |
| 268 | 280 |
| 269 TEST(DeclarativeContentActionTest, RequestContentScriptMatchAboutBlankBadType) { | 281 TEST(DeclarativeContentActionTest, RequestContentScriptMatchAboutBlankBadType) { |
| 270 TestExtensionEnvironment env; | 282 TestExtensionEnvironment env; |
| 271 | 283 |
| 272 std::string error; | 284 std::string error; |
| 273 bool bad_message = false; | 285 bool bad_message = false; |
| 274 scoped_refptr<const ContentAction> result = ContentAction::Create( | 286 scoped_refptr<const ContentAction> result = ContentAction::Create( |
| 275 NULL, | 287 NULL, |
| 288 NULL, |
| 276 *ParseJson( | 289 *ParseJson( |
| 277 "{\n" | 290 "{\n" |
| 278 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 291 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
| 279 " \"js\": [\"script.js\"],\n" | 292 " \"js\": [\"script.js\"],\n" |
| 280 " \"matchAboutBlank\": null\n" | 293 " \"matchAboutBlank\": null\n" |
| 281 "}"), | 294 "}"), |
| 282 &error, | 295 &error, |
| 283 &bad_message); | 296 &bad_message); |
| 284 EXPECT_TRUE(bad_message); | 297 EXPECT_TRUE(bad_message); |
| 285 ASSERT_FALSE(result.get()); | 298 ASSERT_FALSE(result.get()); |
| 286 } | 299 } |
| 287 | 300 |
| 288 } // namespace | 301 } // namespace |
| 289 } // namespace extensions | 302 } // namespace extensions |
| OLD | NEW |