diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2015-07-31 23:18:40 +0100 |
---|---|---|
committer | Michael Drake <tlsa@netsurf-browser.org> | 2015-07-31 23:18:40 +0100 |
commit | f9e63b6e91ea15cc2e93f586b1b82ae6d464d658 (patch) | |
tree | badcbc8dc301b32b67d183bcba17999d59b35fad | |
parent | 0367977ad546bd82a4c5c6bb747f913e4c40e00a (diff) | |
download | libnslayout-f9e63b6e91ea15cc2e93f586b1b82ae6d464d658.tar.gz libnslayout-f9e63b6e91ea15cc2e93f586b1b82ae6d464d658.tar.bz2 |
Test: not getting mutation events out of listener.
-rw-r--r-- | src/layout.c | 56 | ||||
-rw-r--r-- | src/layout.h | 6 | ||||
-rw-r--r-- | test/main.c | 22 | ||||
-rw-r--r-- | test/test-loader.c | 298 | ||||
-rw-r--r-- | test/test-writing-mode.html | 40 |
5 files changed, 420 insertions, 2 deletions
diff --git a/src/layout.c b/src/layout.c index b5d74c2..a29c8a6 100644 --- a/src/layout.c +++ b/src/layout.c @@ -10,10 +10,47 @@ #include <assert.h> #include <stdlib.h> +#include <stdio.h> #include "layout.h" +static void nsl__event_handler(struct dom_event *evt, void *pw) +{ + dom_event_target *node; + dom_node_type type; + dom_string *name, *evt_type; + dom_exception exc; + + UNUSED(pw); + + printf("Some kind of event!\n"); + + /* Ugly test to see what events come out */ + exc = dom_event_get_target(evt, &node); + if ((exc == DOM_NO_ERR) && (node != NULL)) { + exc = dom_node_get_node_type(node, &type); + if ((exc == DOM_NO_ERR) && (type == DOM_ELEMENT_NODE)) { + /* an element node has an event */ + exc = dom_node_get_node_name(node, &name); + if ((exc == DOM_NO_ERR) && (name != NULL)) { + + exc = _dom_event_get_type(evt, &evt_type); + if ((exc == DOM_NO_ERR) && (evt_type != NULL)) { + /* Print the event */ + printf("DOM Event: <%s>: %s\n", + dom_string_data(name), + dom_string_data(evt_type)); + dom_string_unref(evt_type); + } + + dom_string_unref(name); + } + } + dom_node_unref(node); + } +} + /* Publically exported function, documented in include/libnslayout/nslayout.h */ nslayout_error nslayout_layout_create( dom_document *doc, @@ -23,13 +60,16 @@ nslayout_error nslayout_layout_create( void *pw, nslayout_layout **layout) { - nslayout_layout *l; + nslayout_layout *l = NULL; + dom_exception exc; assert(doc != NULL); assert(css_ctx != NULL); assert(media != NULL); assert(cb != NULL); + printf("Called layout_create\n"); + l = calloc(1, sizeof(nslayout_layout)); if (l == NULL) { return NSLAYOUT_NO_MEM; @@ -42,6 +82,16 @@ nslayout_error nslayout_layout_create( l->cb = cb; l->pw = pw; + exc = dom_event_listener_create(doc, nsl__event_handler, + l, &l->listener); + if (exc != DOM_NO_ERR) { + /* TODO: free stuff, return value */ + printf("Failed to register even handler!\n"); + return NSLAYOUT_NO_MEM; + } + + printf("Registered event handler!\n"); + *layout = l; return NSLAYOUT_OK; } @@ -51,9 +101,11 @@ nslayout_error nslayout_layout_create( nslayout_error nslayout_layout_destroy( nslayout_layout *layout) { - /* TODO: free/unref the stuff we own in the layout */ assert(layout != NULL); + /* TODO: free/unref the stuff we own in the layout */ + dom_event_listener_unref(layout->listener); + free(layout); return NSLAYOUT_OK; } diff --git a/src/layout.h b/src/layout.h index 9946663..e689246 100644 --- a/src/layout.h +++ b/src/layout.h @@ -13,12 +13,18 @@ #include <libnslayout/nslayout.h> +#ifndef UNUSED +#define UNUSED(x) (void)(x) +#endif + struct nslayout_layout { dom_document *doc; css_select_ctx *css_ctx; css_media_type *media; nslayout_callback cb; void *pw; + + dom_event_listener *listener; }; #endif diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..b48eb46 --- /dev/null +++ b/test/main.c @@ -0,0 +1,22 @@ +/* + * This file is part of LibNSLayout's tests + * Licensed under the ISC License, http://opensource.org/licenses/ISC + * Copyright 2015 Michael Drake <tlsa@netsurf-browser.org> + */ + +#include <check.h> +#include <stdlib.h> +#include <string.h> + +#include "tests.h" + +#include "test-loader.c" + +/* + * cd ../ && make && make install && cd test && gcc `pkg-config libnslayout --cflags` main.c `pkg-config libnslayout --libs` && ./a.out + */ + +int main(void) +{ + return test_loader("test-writing-mode.html", CSS_MEDIA_ALL, 15); +} diff --git a/test/test-loader.c b/test/test-loader.c new file mode 100644 index 0000000..e9be591 --- /dev/null +++ b/test/test-loader.c @@ -0,0 +1,298 @@ +/* + * This file is part of LibNSLayout's tests + * Licensed under the ISC License, http://opensource.org/licenses/ISC + * Copyright 2015 Michael Drake <tlsa@netsurf-browser.org> + */ + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +#include <dom/dom.h> +#include <dom/bindings/hubbub/parser.h> +#include <libcss/libcss.h> + +#include <libnslayout/nslayout.h> + +#ifndef UNUSED +#define UNUSED(x) (void)(x) +#endif + +static nslayout_error nslayout_test_callback( + nslayout_layout *layout, + void *pw, + nslayout_request *req) +{ + UNUSED(req); + UNUSED(layout); + UNUSED(pw); + return NSLAYOUT_OK; +} + +struct doc_load_ctx { + dom_hubbub_parser *parser; + dom_document *doc; + unsigned char *buffer; + size_t buffer_size; + FILE *handle; + css_select_ctx *css_ctx; + css_stylesheet *css_sheet; +}; + + +static bool doc_load_start(const char *file, size_t buffer_size, + struct doc_load_ctx *load_ctx) +{ + dom_hubbub_parser_params params; + dom_hubbub_error error; + size_t chunk_length; + + params.enc = NULL; + params.fix_enc = true; + params.enable_script = false; + params.msg = NULL; + params.script = NULL; + params.ctx = NULL; + params.daf = NULL; + + load_ctx->buffer = malloc(buffer_size); + if (load_ctx->buffer == NULL) { + return false; + } + + load_ctx->buffer_size = buffer_size; + + /* Create Hubbub parser */ + error = dom_hubbub_parser_create(¶ms, &load_ctx->parser, + &load_ctx->doc); + if (error != DOM_HUBBUB_OK) { + free(load_ctx->buffer); + return false; + } + + /* Open input file */ + load_ctx->handle = fopen(file, "rb"); + if (load_ctx->handle == NULL) { + dom_hubbub_parser_destroy(load_ctx->parser); + free(load_ctx->buffer); + return false; + } + + /* Parse input file in chunks */ + chunk_length = buffer_size; + chunk_length = fread(load_ctx->buffer, 1, load_ctx->buffer_size, + load_ctx->handle); + error = dom_hubbub_parser_parse_chunk(load_ctx->parser, + load_ctx->buffer, chunk_length); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(load_ctx->parser); + printf("Parsing errors occur\n"); + return false; + } + + return true; +} + + +static bool doc_load_next(struct doc_load_ctx *load_ctx, bool *complete) +{ + dom_hubbub_error error; + int chunk_length; + + /* Parse input file in chunks */ + chunk_length = fread(load_ctx->buffer, 1, load_ctx->buffer_size, + load_ctx->handle); + if (chunk_length != 0) { + error = dom_hubbub_parser_parse_chunk(load_ctx->parser, + load_ctx->buffer, chunk_length); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(load_ctx->parser); + printf("Parsing errors occur\n"); + return false; + } + *complete = false; + return true; + } + + *complete = true; + + /* Done parsing file */ + error = dom_hubbub_parser_completed(load_ctx->parser); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(load_ctx->parser); + printf("Parsing error when construct DOM\n"); + return false; + } + + /* Finished with parser */ + dom_hubbub_parser_destroy(load_ctx->parser); + + /* Close input file */ + if (fclose(load_ctx->handle) != 0) { + printf("Can't close test input file\n"); + return false; + } + + return true; +} + + +static css_error resolve_url(void *pw, const char *base, + lwc_string *rel, lwc_string **abs) +{ + UNUSED(pw); + UNUSED(base); + + /* No join implementation; just copy rel to abs for now. */ + *abs = lwc_string_ref(rel); + + return CSS_OK; +} + + +static bool test_loader_css_fini(struct doc_load_ctx *load_ctx) +{ + css_error css_err = CSS_OK; + + if (load_ctx->css_ctx != NULL) { + css_err = css_select_ctx_destroy(load_ctx->css_ctx); + if (css_err != CSS_OK) { + printf("ERROR: css_select_ctx_destroy\n"); + } + } + if (load_ctx->css_sheet != NULL) { + css_err = css_stylesheet_destroy(load_ctx->css_sheet); + if (css_err != CSS_OK) { + printf("ERROR: css_stylesheet_destroy\n"); + } + } + + return (css_err == CSS_OK); +} + + +static bool test_loader_css_init(struct doc_load_ctx *load_ctx) +{ + css_error css_err; + css_stylesheet_params params; + const char *ua_style = + "div, p, h1, h2, h3, h4, h5 {display:block}"; + + params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1; + params.level = CSS_LEVEL_21; + params.charset = "UTF-8"; + params.url = "foo"; + params.title = "foo"; + params.allow_quirks = false; + params.inline_style = false; + params.resolve = resolve_url; + params.resolve_pw = NULL; + params.import = NULL; + params.import_pw = NULL; + params.color = NULL; + params.color_pw = NULL; + params.font = NULL; + params.font_pw = NULL; + + /* create a stylesheet */ + css_err = css_stylesheet_create(¶ms, &load_ctx->css_sheet); + if (css_err != CSS_OK) { + printf("ERROR: css_stylesheet_create\n"); + goto fail; + } + + css_err = css_stylesheet_append_data(load_ctx->css_sheet, + (const uint8_t *) ua_style, sizeof ua_style); + if (css_err != CSS_OK && css_err != CSS_NEEDDATA) { + printf("ERROR: css_stylesheet_append_data\n"); + goto fail; + } + css_err = css_stylesheet_data_done(load_ctx->css_sheet); + if (css_err != CSS_OK) { + printf("ERROR: css_stylesheet_data_done\n"); + goto fail; + } + + /* Create a selection context (with no sheets added) */ + css_err = css_select_ctx_create(&load_ctx->css_ctx); + if (css_err != CSS_OK) { + printf("ERROR: css_select_ctx_create\n"); + goto fail; + } + + css_err = css_select_ctx_append_sheet(load_ctx->css_ctx, + load_ctx->css_sheet, CSS_ORIGIN_UA, CSS_MEDIA_ALL); + if (css_err != CSS_OK) { + printf("ERROR: css_select_ctx_append_sheet\n"); + goto fail; + } + + return true; + +fail: + test_loader_css_fini(load_ctx); + + return false; +} + + +static bool test_loader(const char *document_path, + css_media_type media, + size_t chunk_size) +{ + nslayout_layout *layout = NULL; + nslayout_error error; + struct doc_load_ctx load_ctx; + bool complete = false; + bool ret = false; + + load_ctx.parser = NULL; + load_ctx.doc = NULL; + load_ctx.buffer = NULL; + load_ctx.buffer_size = 0; + load_ctx.handle = NULL; + load_ctx.css_sheet = NULL; + load_ctx.css_ctx = NULL; +printf(" Test loader\n"); + if (!doc_load_start(document_path, chunk_size, &load_ctx)) { + printf("ERROR: doc_load_start\n"); + goto fail; + } +printf(" Started load\n"); + if (!test_loader_css_init(&load_ctx)) { + printf("ERROR: create_style_context\n"); + goto fail; + } +printf(" Created style context\n"); + error = nslayout_layout_create(load_ctx.doc, + load_ctx.css_ctx, + &media, + nslayout_test_callback, + NULL, + &layout); + if (error != NSLAYOUT_OK) { + goto fail; + } +printf(" Created nsl layout\n"); + while (!complete) { + if (!doc_load_next(&load_ctx, &complete)) { + printf("ERROR: doc_load_next\n"); + goto fail; + } + printf(" Loaded a chunk of the document\n"); + } +printf(" Finished loading document\n"); + error = nslayout_layout_destroy(layout); + layout = NULL; +printf(" Destroyed layout\n"); + ret = (error == NSLAYOUT_OK); +fail: + if (layout != NULL) { + nslayout_layout_destroy(layout); + } + test_loader_css_fini(&load_ctx); + dom_node_unref(load_ctx.doc); + + return ret; +} + diff --git a/test/test-writing-mode.html b/test/test-writing-mode.html new file mode 100644 index 0000000..96c811e --- /dev/null +++ b/test/test-writing-mode.html @@ -0,0 +1,40 @@ +<!DOCTYPE html> +<html> +<head> +<style> +html { + -ms-writing-mode: lr-tb; + -webkit-writing-mode: horizontal-tb; + -moz-writing-mode: horizontal-tb; + -ms-writing-mode: horizontal-tb; + writing-mode: horizontal-tb; +} +html:hover { + -ms-writing-mode: tb-rl; + -webkit-writing-mode: vertical-rl; + -moz-writing-mode: vertical-rl; + -ms-writing-mode: vertical-rl; + writing-mode: vertical-rl; +} +h1 { + background: #600; + color: #fff; + width: 50%; + margin: 0; + padding: 3px; + border-bottom: 2px solid black; +} +p { + margin: 0; + padding: 3px; + border: 1px solid green; + border-left-width: 1em; + margin-top: 3em; +} +</style> +</head> +<body> +<h1>Test</h1> +<p>Here's some text to test CSS3 writing modes <a href="https://drafts.csswg.org/css-writing-modes/#abstract-layout">abstract box layout</a>!</p> +</body> +</html> |