1/*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include "UnitTestUtils/EWK2UnitTestBase.h"
29#include "UnitTestUtils/EWK2UnitTestServer.h"
30
31using namespace EWK2UnitTest;
32
33extern EWK2UnitTestEnvironment* environment;
34
35class EWK2FileChooserRequestTest : public EWK2UnitTestBase {
36public:
37    static void onFileChooserRequest(void* userData, Evas_Object*, void* eventInfo)
38    {
39        Ewk_File_Chooser_Request** returnRequest = static_cast<Ewk_File_Chooser_Request**>(userData);
40        ASSERT_TRUE(returnRequest);
41        Ewk_File_Chooser_Request* request = static_cast<Ewk_File_Chooser_Request*>(eventInfo);
42        ASSERT_TRUE(request);
43
44        // Ref the request to process asynchronously.
45        *returnRequest = ewk_object_ref(request);
46    }
47
48    static int compareStrings(const void* string1, const void* string2)
49    {
50        ASSERT(string1);
51        ASSERT(string2);
52
53        return strcmp(static_cast<const char*>(string1), static_cast<const char*>(string2));
54    }
55
56protected:
57    void freeStringList(Eina_List** list)
58    {
59        void* data;
60        EINA_LIST_FREE(*list, data) {
61        eina_stringshare_del(static_cast<char*>(data));
62        }
63    }
64
65    void clickFileInput()
66    {
67        mouseClick(15, 15);
68    }
69};
70
71TEST_F(EWK2FileChooserRequestTest, ewk_file_chooser_request_files_choose)
72{
73    Ewk_File_Chooser_Request* request = 0;
74    evas_object_smart_callback_add(webView(), "file,chooser,request", onFileChooserRequest, &request);
75    ASSERT_TRUE(loadUrlSync(environment->urlForResource("file_chooser.html").data()));
76
77    clickFileInput();
78
79    // Wait for the file chooser request.
80    while (!request)
81        ecore_main_loop_iterate();
82
83    evas_object_smart_callback_del(webView(), "file,chooser,request", onFileChooserRequest);
84    ASSERT_TRUE(request);
85    // Validate file chooser request.
86    EXPECT_TRUE(ewk_file_chooser_request_allow_multiple_files_get(request));
87    Eina_List* mimeTypes = ewk_file_chooser_request_accepted_mimetypes_get(request);
88    mimeTypes = eina_list_sort(mimeTypes, eina_list_count(mimeTypes), compareStrings);
89
90    ASSERT_EQ(2, eina_list_count(mimeTypes));
91    EXPECT_STREQ("image/*", static_cast<char*>(eina_list_nth(mimeTypes, 0)));
92    EXPECT_STREQ("video/*", static_cast<char*>(eina_list_nth(mimeTypes, 1)));
93    freeStringList(&mimeTypes);
94
95    ASSERT_FALSE(ewk_file_chooser_request_files_choose(request, 0));
96    Eina_List* files = 0;
97    files = eina_list_append(files, eina_stringshare_add("/tmp/file1.png"));
98    files = eina_list_append(files, eina_stringshare_add("/tmp/file2.png"));
99    ASSERT_TRUE(ewk_file_chooser_request_files_choose(request, files));
100    ASSERT_FALSE(ewk_file_chooser_request_files_choose(request, files));
101    freeStringList(&files);
102
103    ewk_object_unref(request);
104
105    // Check that the JS side received the files.
106    EXPECT_TRUE(waitUntilTitleChangedTo("file1.png|file2.png"));
107}
108
109TEST_F(EWK2FileChooserRequestTest, ewk_file_chooser_request_file_choose)
110{
111    Ewk_File_Chooser_Request* request = 0;
112    evas_object_smart_callback_add(webView(), "file,chooser,request", onFileChooserRequest, &request);
113    ASSERT_TRUE(loadUrlSync(environment->urlForResource("file_chooser.html").data()));
114
115    clickFileInput();
116
117    // Wait for the file chooser request.
118    while (!request)
119        ecore_main_loop_iterate();
120
121    evas_object_smart_callback_del(webView(), "file,chooser,request", onFileChooserRequest);
122    ASSERT_TRUE(request);
123
124    ASSERT_FALSE(ewk_file_chooser_request_file_choose(request, 0));
125    ASSERT_TRUE(ewk_file_chooser_request_file_choose(request, "/tmp/file3.png"));
126    ASSERT_FALSE(ewk_file_chooser_request_file_choose(request, "/tmp/file3.png"));
127
128    ewk_object_unref(request);
129
130    // Check that the JS side received the file.
131    EXPECT_TRUE(waitUntilTitleChangedTo("file3.png"));
132}
133
134TEST_F(EWK2FileChooserRequestTest, ewk_file_chooser_request_file_cancel)
135{
136    Ewk_File_Chooser_Request* request = 0;
137    evas_object_smart_callback_add(webView(), "file,chooser,request", onFileChooserRequest, &request);
138    ASSERT_TRUE(loadUrlSync(environment->urlForResource("file_chooser.html").data()));
139
140    clickFileInput();
141
142    // Wait for the file chooser request.
143    while (!request)
144        ecore_main_loop_iterate();
145
146    evas_object_smart_callback_del(webView(), "file,chooser,request", onFileChooserRequest);
147    ASSERT_TRUE(request);
148
149    ASSERT_TRUE(ewk_file_chooser_request_cancel(request));
150    ASSERT_FALSE(ewk_file_chooser_request_cancel(request));
151
152    ewk_object_unref(request);
153
154    ecore_main_loop_iterate();
155    EXPECT_STREQ("File chooser test", ewk_view_title_get(webView()));
156
157    // Default behavior is to cancel if the client does not act on the request.
158    request = 0;
159    evas_object_smart_callback_add(webView(), "file,chooser,request", onFileChooserRequest, &request);
160
161    clickFileInput();
162
163    // Wait for the file chooser request.
164    while (!request)
165        ecore_main_loop_iterate();
166
167    evas_object_smart_callback_del(webView(), "file,chooser,request", onFileChooserRequest);
168    ASSERT_TRUE(request);
169
170    ewk_object_unref(request);
171
172    ecore_main_loop_iterate();
173    EXPECT_STREQ("File chooser test", ewk_view_title_get(webView()));
174}
175