From c2c0118ab769bf89c2aa7900760778a021fd4a51 Mon Sep 17 00:00:00 2001 From: David Dick Date: Fri, 5 Jan 2024 09:27:38 +1100 Subject: [PATCH] Adding language method --- README | 7 +++++++ README.md | 4 ++++ lib/Firefox/Marionette.pm | 21 +++++++++++++++++++++ t/01-marionette.t | 13 +++++++++++++ 4 files changed, 45 insertions(+) diff --git a/README b/README index 02bafbc..14e9097 100644 --- a/README +++ b/README @@ -1740,6 +1740,13 @@ SUBROUTINES/METHODS $firefox->key_up(CONTROL()) )->content(); + language + + accepts an optional value for the Accept-Language + + header and sets this using the profile preferences. It returns the + current value, such as 'en-US, en'. + loaded returns true if document.readyState === "complete" diff --git a/README.md b/README.md index f8a45ac..e15966b 100644 --- a/README.md +++ b/README.md @@ -1348,6 +1348,10 @@ accepts a parameter describing a key and returns an action for use in the [perfo $firefox->key_up(CONTROL()) )->content(); +## language + +accepts an optional value for the [Accept-Language](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language) header and sets this using the profile preferences. It returns the current value, such as 'en-US, en'. + ## loaded returns true if `document.readyState === "complete"` diff --git a/lib/Firefox/Marionette.pm b/lib/Firefox/Marionette.pm index e834338..66921ed 100644 --- a/lib/Firefox/Marionette.pm +++ b/lib/Firefox/Marionette.pm @@ -199,6 +199,23 @@ sub BY_PARTIAL { return 'partial link text'; } +sub language { + my ( $self, $new_language ) = @_; + my $pref_name = 'intl.accept_languages'; + my $script = +'return branch.getComplexValue(arguments[0], Components.interfaces.nsIPrefLocalizedString).data'; + my $old = $self->_context('chrome'); + my $old_language = $self->script( + $self->_compress_script( $self->_prefs_interface_preamble() . $script ), + args => [$pref_name] + ); + $self->_context($old); + if ( defined $new_language ) { + $self->set_pref( $pref_name, $new_language ); + } + return $old_language; +} + sub _prefs_interface_preamble { my ($self) = @_; return <<'_JS_'; # modules/libpref/nsIPrefService.idl @@ -13026,6 +13043,10 @@ accepts a parameter describing a key and returns an action for use in the Lkey_up(CONTROL()) )->content(); +=head2 language + +accepts an optional value for the L header and sets this using the profile preferences. It returns the current value, such as 'en-US, en'. + =head2 loaded returns true if C diff --git a/t/01-marionette.t b/t/01-marionette.t index c4dbcdc..bf98627 100755 --- a/t/01-marionette.t +++ b/t/01-marionette.t @@ -1478,6 +1478,19 @@ SKIP: { ok($firefox->aria_role($element) =~ /^(?:toggle[ ])?button$/smx, "Retrieved the ARIA role correctly:" . $firefox->aria_role($element)); ok($firefox->find_id('save')->aria_role() =~ /^(?:toggle[ ])?button$/smx, "Retrieved the ARIA label correctly:" . $firefox->find_id('save')->aria_role()); } + my $browser_language = $firefox->script('return navigator.language'); + my $quoted_browser_language = quotemeta $browser_language; + my $original_language = $firefox->language(); + ok($original_language =~ /^$quoted_browser_language/smx, "\$firefox->language() contains navigator.language as it's first entry:$original_language vs $browser_language"); + my $new_language = 'en-AU,en-GB, en'; + ok($firefox->language($new_language) eq $original_language, "\$firefox->language(\"$new_language\") returns correctly"); + $browser_language = $firefox->script('return navigator.language'); + $quoted_browser_language = quotemeta $browser_language; + ok($new_language =~ /^$quoted_browser_language/smx, "\$firefox->language() contains navigator.language as it's first entry:$new_language vs $browser_language"); + my $lone_language = 'en-GB'; + ok($firefox->language($lone_language) eq $new_language, "\$firefox->language(\"$lone_language\") returns correctly"); + $browser_language = $firefox->script('return navigator.language'); + ok($lone_language eq $browser_language, "\$firefox->language() matches navigator.language b/c there is only one entry:$lone_language vs $browser_language"); if ($ENV{FIREFOX_HOST}) { } elsif (($^O eq 'openbsd') && (Cwd::cwd() !~ /^($quoted_home_directory\/Downloads|\/tmp)/)) { diag("Skipping checks that use a file:// url b/c of OpenBSD's unveil functionality - see https://bugzilla.mozilla.org/show_bug.cgi?id=1580271");