summaryrefslogtreecommitdiff
path: root/src/check-box.c
blob: c1abbfd1dd8e6fc0595be4122dc2867f0d6a2afc (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Linux Desktop Testing Project http://ldtp.freedesktop.org
 *
 * Author:
 *    Nagappan Alagappan <nagappan@gmail.com>
 *    Poornima <pnayak@novell.com>
 *
 * Copyright 2004 - 2006 Novell, Inc.
 * Copyright 2007 - 2008 Nagappan Alagappan
 *
 * 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"

/*
  Get the check box state of the given object
*/
static gboolean
get_check_box_state (Accessible *object)
{
	AccessibleStateSet *state;
	
	state = Accessible_getStateSet (object);

	/*
	  Check, if check box state is already checked
	*/
	if (AccessibleStateSet_contains (state, SPI_STATE_CHECKED))
		return TRUE; // Checked state
	else
		return FALSE; // Unchecked state
}

static gboolean
is_check_box_enabled (Accessible *object)
{
	AccessibleStateSet *state;

	if (!object)
		return FALSE;
	if (wait_till_object_state_contains (object, CHECK_BOX, NULL) == -1) {
		LDTPErrorCode error;
		error = LDTP_ERROR_INVALID_OBJECT_STATE;
		log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), NULL);
		return FALSE;
	}
	state = Accessible_getStateSet (object);

	/* Verifies it is enabled or not */
	if (AccessibleStateSet_contains (state, SPI_STATE_ENABLED) == TRUE)
		return TRUE;
	else
		return FALSE;
}

static LDTPErrorCode
check_check_box (Accessible *object, FILE *log_fp)
{
	LDTPErrorCode error;
	AccessibleAction *action;

	if (wait_till_object_state_contains (object, CHECK_BOX, log_fp) != 0) {
		error = LDTP_ERROR_INVALID_OBJECT_STATE;
		goto error;
	}
	/*
	  Pre-condition
	  If state already selected then unselect ?
	*/
	if (get_check_box_state (object) == FALSE) {
		SPIBoolean flag;
		action = Accessible_getAction (object);
		flag = AccessibleAction_doAction (action, 0);
		Accessible_unref (action);
		if (flag)
			return LDTP_ERROR_SUCCESS;
		else {
			error = LDTP_ERROR_CHECK_ACTION_FAILED;
			goto error;
		}
	}
	else 
		log_msg (LDTP_LOG_WARNING, "Check box is already checked", log_fp);
	return LDTP_ERROR_SUCCESS;
 error:
	log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), log_fp);
	return error;
}

static LDTPErrorCode
uncheck_check_box (Accessible *object, FILE *log_fp)
{
	LDTPErrorCode error;
	AccessibleAction *action;

	if (wait_till_object_state_contains (object, CHECK_BOX, log_fp) != 0) {
		error = LDTP_ERROR_INVALID_OBJECT_STATE;
		goto error;
	}
	/*
	  Pre-condition
	  If state is checked then only uncheck it 
	*/
	if (get_check_box_state (object) == TRUE) {
		SPIBoolean flag;
		action = Accessible_getAction (object);
		flag = AccessibleAction_doAction (action, 0);
		Accessible_unref (action);
		if (flag)
			return LDTP_ERROR_SUCCESS;
		else {
			error = LDTP_ERROR_UNCHECK_ACTION_FAILED;
			goto error;
		}
	}
	else
		log_msg (LDTP_LOG_WARNING, "Check box state already unchecked", log_fp);
	return LDTP_ERROR_SUCCESS;
 error:
	log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), log_fp);
	return error;
}

static LDTPErrorCode
click (Accessible *object, FILE *log_fp)
{
	LDTPErrorCode error;
	SPIBoolean flag = FALSE;
	AccessibleAction *action;
	
	if (wait_till_object_state_contains (object, CHECK_BOX, log_fp) != 0) {
		error = LDTP_ERROR_INVALID_OBJECT_STATE;
		log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), log_fp);
		return error;
	}

	action = Accessible_getAction (object);
	flag = AccessibleAction_doAction (action, 0);
	Accessible_unref (action);

	if (flag)
		return LDTP_ERROR_SUCCESS;
	else
		return LDTP_ERROR_CLICK_FAILED;
}
	
static LDTPErrorCode
verify_check_check_box (Accessible *object, FILE *log_fp)
{
	LDTPErrorCode error;
	if (wait_till_object_state_contains (object, CHECK_BOX, log_fp) != 0) {
		error = LDTP_ERROR_INVALID_OBJECT_STATE;
		log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), log_fp);
		return error;
	}
	if (get_check_box_state (object) == FALSE)
		return LDTP_ERROR_STATE_UNCHECKED;
	else
		return LDTP_ERROR_SUCCESS;
}

static LDTPErrorCode
verify_uncheck_check_box (Accessible *object, FILE *log_fp)
{
	LDTPErrorCode error;
	if (wait_till_object_state_contains (object, CHECK_BOX, log_fp) != 0) {
		error = LDTP_ERROR_INVALID_OBJECT_STATE;
		log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (error), log_fp);
		return error;
	}
	if (get_check_box_state (object) == TRUE)
		return LDTP_ERROR_STATE_CHECKED;
	else
		return LDTP_ERROR_SUCCESS;
}

LDTPErrorCode
check_box_main (LDTPClientContext* cctxt, int command)
{
	LDTPErrorCode error;
	switch (command) {
	case LDTP_CMD_CHECK:
		error = check_check_box (cctxt->gui_handle->handle, cctxt->log_fp);
		break;
	case LDTP_CMD_UNCHECK:
		error = uncheck_check_box (cctxt->gui_handle->handle, cctxt->log_fp);
		break;
        case LDTP_CMD_GRABFOCUS:
                error = grab_focus (cctxt->gui_handle->handle, cctxt->log_fp);
                break;
	case LDTP_CMD_STATEENABLED:
		if (is_check_box_enabled (cctxt->gui_handle->handle))
			error = LDTP_ERROR_SUCCESS;
		else
			error = LDTP_ERROR_CHECK_BOX_STATE_NOT_ENABLED;
		break;
	case LDTP_CMD_CLICK:
		error = click (cctxt->gui_handle->handle, cctxt->log_fp);
		break;
	case LDTP_CMD_VERIFYCHECK:
		error = verify_check_check_box (cctxt->gui_handle->handle, cctxt->log_fp);
		break;
	case LDTP_CMD_VERIFYUNCHECK:
		error = verify_uncheck_check_box (cctxt->gui_handle->handle, cctxt->log_fp);
		break;
	case LDTP_CMD_GETOBJECTSIZE:
		cctxt->resp->data_len = 0;
		cctxt->resp->data = get_size (cctxt->gui_handle->handle, &error);
		if (cctxt->resp->data) {
			cctxt->resp->data_len = g_utf8_strlen (cctxt->resp->data, -1);
		}
		break;
	default:
		error = LDTP_ERROR_COMMAND_NOT_IMPLEMENTED;
	}
	return error;
}