[EFL][WK2] Add an API for getting context menu item's parent menu
[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     // When context menu is created by WebKit, an item should have parent menu.
50     EXPECT_EQ(contextMenu, ewk_context_menu_item_parent_menu_get(item));
51
52     item = static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(list, 1));
53     checkBasicContextMenuItem(item, EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_GO_FORWARD, "Go Forward", false, true);
54     ewk_context_menu_item_enabled_set(item, false);
55     EXPECT_FALSE(ewk_context_menu_item_enabled_get(item));
56
57     item = static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(list, 2));
58     checkBasicContextMenuItem(item, EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_STOP, "Stop", false, true);
59     ewk_context_menu_item_checked_set(item, true);
60     EXPECT_TRUE(ewk_context_menu_item_checked_get(item));
61
62     item = static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(list, 3));
63     checkBasicContextMenuItem(item, EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_RELOAD, "Reload", false, true);
64     ewk_context_menu_item_title_set(item, "Refresh");
65     EXPECT_STREQ("Refresh", ewk_context_menu_item_title_get(item));
66
67     // Makes new context menu items.
68     Ewk_Context_Menu_Item* newItem = ewk_context_menu_item_new(EWK_ACTION_TYPE, EWK_CONTEXT_MENU_ITEM_TAG_OTHER, "New Custom Item", false, true);
69     // When context menu item is created using ewk_context_menu_item_new, it should not have parent menu.
70     EXPECT_EQ(0, ewk_context_menu_item_parent_menu_get(newItem));
71     ewk_context_menu_item_append(contextMenu, newItem);
72
73     // When context menu item is added to menu using ewk_context_menu_item_append, it should have parent menu.
74     EXPECT_EQ(contextMenu, ewk_context_menu_item_parent_menu_get(newItem));
75
76     Eina_List* subMenuItemList = 0;
77     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);
78     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);
79     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);
80     subMenuItemList = eina_list_append(subMenuItemList, subMenuItem1);
81     subMenuItemList = eina_list_append(subMenuItemList, subMenuItem2);
82     subMenuItemList = eina_list_append(subMenuItemList, subMenuItem3);
83     Ewk_Context_Menu* subMenu = ewk_context_menu_new_with_items(subMenuItemList);
84
85     // When context menu is created using ewk_context_menu_new_with_items, items should have parent menu.
86     EXPECT_EQ(subMenu, ewk_context_menu_item_parent_menu_get(subMenuItem1));
87
88     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);
89     // When context menu item is created using ewk_context_menu_item_new_with_submenu, it should not have parent menu.
90     EXPECT_EQ(0, ewk_context_menu_item_parent_menu_get(newItem));
91     ewk_context_menu_item_append(contextMenu, newItem2);
92
93     // When context menu item is created using ewk_context_menu_item_new_with_submenu and added using ewk_context_menu_item_append it should have parent menu.
94     EXPECT_EQ(contextMenu, ewk_context_menu_item_parent_menu_get(newItem));
95
96     Ewk_Context_Menu* subMenu2 = ewk_context_menu_new();
97     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);
98     ewk_context_menu_item_append(contextMenu, newItem3);
99
100     list = ewk_context_menu_items_get(contextMenu);
101     EXPECT_EQ(7, eina_list_count(list));
102
103     ewk_context_menu_item_remove(contextMenu, newItem);
104     ewk_context_menu_item_remove(contextMenu, newItem2);
105     ewk_context_menu_item_remove(contextMenu, newItem3);
106     list = ewk_context_menu_items_get(contextMenu);
107     EXPECT_EQ(4, eina_list_count(list));
108
109     EXPECT_TRUE(ewk_context_menu_item_select(contextMenu, item));
110
111     return true;
112 }
113
114 TEST_F(EWK2UnitTestBase, ewk_context_menu_item_select)
115 {
116     const char* itemSelectHTML =
117         "<html>"
118         "<body><a href=http://www.google.com>Test Link</a></body>"
119         "</body></html>"; 
120
121     ewkViewClass()->context_menu_show = showContextMenu;
122
123     ewk_view_html_string_load(webView(), itemSelectHTML, "file:///", 0);
124     mouseClick(20, 30, /*Right*/ 3);
125     ASSERT_TRUE(waitUntilLoadFinished());
126 }