/* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
**
** Copyright (C) 1995-2008 Opera Software AS.  All rights reserved.
**
** This file is part of the Opera web browser.
** It may not be distributed under any circumstances.
**
*/

group "dom.js.history";

language ecmascript;
require init;

test("History basics")
{
	verify("History" in window);

	verify(History.prototype);
	verify(History.prototype.constructor);
	verify(History.prototype.constructor === History);
	verify(History.prototype.constructor.prototype === History.prototype);
}

test("history object")
{
	verify("history" in window);
	verify(history instanceof History);

	verify("length" in history);
	verify("go" in history);
	verify("back" in history);
	verify("forward" in history);
	verify(!("current" in history));

	function is_readonly()
	{
		"use strict";
		try {
			window.history.length = "some value";
			verify(!"Expected an exception");
		} catch (ex) {
			verify(ex instanceof TypeError);
		}
	}
	is_readonly();
}

test ("html5 history extensions")
require ES_PERSISTENT_SUPPORT;
{
	verify("pushState" in history);
	verify("replaceState" in history);
	verify(typeof history.pushState == "function");
	verify(typeof history.replaceState == "function");
}

test("history.state")
require success "history object";
require success "html5 history extensions";
{
	verify("state" in window.history);
	window.history.replaceState({o:'string data'}, 'title');
	verify("o" in window.history.state);
	var obj = window.history.state;
	verify(obj.o === "string data");

	function is_readonly()
	{
		"use strict";

		try {
			window.history.state = 2;
			verify(!"Expected an exception");
		} catch (ex) {
			verify(ex instanceof TypeError);
		}
	}
	is_readonly();
}

html
{
	//! <body>
	//! <script> popstate_counter = 0; timeout_id = 0; </script>
	//! <iframe src="data:text/html,
	//! <head><script>
	//! function HandlePopState(e){
	//! ++top.popstate_counter;
	//! if (top.popstate_counter == 1)
	//! {
	//!     top.clearTimeout(top.timeout_id);
	//!     if (!('state' in e) || !('o' in e.state) || e.state.o !== 1)
	//!         top.ST_failed('no expected state in the popstate event', '', '');
	//!     else
	//!         top.ST_passed();
	//! }}
	//! </script></head><body onpopstate='HandlePopState(event);'></body>"></iframe>
	//! </body>
}

test("html5 history extensions functional - pushState")
require ES_PERSISTENT_SUPPORT;
async;
{
	var iframe = document.getElementsByTagName('iframe')[0];
	if (!iframe)
		ST_failed("iframe is not present which is not expected", "", "");

	if (iframe.contentWindow)
		iframe = iframe.contentWindow;
	else
		ST_failed("iframe's window is not present which is not expected", "", "");

	timeout_id = setTimeout(function() { ST_failed("not all expected popstate evenets were received", "", ""); }, 5000);
	iframe.history.pushState({o:1}, 'title');
	iframe.location.hash='#test';
	history.back();
}

test("html5 history extensions functional - replaceState")
require ES_PERSISTENT_SUPPORT;
async;
{
	var iframe = document.getElementsByTagName('iframe')[0];
	if (!iframe)
		ST_failed("iframe is not present which is not expected", "", "");

	if (iframe.contentWindow)
		iframe = iframe.contentWindow;
	else
		ST_failed("iframe's window is not present which is not expected", "", "");

	popstate_counter = 0;
	timeout_id = setTimeout(function() { ST_failed("not all expected popstate evenets were received", "", ""); }, 5000);
	iframe.history.pushState({o:2}, 'title');
	iframe.history.replaceState({o:1}, 'title');
	iframe.location.hash='#test';
	history.back();
}
