/*
* Copyright (c) 2005 X.Org Foundation L.L.C.
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* 
* Copyright (c) Applied Testing and Technology, Inc. 1995
* All Rights Reserved.
* 
* 
* Portions of this software are based on Xlib and X Protocol Test Suite.
* We have used this material under the terms of its copyright, which grants
* free use, subject to the conditions below.  Note however that those
* portions of this software that are based on the original Test Suite have
* been significantly revised and that all such revisions are copyright (c)
* 1995 Applied Testing and Technology, Inc.  Insomuch as the proprietary
* revisions cannot be separated from the freely copyable material, the net
* result is that use of this software is governed by the ApTest copyright.
* 
* Copyright (c) 1990, 1991  X Consortium
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
* X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* 
* Except as contained in this notice, the name of the X Consortium shall not be
* used in advertising or otherwise to promote the sale, use or other dealings
* in this Software without prior written authorization from the X Consortium.
* 
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of UniSoft not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.  UniSoft
* makes no representations about the suitability of this software for any
* purpose.  It is provided "as is" without express or implied warranty.
* 
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	"xtest.h"
#include	"X11/Xlib.h"
#include	"X11/Xutil.h"
#include	"X11/Xresource.h"
#include	"X11/keysym.h"
#include	"tet_api.h"
#include	"xtestlib.h"
#include	"pixval.h"
#ifdef INPUTEXTENSION
#include        "X11/extensions/XInput.h"
#include        "XItest.h"
#endif

extern	Display	*Dsp;
extern	Window	Win;

extern	Window	ErrdefWindow;
extern	Drawable ErrdefDrawable;
extern	GC		ErrdefGC;
extern	Colormap ErrdefColormap;
extern	Pixmap	ErrdefPixmap;
extern	Atom	ErrdefAtom;
extern	Cursor	ErrdefCursor;
extern	Font	ErrdefFont;


#define T_GravityNotify	1
char    *TestName = "GravityNotify";



#define	EVENT		GravityNotify
#define	MASK		StructureNotifyMask
#define	MASKP		SubstructureNotifyMask

int 	tet_thistest;

static void t001(){

Display	*display = Dsp;
Display	*client2;
Display	*client3;
Winh	*parent, *eventw;
XSetWindowAttributes attrs;
Winhg	winhg;
int	status;
XEvent	event;
int 	pass = 0, fail = 0;

 	report_purpose(1);

	report_assertion("Assertion GravityNotify-1.(A)");
	report_assertion("When a GravityNotify event is generated, then all clients");
	report_assertion("having set StructureNotifyMask event mask bits on the moved");
	report_assertion("window are delivered a GravityNotify event.");

	report_strategy("Create clients client2 and client3.");
	report_strategy("Build and create window hierarchy with child having");
	report_strategy("win-gravity SouthEastGravity.");
	report_strategy("Select for GravityNotify events using StructureNotifyMask.");
	report_strategy("Select for GravityNotify events using StructureNotifyMask with client2.");
	report_strategy("Select for no events with client3.");
	report_strategy("Generate GravityNotify event.");
	report_strategy("Verify that a GravityNotify event is delivered.");
	report_strategy("Verify that a GravityNotify event is delivered to client2.");
	report_strategy("Verify that no events are delivered to client3.");
	report_strategy("Verify delivered event structure members.");

	tpstartup();
/* Create clients client2 and client3. */
	if ((client2 = opendisplay()) == (Display *) NULL) {
		delete("Couldn't create client2.");
		return;
	}
	else
		CHECK;
	if ((client3 = opendisplay()) == (Display *) NULL) {
		delete("Couldn't create client3.");
		return;
	}
	else
		CHECK;
/* Build and create window hierarchy with child having */
/* win-gravity SouthEastGravity. */
	/* can't use winh() because the windows need to overlap */
	winhg.border_width = 1;
	winhg.area.x = 1;
	winhg.area.y = 1;
	winhg.area.width = 100;
	winhg.area.height = 100;
	parent = winh_adopt(display, (Winh *) NULL, 0L, (XSetWindowAttributes *) NULL, &winhg, WINH_NOMASK);
	if (parent == (Winh *) NULL) {
		report("Could not create parent");
		return;
	}
	else
		CHECK;
	winhg.area.x = 1;
	winhg.area.y = 1;
	winhg.area.width /= 2;
	winhg.area.height /= 2;
	attrs.win_gravity = SouthEastGravity;
	eventw = winh_adopt(display, parent, CWWinGravity, &attrs, &winhg, WINH_NOMASK);
	if (eventw == (Winh *) NULL) {
		report("Could not create child");
		return;
	}
	else
		CHECK;
	if (winh_create(display, (Winh *) NULL, WINH_MAP))
		return;
	else
		CHECK;
/* Select for GravityNotify events using StructureNotifyMask. */
	if (winh_selectinput(display, (Winh *) NULL, MASK)) {
		report("Selection with first client failed.");
		return;
	}
	else
		CHECK;
