1/*
2    Copyright (C) 2012 Samsung Electronics
3    Copyright (C) 2012 Intel Corporation. All rights reserved.
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public License
16    along with this library; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20#include "config.h"
21
22#include "UnitTestUtils/EWK2UnitTestBase.h"
23
24using namespace EWK2UnitTest;
25
26extern EWK2UnitTestEnvironment* environment;
27
28#if ENABLE(INPUT_TYPE_COLOR)
29static const int initialRed = 0x12;
30static const int initialGreen = 0x34;
31static const int initialBlue = 0x56;
32static const int initialAlpha = 0xff;
33static const int changedRed = 0x98;
34static const int changedGreen = 0x76;
35static const int changedBlue = 0x54;
36static const int changedAlpha = 0xff;
37
38static bool s_isColorPickerShown = false;
39static Ewk_Color_Picker* s_colorPicker = 0;
40
41class EWK2ColorPickerTest : public EWK2UnitTestBase {
42public:
43    static void onColorPickerDone(void* userData, Evas_Object*, void*)
44    {
45        bool* handled = static_cast<bool*>(userData);
46
47        *handled = true;
48    }
49
50    static unsigned char setColorPickerColor(void* data)
51    {
52        Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(data);
53
54        // 4. Change color to changed color.
55        EXPECT_TRUE(ewk_color_picker_color_set(s_colorPicker, changedRed, changedGreen, changedBlue, changedAlpha));
56
57        evas_object_smart_callback_call(smartData->self, "input,type,color,request", 0);
58
59        return 0;
60    }
61
62    static Eina_Bool showColorPicker(Ewk_View_Smart_Data* smartData, Ewk_Color_Picker* colorPicker)
63    {
64        static bool isFirstRun = true;
65
66        s_colorPicker = colorPicker;
67        s_isColorPickerShown = true;
68
69        int r, g, b, a;
70        EXPECT_TRUE(ewk_color_picker_color_get(colorPicker, &r, &g, &b, &a));
71
72        if (isFirstRun) {
73            // 2. Check initial value from html file.
74            EXPECT_EQ(initialRed, r);
75            EXPECT_EQ(initialGreen, g);
76            EXPECT_EQ(initialBlue, b);
77            EXPECT_EQ(initialAlpha, a);
78
79            isFirstRun = false;
80        } else {
81            // 7. Input values should be same as changed color.
82            EXPECT_EQ(changedRed, r);
83            EXPECT_EQ(changedGreen, g);
84            EXPECT_EQ(changedBlue, b);
85            EXPECT_EQ(changedAlpha, a);
86
87            evas_object_smart_callback_call(smartData->self, "input,type,color,request", 0);
88            return true;
89        }
90
91        // 3. Return after making a color picker.
92        ecore_timer_add(0.0, setColorPickerColor, smartData);
93        return true;
94    }
95
96    static Eina_Bool hideColorPicker(Ewk_View_Smart_Data*)
97    {
98        // 5. Test color picker is shown.
99        EXPECT_TRUE(s_isColorPickerShown);
100        s_isColorPickerShown = false;
101    }
102
103    static Eina_Bool hideColorPickerByRemovingElement(Ewk_View_Smart_Data* smartData)
104    {
105        // 9. input_picker_color_dismiss() is called if the element is removed.
106        EXPECT_TRUE(s_isColorPickerShown);
107        s_isColorPickerShown = false;
108        evas_object_smart_callback_call(smartData->self, "input,type,color,request", 0);
109    }
110
111protected:
112    enum Button { ShowColorPickerButton, HideColorPickerButton };
113
114    void clickButton(Button button)
115    {
116        switch (button) {
117        case ShowColorPickerButton:
118            mouseClick(30, 20);
119            break;
120        case HideColorPickerButton:
121            mouseClick(80, 20);
122            break;
123        }
124    }
125};
126
127TEST_F(EWK2ColorPickerTest, ewk_color_picker_color_set)
128{
129    Ewk_View_Smart_Class* api = ewkViewClass();
130    api->input_picker_color_request = showColorPicker;
131    api->input_picker_color_dismiss = hideColorPicker;
132
133    const char colorPickerHTML[] =
134        "<!DOCTYPE html>"
135        "<html>"
136        "<head>"
137        "<script>function removeInputElement(){"
138        "var parentElement = document.getElementById('parent');"
139        "var inputElement = document.getElementById('color');"
140        "parentElement.removeChild(inputElement);"
141        "}</script>"
142        "</head>"
143        "<body>"
144        "<div id='parent'>"
145        "<input type='color' value='#123456' id='color'>"
146        "<button onclick='removeInputElement();'>Remove Element</button>"
147        "</div>"
148        "</body>"
149        "</html>";
150
151    ewk_view_html_string_load(webView(), colorPickerHTML, 0, 0);
152    waitUntilLoadFinished();
153
154    clickButton(ShowColorPickerButton);
155
156    bool handled = false;
157    evas_object_smart_callback_add(webView(), "input,type,color,request", onColorPickerDone, &handled);
158    waitUntilTrue(handled);
159
160    clickButton(ShowColorPickerButton);
161
162    handled = false;
163    waitUntilTrue(handled);
164
165    api->input_picker_color_dismiss = hideColorPickerByRemovingElement;
166    clickButton(HideColorPickerButton);
167
168    handled = false;
169    waitUntilTrue(handled);
170    evas_object_smart_callback_del(webView(), "input,type,color,request", onColorPickerDone);
171}
172#endif // ENABLE(INPUT_TYPE_COLOR)
173