use strict; use warnings FATAL => 'all'; use Test::More tests => 135; use HTML::Tested::JavaScript qw(HTJ); use HTML::Tested::Test; use File::Slurp; use URI::file; use File::Basename qw(dirname); use Cwd qw(abs_path); use File::Temp qw(tempdir); BEGIN { use_ok('HTML::Tested::JavaScript::RichEdit'); use_ok('HTML::Tested::JavaScript::Test'); my $_exit = 1; eval "use Mozilla::Mechanize::GUITester"; SKIP: { skip "No Mozilla::Mechanize::GUITester installed", 119 if $@; $_exit = undef; }; exit if $_exit; } use constant HTJRE => HTJ."::RichEdit"; package T; use base 'HTML::Tested'; __PACKAGE__->ht_add_widget(::HTJRE, "v"); package main; my @v_fn = (v_fontname => <<'ENDS'); ENDS my @v_fs = (v_fontsize => <<'ENDS'); ENDS my $obj = T->new; my $stash = {}; $obj->ht_render($stash); is_deeply($stash, { v => "", v_script => <<'ENDS' ENDS , @v_fn, @v_fs }); $HTML::Tested::JavaScript::Location = "javascript"; $obj->ht_render($stash); is_deeply($stash, { v => "", v_script => <<'ENDS' ENDS , @v_fn, @v_fs }); my $str = sprintf(' %s ', $stash->{v}); my @err = HTML::Tested::Test->check_text('T', $str, { v => '' }); isnt($err[0], undef); $str = sprintf(<<'ENDS', $stash->{v_script}, $stash->{v}); %s %s ENDS is_deeply([ HTML::Tested::Test->check_text('T', $str, { v => '' }) ], []); my $td = tempdir('/tmp/060_re_XXXXXX', CLEANUP => 1); my $pf = "$td/paste.html"; write_file($pf, <<'ENDS');

