[EFL][WK2] ewk_intent_request API test does not free Eina_List
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / tests / test_ewk2_intents.cpp
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/EWK2UnitTestEnvironment.h"
30 #include "UnitTestUtils/EWK2UnitTestServer.h"
31 #include <EWebKit2.h>
32 #include <Ecore.h>
33
34 using namespace EWK2UnitTest;
35
36 extern EWK2UnitTestEnvironment* environment;
37
38 static void onIntentServiceRegistration(void* userData, Evas_Object*, void* eventInfo)
39 {
40     bool* intentRegistered = static_cast<bool*>(userData);
41     ASSERT_FALSE(*intentRegistered);
42     *intentRegistered = true;
43
44     Ewk_Intent_Service* service = static_cast<Ewk_Intent_Service*>(eventInfo);
45     ASSERT_TRUE(service);
46     EXPECT_STREQ(ewk_intent_service_action_get(service), "action");
47     EXPECT_STREQ(ewk_intent_service_type_get(service), "type");
48     EXPECT_STREQ(ewk_intent_service_title_get(service), "Title");
49     EXPECT_STREQ(ewk_intent_service_href_get(service), "http://example.com/service");
50     EXPECT_STREQ(ewk_intent_service_disposition_get(service), "inline");
51 }
52
53 TEST_F(EWK2UnitTestBase, ewk_intent_service_registration)
54 {
55     bool intentRegistered = false;
56     evas_object_smart_callback_add(webView(), "intent,service,register", onIntentServiceRegistration, &intentRegistered);
57     loadUrlSync(environment->urlForResource("intent-service.html").data());
58     evas_object_smart_callback_del(webView(), "intent,service,register", onIntentServiceRegistration);
59     ASSERT_TRUE(intentRegistered);
60 }
61
62 int stringSortCb(const void* d1, const void* d2)
63 {
64     return strcmp(static_cast<const char*>(d1), static_cast<const char*>(d2));
65 }
66
67 static void onIntentReceived(void* userData, Evas_Object*, void* eventInfo)
68 {
69     unsigned* intentReceivedCount = static_cast<unsigned*>(userData);
70     ASSERT_GE(*intentReceivedCount, 0);
71     ASSERT_LE(*intentReceivedCount, 1);
72     ++(*intentReceivedCount);
73
74     Ewk_Intent* intent = static_cast<Ewk_Intent*>(eventInfo);
75     ASSERT_TRUE(intent);
76
77     if (*intentReceivedCount == 1) {
78         // First intent.
79         EXPECT_STREQ(ewk_intent_action_get(intent), "action1");
80         EXPECT_STREQ(ewk_intent_type_get(intent), "mime/type1");
81         EXPECT_STREQ(ewk_intent_service_get(intent), "http://service1.com/");
82         EXPECT_STREQ(ewk_intent_extra_get(intent, "key1"), "value1");
83         EXPECT_STREQ(ewk_intent_extra_get(intent, "key2"), "value2");
84     } else {
85         // Second intent.
86         EXPECT_STREQ(ewk_intent_action_get(intent), "action2");
87         EXPECT_STREQ(ewk_intent_type_get(intent), "mime/type2");
88         Eina_List* suggestions = ewk_intent_suggestions_get(intent);
89         ASSERT_TRUE(suggestions);
90         ASSERT_EQ(eina_list_count(suggestions), 2);
91         // We need to sort the suggestions since Intent is using a HashSet internally.
92         suggestions = eina_list_sort(suggestions, 2, stringSortCb);
93         EXPECT_STREQ(static_cast<const char*>(eina_list_nth(suggestions, 0)), "http://service1.com/");
94         EXPECT_STREQ(static_cast<const char*>(eina_list_nth(suggestions, 1)), "http://service2.com/");
95
96         void* listData = 0;
97         EINA_LIST_FREE(suggestions, listData)
98              eina_stringshare_del(static_cast<const char*>(listData));
99     }
100 }
101
102 TEST_F(EWK2UnitTestBase, ewk_intent_request)
103 {
104     unsigned intentReceivedCount = 0;
105     evas_object_smart_callback_add(webView(), "intent,request,new", onIntentReceived, &intentReceivedCount);
106     loadUrlSync(environment->urlForResource("intent-request.html").data());
107
108     // A user gesture is required for the intent to start.
109     mouseClick(5, 5);
110     while (intentReceivedCount != 1)
111         ecore_main_loop_iterate();
112     ASSERT_EQ(intentReceivedCount, 1);
113
114     // Generate a second intent request.
115     mouseClick(5, 5);
116     while (intentReceivedCount != 2)
117         ecore_main_loop_iterate();
118     ASSERT_EQ(intentReceivedCount, 2);
119     evas_object_smart_callback_del(webView(), "intent,request,new", onIntentReceived);
120 }