summaryrefslogtreecommitdiff
path: root/src/menu.c
blob: ea20868958a2ec63de2de0891cfdfa9222d7bb65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Linux Desktop Testing Project http://ldtp.freedesktop.org
 *
 * Author:
 *    Poornima Nayak <pnayak@novell.com>
 *    Khasim Shaheed <khasim.shaheed@gmail.com>
 *
 * Copyright 2004 - 2006 Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110, USA.
 */

#include "ldtp.h"
#include "ldtp-gui.h"
#include "ldtp-error.h"
#include "ldtp-logger.h"
#include "ldtp-command.h"
#include "ldtp-gui-comp.h"

static LDTPErrorCode
click_menu_item (Accessible *object)
{
	AccessibleAction *action;
	
	action = Accessible_getAction (object);
	if (action) {
		AccessibleAction_doAction (action, 0);
		Accessible_unref (action);
	}
	
	return LDTP_ERROR_SUCCESS;
}

static LDTPErrorCode
select_menu_item (Accessible *object, char *param)
{
	/*
	  FIXME: Commented by Nagappan. When menu->menu item selected, menu's focus
	  is still there. By commenting this code we are not clicking menu directly.
	  Instead we are clicking menu item. This bug is fixed in cvs of at-spi package
	  Thursday, February 03 2005 - 05:43 PM IST
	*/
	//int i;
	//Accessible *child_object;
	//AccessibleSelection *selection;
	//AccessibleAction *action;
	//
	//i = menu_item_exist (object, param);
	//if (i > -1)
	//  {
	//    child_object = Accessible_getChildAtIndex (object, i);
	//    selection = Accessible_getSelection (object);
	///*
	//  Select child
	//*/    
	//    AccessibleSelection_selectChild (selection, i);
	//    action = Accessible_getAction (child_object);
	//    AccessibleAction_doAction (action, 0);
	//  }
	//
	//action = Accessible_getAction (object);
	///*
	//  FIXME: If we do this action, then the menu selected doesn't
	//  get deselected. Need to be resolved
	//*/
	//AccessibleAction_doAction (action, 0);
	//Accessible_unref (action);
	return LDTP_ERROR_SUCCESS;
}

static LDTPErrorCode
is_menu_item_enabled (Accessible *object, FILE *log_fp)
{
	SPIBoolean enabled;
	AccessibleStateSet *state;

	state = Accessible_getStateSet (object);
	enabled = AccessibleStateSet_contains (state, SPI_STATE_SENSITIVE);
	if (enabled)
		return LDTP_ERROR_SUCCESS;
	else {
		LDTPErrorCode error;
		error = LDTP_ERROR_MENU_ITEM_STATE_DISABLED;
		log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), log_fp);
		return error;
	}
}

static LDTPErrorCode
list_child_menu_items (Accessible *object, char **data, FILE *log_fp)
{
	long i;
	long child_count;
	char *name;
	Accessible *child;

	child_count = Accessible_getChildCount (object);
	if (child_count <= 0) {
		LDTPErrorCode error = LDTP_ERROR_MENU_ITEM_DOES_NOT_EXIST;
		log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), log_fp);
		return error;
	}
	for (i = 0; i < child_count; i++) {
		child = Accessible_getChildAtIndex (object, i);
		if (!child)
			continue;
		name = Accessible_getName (child);
		if (name) {
			if (g_ascii_strcasecmp (name, "") == 0) {
				SPI_freeString (name);
				Accessible_unref (child);
				continue;
			}
			if (*data == NULL)
				*data = g_strdup (name);
			else {
				char *tmp;
				tmp = g_strdup_printf ("%s;%s", *data, name);
				g_free (*data);
				*data = tmp;
			}
			g_print ("Child menu item: %s", name);
			log_msg (LDTP_LOG_INFO, name, log_fp);
			SPI_freeString (name);
		}
		Accessible_unref (child);
	}
	return LDTP_ERROR_SUCCESS;
}

