/* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
group "doc.scrolltosavedelement";
require init;

language C++;

include "modules/doc/frm_doc.h";
include "modules/doc/html_doc.h";
include "modules/logdoc/htm_elm.h";

html
{
	//!	<html><body id="b">
	//! <div style="position:relative;width:800px;height:1000px;">
	//! <div id="d1" style="position:absolute;top:200px;left:200px;width:200px;font-size:16px;line-height:16px;font-family:'AHEM'";>
	//! XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX</div>
	//! <div id="d2" style="position:absolute;top:350px;left:450px;width:200px;height:200px;font-size:16px;line-height:16px;font-family:'AHEM'";>
	//! <img src="" style="width:100%;height:100%;"></div>
	//! <div id="d3" style="position:absolute;top:350px;left:700px;width:200px;font-size:16px;line-height:16px;font-family:'AHEM'";>
	//! XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX</div>
	//! </div></body></html>
}

table Tests (OpPoint, HTML_ElementType, int, const uni_char*)
{
	{ OpPoint(208, 208), HE_TEXT, 1,  UNI_L("d1") }, // point at upper left of paragraph
	{ OpPoint(224, 224), HE_TEXT, 13, UNI_L("d1") }, // point a bit closer to the middle
	{ OpPoint(300, 190), HE_TEXT, 1,  UNI_L("d1") }, // choose paragraph below point
	{ OpPoint(150, 240), HE_TEXT, 1,  UNI_L("d1") }, // choose paragraph to the right of point
	{ OpPoint(450, 230), HE_TEXT, 1,  UNI_L("d1") }, // choose paragraph to the left of point
	{ OpPoint(300, 340), HE_TEXT, 25, UNI_L("d1") }, // choose paragraph above point
	{ OpPoint(360, 360), HE_IMG, -1,  UNI_L("d2") }, // prioritize paragraph above before to the right
	{ OpPoint(650, 360), HE_IMG, -1,  UNI_L("d2") }, // prioritize paragraph to the left before to the right
	{ OpPoint(50,  50),  HE_DIV, -1,  UNI_L("b")  }  // paragraphs too far away, select element under point
}

test("SaveScrollToElement")
{
	verify(state.doc);
	verify(state.doc->GetHtmlDocument());
	iterate (point, expected_elm_type, expected_text_offset, expected_parent_id) from Tests
	{
		HTML_Element* elm;
		int text_offset;
		state.doc->GetHtmlDocument()->SaveScrollToElement(point);
		state.doc->GetHtmlDocument()->GetSavedScrollToElement(&elm, text_offset);
		verify(elm && elm->Parent() && elm->Parent()->GetId());
		verify(elm->Type() == expected_elm_type);
		verify(text_offset == expected_text_offset);
		verify_string(elm->Parent()->GetId(), expected_parent_id);
	}
}

html
{
	//! <html></html>
}

test("SaveScrollToElement - don't use HE_DOC_ROOT")
{
	verify(state.doc);
	verify(state.doc->GetHtmlDocument());
	HTML_Element* elm;
	int text_offset;
	OpPoint p(300, 300);
	state.doc->GetHtmlDocument()->SaveScrollToElement(p);
	state.doc->GetHtmlDocument()->GetSavedScrollToElement(&elm, text_offset);
	verify(elm->Type() == HE_HTML);
	verify(text_offset == -1);
}