/* Select for GravityNotify events using StructureNotifyMask with client2. */
	if (winh_selectinput(client2, (Winh *) NULL, MASK)) {
		report("Selection with client2 failed.");
		return;
	}
	else
		CHECK;
/* Select for no events with client3. */
	if (winh_selectinput(client3, (Winh *) NULL, NoEventMask)) {
		report("Selection with client3 failed.");
		return;
	}
	else
		CHECK;
/* Generate GravityNotify event. */
	XSync(display, True);
	XSync(client2, True);
	XSync(client3, True);
	XResizeWindow(display, parent->window, ++(parent->winhg.area.width), ++(parent->winhg.area.height));
	event.xany.type = EVENT;
	event.xany.window = eventw->window;
	if (winh_plant(eventw, &event, MASK, WINH_NOMASK)) {
		report("Could not plant events.");
		return;
	}
	else
		CHECK;
	XSync(display, False);
	XSync(client2, False);
	XSync(client3, False);
/* Verify that a GravityNotify event is delivered. */
/* Verify that a GravityNotify event is delivered to client2. */
/* Verify that no events are delivered to client3. */
	if (winh_harvest(display, (Winh *) NULL)) {
		report("Could not harvest events for display");
		return;
	}
	else
		CHECK;
	if (winh_harvest(client2, (Winh *) NULL)) {
		report("Could not harvest events for client2");
		return;
	}
	else
		CHECK;
	if (winh_harvest(client3, (Winh *) NULL)) {
		report("Could not harvest events for client3");
		return;
	}
	else
		CHECK;
	/* not interested in resulting ConfigureNotify events */
	if (winh_ignore_event((Winh *) NULL, ConfigureNotify, WINH_NOMASK))
		return;
	else
		CHECK;
	status = winh_weed((Winh *) NULL, -1, WINH_WEED_IDENTITY);
	if (status == -1)
		return;
	else if (status) {
		report("Event delivery not as expected");
		FAIL;
	}
	else {
		XGravityEvent	good;
		Winhe	*winhe;

/* Verify delivered event structure members. */
		/* ignore non-GravityNotify events */
		for (winhe=winh_qdel; winhe!=(Winhe *) NULL; winhe=winhe->next){
			if (winhe->event->type == EVENT) {
				CHECK;
				break;
			}
		}
		if (winhe == (Winhe *) NULL) {
			delete("Inconsistencies in delivered event queue");
			FAIL;
		}
		else
		{
			good = winhe->event->xgravity;
			good.type = EVENT;
			good.event = eventw->window;
			good.window = eventw->window;
			good.x = eventw->winhg.area.x + 1;
			good.y = eventw->winhg.area.y + 1;
			if (checkevent((XEvent *) &good, winhe->event)) {
				report("Unexpected event member values");
				FAIL;
			}
			else
				CHECK;
		}
	}

	CHECKPASS(15);
	tpcleanup();
	pfcount(pass, fail);
}

