1 // Copyright (c) 2013 Intel Corporation. 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.
5 #include "xwalk/extensions/browser/xwalk_extension_function_handler.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 using xwalk::extensions::XWalkExtensionFunctionHandler;
10 using xwalk::extensions::XWalkExtensionFunctionInfo;
14 const char kTestString[] = "crosswalk1234567890";
16 void DispatchResult(std::string* str, scoped_ptr<base::ListValue> result) {
17 result->GetString(0, str);
20 void StoreFunctionInfo(XWalkExtensionFunctionInfo** info_ptr,
21 scoped_ptr<XWalkExtensionFunctionInfo> info) {
22 *info_ptr = info.release();
25 void EchoData(int* counter, scoped_ptr<XWalkExtensionFunctionInfo> info) {
27 info->arguments()->GetString(0, &str);
28 EXPECT_EQ(str, kTestString);
30 scoped_ptr<base::ListValue> result(new base::ListValue());
31 result->AppendString(str);
33 info->PostResult(result.Pass());
38 void ResetCounter(int* counter, scoped_ptr<XWalkExtensionFunctionInfo> info) {
44 TEST(XWalkExtensionFunctionHandlerTest, PostResult) {
47 XWalkExtensionFunctionInfo info(
49 make_scoped_ptr(new base::ListValue()).Pass(),
50 base::Bind(&DispatchResult, &str));
52 scoped_ptr<base::ListValue> data(new base::ListValue());
53 data->AppendString(kTestString);
55 info.PostResult(data.Pass());
56 EXPECT_EQ(str, kTestString);
59 TEST(XWalkExtensionFunctionHandlerTest, RegisterAndHandleFunction) {
60 XWalkExtensionFunctionHandler handler(NULL);
63 handler.Register("echoData", base::Bind(&EchoData, &counter));
64 handler.Register("reset", base::Bind(&ResetCounter, &counter));
66 for (unsigned i = 0; i < 1000; ++i) {
68 scoped_ptr<base::ListValue> data1(new base::ListValue());
69 data1->AppendString(kTestString);
71 scoped_ptr<XWalkExtensionFunctionInfo> info1(new XWalkExtensionFunctionInfo(
74 base::Bind(&DispatchResult, &str1)));
76 handler.HandleFunction(info1.Pass());
77 EXPECT_EQ(counter, i + 1);
78 EXPECT_EQ(str1, kTestString);
82 scoped_ptr<base::ListValue> data2(new base::ListValue());
83 data2->AppendString(kTestString);
85 scoped_ptr<XWalkExtensionFunctionInfo> info2(new XWalkExtensionFunctionInfo(
88 base::Bind(&DispatchResult, &str2)));
90 handler.HandleFunction(info2.Pass());
91 EXPECT_EQ(counter, 0);
93 // Dispatching to a non registered handler should not crash.
95 scoped_ptr<base::ListValue> data3(new base::ListValue());
96 data3->AppendString(kTestString);
98 scoped_ptr<XWalkExtensionFunctionInfo> info3(new XWalkExtensionFunctionInfo(
101 base::Bind(&DispatchResult, &str3)));
103 handler.HandleFunction(info3.Pass());
106 TEST(XWalkExtensionFunctionHandlerTest, PostingResultAfterDeletingTheHandler) {
107 scoped_ptr<XWalkExtensionFunctionHandler> handler(
108 new XWalkExtensionFunctionHandler(NULL));
110 XWalkExtensionFunctionInfo* info;
111 handler->Register("storeFunctionInfo", base::Bind(&StoreFunctionInfo, &info));
113 scoped_ptr<base::ListValue> msg(new base::ListValue);
114 msg->AppendString("storeFunctionInfo"); // Function name.
115 msg->AppendString("id"); // Callback ID.
117 handler->HandleMessage(msg.Pass());
120 // Posting a result after deleting the handler should not
121 // crash because internally, the reference to the handler
123 info->PostResult(make_scoped_ptr(new base::ListValue));