static LDTPErrorCode
select_popup_menu (LDTPClientContext* cctxt)
{
	long i;
	long count = 0;
	char *name = NULL;
	char *menu_item_name = cctxt->req->arg_list->data;

	LDTPErrorCode error;
	Accessible *pop_menu = NULL;
	Accessible *menu = NULL;
	Accessible *menu_item = NULL;

	count = Accessible_getChildCount (cctxt->app_handle);
	pop_menu = Accessible_getChildAtIndex (cctxt->app_handle, count - 1);

	if (Accessible_getChildCount (pop_menu) == 1) {
		menu = Accessible_getChildAtIndex (pop_menu, 0);
		if (Accessible_getRole (menu) == SPI_ROLE_MENU) {
			count = Accessible_getChildCount (menu);
			Accessible_unref (pop_menu);
			for (i = 0; i < count; i++) {
				menu_item = Accessible_getChildAtIndex (menu, i);
				name = Accessible_getName (menu_item);
				g_print ("ITEM NAME: %s\n", name);
				if (name && g_utf8_collate (name, menu_item_name) == 0) {
					Accessible *object = cctxt->gui_handle->handle;
					cctxt->gui_handle->handle = menu_item;
					error = menu_item_main (cctxt, LDTP_CMD_SELECTMENUITEM);
					SPI_freeString (name);
					Accessible_unref (menu_item);
					Accessible_unref (menu);
					cctxt->gui_handle->handle = object;
					return error;
				}
				Accessible_unref (menu_item);
				SPI_freeString (name);
			}
			error = LDTP_ERROR_MENU_ITEM_DOES_NOT_EXIST;
			log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), cctxt->log_fp);
		} else {
			error = LDTP_ERROR_UNABLE_TO_GET_MENU_HANDLE;
			log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), cctxt->log_fp);
			Accessible_unref (pop_menu);
		}
		Accessible_unref (menu);
		return error;
	} else {
		error = LDTP_ERROR_UNABLE_TO_FIND_POPUP_MENU;
		log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), cctxt->log_fp);
	}
	Accessible_unref (pop_menu);
	return error;
}

