upload tizen1.0 source
[framework/web/wrt-plugins-common.git] / src / standards / W3C / Widget / Test.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    Test.cpp
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version 1.0
20  * @date    09/13/2011 08:31:09 AM
21  * @brief
22  */
23
24 #include <unistd.h>
25 #include <signal.h>
26 #include <cstdlib>
27 #include <cstdio>
28 #include <dpl/log/log.h>
29 #include <dpl/assert.h>
30 #include <JavaScriptCore/JavaScript.h>
31 #include <Commons/WrtWrapper/WrtWrappersMgr.h>
32 #include <WidgetDB/WidgetDBMgr.h>
33 #include <WidgetDB/IWidgetDB.h>
34 #include "Test.h"
35
36 using namespace WrtDeviceApis;
37 //using namespace WrtDeviceApis::Commons;
38 //using namespace WrtDeviceApis::CommonsJavaScript;
39
40 namespace WrtPlugins {
41 namespace W3C {
42
43 Test::Test()
44   : m_context(NULL),
45     m_evaluator(NULL)
46 {
47     DPL::Event::ControllerEventHandler<FinishEvent>::Touch();
48 }
49
50 void Test::run(double timeout) {
51     PostTimedEvent(FinishEvent(), timeout);
52 }
53
54 void Test::setEvaluator(JSObjectRef evaluator) {
55     m_evaluator = evaluator;
56 }
57
58 void Test::setContext(JSContextRef context) {
59     m_context = context;
60 }
61
62 void Test::OnEventReceived(const FinishEvent& event) {
63     LogDebug("ENTER");
64
65     Result result = Result::Success;
66     if (m_evaluator && m_context) {
67         JSValueRef eval = JSObjectCallAsFunction(m_context,
68                                                  m_evaluator,
69                                                  NULL,
70                                                  0,
71                                                  NULL,
72                                                  NULL);
73         if (eval) {
74             if (JSValueIsBoolean(m_context, eval)) {
75                 if (!JSValueToBoolean(m_context, eval)) {
76                     result = Result::Fail;
77                 }
78             }
79             else {
80                 LogError("Evaluator returned value of wrong type.");
81                 result = Result::Exception;
82             }
83         }
84         else {
85             LogError("Evaluator did not return any value.");
86             result = Result::Exception;
87         }
88     }
89
90
91     FILE* output = fdopen(4, "w");
92     if (output) {
93         Commons::IWrtWrapperPtr wrt =
94                 Commons::WrtWrappersMgr::getInstance().getWrtWrapper(m_context);
95         WidgetDB::Api::IWidgetDBPtr widgetDB =
96                 WidgetDB::Api::getWidgetDB(wrt->getWidgetId());
97
98         fprintf(output,
99                 "%d\t%s\t%s\n",
100                 wrt->getWidgetId(),
101                 widgetDB->getConfigValue(
102                     WidgetDB::Api::ConfigAttribute::NAME).c_str(),
103                 resultToString(result).c_str());
104         fclose(output);
105     } else {
106         LogInfo("Output descriptor not created.");
107     }
108
109     pid_t pid = getpid();
110     LogDebug("Sending SIGINT to " << pid);
111     if (kill(pid, SIGINT) != 0) {
112         LogError("Sending SIGINT failed.");
113     }
114 }
115
116 std::string Test::resultToString(Result result) {
117     switch (result) {
118         case Result::Success:   return "OK";
119         case Result::Fail:      return "FAIL";
120         case Result::Exception: return "EXCEPTION";
121         default:
122             Assert(false && "Unsupported result type.");
123     }
124 }
125
126 }
127 }