#include "ui_core/layout_xml.h"
#include "test_harness.h"

using pp::foundation::StatusCode;
using pp::ui::parse_layout_xml;

namespace {

void parses_nested_layout_xml_and_lengths(pp::tests::Harness& h)
{
    constexpr auto xml =
        "<layout width=\"100%\" height=\"auto\">"
        "  <panel width=\"320\" height=\"200\">"
        "    <button width=\"64\" height=\"28\"/>"
        "  </panel>"
        "</layout>";

    const auto summary = parse_layout_xml(xml);

    PP_EXPECT(h, summary.ok());
    PP_EXPECT(h, summary.value().node_count == 3U);
    PP_EXPECT(h, summary.value().length_attribute_count == 6U);
}

void rejects_malformed_empty_and_invalid_lengths(pp::tests::Harness& h)
{
    const auto empty = parse_layout_xml("");
    const auto malformed = parse_layout_xml("<layout><panel></layout>");
    const auto invalid_length = parse_layout_xml("<layout width=\"101%\"/>");

    PP_EXPECT(h, !empty.ok());
    PP_EXPECT(h, empty.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !malformed.ok());
    PP_EXPECT(h, malformed.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !invalid_length.ok());
    PP_EXPECT(h, invalid_length.status().code == StatusCode::out_of_range);
}

}

int main()
{
    pp::tests::Harness harness;
    harness.run("parses_nested_layout_xml_and_lengths", parses_nested_layout_xml_and_lengths);
    harness.run("rejects_malformed_empty_and_invalid_lengths", rejects_malformed_empty_and_invalid_lengths);
    return harness.finish();
}
