| 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_webrequest/webrequest_action
.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action
.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/json/json_file_value_serializer.h" | 8 #include "base/json/json_file_value_serializer.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 ++it) { | 51 ++it) { |
| 52 const base::DictionaryValue* dict; | 52 const base::DictionaryValue* dict; |
| 53 CHECK((*it)->GetAsDictionary(&dict)); | 53 CHECK((*it)->GetAsDictionary(&dict)); |
| 54 actions.push_back(linked_ptr<base::Value>(dict->DeepCopy())); | 54 actions.push_back(linked_ptr<base::Value>(dict->DeepCopy())); |
| 55 } | 55 } |
| 56 | 56 |
| 57 std::string error; | 57 std::string error; |
| 58 bool bad_message = false; | 58 bool bad_message = false; |
| 59 | 59 |
| 60 scoped_ptr<WebRequestActionSet> action_set( | 60 scoped_ptr<WebRequestActionSet> action_set( |
| 61 WebRequestActionSet::Create(NULL, actions, &error, &bad_message)); | 61 WebRequestActionSet::Create(NULL, NULL, actions, &error, &bad_message)); |
| 62 EXPECT_EQ("", error); | 62 EXPECT_EQ("", error); |
| 63 EXPECT_FALSE(bad_message); | 63 EXPECT_FALSE(bad_message); |
| 64 CHECK(action_set); | 64 CHECK(action_set); |
| 65 return action_set.Pass(); | 65 return action_set.Pass(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 } // namespace | 68 } // namespace |
| 69 | 69 |
| 70 namespace keys = declarative_webrequest_constants; | 70 namespace keys = declarative_webrequest_constants; |
| 71 | 71 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 } | 179 } |
| 180 | 180 |
| 181 TEST(WebRequestActionTest, CreateAction) { | 181 TEST(WebRequestActionTest, CreateAction) { |
| 182 std::string error; | 182 std::string error; |
| 183 bool bad_message = false; | 183 bool bad_message = false; |
| 184 scoped_refptr<const WebRequestAction> result; | 184 scoped_refptr<const WebRequestAction> result; |
| 185 | 185 |
| 186 // Test wrong data type passed. | 186 // Test wrong data type passed. |
| 187 error.clear(); | 187 error.clear(); |
| 188 base::ListValue empty_list; | 188 base::ListValue empty_list; |
| 189 result = WebRequestAction::Create(NULL, empty_list, &error, &bad_message); | 189 result = WebRequestAction::Create( |
| 190 NULL, NULL, empty_list, &error, &bad_message); |
| 190 EXPECT_TRUE(bad_message); | 191 EXPECT_TRUE(bad_message); |
| 191 EXPECT_FALSE(result.get()); | 192 EXPECT_FALSE(result.get()); |
| 192 | 193 |
| 193 // Test missing instanceType element. | 194 // Test missing instanceType element. |
| 194 base::DictionaryValue input; | 195 base::DictionaryValue input; |
| 195 error.clear(); | 196 error.clear(); |
| 196 result = WebRequestAction::Create(NULL, input, &error, &bad_message); | 197 result = WebRequestAction::Create(NULL, NULL, input, &error, &bad_message); |
| 197 EXPECT_TRUE(bad_message); | 198 EXPECT_TRUE(bad_message); |
| 198 EXPECT_FALSE(result.get()); | 199 EXPECT_FALSE(result.get()); |
| 199 | 200 |
| 200 // Test wrong instanceType element. | 201 // Test wrong instanceType element. |
| 201 input.SetString(keys::kInstanceTypeKey, kUnknownActionType); | 202 input.SetString(keys::kInstanceTypeKey, kUnknownActionType); |
| 202 error.clear(); | 203 error.clear(); |
| 203 result = WebRequestAction::Create(NULL, input, &error, &bad_message); | 204 result = WebRequestAction::Create(NULL, NULL, input, &error, &bad_message); |
| 204 EXPECT_NE("", error); | 205 EXPECT_NE("", error); |
| 205 EXPECT_FALSE(result.get()); | 206 EXPECT_FALSE(result.get()); |
| 206 | 207 |
| 207 // Test success | 208 // Test success |
| 208 input.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 209 input.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
| 209 error.clear(); | 210 error.clear(); |
| 210 result = WebRequestAction::Create(NULL, input, &error, &bad_message); | 211 result = WebRequestAction::Create(NULL, NULL, input, &error, &bad_message); |
| 211 EXPECT_EQ("", error); | 212 EXPECT_EQ("", error); |
| 212 EXPECT_FALSE(bad_message); | 213 EXPECT_FALSE(bad_message); |
| 213 ASSERT_TRUE(result.get()); | 214 ASSERT_TRUE(result.get()); |
| 214 EXPECT_EQ(WebRequestAction::ACTION_CANCEL_REQUEST, result->type()); | 215 EXPECT_EQ(WebRequestAction::ACTION_CANCEL_REQUEST, result->type()); |
| 215 } | 216 } |
| 216 | 217 |
| 217 TEST(WebRequestActionTest, CreateActionSet) { | 218 TEST(WebRequestActionTest, CreateActionSet) { |
| 218 std::string error; | 219 std::string error; |
| 219 bool bad_message = false; | 220 bool bad_message = false; |
| 220 scoped_ptr<WebRequestActionSet> result; | 221 scoped_ptr<WebRequestActionSet> result; |
| 221 | 222 |
| 222 WebRequestActionSet::AnyVector input; | 223 WebRequestActionSet::AnyVector input; |
| 223 | 224 |
| 224 // Test empty input. | 225 // Test empty input. |
| 225 error.clear(); | 226 error.clear(); |
| 226 result = WebRequestActionSet::Create(NULL, input, &error, &bad_message); | 227 result = WebRequestActionSet::Create(NULL, NULL, input, &error, &bad_message); |
| 227 EXPECT_TRUE(error.empty()) << error; | 228 EXPECT_TRUE(error.empty()) << error; |
| 228 EXPECT_FALSE(bad_message); | 229 EXPECT_FALSE(bad_message); |
| 229 ASSERT_TRUE(result.get()); | 230 ASSERT_TRUE(result.get()); |
| 230 EXPECT_TRUE(result->actions().empty()); | 231 EXPECT_TRUE(result->actions().empty()); |
| 231 EXPECT_EQ(std::numeric_limits<int>::min(), result->GetMinimumPriority()); | 232 EXPECT_EQ(std::numeric_limits<int>::min(), result->GetMinimumPriority()); |
| 232 | 233 |
| 233 base::DictionaryValue correct_action; | 234 base::DictionaryValue correct_action; |
| 234 correct_action.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); | 235 correct_action.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); |
| 235 correct_action.SetInteger(keys::kLowerPriorityThanKey, 10); | 236 correct_action.SetInteger(keys::kLowerPriorityThanKey, 10); |
| 236 base::DictionaryValue incorrect_action; | 237 base::DictionaryValue incorrect_action; |
| 237 incorrect_action.SetString(keys::kInstanceTypeKey, kUnknownActionType); | 238 incorrect_action.SetString(keys::kInstanceTypeKey, kUnknownActionType); |
| 238 | 239 |
| 239 // Test success. | 240 // Test success. |
| 240 input.push_back(linked_ptr<base::Value>(correct_action.DeepCopy())); | 241 input.push_back(linked_ptr<base::Value>(correct_action.DeepCopy())); |
| 241 error.clear(); | 242 error.clear(); |
| 242 result = WebRequestActionSet::Create(NULL, input, &error, &bad_message); | 243 result = WebRequestActionSet::Create(NULL, NULL, input, &error, &bad_message); |
| 243 EXPECT_TRUE(error.empty()) << error; | 244 EXPECT_TRUE(error.empty()) << error; |
| 244 EXPECT_FALSE(bad_message); | 245 EXPECT_FALSE(bad_message); |
| 245 ASSERT_TRUE(result.get()); | 246 ASSERT_TRUE(result.get()); |
| 246 ASSERT_EQ(1u, result->actions().size()); | 247 ASSERT_EQ(1u, result->actions().size()); |
| 247 EXPECT_EQ(WebRequestAction::ACTION_IGNORE_RULES, | 248 EXPECT_EQ(WebRequestAction::ACTION_IGNORE_RULES, |
| 248 result->actions()[0]->type()); | 249 result->actions()[0]->type()); |
| 249 EXPECT_EQ(10, result->GetMinimumPriority()); | 250 EXPECT_EQ(10, result->GetMinimumPriority()); |
| 250 | 251 |
| 251 // Test failure. | 252 // Test failure. |
| 252 input.push_back(linked_ptr<base::Value>(incorrect_action.DeepCopy())); | 253 input.push_back(linked_ptr<base::Value>(incorrect_action.DeepCopy())); |
| 253 error.clear(); | 254 error.clear(); |
| 254 result = WebRequestActionSet::Create(NULL, input, &error, &bad_message); | 255 result = WebRequestActionSet::Create(NULL, NULL, input, &error, &bad_message); |
| 255 EXPECT_NE("", error); | 256 EXPECT_NE("", error); |
| 256 EXPECT_FALSE(result.get()); | 257 EXPECT_FALSE(result.get()); |
| 257 } | 258 } |
| 258 | 259 |
| 259 // Test capture group syntax conversions of WebRequestRedirectByRegExAction | 260 // Test capture group syntax conversions of WebRequestRedirectByRegExAction |
| 260 TEST(WebRequestActionTest, PerlToRe2Style) { | 261 TEST(WebRequestActionTest, PerlToRe2Style) { |
| 261 #define CallPerlToRe2Style WebRequestRedirectByRegExAction::PerlToRe2Style | 262 #define CallPerlToRe2Style WebRequestRedirectByRegExAction::PerlToRe2Style |
| 262 // foo$1bar -> foo\1bar | 263 // foo$1bar -> foo\1bar |
| 263 EXPECT_EQ("foo\\1bar", CallPerlToRe2Style("foo$1bar")); | 264 EXPECT_EQ("foo\\1bar", CallPerlToRe2Style("foo$1bar")); |
| 264 // foo\$1bar -> foo$1bar | 265 // foo\$1bar -> foo$1bar |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 for (WebRequestActionSet::Actions::const_iterator it = | 582 for (WebRequestActionSet::Actions::const_iterator it = |
| 582 action_set->actions().begin(); | 583 action_set->actions().begin(); |
| 583 it != action_set->actions().end(); | 584 it != action_set->actions().end(); |
| 584 ++it) { | 585 ++it) { |
| 585 EXPECT_EQ(kExpectedNames[index], (*it)->GetName()); | 586 EXPECT_EQ(kExpectedNames[index], (*it)->GetName()); |
| 586 ++index; | 587 ++index; |
| 587 } | 588 } |
| 588 } | 589 } |
| 589 | 590 |
| 590 } // namespace extensions | 591 } // namespace extensions |
| OLD | NEW |