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 #import "ios/web/web_state/ui/web_view_js_utils.h" | 5 #import "ios/web/web_state/ui/web_view_js_utils.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/mac/bind_objc_block.h" | 9 #include "base/mac/bind_objc_block.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 86 |
87 base::DictionaryValue const* inner_dictionary = nullptr; | 87 base::DictionaryValue const* inner_dictionary = nullptr; |
88 dictionary->GetDictionary("Key2", &inner_dictionary); | 88 dictionary->GetDictionary("Key2", &inner_dictionary); |
89 EXPECT_NE(nullptr, inner_dictionary); | 89 EXPECT_NE(nullptr, inner_dictionary); |
90 | 90 |
91 double value3; | 91 double value3; |
92 inner_dictionary->GetDouble("Key3", &value3); | 92 inner_dictionary->GetDouble("Key3", &value3); |
93 EXPECT_EQ(42, value3); | 93 EXPECT_EQ(42, value3); |
94 } | 94 } |
95 | 95 |
| 96 // Tests that ValueResultFromWKResult converts NSArray to properly |
| 97 // initialized base::ListValue. |
| 98 TEST(WebViewJsUtilsTest, ValueResultFromArrayWKResult) { |
| 99 NSArray* test_array = @[ @"Value1", @[ @YES ], @42 ]; |
| 100 |
| 101 std::unique_ptr<base::Value> value(web::ValueResultFromWKResult(test_array)); |
| 102 base::ListValue* list = nullptr; |
| 103 value->GetAsList(&list); |
| 104 EXPECT_NE(nullptr, list); |
| 105 |
| 106 size_t list_size = 3; |
| 107 EXPECT_EQ(list_size, list->GetSize()); |
| 108 |
| 109 std::string value1; |
| 110 list->GetString(0, &value1); |
| 111 EXPECT_EQ("Value1", value1); |
| 112 |
| 113 base::ListValue const* inner_list = nullptr; |
| 114 list->GetList(1, &inner_list); |
| 115 EXPECT_NE(nullptr, inner_list); |
| 116 |
| 117 double value3; |
| 118 list->GetDouble(2, &value3); |
| 119 EXPECT_EQ(42, value3); |
| 120 } |
| 121 |
96 // Tests that an NSDictionary with a cycle does not cause infinite recursion. | 122 // Tests that an NSDictionary with a cycle does not cause infinite recursion. |
97 TEST(WebViewJsUtilsTest, ValueResultFromDictionaryWithDepthCheckWKResult) { | 123 TEST(WebViewJsUtilsTest, ValueResultFromDictionaryWithDepthCheckWKResult) { |
98 // Create a dictionary with a cycle. | 124 // Create a dictionary with a cycle. |
99 NSMutableDictionary* test_dictionary = | 125 NSMutableDictionary* test_dictionary = |
100 [NSMutableDictionary dictionaryWithCapacity:1]; | 126 [NSMutableDictionary dictionaryWithCapacity:1]; |
101 NSMutableDictionary* test_dictionary_2 = | 127 NSMutableDictionary* test_dictionary_2 = |
102 [NSMutableDictionary dictionaryWithCapacity:1]; | 128 [NSMutableDictionary dictionaryWithCapacity:1]; |
103 const char* key = "key"; | 129 const char* key = "key"; |
104 NSString* obj_c_key = | 130 NSString* obj_c_key = |
105 [NSString stringWithCString:key encoding:NSASCIIStringEncoding]; | 131 [NSString stringWithCString:key encoding:NSASCIIStringEncoding]; |
(...skipping 18 matching lines...) Expand all Loading... |
124 for (int current_depth = 0; current_depth <= kMaximumParsingRecursionDepth; | 150 for (int current_depth = 0; current_depth <= kMaximumParsingRecursionDepth; |
125 current_depth++) { | 151 current_depth++) { |
126 EXPECT_NE(nullptr, current_dictionary); | 152 EXPECT_NE(nullptr, current_dictionary); |
127 inner_dictionary = nullptr; | 153 inner_dictionary = nullptr; |
128 current_dictionary->GetDictionary(key, &inner_dictionary); | 154 current_dictionary->GetDictionary(key, &inner_dictionary); |
129 current_dictionary = inner_dictionary; | 155 current_dictionary = inner_dictionary; |
130 } | 156 } |
131 EXPECT_EQ(nullptr, current_dictionary); | 157 EXPECT_EQ(nullptr, current_dictionary); |
132 } | 158 } |
133 | 159 |
| 160 // Tests that an NSArray with a cycle does not cause infinite recursion. |
| 161 TEST(WebViewJsUtilsTest, ValueResultFromArrayWithDepthCheckWKResult) { |
| 162 // Create an array with a cycle. |
| 163 NSMutableArray* test_array = [NSMutableArray arrayWithCapacity:1]; |
| 164 NSMutableArray* test_array_2 = [NSMutableArray arrayWithCapacity:1]; |
| 165 test_array[0] = test_array_2; |
| 166 test_array_2[0] = test_array; |
| 167 |
| 168 // Break the retain cycle so that the arrays are freed. |
| 169 base::ScopedClosureRunner runner(base::BindBlock(^{ |
| 170 [test_array removeAllObjects]; |
| 171 })); |
| 172 |
| 173 // Check that parsing the array stopped at a depth of |
| 174 // |kMaximumParsingRecursionDepth|. |
| 175 std::unique_ptr<base::Value> value = web::ValueResultFromWKResult(test_array); |
| 176 base::ListValue* current_list = nullptr; |
| 177 base::ListValue* inner_list = nullptr; |
| 178 |
| 179 value->GetAsList(¤t_list); |
| 180 EXPECT_NE(nullptr, current_list); |
| 181 |
| 182 for (int current_depth = 0; current_depth <= kMaximumParsingRecursionDepth; |
| 183 current_depth++) { |
| 184 EXPECT_NE(nullptr, current_list); |
| 185 inner_list = nullptr; |
| 186 current_list->GetList(0, &inner_list); |
| 187 current_list = inner_list; |
| 188 } |
| 189 EXPECT_EQ(nullptr, current_list); |
| 190 } |
| 191 |
134 } // namespace web | 192 } // namespace web |
OLD | NEW |