static void t002(){

Display	*display = Dsp;
Display	*client2;
Display	*client3;
Winh	*parent, *eventw;
XSetWindowAttributes attrs;
Winhg	winhg;
int	status;
XEvent	event;
int 	pass = 0, fail = 0;

 	report_purpose(2);

	report_assertion("Assertion GravityNotify-2.(A)");
	report_assertion("When a GravityNotify event is generated, then all clients");
	report_assertion("having set SubstructureNotifyMask event mask bits on the");
	report_assertion("parent of the moved window are delivered a GravityNotify");
	report_assertion("event.");

	report_strategy("Create clients client2 and client3.");
	report_strategy("Build and create window hierarchy with child having");
	report_strategy("win-gravity SouthEastGravity.");
	report_strategy("Select for GravityNotify events using SubstructureNotifyMask.");
	report_strategy("Select for GravityNotify events using SubstructureNotifyMask with client2.");
	report_strategy("Select for no events with client3.");
	report_strategy("Generate GravityNotify event.");
	report_strategy("Verify that a GravityNotify event is delivered.");
	report_strategy("Verify that a GravityNotify event is delivered to client2.");
	report_strategy("Verify that no events are delivered to client3.");
	report_strategy("Verify delivered event structure members.");

	tpstartup();
/* Create clients client2 and client3. */
	if ((client2 = opendisplay()) == (Display *) NULL) {
		delete("Couldn't create client2.");
		return;
	}
	else
		CHECK;
	if ((client3 = opendisplay()) == (Display *) NULL) {
		delete("Couldn't create client3.");
		return;
	}
	else
		CHECK;
/* Build and create window hierarchy with child having */
/* win-gravity SouthEastGravity. */
	/* can't use winh() because the windows need to overlap */
	winhg.border_width = 1;
	winhg.area.x = 1;
	winhg.area.y = 1;
	winhg.area.width = 100;
	winhg.area.height = 100;
	parent = winh_adopt(display, (Winh *) NULL, 0L, (XSetWindowAttributes *) NULL, &winhg, WINH_NOMASK);
	if (parent == (Winh *) NULL) {
		report("Could not create parent");
		return;
	}
	else
		CHECK;
	winhg.area.x = 1;
	winhg.area.y = 1;
	winhg.area.width /= 2;
	winhg.area.height /= 2;
	attrs.win_gravity = SouthEastGravity;
	eventw = winh_adopt(display, parent, CWWinGravity, &attrs, &winhg, WINH_NOMASK);
	if (eventw == (Winh *) NULL) {
		report("Could not create child");
		return;
	}
	else
		CHECK;
	if (winh_create(display, (Winh *) NULL, WINH_MAP))
		return;
	else
		CHECK;
/* Select for GravityNotify events using SubstructureNotifyMask. */
	if (winh_selectinput(display, (Winh *) NULL, MASKP)) {
		report("Selection with first client failed.");
		return;
	}
	else
		CHECK;
/* Select for GravityNotify events using SubstructureNotifyMask with client2. */
	if (winh_selectinput(client2, (Winh *) NULL, MASKP)) {
		report("Selection with client2 failed.");
		return;
	}
	else
		CHECK;
/* Select for no events with client3. */
	if (winh_selectinput(client3, (Winh *) NULL, NoEventMask)) {
		report("Selection with client3 failed.");
		return;
	}
	else
		CHECK;
/* Generate GravityNotify event. */
	XSync(display, True);
	XSync(client2, True);
	XSync(client3, True);
	XResizeWindow(display, parent->window, ++(parent->winhg.area.width), ++(parent->winhg.area.height));
	event.xany.type = EVENT;
	event.xany.window = parent->window;
	if (winh_plant(parent, &event, MASKP, WINH_NOMASK)) {
		report("Could not plant events");
		return;
	}
	else
		CHECK;
	XSync(display, False);
	XSync(client2, False);
	XSync(client3, False);
/* Verify that a GravityNotify event is delivered. */
/* Verify that a GravityNotify event is delivered to client2. */
/* Verify that no events are delivered to client3. */
	if (winh_harvest(display, (Winh *) NULL)) {
		report("Could not harvest events for display");
		return;
	}
	else
		CHECK;
	if (winh_harvest(client2, (Winh *) NULL)) {
		report("Could not harvest events for client2");
		return;
	}
	else
		CHECK;
	if (winh_harvest(client3, (Winh *) NULL)) {
		report("Could not harvest events for client3");
		return;
	}
	else
		CHECK;
	/* not interested in resulting ConfigureNotify events */
	if (winh_ignore_event((Winh *) NULL, ConfigureNotify, WINH_NOMASK))
		return;
	else
		CHECK;
	status = winh_weed((Winh *) NULL, -1, WINH_WEED_IDENTITY);
	if (status == -1)
		return;
	else if (status) {
		report("Event delivery not as expected");
		FAIL;
	}
	else {
		XGravityEvent	good;
		Winhe	*winhe;

/* Verify delivered event structure members. */
		/* ignore non-GravityNotify events */
		for (winhe=winh_qdel; winhe!=(Winhe *) NULL; winhe=winhe->next){
			if (winhe->event->type == EVENT) {
				CHECK;
				break;
			}
		}
		if (winhe == (Winhe *) NULL) {
			delete("Inconsistencies in delivered event queue");
			FAIL;
		}
		else
		{
			good = winhe->event->xgravity;
			good.type = EVENT;
			good.event = parent->window;
			good.window = eventw->window;
			good.x = eventw->winhg.area.x + 1;
			good.y = eventw->winhg.area.y + 1;
			if (checkevent((XEvent *) &good, winhe->event)) {
				report("Unexpected event member values");
				FAIL;
			}
			else
				CHECK;
		}
	}

	CHECKPASS(15);
	tpcleanup();
	pfcount(pass, fail);
}

static void t003(){


 	report_purpose(3);

	report_assertion("Assertion GravityNotify-3.(-)");
	report_assertion("When a GravityNotify event is generated, then clients not");
	report_assertion("having set StructureNotifyMask event mask bits on the moved");
	report_assertion("window and also not having set SubstructureNotifyMask event");
	report_assertion("mask bits on the parent of the moved window are not");
	report_assertion("delivered a GravityNotify event.");
	report_assertion("The assertion is descriptive or is tested elsewhere.");
	tet_result(TET_NOTINUSE);
}

/* End of Test Cases */


struct tet_testlist tet_testlist[] = {
	{ t001, 1 },
	{ t002, 2 },
	{ t003, 3 },
	{ NULL, 0 }
};

int 	ntests = sizeof(tet_testlist)/sizeof(struct tet_testlist)-1;

void	(*tet_startup)() = startup;
void	(*tet_cleanup)() = cleanup;
