2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2009 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "include/cef.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
#include "test_handler.h"
|
|
|
|
|
|
|
|
bool g_V8TestV8HandlerExecuteCalled;
|
|
|
|
bool g_V8TestV8HandlerExecute2Called;
|
|
|
|
|
|
|
|
class V8TestV8Handler : public CefThreadSafeBase<CefV8Handler>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
V8TestV8Handler(bool bindingTest) { binding_test_ = bindingTest; }
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
virtual bool Execute(const CefString& name,
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments,
|
|
|
|
CefRefPtr<CefV8Value>& retval,
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString& exception)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
TestExecute(name, object, arguments, retval, exception);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
void TestExecute(const CefString& name,
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments,
|
|
|
|
CefRefPtr<CefV8Value>& retval,
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString& exception)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2010-11-22 18:49:46 +01:00
|
|
|
if(name == "execute") {
|
2010-10-03 23:04:50 +02:00
|
|
|
g_V8TestV8HandlerExecuteCalled = true;
|
|
|
|
|
2010-11-15 16:39:56 +01:00
|
|
|
ASSERT_EQ((size_t)8, arguments.size());
|
2010-10-03 23:04:50 +02:00
|
|
|
int argct = 0;
|
|
|
|
|
|
|
|
// basic types
|
|
|
|
ASSERT_TRUE(arguments[argct]->IsInt());
|
|
|
|
ASSERT_EQ(5, arguments[argct]->GetIntValue());
|
|
|
|
argct++;
|
|
|
|
|
|
|
|
ASSERT_TRUE(arguments[argct]->IsDouble());
|
|
|
|
ASSERT_EQ(6.543, arguments[argct]->GetDoubleValue());
|
|
|
|
argct++;
|
|
|
|
|
|
|
|
ASSERT_TRUE(arguments[argct]->IsBool());
|
|
|
|
ASSERT_EQ(true, arguments[argct]->GetBoolValue());
|
|
|
|
argct++;
|
|
|
|
|
|
|
|
ASSERT_TRUE(arguments[argct]->IsString());
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_EQ(arguments[argct]->GetStringValue(), "test string");
|
2010-10-03 23:04:50 +02:00
|
|
|
argct++;
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> value;
|
|
|
|
|
|
|
|
// array
|
|
|
|
ASSERT_TRUE(arguments[argct]->IsArray());
|
|
|
|
ASSERT_EQ(4, arguments[argct]->GetArrayLength());
|
|
|
|
{
|
|
|
|
int subargct = 0;
|
|
|
|
value = arguments[argct]->GetValue(subargct);
|
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsInt());
|
|
|
|
ASSERT_EQ(7, value->GetIntValue());
|
|
|
|
subargct++;
|
|
|
|
|
|
|
|
value = arguments[argct]->GetValue(subargct);
|
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsDouble());
|
|
|
|
ASSERT_EQ(5.432, value->GetDoubleValue());
|
|
|
|
subargct++;
|
|
|
|
|
|
|
|
value = arguments[argct]->GetValue(subargct);
|
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsBool());
|
|
|
|
ASSERT_EQ(false, value->GetBoolValue());
|
|
|
|
subargct++;
|
|
|
|
|
|
|
|
value = arguments[argct]->GetValue(subargct);
|
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsString());
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_EQ(value->GetStringValue(), "another string");
|
2010-10-03 23:04:50 +02:00
|
|
|
subargct++;
|
|
|
|
}
|
|
|
|
argct++;
|
|
|
|
|
|
|
|
// object
|
|
|
|
ASSERT_TRUE(arguments[argct]->IsObject());
|
|
|
|
{
|
2010-11-22 18:49:46 +01:00
|
|
|
value = arguments[argct]->GetValue("arg0");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsInt());
|
|
|
|
ASSERT_EQ(2, value->GetIntValue());
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
value = arguments[argct]->GetValue("arg1");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsDouble());
|
|
|
|
ASSERT_EQ(3.433, value->GetDoubleValue());
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
value = arguments[argct]->GetValue("arg2");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsBool());
|
|
|
|
ASSERT_EQ(true, value->GetBoolValue());
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
value = arguments[argct]->GetValue("arg3");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsString());
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_EQ(value->GetStringValue(), "some string");
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
argct++;
|
|
|
|
|
|
|
|
// function that returns a value
|
|
|
|
ASSERT_TRUE(arguments[argct]->IsFunction());
|
|
|
|
{
|
|
|
|
CefV8ValueList args;
|
|
|
|
args.push_back(CefV8Value::CreateInt(5));
|
|
|
|
args.push_back(CefV8Value::CreateDouble(3.5));
|
|
|
|
args.push_back(CefV8Value::CreateBool(true));
|
2010-11-22 18:49:46 +01:00
|
|
|
args.push_back(CefV8Value::CreateString("10"));
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefV8Value> rv;
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString exception;
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(arguments[argct]->ExecuteFunction(
|
|
|
|
arguments[argct], args, rv, exception));
|
|
|
|
ASSERT_TRUE(rv.get() != NULL);
|
|
|
|
ASSERT_TRUE(rv->IsDouble());
|
|
|
|
ASSERT_EQ(19.5, rv->GetDoubleValue());
|
|
|
|
}
|
|
|
|
argct++;
|
|
|
|
|
|
|
|
// function that throws an exception
|
|
|
|
ASSERT_TRUE(arguments[argct]->IsFunction());
|
|
|
|
{
|
|
|
|
CefV8ValueList args;
|
|
|
|
args.push_back(CefV8Value::CreateDouble(5));
|
|
|
|
args.push_back(CefV8Value::CreateDouble(0));
|
|
|
|
CefRefPtr<CefV8Value> rv;
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString exception;
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(arguments[argct]->ExecuteFunction(
|
|
|
|
arguments[argct], args, rv, exception));
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_EQ(exception, "Uncaught My Exception");
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
argct++;
|
|
|
|
|
|
|
|
if(binding_test_)
|
|
|
|
{
|
|
|
|
// values
|
2010-11-22 18:49:46 +01:00
|
|
|
value = object->GetValue("intVal");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsInt());
|
|
|
|
ASSERT_EQ(12, value->GetIntValue());
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
value = object->GetValue("doubleVal");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsDouble());
|
|
|
|
ASSERT_EQ(5.432, value->GetDoubleValue());
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
value = object->GetValue("boolVal");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsBool());
|
|
|
|
ASSERT_EQ(true, value->GetBoolValue());
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
value = object->GetValue("stringVal");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsString());
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_EQ(value->GetStringValue(), "the string");
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
value = object->GetValue("arrayVal");
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(value.get() != NULL);
|
|
|
|
ASSERT_TRUE(value->IsArray());
|
|
|
|
{
|
|
|
|
CefRefPtr<CefV8Value> value2;
|
|
|
|
int subargct = 0;
|
|
|
|
value2 = value->GetValue(subargct);
|
|
|
|
ASSERT_TRUE(value2.get() != NULL);
|
|
|
|
ASSERT_TRUE(value2->IsInt());
|
|
|
|
ASSERT_EQ(4, value2->GetIntValue());
|
|
|
|
subargct++;
|
|
|
|
|
|
|
|
value2 = value->GetValue(subargct);
|
|
|
|
ASSERT_TRUE(value2.get() != NULL);
|
|
|
|
ASSERT_TRUE(value2->IsDouble());
|
|
|
|
ASSERT_EQ(120.43, value2->GetDoubleValue());
|
|
|
|
subargct++;
|
|
|
|
|
|
|
|
value2 = value->GetValue(subargct);
|
|
|
|
ASSERT_TRUE(value2.get() != NULL);
|
|
|
|
ASSERT_TRUE(value2->IsBool());
|
|
|
|
ASSERT_EQ(true, value2->GetBoolValue());
|
|
|
|
subargct++;
|
|
|
|
|
|
|
|
value2 = value->GetValue(subargct);
|
|
|
|
ASSERT_TRUE(value2.get() != NULL);
|
|
|
|
ASSERT_TRUE(value2->IsString());
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_EQ(value2->GetStringValue(), "a string");
|
2010-10-03 23:04:50 +02:00
|
|
|
subargct++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = CefV8Value::CreateInt(5);
|
2010-11-22 18:49:46 +01:00
|
|
|
} else if(name == "execute2") {
|
2010-10-03 23:04:50 +02:00
|
|
|
g_V8TestV8HandlerExecute2Called = true;
|
|
|
|
|
|
|
|
// check the result of calling the "execute" function
|
2010-11-15 16:39:56 +01:00
|
|
|
ASSERT_EQ((size_t)1, arguments.size());
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(arguments[0]->IsInt());
|
|
|
|
ASSERT_EQ(5, arguments[0]->GetIntValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool binding_test_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class V8TestHandler : public TestHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
V8TestHandler(bool bindingTest) { binding_test_ = bindingTest; }
|
|
|
|
|
|
|
|
virtual void RunTest()
|
|
|
|
{
|
|
|
|
std::string object;
|
|
|
|
if(binding_test_) {
|
|
|
|
// binding uses the window object
|
|
|
|
object = "window.test";
|
|
|
|
} else {
|
|
|
|
// extension uses a global object
|
|
|
|
object = "test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream testHtml;
|
|
|
|
testHtml <<
|
|
|
|
"<html><body>"
|
|
|
|
"<script language=\"JavaScript\">"
|
|
|
|
"function func(a,b,c,d) { return a+b+(c?1:0)+parseFloat(d); }"
|
|
|
|
"function func2(a,b) { throw('My Exception'); }"
|
|
|
|
<< object << ".execute2("
|
|
|
|
" " << object << ".execute(5, 6.543, true, \"test string\","
|
|
|
|
" [7, 5.432, false, \"another string\"],"
|
|
|
|
" {arg0:2, arg1:3.433, arg2:true, arg3:\"some string\"}, func, func2)"
|
|
|
|
");"
|
|
|
|
"</script>"
|
|
|
|
"</body></html>";
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
AddResource("http://tests/run.html", testHtml.str(), "text/html");
|
|
|
|
CreateBrowser("http://tests/run.html");
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleLoadEnd(CefRefPtr<CefBrowser> browser,
|
2011-01-12 00:46:14 +01:00
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
bool isMainContent)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
if(!browser->IsPopup() && !frame.get())
|
|
|
|
DestroyTest();
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleJSBinding(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefV8Value> object)
|
|
|
|
{
|
|
|
|
if(binding_test_) {
|
|
|
|
TestHandleJSBinding(browser, frame, object);
|
|
|
|
return RV_HANDLED;
|
|
|
|
}
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandleJSBinding(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefV8Value> object)
|
|
|
|
{
|
|
|
|
// Create the new V8 object
|
|
|
|
CefRefPtr<CefV8Value> testObj = CefV8Value::CreateObject(NULL);
|
|
|
|
ASSERT_TRUE(testObj.get() != NULL);
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(object->SetValue("test", testObj));
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Create an instance of V8ExecuteV8Handler
|
|
|
|
CefRefPtr<CefV8Handler> testHandler(new V8TestV8Handler(true));
|
|
|
|
ASSERT_TRUE(testHandler.get() != NULL);
|
|
|
|
|
|
|
|
// Add the functions
|
|
|
|
CefRefPtr<CefV8Value> testFunc;
|
2010-11-22 18:49:46 +01:00
|
|
|
testFunc = CefV8Value::CreateFunction("execute", testHandler);
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(testFunc.get() != NULL);
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(testObj->SetValue("execute", testFunc));
|
|
|
|
testFunc = CefV8Value::CreateFunction("execute2", testHandler);
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(testFunc.get() != NULL);
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(testObj->SetValue("execute2", testFunc));
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Add the values
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(testObj->SetValue("intVal",
|
2010-10-03 23:04:50 +02:00
|
|
|
CefV8Value::CreateInt(12)));
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(testObj->SetValue("doubleVal",
|
2010-10-03 23:04:50 +02:00
|
|
|
CefV8Value::CreateDouble(5.432)));
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(testObj->SetValue("boolVal",
|
2010-10-03 23:04:50 +02:00
|
|
|
CefV8Value::CreateBool(true)));
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(testObj->SetValue("stringVal",
|
|
|
|
CefV8Value::CreateString("the string")));
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> testArray(CefV8Value::CreateArray());
|
|
|
|
ASSERT_TRUE(testArray.get() != NULL);
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(testObj->SetValue("arrayVal", testArray));
|
2010-10-03 23:04:50 +02:00
|
|
|
ASSERT_TRUE(testArray->SetValue(0, CefV8Value::CreateInt(4)));
|
|
|
|
ASSERT_TRUE(testArray->SetValue(1, CefV8Value::CreateDouble(120.43)));
|
|
|
|
ASSERT_TRUE(testArray->SetValue(2, CefV8Value::CreateBool(true)));
|
2010-11-22 18:49:46 +01:00
|
|
|
ASSERT_TRUE(testArray->SetValue(3, CefV8Value::CreateString("a string")));
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool binding_test_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Verify window binding
|
|
|
|
TEST(V8Test, Binding)
|
|
|
|
{
|
|
|
|
g_V8TestV8HandlerExecuteCalled = false;
|
|
|
|
g_V8TestV8HandlerExecute2Called = false;
|
|
|
|
|
|
|
|
CefRefPtr<V8TestHandler> handler = new V8TestHandler(true);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
ASSERT_TRUE(g_V8TestV8HandlerExecuteCalled);
|
|
|
|
ASSERT_TRUE(g_V8TestV8HandlerExecute2Called);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify extensions
|
|
|
|
TEST(V8Test, Extension)
|
|
|
|
{
|
|
|
|
g_V8TestV8HandlerExecuteCalled = false;
|
|
|
|
g_V8TestV8HandlerExecute2Called = false;
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
std::string extensionCode =
|
|
|
|
"var test;"
|
|
|
|
"if (!test)"
|
|
|
|
" test = {};"
|
|
|
|
"(function() {"
|
|
|
|
" test.execute = function(a,b,c,d,e,f,g,h) {"
|
|
|
|
" native function execute();"
|
|
|
|
" return execute(a,b,c,d,e,f,g,h);"
|
|
|
|
" };"
|
|
|
|
" test.execute2 = function(a) {"
|
|
|
|
" native function execute2();"
|
|
|
|
" return execute2(a);"
|
|
|
|
" };"
|
|
|
|
"})();";
|
|
|
|
CefRegisterExtension("v8/test", extensionCode, new V8TestV8Handler(false));
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
CefRefPtr<V8TestHandler> handler = new V8TestHandler(false);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
ASSERT_TRUE(g_V8TestV8HandlerExecuteCalled);
|
|
|
|
ASSERT_TRUE(g_V8TestV8HandlerExecute2Called);
|
|
|
|
}
|