LDTPErrorCode
menu_main (LDTPClientContext* cctxt, int command, char *window_name)
{
	LDTPErrorCode error;
	char msg [256];
	gchar *role_name;

	switch (command) {
	case LDTP_CMD_CLICK:
	case LDTP_CMD_SELECTMENUITEM:
	case LDTP_CMD_MENUITEMENABLED:
	case LDTP_CMD_MENUCHECK:
	case LDTP_CMD_MENUUNCHECK:
	case LDTP_CMD_VERIFYMENUCHECK:
	case LDTP_CMD_VERIFYMENUUNCHECK:
	case LDTP_CMD_DOESMENUITEMEXIST: {
		AccessibleRole class;
		LDTPRequest* ldtp_req = NULL;
		LDTPGuiHandle *accessible = NULL;
		Accessible *object = cctxt->gui_handle->handle;

		if (command == LDTP_CMD_CLICK) {
			click_menu_item (object);
			return LDTP_ERROR_SUCCESS;
		}
		// AC: BUG FIX, if incorrect value of selectmenuitem ('dlg0', 'mnu0')
		if (!cctxt->req->arg_list)
			return LDTP_ERROR_ARGUMENT_NULL;
		// Invoke menu
		select_menu_item (cctxt->gui_handle->handle,
				  cctxt->req->arg_list->data);

		if (cctxt && cctxt->req && cctxt->req->component)
			g_print ("DEBUG: %s - %d - %s\n", __FILE__, __LINE__,
				 cctxt->req->component);

		ldtp_req = cctxt->req;
		//params = mnuFile;mnuNew
		if (cctxt->req && cctxt->req->arg_list &&
		    cctxt->req->arg_list->data) {
			gchar **token = NULL;
			// If 1 is specified to 3rd arg, the result is not correct
			token = g_strsplit (ldtp_req->arg_list->data, ";", 2);
			gchar *tmp = token [1];
			ldtp_req->arg_list = g_slist_remove_all (ldtp_req->arg_list,
								 ldtp_req->arg_list->data);
			if (tmp) {
				g_print ("Tmp: %s\n", tmp);
				ldtp_req->arg_list = g_slist_append (ldtp_req->arg_list, 
								     g_strdup (tmp));
			}
			if (cctxt->parent_name)
				g_free (cctxt->parent_name);
			cctxt->parent_name = ldtp_req->component;
			g_print ("Token: %s - %s\n", token [0], cctxt->parent_name);
			ldtp_req->component = g_strdup (token [0]);
			g_strfreev (token);
			if (ldtp_req->component)
				accessible = ldtp_gui_get_gui_handle (cctxt, &error);
		} else {
			g_sprintf (msg, "Unable to get gui handle %s %d",
				   __FILE__, __LINE__);
			g_print ("%s\n", msg);
			log_msg (LDTP_LOG_CAUSE, msg, cctxt->log_fp);
			return LDTP_ERROR_UNABLE_TO_GET_CHILD_MENU_ITEM;
		}
					
		if (!accessible) {
			if (cctxt->req->arg_list && cctxt->req->arg_list->data)
				g_sprintf (msg, "Unable to get gui handle %s %s %d",
					   (char *)cctxt->req->arg_list->data,
					   __FILE__, __LINE__);
			else
				g_sprintf (msg, "Unable to get gui handle %s %d",
					   __FILE__, __LINE__);
			g_print ("%s\n", msg);
			log_msg (LDTP_LOG_CAUSE, msg, cctxt->log_fp);
			return LDTP_ERROR_UNABLE_TO_GET_CHILD_MENU_ITEM;
		}
		class = Accessible_getRole (accessible->handle);
		role_name = Accessible_getRoleName (accessible->handle);
		if (role_name) {
			g_print ("Role: %s\n", role_name);
			SPI_freeString (role_name);
		}
		cctxt->gui_handle->handle = accessible->handle;
		if (ldtp_req->arg_list && g_slist_length (ldtp_req->arg_list) == 0 &&
		    class == SPI_ROLE_MENU && command == LDTP_CMD_MENUITEMENABLED)
			error = is_menu_item_enabled (cctxt->gui_handle->handle, cctxt->log_fp);
		else if (class == SPI_ROLE_MENU)
			error = menu_main (cctxt, command, window_name);
		else if ((command == LDTP_CMD_DOESMENUITEMEXIST) && class == SPI_ROLE_MENU_ITEM) {
			if (accessible)
				error = LDTP_ERROR_SUCCESS;
			else
				error = LDTP_ERROR_MENU_ITEM_DOES_NOT_EXIST;
		}
		else if (class == SPI_ROLE_MENU_ITEM)
			error = menu_item_main (cctxt, command);
		else if (class == SPI_ROLE_RADIO_MENU_ITEM)
			error = radio_menu_item_main (cctxt, command);
		else if (class == SPI_ROLE_CHECK_MENU_ITEM)
			error = check_menu_item_main (cctxt, command);
		Accessible_unref (accessible->handle);
		g_free (accessible);
		cctxt->gui_handle->handle = object;
		return error;
	}
	case LDTP_CMD_LISTSUBMENUS:
		cctxt->resp->data = NULL;
		cctxt->resp->data_len = 0;
		error = list_child_menu_items (cctxt->gui_handle->handle,
					       &cctxt->resp->data, cctxt->log_fp);
		if (error == LDTP_ERROR_SUCCESS && cctxt->resp->data)
			cctxt->resp->data_len = g_utf8_strlen (cctxt->resp->data, -1);
		if (cctxt->resp->data)
			g_print ("\nSubmenus: %s - %ld\n", cctxt->resp->data, cctxt->resp->data_len);
		break;
        case LDTP_CMD_GRABFOCUS:
                error = grab_focus (cctxt->gui_handle->handle, cctxt->log_fp);
                break;
	case LDTP_CMD_SELECTPOPUPMENU:
		error = select_popup_menu (cctxt);
		break;
	default:
		error = LDTP_ERROR_COMMAND_NOT_IMPLEMENTED;
	}
	return error;
}