| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <list> | |
| 6 #include <string> | |
| 7 | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/test/chromedriver/devtools_client.h" | |
| 12 #include "chrome/test/chromedriver/status.h" | |
| 13 #include "chrome/test/chromedriver/web_view_impl.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 class FakeDevToolsClient : public DevToolsClient { | |
| 19 public: | |
| 20 FakeDevToolsClient() : status_(kOk) {} | |
| 21 virtual ~FakeDevToolsClient() {} | |
| 22 | |
| 23 void set_status(const Status& status) { | |
| 24 status_ = status; | |
| 25 } | |
| 26 void set_result(const base::DictionaryValue& result) { | |
| 27 result_.Clear(); | |
| 28 result_.MergeDictionary(&result); | |
| 29 } | |
| 30 | |
| 31 // Overridden from DevToolsClient: | |
| 32 virtual Status ConnectIfNecessary() OVERRIDE { | |
| 33 return Status(kOk); | |
| 34 } | |
| 35 virtual Status SendCommand(const std::string& method, | |
| 36 const base::DictionaryValue& params) OVERRIDE { | |
| 37 return SendCommandAndGetResult(method, params, NULL); | |
| 38 } | |
| 39 virtual Status SendCommandAndGetResult( | |
| 40 const std::string& method, | |
| 41 const base::DictionaryValue& params, | |
| 42 scoped_ptr<base::DictionaryValue>* result) OVERRIDE { | |
| 43 if (status_.IsError()) | |
| 44 return status_; | |
| 45 result->reset(result_.DeepCopy()); | |
| 46 return Status(kOk); | |
| 47 } | |
| 48 virtual void AddListener(DevToolsEventListener* listener) OVERRIDE {} | |
| 49 virtual Status HandleEventsUntil( | |
| 50 const ConditionalFunc& conditional_func) OVERRIDE { | |
| 51 return Status(kOk); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 Status status_; | |
| 56 base::DictionaryValue result_; | |
| 57 }; | |
| 58 | |
| 59 void AssertEvalFails(const base::DictionaryValue& command_result) { | |
| 60 scoped_ptr<base::DictionaryValue> result; | |
| 61 FakeDevToolsClient client; | |
| 62 client.set_result(command_result); | |
| 63 Status status = internal::EvaluateScript(&client, 0, "", | |
| 64 internal::ReturnByValue, &result); | |
| 65 ASSERT_EQ(kUnknownError, status.code()); | |
| 66 ASSERT_FALSE(result); | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 TEST(EvaluateScript, CommandError) { | |
| 72 scoped_ptr<base::DictionaryValue> result; | |
| 73 FakeDevToolsClient client; | |
| 74 client.set_status(Status(kUnknownError)); | |
| 75 Status status = internal::EvaluateScript(&client, 0, "", | |
| 76 internal::ReturnByValue, &result); | |
| 77 ASSERT_EQ(kUnknownError, status.code()); | |
| 78 ASSERT_FALSE(result); | |
| 79 } | |
| 80 | |
| 81 TEST(EvaluateScript, MissingWasThrown) { | |
| 82 base::DictionaryValue dict; | |
| 83 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); | |
| 84 } | |
| 85 | |
| 86 TEST(EvaluateScript, MissingResult) { | |
| 87 base::DictionaryValue dict; | |
| 88 dict.SetBoolean("wasThrown", false); | |
| 89 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); | |
| 90 } | |
| 91 | |
| 92 TEST(EvaluateScript, Throws) { | |
| 93 base::DictionaryValue dict; | |
| 94 dict.SetBoolean("wasThrown", true); | |
| 95 dict.SetString("result.type", "undefined"); | |
| 96 ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict)); | |
| 97 } | |
| 98 | |
| 99 TEST(EvaluateScript, Ok) { | |
| 100 scoped_ptr<base::DictionaryValue> result; | |
| 101 base::DictionaryValue dict; | |
| 102 dict.SetBoolean("wasThrown", false); | |
| 103 dict.SetInteger("result.key", 100); | |
| 104 FakeDevToolsClient client; | |
| 105 client.set_result(dict); | |
| 106 ASSERT_TRUE(internal::EvaluateScript( | |
| 107 &client, 0, "", internal::ReturnByValue, &result).IsOk()); | |
| 108 ASSERT_TRUE(result); | |
| 109 ASSERT_TRUE(result->HasKey("key")); | |
| 110 } | |
| 111 | |
| 112 TEST(EvaluateScriptAndGetValue, MissingType) { | |
| 113 scoped_ptr<base::Value> result; | |
| 114 FakeDevToolsClient client; | |
| 115 base::DictionaryValue dict; | |
| 116 dict.SetBoolean("wasThrown", false); | |
| 117 dict.SetInteger("result.value", 1); | |
| 118 client.set_result(dict); | |
| 119 ASSERT_TRUE(internal::EvaluateScriptAndGetValue( | |
| 120 &client, 0, "", &result).IsError()); | |
| 121 } | |
| 122 | |
| 123 TEST(EvaluateScriptAndGetValue, Undefined) { | |
| 124 scoped_ptr<base::Value> result; | |
| 125 FakeDevToolsClient client; | |
| 126 base::DictionaryValue dict; | |
| 127 dict.SetBoolean("wasThrown", false); | |
| 128 dict.SetString("result.type", "undefined"); | |
| 129 client.set_result(dict); | |
| 130 Status status = internal::EvaluateScriptAndGetValue( | |
| 131 &client, 0, "", &result); | |
| 132 ASSERT_EQ(kOk, status.code()); | |
| 133 ASSERT_TRUE(result && result->IsType(base::Value::TYPE_NULL)); | |
| 134 } | |
| 135 | |
| 136 TEST(EvaluateScriptAndGetValue, Ok) { | |
| 137 scoped_ptr<base::Value> result; | |
| 138 FakeDevToolsClient client; | |
| 139 base::DictionaryValue dict; | |
| 140 dict.SetBoolean("wasThrown", false); | |
| 141 dict.SetString("result.type", "integer"); | |
| 142 dict.SetInteger("result.value", 1); | |
| 143 client.set_result(dict); | |
| 144 Status status = internal::EvaluateScriptAndGetValue( | |
| 145 &client, 0, "", &result); | |
| 146 ASSERT_EQ(kOk, status.code()); | |
| 147 int value; | |
| 148 ASSERT_TRUE(result && result->GetAsInteger(&value)); | |
| 149 ASSERT_EQ(1, value); | |
| 150 } | |
| 151 | |
| 152 TEST(EvaluateScriptAndGetObject, NoObject) { | |
| 153 FakeDevToolsClient client; | |
| 154 base::DictionaryValue dict; | |
| 155 dict.SetBoolean("wasThrown", false); | |
| 156 dict.SetString("result.type", "integer"); | |
| 157 client.set_result(dict); | |
| 158 std::string object_id; | |
| 159 ASSERT_TRUE(internal::EvaluateScriptAndGetObject( | |
| 160 &client, 0, "", &object_id).IsError()); | |
| 161 ASSERT_TRUE(object_id.empty()); | |
| 162 } | |
| 163 | |
| 164 TEST(EvaluateScriptAndGetObject, Ok) { | |
| 165 FakeDevToolsClient client; | |
| 166 base::DictionaryValue dict; | |
| 167 dict.SetBoolean("wasThrown", false); | |
| 168 dict.SetString("result.objectId", "id"); | |
| 169 client.set_result(dict); | |
| 170 std::string object_id; | |
| 171 ASSERT_TRUE(internal::EvaluateScriptAndGetObject( | |
| 172 &client, 0, "", &object_id).IsOk()); | |
| 173 ASSERT_STREQ("id", object_id.c_str()); | |
| 174 } | |
| 175 | |
| 176 TEST(ParseCallFunctionResult, NotDict) { | |
| 177 scoped_ptr<base::Value> result; | |
| 178 base::FundamentalValue value(1); | |
| 179 ASSERT_NE(kOk, internal::ParseCallFunctionResult(value, &result).code()); | |
| 180 } | |
| 181 | |
| 182 TEST(ParseCallFunctionResult, Ok) { | |
| 183 scoped_ptr<base::Value> result; | |
| 184 base::DictionaryValue dict; | |
| 185 dict.SetInteger("status", 0); | |
| 186 dict.SetInteger("value", 1); | |
| 187 Status status = internal::ParseCallFunctionResult(dict, &result); | |
| 188 ASSERT_EQ(kOk, status.code()); | |
| 189 int value; | |
| 190 ASSERT_TRUE(result && result->GetAsInteger(&value)); | |
| 191 ASSERT_EQ(1, value); | |
| 192 } | |
| 193 | |
| 194 TEST(ParseCallFunctionResult, ScriptError) { | |
| 195 scoped_ptr<base::Value> result; | |
| 196 base::DictionaryValue dict; | |
| 197 dict.SetInteger("status", 1); | |
| 198 dict.SetInteger("value", 1); | |
| 199 Status status = internal::ParseCallFunctionResult(dict, &result); | |
| 200 ASSERT_EQ(1, status.code()); | |
| 201 ASSERT_FALSE(result); | |
| 202 } | |
| OLD | NEW |