Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / browser / xwalk_extension_function_handler_unittest.cc
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.
4
5 #include "xwalk/extensions/browser/xwalk_extension_function_handler.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 using xwalk::extensions::XWalkExtensionFunctionHandler;
10 using xwalk::extensions::XWalkExtensionFunctionInfo;
11
12 namespace {
13
14 const char kTestString[] = "crosswalk1234567890";
15
16 void DispatchResult(std::string* str, scoped_ptr<base::ListValue> result) {
17   result->GetString(0, str);
18 }
19
20 void StoreFunctionInfo(XWalkExtensionFunctionInfo** info_ptr,
21     scoped_ptr<XWalkExtensionFunctionInfo> info) {
22   *info_ptr = info.release();
23 }
24
25 void EchoData(int* counter, scoped_ptr<XWalkExtensionFunctionInfo> info) {
26   std::string str;
27   info->arguments()->GetString(0, &str);
28   EXPECT_EQ(str, kTestString);
29
30   scoped_ptr<base::ListValue> result(new base::ListValue());
31   result->AppendString(str);
32
33   info->PostResult(result.Pass());
34
35   (*counter)++;
36 }
37
38 void ResetCounter(int* counter, scoped_ptr<XWalkExtensionFunctionInfo> info) {
39   *counter = 0;
40 }
41
42 }  // namespace
43
44 TEST(XWalkExtensionFunctionHandlerTest, PostResult) {
45   std::string str;
46
47   XWalkExtensionFunctionInfo info(
48       "test",
49       make_scoped_ptr(new base::ListValue()).Pass(),
50       base::Bind(&DispatchResult, &str));
51
52   scoped_ptr<base::ListValue> data(new base::ListValue());
53   data->AppendString(kTestString);
54
55   info.PostResult(data.Pass());
56   EXPECT_EQ(str, kTestString);
57 }
58
59 TEST(XWalkExtensionFunctionHandlerTest, RegisterAndHandleFunction) {
60   XWalkExtensionFunctionHandler handler(NULL);
61
62   int counter = 0;
63   handler.Register("echoData", base::Bind(&EchoData, &counter));
64   handler.Register("reset", base::Bind(&ResetCounter, &counter));
65
66   for (unsigned i = 0; i < 1000; ++i) {
67     std::string str1;
68     scoped_ptr<base::ListValue> data1(new base::ListValue());
69     data1->AppendString(kTestString);
70
71     scoped_ptr<XWalkExtensionFunctionInfo> info1(new XWalkExtensionFunctionInfo(
72         "echoData",
73         data1.Pass(),
74         base::Bind(&DispatchResult, &str1)));
75
76     handler.HandleFunction(info1.Pass());
77     EXPECT_EQ(counter, i + 1);
78     EXPECT_EQ(str1, kTestString);
79   }
80
81   std::string str2;
82   scoped_ptr<base::ListValue> data2(new base::ListValue());
83   data2->AppendString(kTestString);
84
85   scoped_ptr<XWalkExtensionFunctionInfo> info2(new XWalkExtensionFunctionInfo(
86       "reset",
87       data2.Pass(),
88       base::Bind(&DispatchResult, &str2)));
89
90   handler.HandleFunction(info2.Pass());
91   EXPECT_EQ(counter, 0);
92
93   // Dispatching to a non registered handler should not crash.
94   std::string str3;
95   scoped_ptr<base::ListValue> data3(new base::ListValue());
96   data3->AppendString(kTestString);
97
98   scoped_ptr<XWalkExtensionFunctionInfo> info3(new XWalkExtensionFunctionInfo(
99       "foobar",
100       data3.Pass(),
101       base::Bind(&DispatchResult, &str3)));
102
103   handler.HandleFunction(info3.Pass());
104 }
105
106 TEST(XWalkExtensionFunctionHandlerTest, PostingResultAfterDeletingTheHandler) {
107   scoped_ptr<XWalkExtensionFunctionHandler> handler(
108       new XWalkExtensionFunctionHandler(NULL));
109
110   XWalkExtensionFunctionInfo* info;
111   handler->Register("storeFunctionInfo", base::Bind(&StoreFunctionInfo, &info));
112
113   scoped_ptr<base::ListValue> msg(new base::ListValue);
114   msg->AppendString("storeFunctionInfo");  // Function name.
115   msg->AppendString("id");  // Callback ID.
116
117   handler->HandleMessage(msg.Pass());
118   handler.reset();
119
120   // Posting a result after deleting the handler should not
121   // crash because internally, the reference to the handler
122   // is weak.
123   info->PostResult(make_scoped_ptr(new base::ListValue));
124   delete info;
125 }