Bug 1045053 - Part 2: tests for OS locale handling. r=bnicholson

This commit is contained in:
Richard Newman 2014-10-01 18:38:43 -07:00
Родитель 98b045f8fe
Коммит ffcde0a833
4 изменённых файлов: 158 добавлений и 7 удалений

Просмотреть файл

@ -82,6 +82,16 @@ abstract class BaseTest extends BaseRobocopTest {
protected int mScreenMidHeight;
private final HashSet<Integer> mKnownTabIDs = new HashSet<Integer>();
protected void blockForDelayedStartup() {
try {
Actions.EventExpecter delayedStartupExpector = mActions.expectGeckoEvent("Gecko:DelayedStartup");
delayedStartupExpector.blockForEvent(GECKO_READY_WAIT_MS, true);
delayedStartupExpector.unregisterListener();
} catch (Exception e) {
mAsserter.dumpLog("Exception in blockForDelayedStartup", e);
}
}
protected void blockForGeckoReady() {
try {
Actions.EventExpecter geckoReadyExpector = mActions.expectGeckoEvent("Gecko:Ready");
@ -127,14 +137,17 @@ abstract class BaseTest extends BaseRobocopTest {
throwIfScreenNotOn();
}
protected void initializeProfile() {
final GeckoProfile profile;
protected GeckoProfile getTestProfile() {
if (mProfile.startsWith("/")) {
profile = GeckoProfile.get(getActivity(), "default", mProfile);
} else {
profile = GeckoProfile.get(getActivity(), mProfile);
return GeckoProfile.get(getActivity(), "default", mProfile);
}
return GeckoProfile.get(getActivity(), mProfile);
}
protected void initializeProfile() {
final GeckoProfile profile = getTestProfile();
// In Robocop tests, we typically don't get initialized correctly, because
// GeckoProfile doesn't create the profile directory.
profile.enqueueInitialization(profile.getDir());

Просмотреть файл

@ -21,11 +21,14 @@ public class JavascriptTest extends BaseTest {
public void testJavascript() throws Exception {
blockForGeckoReady();
doTestJavascript();
}
protected void doTestJavascript() throws Exception {
// We want to be waiting for Robocop messages before the page is loaded
// because the test harness runs each test in the suite (and possibly
// completes testing) before the page load event is fired.
final Actions.EventExpecter expecter =
mActions.expectGeckoEvent(EVENT_TYPE);
final Actions.EventExpecter expecter = mActions.expectGeckoEvent(EVENT_TYPE);
mAsserter.dumpLog("Registered listener for " + EVENT_TYPE);
final String url = getAbsoluteUrl(StringHelper.getHarnessUrlForJavascript(javascriptUrl));

Просмотреть файл

@ -104,6 +104,7 @@ skip-if = android_version == "10"
[testJNI]
# [testMozPay] # see bug 945675
[testOrderedBroadcast]
[testOSLocale]
[testResourceSubstitutions]
[testRestrictedProfiles]
[testSharedPreferences]

Просмотреть файл

@ -0,0 +1,134 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests;
import java.util.Locale;
import org.mozilla.gecko.BrowserLocaleManager;
import org.mozilla.gecko.GeckoSharedPrefs;
import org.mozilla.gecko.PrefsHelper;
import android.content.SharedPreferences;
public class testOSLocale extends BaseTest {
@Override
public void setUp() throws Exception {
super.setUp();
// Clear per-profile SharedPreferences as a workaround for Bug 1069687.
// We're trying to exercise logic that only applies on first onCreate!
// We can't rely on this occurring prior to the first broadcast, though,
// so see the main test method for more logic.
final String profileName = getTestProfile().getName();
mAsserter.info("Setup", "Clearing pref in " + profileName + ".");
GeckoSharedPrefs.forProfileName(getActivity(), profileName)
.edit()
.remove("osLocale")
.apply();
}
public static class PrefState extends PrefsHelper.PrefHandlerBase {
private static final String PREF_LOCALE_OS = "intl.locale.os";
private static final String PREF_ACCEPT_LANG = "intl.accept_languages";
private static final String[] TO_FETCH = {PREF_LOCALE_OS, PREF_ACCEPT_LANG};
public volatile String osLocale;
public volatile String acceptLanguages;
private final Object waiter = new Object();
public void fetch() throws InterruptedException {
synchronized (waiter) {
PrefsHelper.getPrefs(TO_FETCH, this);
waiter.wait(MAX_WAIT_MS);
}
}
@Override
public void prefValue(String pref, String value) {
switch (pref) {
case PREF_LOCALE_OS:
osLocale = value;
return;
case PREF_ACCEPT_LANG:
acceptLanguages = value;
return;
}
}
@Override
public void finish() {
synchronized (waiter) {
waiter.notify();
}
}
}
public void testOSLocale() throws Exception {
blockForDelayedStartup();
final SharedPreferences prefs = GeckoSharedPrefs.forProfile(getActivity());
final PrefState state = new PrefState();
state.fetch();
// We don't know at this point whether we were run against a dirty profile or not.
//
// If we cleared the pref above prior to BrowserApp's delayed init, or our Gecko
// profile has been used before, then we're already going to be set up for en-US.
//
// If we cleared the pref after the initial broadcast, and our Android-side profile
// has been used before but the Gecko profile is clean, then the Gecko prefs won't
// have been set.
//
// Instead, we always send a new locale code, and see what we get.
final Locale fr = BrowserLocaleManager.parseLocaleCode("fr");
BrowserLocaleManager.storeAndNotifyOSLocale(prefs, fr);
state.fetch();
mAsserter.is(state.osLocale, "fr", "We're in fr.");
// Now we can see what the expected Accept-Languages header should be.
// The OS locale is 'fr', so we have our app locale (en-US),
// the OS locale (fr), then any remaining fallbacks from intl.properties.
mAsserter.is(state.acceptLanguages, "en-us,fr,en", "We have the default en-US+fr Accept-Languages.");
// Now set the app locale to be es-ES.
BrowserLocaleManager.getInstance().setSelectedLocale(getActivity(), "es-ES");
state.fetch();
mAsserter.is(state.osLocale, "fr", "We're still in fr.");
// The correct set here depends on whether the
// browser was built with multiple locales or not.
// This is exasperating, but hey.
final boolean isMultiLocaleBuild = false;
// This never changes.
final String SELECTED_LOCALES = "es-es,fr,";
// Expected, from es-ES's intl.properties:
final String EXPECTED = SELECTED_LOCALES +
(isMultiLocaleBuild ? "es,en-us,en" : // Expected, from es-ES's intl.properties.
"en-us,en"); // Expected, from en-US (the default).
mAsserter.is(state.acceptLanguages, EXPECTED, "We have the right es-ES+fr Accept-Languages for this build.");
// And back to en-US.
final Locale en_US = BrowserLocaleManager.parseLocaleCode("en-US");
BrowserLocaleManager.storeAndNotifyOSLocale(prefs, en_US);
BrowserLocaleManager.getInstance().resetToSystemLocale(getActivity());
state.fetch();
mAsserter.is(state.osLocale, "en-US", "We're in en-US.");
mAsserter.is(state.acceptLanguages, "en-us,en", "We have the default processed en-US Accept-Languages.");
}
}