/* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */

group "doc.createsearchurl";
require init;

language C++;

include "modules/doc/frm_doc.h";
include "modules/doc/documentinteractioncontext.h";

html
{
  "<html>"
    "<body>"
      "<form name='f' action='http://example.org/search' accept-charset='utf-8'>"
        "<input type='hidden' name='hidden1' value='value1'>"
        "<input name='text' value='value2' id='text'>"
        "<input type='hidden' name='hidden2' value='value3'>"
      "</form>"
    "</body>"
  "</html>"
}

test("FramesDocument::CreateSearchURL() #1")
{
  HTML_Element *text = find_element("input", 2);
  verify(text);

  OpPoint dummy;
  DocumentInteractionContext ctx(state.doc, dummy, state.doc->GetVisualDevice()->GetOpView(), state.doc->GetWindow()->GetOpWindow(), text);

  OpString8 url, query, charset;
  BOOL is_post;

  OP_STATUS status = FramesDocument::CreateSearchURL(ctx, url, query, charset, is_post);

  verify_success(status);
  verify_string("http://example.org/search?hidden1=value1&text=%s&hidden2=value3", url);
  verify_string("hidden1=value1&text=%s&hidden2=value3", query);
  verify("utf-8", charset);
  verify(is_post == FALSE);
}

test("FramesDocument::CreateSearchURL() #1b")
{
  HTML_Element *text = find_element("input", 1);
  verify(text);

  OpPoint dummy;
  DocumentInteractionContext ctx(state.doc, dummy, state.doc->GetVisualDevice()->GetOpView(), state.doc->GetWindow()->GetOpWindow(), text);

  OpString8 url, query, charset;
  BOOL is_post;

  OP_STATUS status = FramesDocument::CreateSearchURL(ctx, url, query, charset, is_post);

  verify_success(status);
  verify_string("http://example.org/search?hidden1=%s&text=value2&hidden2=value3", url);
  verify_string("hidden1=%s&text=value2&hidden2=value3", query);
  verify("utf-8", charset);
  verify(is_post == FALSE);
}

html
{
  "<html>"
    "<body>"
      "<form name='f' method='post' action='http://example.org/search' accept-charset='utf-8'>"
        "<input type='hidden' name='hidden1' value='value1'>"
        "<input name='text' value='value2' id='text'>"
        "<input type='hidden' name='hidden2' value='value3'>"
      "</form>"
    "</body>"
  "</html>"
}

test("FramesDocument::CreateSearchURL() #2")
{
  HTML_Element *text = find_element("input", 2);
  verify(text);

  OpPoint dummy;
  DocumentInteractionContext ctx(state.doc, dummy, state.doc->GetVisualDevice()->GetOpView(), state.doc->GetWindow()->GetOpWindow(), text);

  OpString8 url, query, charset;
  BOOL is_post;

  OP_STATUS status = FramesDocument::CreateSearchURL(ctx, url, query, charset, is_post);

  verify_success(status);
  verify_string("http://example.org/search", url);
  verify_string("hidden1=value1&text=%s&hidden2=value3", query);
  verify("utf-8", charset);
  verify(is_post == TRUE);
}

html
{
  "<html>"
    "<body>"
      "<form name='f' method='post' action='http://example.org/search' enctype='multipart/form-data' accept-charset='utf-8'>"
        "<input type='hidden' name='hidden1' value='value1'>"
        "<input name='text' value='value2' id='text'>"
        "<input type='hidden' name='hidden2' value='value3'>"
      "</form>"
    "</body>"
  "</html>"
}

test("FramesDocument::CreateSearchURL() #2")
{
  HTML_Element *text = find_element("input", 2);
  verify(text);

  OpPoint dummy;
  DocumentInteractionContext ctx(state.doc, dummy, state.doc->GetVisualDevice()->GetOpView(), state.doc->GetWindow()->GetOpWindow(), text);

  OpString8 url, query, charset;
  BOOL is_post;

  OP_STATUS status = FramesDocument::CreateSearchURL(ctx, url, query, charset, is_post);

  verify_status(OpStatus::ERR, status);
}
