| 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_condit
ion_attribute.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 namespace keys = declarative_webrequest_constants; | 30 namespace keys = declarative_webrequest_constants; |
| 31 | 31 |
| 32 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { | 32 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { |
| 33 // Necessary for TestURLRequest. | 33 // Necessary for TestURLRequest. |
| 34 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 34 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 35 | 35 |
| 36 std::string error; | 36 std::string error; |
| 37 scoped_refptr<const WebRequestConditionAttribute> result; | 37 scoped_refptr<const WebRequestConditionAttribute> result; |
| 38 StringValue string_value("main_frame"); | 38 StringValue string_value("main_frame"); |
| 39 ListValue resource_types; | 39 ListValue resource_types; |
| 40 resource_types.Append(Value::CreateStringValue("main_frame")); | 40 resource_types.Append(new base::StringValue("main_frame")); |
| 41 | 41 |
| 42 // Test wrong condition name passed. | 42 // Test wrong condition name passed. |
| 43 error.clear(); | 43 error.clear(); |
| 44 result = WebRequestConditionAttribute::Create( | 44 result = WebRequestConditionAttribute::Create( |
| 45 kUnknownConditionName, &resource_types, &error); | 45 kUnknownConditionName, &resource_types, &error); |
| 46 EXPECT_FALSE(error.empty()); | 46 EXPECT_FALSE(error.empty()); |
| 47 EXPECT_FALSE(result.get()); | 47 EXPECT_FALSE(result.get()); |
| 48 | 48 |
| 49 // Test wrong data type passed | 49 // Test wrong data type passed |
| 50 error.clear(); | 50 error.clear(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 71 } | 71 } |
| 72 | 72 |
| 73 TEST(WebRequestConditionAttributeTest, ResourceType) { | 73 TEST(WebRequestConditionAttributeTest, ResourceType) { |
| 74 // Necessary for TestURLRequest. | 74 // Necessary for TestURLRequest. |
| 75 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 75 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 76 | 76 |
| 77 std::string error; | 77 std::string error; |
| 78 ListValue resource_types; | 78 ListValue resource_types; |
| 79 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding | 79 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding |
| 80 // ResourceType::Type is not 0, the default value. | 80 // ResourceType::Type is not 0, the default value. |
| 81 resource_types.Append(Value::CreateStringValue("sub_frame")); | 81 resource_types.Append(new base::StringValue("sub_frame")); |
| 82 | 82 |
| 83 scoped_refptr<const WebRequestConditionAttribute> attribute = | 83 scoped_refptr<const WebRequestConditionAttribute> attribute = |
| 84 WebRequestConditionAttribute::Create( | 84 WebRequestConditionAttribute::Create( |
| 85 keys::kResourceTypeKey, &resource_types, &error); | 85 keys::kResourceTypeKey, &resource_types, &error); |
| 86 EXPECT_EQ("", error); | 86 EXPECT_EQ("", error); |
| 87 ASSERT_TRUE(attribute.get()); | 87 ASSERT_TRUE(attribute.get()); |
| 88 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); | 88 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); |
| 89 | 89 |
| 90 net::TestURLRequestContext context; | 90 net::TestURLRequestContext context; |
| 91 net::TestURLRequest url_request_ok( | 91 net::TestURLRequest url_request_ok( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 118 ASSERT_TRUE(test_server.Start()); | 118 ASSERT_TRUE(test_server.Start()); |
| 119 | 119 |
| 120 net::TestURLRequestContext context; | 120 net::TestURLRequestContext context; |
| 121 net::TestDelegate delegate; | 121 net::TestDelegate delegate; |
| 122 net::TestURLRequest url_request( | 122 net::TestURLRequest url_request( |
| 123 test_server.GetURL("files/headers.html"), &delegate, &context, NULL); | 123 test_server.GetURL("files/headers.html"), &delegate, &context, NULL); |
| 124 url_request.Start(); | 124 url_request.Start(); |
| 125 base::MessageLoop::current()->Run(); | 125 base::MessageLoop::current()->Run(); |
| 126 | 126 |
| 127 ListValue content_types; | 127 ListValue content_types; |
| 128 content_types.Append(Value::CreateStringValue("text/plain")); | 128 content_types.Append(new base::StringValue("text/plain")); |
| 129 scoped_refptr<const WebRequestConditionAttribute> attribute_include = | 129 scoped_refptr<const WebRequestConditionAttribute> attribute_include = |
| 130 WebRequestConditionAttribute::Create( | 130 WebRequestConditionAttribute::Create( |
| 131 keys::kContentTypeKey, &content_types, &error); | 131 keys::kContentTypeKey, &content_types, &error); |
| 132 EXPECT_EQ("", error); | 132 EXPECT_EQ("", error); |
| 133 ASSERT_TRUE(attribute_include.get()); | 133 ASSERT_TRUE(attribute_include.get()); |
| 134 EXPECT_FALSE(attribute_include->IsFulfilled( | 134 EXPECT_FALSE(attribute_include->IsFulfilled( |
| 135 WebRequestData(&url_request, ON_BEFORE_REQUEST, | 135 WebRequestData(&url_request, ON_BEFORE_REQUEST, |
| 136 url_request.response_headers()))); | 136 url_request.response_headers()))); |
| 137 EXPECT_TRUE(attribute_include->IsFulfilled( | 137 EXPECT_TRUE(attribute_include->IsFulfilled( |
| 138 WebRequestData(&url_request, ON_HEADERS_RECEIVED, | 138 WebRequestData(&url_request, ON_HEADERS_RECEIVED, |
| 139 url_request.response_headers()))); | 139 url_request.response_headers()))); |
| 140 EXPECT_EQ(std::string(keys::kContentTypeKey), attribute_include->GetName()); | 140 EXPECT_EQ(std::string(keys::kContentTypeKey), attribute_include->GetName()); |
| 141 | 141 |
| 142 scoped_refptr<const WebRequestConditionAttribute> attribute_exclude = | 142 scoped_refptr<const WebRequestConditionAttribute> attribute_exclude = |
| 143 WebRequestConditionAttribute::Create( | 143 WebRequestConditionAttribute::Create( |
| 144 keys::kExcludeContentTypeKey, &content_types, &error); | 144 keys::kExcludeContentTypeKey, &content_types, &error); |
| 145 EXPECT_EQ("", error); | 145 EXPECT_EQ("", error); |
| 146 ASSERT_TRUE(attribute_exclude.get()); | 146 ASSERT_TRUE(attribute_exclude.get()); |
| 147 EXPECT_FALSE(attribute_exclude->IsFulfilled( | 147 EXPECT_FALSE(attribute_exclude->IsFulfilled( |
| 148 WebRequestData(&url_request, ON_HEADERS_RECEIVED, | 148 WebRequestData(&url_request, ON_HEADERS_RECEIVED, |
| 149 url_request.response_headers()))); | 149 url_request.response_headers()))); |
| 150 | 150 |
| 151 content_types.Clear(); | 151 content_types.Clear(); |
| 152 content_types.Append(Value::CreateStringValue("something/invalid")); | 152 content_types.Append(new base::StringValue("something/invalid")); |
| 153 scoped_refptr<const WebRequestConditionAttribute> attribute_unincluded = | 153 scoped_refptr<const WebRequestConditionAttribute> attribute_unincluded = |
| 154 WebRequestConditionAttribute::Create( | 154 WebRequestConditionAttribute::Create( |
| 155 keys::kContentTypeKey, &content_types, &error); | 155 keys::kContentTypeKey, &content_types, &error); |
| 156 EXPECT_EQ("", error); | 156 EXPECT_EQ("", error); |
| 157 ASSERT_TRUE(attribute_unincluded.get()); | 157 ASSERT_TRUE(attribute_unincluded.get()); |
| 158 EXPECT_FALSE(attribute_unincluded->IsFulfilled( | 158 EXPECT_FALSE(attribute_unincluded->IsFulfilled( |
| 159 WebRequestData(&url_request, ON_HEADERS_RECEIVED, | 159 WebRequestData(&url_request, ON_HEADERS_RECEIVED, |
| 160 url_request.response_headers()))); | 160 url_request.response_headers()))); |
| 161 | 161 |
| 162 scoped_refptr<const WebRequestConditionAttribute> attribute_unexcluded = | 162 scoped_refptr<const WebRequestConditionAttribute> attribute_unexcluded = |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 scoped_ptr<Value> entry_owned; | 351 scoped_ptr<Value> entry_owned; |
| 352 ListValue* list = NULL; | 352 ListValue* list = NULL; |
| 353 if (!dictionary->GetWithoutPathExpansion(*name, &entry)) | 353 if (!dictionary->GetWithoutPathExpansion(*name, &entry)) |
| 354 return scoped_ptr<DictionaryValue>(); | 354 return scoped_ptr<DictionaryValue>(); |
| 355 switch (entry->GetType()) { | 355 switch (entry->GetType()) { |
| 356 case Value::TYPE_STRING: // Replace the present string with a list. | 356 case Value::TYPE_STRING: // Replace the present string with a list. |
| 357 list = new ListValue; | 357 list = new ListValue; |
| 358 // Ignoring return value, we already verified the entry is there. | 358 // Ignoring return value, we already verified the entry is there. |
| 359 dictionary->RemoveWithoutPathExpansion(*name, &entry_owned); | 359 dictionary->RemoveWithoutPathExpansion(*name, &entry_owned); |
| 360 list->Append(entry_owned.release()); | 360 list->Append(entry_owned.release()); |
| 361 list->Append(Value::CreateStringValue(*value)); | 361 list->Append(new base::StringValue(*value)); |
| 362 dictionary->SetWithoutPathExpansion(*name, list); | 362 dictionary->SetWithoutPathExpansion(*name, list); |
| 363 break; | 363 break; |
| 364 case Value::TYPE_LIST: // Just append to the list. | 364 case Value::TYPE_LIST: // Just append to the list. |
| 365 CHECK(entry->GetAsList(&list)); | 365 CHECK(entry->GetAsList(&list)); |
| 366 list->Append(Value::CreateStringValue(*value)); | 366 list->Append(new base::StringValue(*value)); |
| 367 break; | 367 break; |
| 368 default: | 368 default: |
| 369 NOTREACHED(); // We never put other Values here. | 369 NOTREACHED(); // We never put other Values here. |
| 370 return scoped_ptr<DictionaryValue>(); | 370 return scoped_ptr<DictionaryValue>(); |
| 371 } | 371 } |
| 372 } else { | 372 } else { |
| 373 dictionary->SetString(*name, *value); | 373 dictionary->SetString(*name, *value); |
| 374 } | 374 } |
| 375 } | 375 } |
| 376 return dictionary.Pass(); | 376 return dictionary.Pass(); |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 keys::kValueEqualsKey, "valueB" | 664 keys::kValueEqualsKey, "valueB" |
| 665 }; | 665 }; |
| 666 const size_t kExistingSize[] = { arraysize(kExisting) }; | 666 const size_t kExistingSize[] = { arraysize(kExisting) }; |
| 667 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 667 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
| 668 MatchAndCheck( | 668 MatchAndCheck( |
| 669 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 669 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
| 670 EXPECT_FALSE(result); | 670 EXPECT_FALSE(result); |
| 671 } | 671 } |
| 672 | 672 |
| 673 } // namespace extensions | 673 } // namespace extensions |
| OLD | NEW |