Hello, World ENDS my $tf = "$td/re.html"; write_file($tf, $str); symlink(abs_path(dirname($0) . "/../javascript"), "$td/javascript"); ok(-f "$td/javascript/serializer.js"); ok(-f "$td/javascript/rich_edit.js"); my $mech = Mozilla::Mechanize::GUITester->new(quiet => 1, visible => 0); ok($mech->get(URI::file->new_abs($pf)->as_string)); $mech->x_send_keys("^(a)"); $mech->x_send_keys("^(c)"); my $url = URI::file->new_abs($tf)->as_string; ok($mech->get($url)); is_deeply($mech->console_messages, []) or diag($mech->content); my $if = $mech->get_html_element_by_id("v", "IFrame")->GetContentDocument() ->GetElementsByTagName("body")->Item(0); isnt($if, undef) or exit 1; use_ok('HTML::Tested::JavaScript::Test::RichEdit', qw(HTRE_Get_Value HTRE_Set_Value HTRE_Get_Body HTRE_Clean)); my $if_ns = $if->QueryInterface(Mozilla::DOM::NSHTMLElement->GetIID); my $br = HTRE_Clean($if_ns->GetInnerHTML); like($br, qr/^
$/); $mech->x_click($if, 10, 10); $mech->x_send_keys('hoho hoho'); is(HTRE_Clean($if_ns->GetInnerHTML), "hoho hoho$br"); is($mech->run_js('return htre_get_value("v");'), "hoho hoho$br"); is($mech->run_js('return htre_document("v");'), '[object HTMLDocument]'); is($mech->run_js('return htre_document("v").getElementsByTagName("head")[0];') , '[object HTMLHeadElement]') or exit 1; is_deeply($mech->console_messages, []) or exit 1; is(HTRE_Get_Value($mech, "v"), "hoho hoho$br"); is(HTRE_Clean(HTRE_Get_Body($mech, "v")->GetInnerHTML), HTRE_Get_Value($mech, "v")); $mech->x_send_keys("^(v)"); is_deeply($mech->console_messages, []) or exit 1; is($mech->run_js('return htre_get_value("v");'), "hoho hoho\nHello, World\n") or exit 1; $mech->run_js('htre_set_value("v", "momo

mama

");'); is($mech->run_js('return htre_get_value("v");'), "momo

mama

"); is(HTRE_Clean($if_ns->GetInnerHTML), "momo

mama

"); $mech->pull_alerts; is($mech->run_js('return htre_escape(htre_get_value("v"));'), 'momo

mama

'); is($mech->run_js('return htre_escape("fooo");'), 'fooo'); ok(shift @{ $mech->console_messages }); # DOMParser error cannot be suppressed is_deeply($mech->console_messages, []) or do { diag($mech->pull_alerts); exit 1; }; T->ht_set_widget_option("v", "no_onload", 1); $obj->ht_render($stash); is_deeply($stash, { v => "", v_script => <<'ENDS' ENDS , @v_fn, @v_fs }); $str = sprintf(<<'ENDS' %s ENDS , $stash->{v_script}, $stash->{v}); my $tf_none = "$td/re_none.html"; write_file($tf_none, $str); my $url_none = URI::file->new_abs($tf_none)->as_string; ok($mech->get($url_none)); is_deeply($mech->console_messages, []); $mech->run_js("init()"); is(HTRE_Get_Value($mech, "v"), "$br"); unlike($mech->pull_alerts, qr/Hello/); $if = $mech->get_html_element_by_id("v"); $mech->x_click($if, 10, 10); $mech->x_send_keys('hoho hoho'); $mech->x_click($if, -5, -5); is(HTRE_Get_Value($mech, "v"), "hoho hoho$br"); like($mech->pull_alerts, qr/Hello hoho hoho/); is_deeply($mech->console_messages, []); HTRE_Set_Value($mech, "v", "baba"); is(HTRE_Get_Value($mech, "v"), "baba"); T->ht_set_widget_option("v", "no_onload", undef); $obj->ht_render($stash); $str = sprintf(<<'ENDS' %s Commands
Bold
Italic
Underline
%s ENDS , $stash->{v_script}, $stash->{v}); my $tf_cmds = "$td/cmds.html"; write_file($tf_cmds, $str); my $url_cmds = URI::file->new_abs($tf_cmds)->as_string; ok($mech->get($url_cmds)); is($mech->title, "Commands"); is_deeply($mech->console_messages, []); my $bo = $mech->get_html_element_by_id("v_bold"); isnt($bo, undef) or exit 1; $mech->x_click($bo, 2, 2); my $vif = $mech->get_html_element_by_id("v"); isnt($vif, undef) or exit 1; $mech->x_send_keys('hoho hoho'); is(HTRE_Get_Value($mech, "v"), '' . "hoho hoho$br"); is_deeply($mech->console_messages, []); is($mech->run_js('return htre_get_selection_state("v").bold;'), 'bold'); is_deeply($mech->console_messages, []) or exit 1; HTRE_Set_Value($mech, "v", '' . 'hoho hoho'); $mech->x_click($if, 10, 10); $mech->x_send_keys('{RIG}'); is($mech->run_js('return htre_get_selection_state("v").bold;'), 'normal'); HTRE_Set_Value($mech, "v", '' . "hoho hoho$br"); $mech->pull_alerts; is($mech->run_js('return htre_escape(htre_get_value("v"));') , 'var _a;hoho hoho' . "
") or do { diag($mech->pull_alerts); exit 1; }; is_deeply($mech->console_messages, []) or exit 1; HTRE_Set_Value($mech, "v", '' . "hoho hoho$br"); is($mech->run_js('return htre_escape(htre_get_value("v"));') , 'var _a;hoho hoho' . "
"); is_deeply($mech->console_messages, []); my $ital = $mech->get_html_element_by_id("v_italic"); isnt($ital, undef) or exit 1; $mech->x_click($bo, 2, 2); $mech->x_click($ital, 2, 2); HTRE_Set_Value($mech, "v", ""); $mech->x_send_keys('haha haha'); is(HTRE_Get_Value($mech, "v"), '' . 'haha haha' . "$br"); is_deeply($mech->console_messages, []); is($mech->run_js('return htre_get_selection_state("v").italic;'), 'italic'); HTRE_Set_Value($mech, "v", ""); my $under = $mech->get_html_element_by_id("v_underline"); isnt($under, undef) or exit 1; $mech->x_click($under, 2, 2); $mech->x_send_keys('haha haha'); is(HTRE_Get_Value($mech, "v") , 'haha haha' . "$br"); is($mech->run_js('return htre_get_selection_state("v").underline;') , 'underline'); is_deeply($mech->console_messages, []); $mech->x_click($bo, 2, 2); $mech->x_send_keys('bbb'); is($mech->run_js('return htre_get_selection_state("v").bold;'), 'bold'); is($mech->run_js('return htre_get_selection_state("v").underline;') , 'underline') or exit 1; my $ix = $mech->run_js('return htre_get_inner_xml(document.body)'); is_deeply($mech->console_messages, []); my $body = $br =~ / / ? q{
Bold
Italic
Underline
} : <Bold
Italic
Underline