78858af1d5eab851a80aec4d7a4fcbdca330eb9d
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / tests / test_ewk2_context_menu.cpp
1 /*
2     Copyright (C) 2012 Samsung Electronics
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12     Lesser General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public License
15     along with this library; if not, write to the Free Software Foundation,
16     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "config.h"
20
21 #include "UnitTestUtils/EWK2UnitTestBase.h"
22 #include "UnitTestUtils/EWK2UnitTestEnvironment.h"
23 #include <EWebKit2.h>
24 #include <Ecore.h>
25
26 using namespace EWK2UnitTest;
27
28 extern EWK2UnitTestEnvironment* environment;
29
30 static inline void checkBasicContextMenuItem(Ewk_Context_Menu_Item* item, Ewk_Context_Menu_Item_Type type, Ewk_Context_Menu_Item_Action action, const char* title, Eina_Bool checked, Eina_Bool enabled)
31 {
32     ASSERT_TRUE(item);
33
34     EXPECT_EQ(type, ewk_context_menu_item_type_get(item));
35     EXPECT_EQ(action, ewk_context_menu_item_action_get(item));
36     EXPECT_STREQ(title, ewk_context_menu_item_title_get(item));
37     EXPECT_EQ(checked, ewk_context_menu_item_checked_get(item));
38     EXPECT_EQ(enabled, ewk_context_menu_item_enabled_get(item));
39 }
40
41 static Eina_Bool showContextMenu(Ewk_View_Smart_Data* smartData, Evas_Coord x, Evas_Coord y, Ewk_Context_Menu* contextMenu)
42 {
43     const Eina_List* list = ewk_context_menu_items_get(contextMenu);
44     EXPECT_EQ(4, eina_list_count(list));
45
46     Ewk_Context_Menu_Item* item = static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(list, 0));
47     checkBasicContextMenuItem(item, EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_GO_BACK, "Go Back", false, true);
48
49     item = static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(list, 1));
50     checkBasicContextMenuItem(item, EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_GO_FORWARD, "Go Forward", false, true);
51     ewk_context_menu_item_enabled_set(item, false);
52     EXPECT_FALSE(ewk_context_menu_item_enabled_get(item));
53
54     item = static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(list, 2));
55     checkBasicContextMenuItem(item, EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_STOP, "Stop", false, true);
56     ewk_context_menu_item_checked_set(item, true);
57     EXPECT_TRUE(ewk_context_menu_item_checked_get(item));
58
59     item = static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(list, 3));
60     checkBasicContextMenuItem(item, EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_RELOAD, "Reload", false, true);
61     ewk_context_menu_item_title_set(item, "Refresh");
62     EXPECT_STREQ("Refresh", ewk_context_menu_item_title_get(item));
63
64     // Makes new context menu items.
65     Ewk_Context_Menu_Item* newItem = ewk_context_menu_item_new(EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_OTHER, "New Custom Item", false, true);
66     ewk_context_menu_item_append(contextMenu, newItem);
67
68     Eina_List* subMenuItemList = 0;
69     Ewk_Context_Menu_Item* subMenuItem1 = ewk_context_menu_item_new(EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_OTHER, "New SubMenu Item 1", false, true);
70     Ewk_Context_Menu_Item* subMenuItem2 = ewk_context_menu_item_new(EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_OTHER, "New SubMenu Item 2", false, true);
71     Ewk_Context_Menu_Item* subMenuItem3 = ewk_context_menu_item_new(EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_OTHER, "New SubMenu Item 3", false, true);
72     subMenuItemList = eina_list_append(subMenuItemList, subMenuItem1);
73     subMenuItemList = eina_list_append(subMenuItemList, subMenuItem2);
74     subMenuItemList = eina_list_append(subMenuItemList, subMenuItem3);
75     Ewk_Context_Menu* subMenu = ewk_context_menu_new_with_items(subMenuItemList);
76     Ewk_Context_Menu_Item* newItem2 = ewk_context_menu_item_new_with_submenu(EWK_SUBMENU_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_OTHER, "New Custom Item 2", false, true, subMenu);
77     ewk_context_menu_item_append(contextMenu, newItem2);
78
79     Ewk_Context_Menu* subMenu2 = ewk_context_menu_new();
80     Ewk_Context_Menu_Item* newItem3 = ewk_context_menu_item_new_with_submenu(EWK_SUBMENU_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_OTHER, "New Custom Item 3", false, true, subMenu2);
81     ewk_context_menu_item_append(contextMenu, newItem3);
82
83     list = ewk_context_menu_items_get(contextMenu);
84     EXPECT_EQ(7, eina_list_count(list));
85
86     ewk_context_menu_item_remove(contextMenu, newItem);
87     ewk_context_menu_item_remove(contextMenu, newItem2);
88     ewk_context_menu_item_remove(contextMenu, newItem3);
89     list = ewk_context_menu_items_get(contextMenu);
90     EXPECT_EQ(4, eina_list_count(list));
91
92     EXPECT_TRUE(ewk_context_menu_item_select(contextMenu, item));
93
94     return true;
95 }
96
97 TEST_F(EWK2UnitTestBase, ewk_context_menu_item_select)
98 {
99     const char* itemSelectHTML =
100         "<html>"
101         "<body><a href=http://www.google.com>Test Link</a></body>"
102         "</body></html>"; 
103
104     ewkViewClass()->context_menu_show = showContextMenu;
105
106     ewk_view_html_string_load(webView(), itemSelectHTML, "file:///", 0);
107     mouseClick(20, 30, /*Right*/ 3);
108     ASSERT_TRUE(waitUntilLoadFinished());
109 }