Fix values of StorageEvent properties.
authorZbigniew Kostrzewa <z.kostrzewa@samsung.com>
Thu, 13 Jun 2013 08:46:54 +0000 (10:46 +0200)
committerZbigniew Kostrzewa <z.kostrzewa@samsung.com>
Thu, 13 Jun 2013 09:08:02 +0000 (11:08 +0200)
[Issue#] N/A
[Problem] On removeItem() newValue was set to empty instead of null. On
setItem() oldValue was set to empty instead of null. On clear() key,
oldValue nad newValue were set to empty instead of null.
[Cause] N/A
[Solution] Pass proper values.
[SCMRequest] N/A

[Verification]
1. Build repository
2. Run W3C_WidgetInterface.wgt test widget.
3. Run tests from [RSA]/test/webapi
   - webapi-w3c-widgetinterface-tests/widgetinterface/w3c/au.wgt
   - webapi-w3c-widgetinterface-tests/widgetinterface/w3c/setItem-fires-event.wgt
   - webapi-w3c-widgetinterface-tests/widgetinterface/w3c/removeItem-fires-event.wgt

Change-Id: Ic39340ec283a038012d38dc1918135ea2809e647

src/dispatch-event/dispatch_event_support.cpp
src/dispatch-event/dispatch_event_support.h
src/standards/W3C/Widget/JSPreferences.cpp

index 24c9f84..d9b4d2b 100644 (file)
@@ -113,24 +113,38 @@ void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string sta
     }
 }
 
-void dispatchStorageEvent(JSContextRef context, const std::string key, const std::string oldValue, const std::string newValue, const std::string url)
+void dispatchStorageEvent(JSContextRef context,
+                          const DPL::Optional<std::string>& key,
+                          const DPL::Optional<std::string>& oldValue,
+                          const DPL::Optional<std::string>& newValue,
+                          const std::string& url)
 {
-    LogDebug("dispatchStorageEvent(" << context << ", " << key << ", " << oldValue << ", " << newValue << ", " << url << ")");
+    LogDebug("dispatchStorageEvent(" <<
+             context << ", " <<
+             (key.IsNull() ? "null" : *key) << ", " <<
+             (oldValue.IsNull() ? "null" : *oldValue) << ", " <<
+             (newValue.IsNull() ? "null" : *newValue) << ", " <<
+             url << ")");
 
     if (context != NULL)
     {
         std::stringstream script;
 
-        script << "var __event = document.createEvent(\"CustomEvent\");\n"
-               << "__event.initCustomEvent(\"storage\", true, true);\n"
-               << "__event.key = \"" << key << "\";\n"
-               << "__event.oldValue = \"" << oldValue << "\";\n"
-               << "__event.newValue = \"" << newValue << "\";\n"
-               << "__event.url = \"" << url << "\";\n"
-               << "__event.storageArea = widget.preferences;\n"
-               << "document.dispatchEvent(__event);\n"
-               << "\n"
-               << "for (var i=0; i < window.frames.length; i++)\n"
+        auto toJSValue =
+            [](const DPL::Optional<std::string>& value) -> std::string
+            {
+                return (value.IsNull() ? "null" : "\"" + *value + "\"");
+            };
+
+        script << "var __event = document.createEvent(\"CustomEvent\");"
+               << "__event.initCustomEvent(\"storage\", true, true);"
+               << "__event.key = " << toJSValue(key) << ";"
+               << "__event.oldValue = " << toJSValue(oldValue) << ";"
+               << "__event.newValue = " << toJSValue(newValue) << ";"
+               << "__event.url = \"" << url << "\";"
+               << "__event.storageArea = widget.preferences;"
+               << "document.dispatchEvent(__event);"
+               << "for (var i=0; i < window.frames.length; i++)"
                << "{ window.frames[i].document.dispatchEvent(__event); }";
 
 
index df74b4b..8a42e20 100644 (file)
@@ -25,6 +25,7 @@
 #define _DISPATCH_EVENT_SUPPORT_H_
 
 #include <string>
+#include <dpl/optional.h>
 #include <dpl/log/log.h>
 #include <EWebKit2.h>
 #include <JavaScriptCore/JavaScript.h>
@@ -33,7 +34,11 @@ namespace DispatchEventSupport {
 
 void dispatchAppServiceEvent(JSContextRef context, const float scale, const std::string bundle);
 void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string state, const int width, const int height);
-void dispatchStorageEvent(JSContextRef context, const std::string key, const std::string oldValue, const std::string newValue, const std::string url);
+void dispatchStorageEvent(JSContextRef context,
+                          const DPL::Optional<std::string>& key,
+                          const DPL::Optional<std::string>& oldValue,
+                          const DPL::Optional<std::string>& newValue,
+                          const std::string& url);
 void dispatchHwKeyEvent(Evas_Object* ewkView, const std::string key);
 }
 #endif //_DISPATCH_EVENT_SUPPORT_H_
index 6f38328..d3ba880 100644 (file)
@@ -263,13 +263,8 @@ JSValueRef JSPreferences::removeItem(JSContextRef context,
         DPL::Optional<std::string> newValue = getIStorage(thisObject)->getValue(key);
 
         JSContextRef g_context  = JSContextGetGlobalContext(context);
-        std::string oldValueStr = "";
-        std::string newValueStr = "";
 
-        if (!!oldValue) { oldValueStr = *oldValue; }
-        if (!!newValue) { newValueStr = *newValue; }
-
-        DispatchEventSupport::dispatchStorageEvent(g_context, key, oldValueStr, newValueStr, "");
+        DispatchEventSupport::dispatchStorageEvent(g_context, key, oldValue, newValue, "");
 
         LogDebug("end");
         return JSValueMakeNull(context);
@@ -303,13 +298,8 @@ JSValueRef JSPreferences::setItem(JSContextRef context,
         DPL::Optional<std::string> newValue = getIStorage(thisObject)->getValue(key);
 
         JSContextRef g_context  = JSContextGetGlobalContext(context);
-        std::string oldValueStr = "";
-        std::string newValueStr = "";
-
-        if (!!oldValue) { oldValueStr = *oldValue; }
-        if (!!newValue) { newValueStr = *newValue; }
 
-        DispatchEventSupport::dispatchStorageEvent(g_context, key, oldValueStr, newValueStr, "");
+        DispatchEventSupport::dispatchStorageEvent(g_context, key, oldValue, newValue, "");
 
         LogDebug("end");
 
@@ -337,7 +327,9 @@ JSValueRef JSPreferences::clear(JSContextRef context,
 
         JSContextRef g_context  = JSContextGetGlobalContext(context);
 
-        DispatchEventSupport::dispatchStorageEvent(g_context, "", "", "", "");
+        const auto& null = DPL::Optional<std::string>::Null;
+
+        DispatchEventSupport::dispatchStorageEvent(g_context, null, null, null, "");
 
         LogDebug("end");