| 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 <list> | 5 #include <list> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 result->reset(result_.DeepCopy()); | 95 result->reset(result_.DeepCopy()); |
| 96 return Status(kOk); | 96 return Status(kOk); |
| 97 } | 97 } |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 Status status_; | 100 Status status_; |
| 101 base::DictionaryValue result_; | 101 base::DictionaryValue result_; |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 void AssertEvalFails(const base::DictionaryValue& command_result) { | 104 void AssertEvalFails(const base::DictionaryValue& command_result) { |
| 105 scoped_ptr<base::Value> result; | 105 scoped_ptr<base::DictionaryValue> result; |
| 106 FakeDevToolsClient client; | 106 FakeDevToolsClient client; |
| 107 client.set_result(command_result); | 107 client.set_result(command_result); |
| 108 Status status = internal::EvaluateScript(&client, "", &result); | 108 Status status = internal::EvaluateScript(&client, 0, "", |
| 109 internal::ReturnByValue, &result); |
| 109 ASSERT_EQ(kUnknownError, status.code()); | 110 ASSERT_EQ(kUnknownError, status.code()); |
| 110 ASSERT_FALSE(result); | 111 ASSERT_FALSE(result); |
| 111 } | 112 } |
| 112 | 113 |
| 113 } // namespace | 114 } // namespace |
| 114 | 115 |
| 115 TEST(ChromeImplEvaluateScript, CommandError) { | 116 TEST(EvaluateScript, CommandError) { |
| 116 scoped_ptr<base::Value> result; | 117 scoped_ptr<base::DictionaryValue> result; |
| 117 FakeDevToolsClient client; | 118 FakeDevToolsClient client; |
| 118 client.set_status(Status(kUnknownError)); | 119 client.set_status(Status(kUnknownError)); |
| 119 Status status = internal::EvaluateScript(&client, "", &result); | 120 Status status = internal::EvaluateScript(&client, 0, "", |
| 121 internal::ReturnByValue, &result); |
| 120 ASSERT_EQ(kUnknownError, status.code()); | 122 ASSERT_EQ(kUnknownError, status.code()); |
| 121 ASSERT_FALSE(result); | 123 ASSERT_FALSE(result); |
| 122 } | 124 } |
| 123 | 125 |
| 124 TEST(ChromeImplEvaluateScript, MissingResult) { | 126 TEST(EvaluateScript, MissingWasThrown) { |
| 125 base::DictionaryValue dict; | 127 base::DictionaryValue dict; |
| 126 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); | 128 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); |
| 127 } | 129 } |
| 128 | 130 |
| 129 TEST(ChromeImplEvaluateScript, Throws) { | 131 TEST(EvaluateScript, MissingResult) { |
| 132 base::DictionaryValue dict; |
| 133 dict.SetBoolean("wasThrown", false); |
| 134 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); |
| 135 } |
| 136 |
| 137 TEST(EvaluateScript, Throws) { |
| 130 base::DictionaryValue dict; | 138 base::DictionaryValue dict; |
| 131 dict.SetBoolean("wasThrown", true); | 139 dict.SetBoolean("wasThrown", true); |
| 132 dict.SetString("result.type", "undefined"); | 140 dict.SetString("result.type", "undefined"); |
| 133 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); | 141 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); |
| 134 } | 142 } |
| 135 | 143 |
| 136 TEST(ChromeImplEvaluateScript, MissingType) { | 144 TEST(EvaluateScript, Ok) { |
| 145 scoped_ptr<base::DictionaryValue> result; |
| 146 base::DictionaryValue dict; |
| 147 dict.SetBoolean("wasThrown", false); |
| 148 dict.SetInteger("result.key", 100); |
| 149 FakeDevToolsClient client; |
| 150 client.set_result(dict); |
| 151 ASSERT_TRUE(internal::EvaluateScript( |
| 152 &client, 0, "", internal::ReturnByValue, &result).IsOk()); |
| 153 ASSERT_TRUE(result); |
| 154 ASSERT_TRUE(result->HasKey("key")); |
| 155 } |
| 156 |
| 157 TEST(EvaluateScriptAndGetValue, MissingType) { |
| 158 scoped_ptr<base::Value> result; |
| 159 FakeDevToolsClient client; |
| 137 base::DictionaryValue dict; | 160 base::DictionaryValue dict; |
| 138 dict.SetBoolean("wasThrown", false); | 161 dict.SetBoolean("wasThrown", false); |
| 139 dict.SetInteger("result.value", 1); | 162 dict.SetInteger("result.value", 1); |
| 140 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); | 163 client.set_result(dict); |
| 164 ASSERT_TRUE(internal::EvaluateScriptAndGetValue( |
| 165 &client, 0, "", &result).IsError()); |
| 141 } | 166 } |
| 142 | 167 |
| 143 TEST(ChromeImplEvaluateScript, Undefined) { | 168 TEST(EvaluateScriptAndGetValue, Undefined) { |
| 144 scoped_ptr<base::Value> result; | 169 scoped_ptr<base::Value> result; |
| 145 FakeDevToolsClient client; | 170 FakeDevToolsClient client; |
| 146 base::DictionaryValue dict; | 171 base::DictionaryValue dict; |
| 147 dict.SetBoolean("wasThrown", false); | 172 dict.SetBoolean("wasThrown", false); |
| 148 dict.SetString("result.type", "undefined"); | 173 dict.SetString("result.type", "undefined"); |
| 149 client.set_result(dict); | 174 client.set_result(dict); |
| 150 Status status = internal::EvaluateScript(&client, "", &result); | 175 Status status = internal::EvaluateScriptAndGetValue( |
| 176 &client, 0, "", &result); |
| 151 ASSERT_EQ(kOk, status.code()); | 177 ASSERT_EQ(kOk, status.code()); |
| 152 ASSERT_TRUE(result && result->IsType(base::Value::TYPE_NULL)); | 178 ASSERT_TRUE(result && result->IsType(base::Value::TYPE_NULL)); |
| 153 } | 179 } |
| 154 | 180 |
| 155 TEST(ChromeImplEvaluateScript, Value) { | 181 TEST(EvaluateScriptAndGetValue, Ok) { |
| 156 scoped_ptr<base::Value> result; | 182 scoped_ptr<base::Value> result; |
| 157 FakeDevToolsClient client; | 183 FakeDevToolsClient client; |
| 158 base::DictionaryValue dict; | 184 base::DictionaryValue dict; |
| 159 dict.SetBoolean("wasThrown", false); | 185 dict.SetBoolean("wasThrown", false); |
| 160 dict.SetString("result.type", "integer"); | 186 dict.SetString("result.type", "integer"); |
| 161 dict.SetInteger("result.value.status", 0); | 187 dict.SetInteger("result.value.status", 0); |
| 162 dict.SetInteger("result.value.value", 1); | 188 dict.SetInteger("result.value.value", 1); |
| 163 client.set_result(dict); | 189 client.set_result(dict); |
| 164 Status status = internal::EvaluateScript(&client, "", &result); | 190 Status status = internal::EvaluateScriptAndGetValue( |
| 191 &client, 0, "", &result); |
| 165 ASSERT_EQ(kOk, status.code()); | 192 ASSERT_EQ(kOk, status.code()); |
| 166 int value; | 193 int value; |
| 167 ASSERT_TRUE(result && result->GetAsInteger(&value)); | 194 ASSERT_TRUE(result && result->GetAsInteger(&value)); |
| 168 ASSERT_EQ(1, value); | 195 ASSERT_EQ(1, value); |
| 169 } | 196 } |
| 170 | 197 |
| 171 TEST(ChromeImplEvaluateScript, ScriptError) { | 198 TEST(EvaluateScriptAndGetValue, ScriptError) { |
| 172 scoped_ptr<base::Value> result; | 199 scoped_ptr<base::Value> result; |
| 173 FakeDevToolsClient client; | 200 FakeDevToolsClient client; |
| 174 base::DictionaryValue dict; | 201 base::DictionaryValue dict; |
| 175 dict.SetBoolean("wasThrown", false); | 202 dict.SetBoolean("wasThrown", false); |
| 176 dict.SetString("result.type", "integer"); | 203 dict.SetString("result.type", "integer"); |
| 177 dict.SetInteger("result.value.status", 1); | 204 dict.SetInteger("result.value.status", 1); |
| 178 dict.SetInteger("result.value.value", 1); | 205 dict.SetInteger("result.value.value", 1); |
| 179 client.set_result(dict); | 206 client.set_result(dict); |
| 180 Status status = internal::EvaluateScript(&client, "", &result); | 207 Status status = internal::EvaluateScriptAndGetValue( |
| 208 &client, 0, "", &result); |
| 181 ASSERT_EQ(1, status.code()); | 209 ASSERT_EQ(1, status.code()); |
| 182 ASSERT_FALSE(result); | 210 ASSERT_FALSE(result); |
| 183 } | 211 } |
| 212 |
| 213 TEST(EvaluateScriptAndGetObject, NoObject) { |
| 214 FakeDevToolsClient client; |
| 215 base::DictionaryValue dict; |
| 216 dict.SetBoolean("wasThrown", false); |
| 217 dict.SetString("result.type", "integer"); |
| 218 client.set_result(dict); |
| 219 std::string object_id; |
| 220 ASSERT_TRUE(internal::EvaluateScriptAndGetObject( |
| 221 &client, 0, "", &object_id).IsError()); |
| 222 ASSERT_TRUE(object_id.empty()); |
| 223 } |
| 224 |
| 225 TEST(EvaluateScriptAndGetObject, Ok) { |
| 226 FakeDevToolsClient client; |
| 227 base::DictionaryValue dict; |
| 228 dict.SetBoolean("wasThrown", false); |
| 229 dict.SetString("result.objectId", "id"); |
| 230 client.set_result(dict); |
| 231 std::string object_id; |
| 232 ASSERT_TRUE(internal::EvaluateScriptAndGetObject( |
| 233 &client, 0, "", &object_id).IsOk()); |
| 234 ASSERT_STREQ("id", object_id.c_str()); |
| 235 } |
| OLD | NEW |