2013-02-19 18:15:55 +00:00
|
|
|
#filter substitution
|
|
|
|
package @ANDROID_PACKAGE_NAME@.tests;
|
|
|
|
|
|
|
|
import @ANDROID_PACKAGE_NAME@.*;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.lang.ClassLoader;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests distribution customization.
|
|
|
|
* mock-package.zip should contain the following directory structure:
|
|
|
|
*
|
|
|
|
* distribution/
|
|
|
|
* preferences.json
|
|
|
|
* bookmarks.json
|
2013-03-18 22:39:08 +00:00
|
|
|
* searchplugins/
|
|
|
|
* common/
|
|
|
|
* engine.xml
|
2013-02-19 18:15:55 +00:00
|
|
|
*/
|
|
|
|
public class testDistribution extends ContentProviderTest {
|
|
|
|
private static final String MOCK_PACKAGE = "mock-package.zip";
|
2013-03-04 10:22:41 +00:00
|
|
|
private static final String PREF_REQUEST_ID = "testDistribution";
|
2013-02-19 18:15:55 +00:00
|
|
|
|
|
|
|
private Activity mActivity;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int getTestType() {
|
|
|
|
return TEST_MOCHITEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testDistribution() {
|
2013-02-21 17:47:43 +00:00
|
|
|
mActivity = getActivity();
|
|
|
|
|
2013-03-04 10:22:41 +00:00
|
|
|
String mockPackagePath = getMockPackagePath();
|
|
|
|
|
|
|
|
// Pre-clear distribution pref, run basic preferences and en-US localized preferences Tests
|
2013-02-21 17:47:43 +00:00
|
|
|
clearDistributionPref();
|
2013-03-04 10:22:41 +00:00
|
|
|
setTestLocale("en-US");
|
|
|
|
initDistribution(mockPackagePath);
|
|
|
|
checkPreferences();
|
|
|
|
checkLocalizedPreferences("en-US");
|
2013-03-18 22:39:08 +00:00
|
|
|
checkSearchPlugin();
|
2013-02-21 17:47:43 +00:00
|
|
|
|
2013-03-04 10:22:41 +00:00
|
|
|
// Pre-clear distribution pref, and run es-MX localized preferences Test
|
|
|
|
clearDistributionPref();
|
|
|
|
setTestLocale("es-MX");
|
|
|
|
initDistribution(mockPackagePath);
|
|
|
|
checkLocalizedPreferences("es-MX");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the distribution from the mock package.
|
|
|
|
private void initDistribution(String aPackagePath) {
|
2013-02-21 17:47:43 +00:00
|
|
|
try {
|
|
|
|
// Call Distribution.init with the mock package.
|
|
|
|
ClassLoader classLoader = mActivity.getClassLoader();
|
|
|
|
Class distributionClass = classLoader.loadClass("org.mozilla.gecko.Distribution");
|
|
|
|
Class contextClass = classLoader.loadClass("android.content.Context");
|
|
|
|
Method init = distributionClass.getMethod("init", contextClass, String.class);
|
|
|
|
|
|
|
|
Actions.EventExpecter distributionSetExpecter = mActions.expectGeckoEvent("Distribution:Set:OK");
|
2013-03-04 10:22:41 +00:00
|
|
|
init.invoke(null, mActivity, aPackagePath);
|
2013-02-21 17:47:43 +00:00
|
|
|
distributionSetExpecter.blockForEvent();
|
|
|
|
} catch (Exception e) {
|
|
|
|
mAsserter.ok(false, "exception initializing distribution", e.toString());
|
|
|
|
}
|
2013-03-04 10:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test distribution and preferences values stored in preferences.json
|
|
|
|
private void checkPreferences() {
|
|
|
|
String prefID = "distribution.id";
|
|
|
|
String prefAbout = "distribution.about";
|
|
|
|
String prefVersion = "distribution.version";
|
|
|
|
String prefTestBoolean = "distribution.test.boolean";
|
|
|
|
String prefTestString = "distribution.test.string";
|
|
|
|
String prefTestInt = "distribution.test.int";
|
2013-02-21 17:47:43 +00:00
|
|
|
|
2013-02-19 18:15:55 +00:00
|
|
|
try {
|
|
|
|
JSONArray getPrefData = new JSONArray();
|
2013-03-04 10:22:41 +00:00
|
|
|
getPrefData.put(prefID);
|
|
|
|
getPrefData.put(prefAbout);
|
|
|
|
getPrefData.put(prefVersion);
|
|
|
|
getPrefData.put(prefTestBoolean);
|
|
|
|
getPrefData.put(prefTestString);
|
|
|
|
getPrefData.put(prefTestInt);
|
2013-02-19 18:15:55 +00:00
|
|
|
|
|
|
|
JSONObject message = new JSONObject();
|
2013-03-04 10:22:41 +00:00
|
|
|
message.put("requestId", PREF_REQUEST_ID);
|
2013-02-19 18:15:55 +00:00
|
|
|
message.put("preferences", getPrefData);
|
|
|
|
|
|
|
|
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
|
|
|
|
mActions.sendGeckoEvent("Preferences:Get", message.toString());
|
|
|
|
|
|
|
|
JSONObject data = null;
|
|
|
|
String requestId = "";
|
|
|
|
|
|
|
|
// Wait until we get the correct "Preferences:Data" event
|
2013-03-04 10:22:41 +00:00
|
|
|
while (!requestId.equals(PREF_REQUEST_ID)) {
|
2013-02-19 18:15:55 +00:00
|
|
|
data = new JSONObject(eventExpecter.blockForEventData());
|
|
|
|
requestId = data.getString("requestId");
|
|
|
|
}
|
|
|
|
|
2013-03-04 10:22:41 +00:00
|
|
|
JSONArray preferences = data.getJSONArray("preferences");
|
|
|
|
for (int i = 0; i < preferences.length(); i++) {
|
|
|
|
JSONObject pref = (JSONObject) preferences.get(i);
|
|
|
|
String name = pref.getString("name");
|
|
|
|
|
|
|
|
if (name.equals(prefID)) {
|
|
|
|
mAsserter.is(pref.getString("value"), "test-partner", "check " + prefID);
|
|
|
|
} else if (name.equals(prefAbout)) {
|
|
|
|
mAsserter.is(pref.getString("value"), "Test Partner", "check " + prefAbout);
|
|
|
|
} else if (name.equals(prefVersion)) {
|
|
|
|
mAsserter.is(pref.getInt("value"), 1, "check " + prefVersion);
|
|
|
|
} else if (name.equals(prefTestBoolean)) {
|
|
|
|
mAsserter.is(pref.getBoolean("value"), true, "check " + prefTestBoolean);
|
|
|
|
} else if (name.equals(prefTestString)) {
|
|
|
|
mAsserter.is(pref.getString("value"), "test", "check " + prefTestString);
|
|
|
|
} else if (name.equals(prefTestInt)) {
|
|
|
|
mAsserter.is(pref.getInt("value"), 5, "check " + prefTestInt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
mAsserter.ok(false, "exception getting preferences", e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 22:39:08 +00:00
|
|
|
private void checkSearchPlugin() {
|
|
|
|
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("SearchEngines:Data");
|
|
|
|
mActions.sendGeckoEvent("SearchEngines:Get", null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSONObject data = new JSONObject(eventExpecter.blockForEventData());
|
|
|
|
JSONArray searchEngines = data.getJSONArray("searchEngines");
|
|
|
|
boolean foundEngine = false;
|
|
|
|
for (int i = 0; i < searchEngines.length(); i++) {
|
|
|
|
JSONObject engine = (JSONObject) searchEngines.get(i);
|
|
|
|
String name = engine.getString("name");
|
|
|
|
if (name.equals("Test search engine")) {
|
|
|
|
foundEngine = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mAsserter.ok(foundEngine, "check search plugin", "found test search plugin");
|
|
|
|
} catch (JSONException e) {
|
|
|
|
mAsserter.ok(false, "exception getting search plugins", e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 10:22:41 +00:00
|
|
|
// Sets the distribution locale preference for the test
|
|
|
|
private void setTestLocale(String aLocale) {
|
|
|
|
String prefUseragentLocale = "general.useragent.locale";
|
|
|
|
|
|
|
|
JSONObject jsonPref = new JSONObject();
|
|
|
|
try {
|
|
|
|
// Request the pref change to the locale.
|
|
|
|
jsonPref.put("name", prefUseragentLocale);
|
|
|
|
jsonPref.put("type", "string");
|
|
|
|
jsonPref.put("value", aLocale);
|
|
|
|
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
|
|
|
|
|
|
|
|
// Wait for confirmation of the pref change.
|
|
|
|
JSONArray getPrefData = new JSONArray();
|
|
|
|
getPrefData.put(prefUseragentLocale);
|
|
|
|
|
|
|
|
JSONObject message = new JSONObject();
|
|
|
|
message.put("requestId", PREF_REQUEST_ID);
|
|
|
|
message.put("preferences", getPrefData);
|
|
|
|
|
|
|
|
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
|
|
|
|
mActions.sendGeckoEvent("Preferences:Get", message.toString());
|
|
|
|
|
|
|
|
JSONObject data = null;
|
|
|
|
String requestId = "";
|
|
|
|
|
|
|
|
// Wait until we get the correct "Preferences:Data" event
|
|
|
|
while (!requestId.equals(PREF_REQUEST_ID)) {
|
|
|
|
data = new JSONObject(eventExpecter.blockForEventData());
|
|
|
|
requestId = data.getString("requestId");
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
mAsserter.ok(false, "exception setting test locale", e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test localized distribution and preferences values stored in preferences.json
|
|
|
|
private void checkLocalizedPreferences(String aLocale) {
|
|
|
|
String prefAbout = "distribution.about";
|
|
|
|
String prefLocalizeable = "distribution.test.localizeable";
|
|
|
|
String prefLocalizeableOverride = "distribution.test.localizeable-override";
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSONArray getPrefData = new JSONArray();
|
|
|
|
getPrefData.put(prefAbout);
|
|
|
|
getPrefData.put(prefLocalizeable);
|
|
|
|
getPrefData.put(prefLocalizeableOverride);
|
|
|
|
|
|
|
|
JSONObject message = new JSONObject();
|
|
|
|
message.put("requestId", PREF_REQUEST_ID);
|
|
|
|
message.put("preferences", getPrefData);
|
|
|
|
|
|
|
|
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
|
|
|
|
mActions.sendGeckoEvent("Preferences:Get", message.toString());
|
|
|
|
|
|
|
|
JSONObject data = null;
|
|
|
|
String requestId = "";
|
|
|
|
|
|
|
|
// Wait until we get the correct "Preferences:Data" event
|
|
|
|
while (!requestId.equals(PREF_REQUEST_ID)) {
|
|
|
|
data = new JSONObject(eventExpecter.blockForEventData());
|
|
|
|
requestId = data.getString("requestId");
|
|
|
|
}
|
2013-02-19 18:15:55 +00:00
|
|
|
|
|
|
|
JSONArray preferences = data.getJSONArray("preferences");
|
|
|
|
for (int i = 0; i < preferences.length(); i++) {
|
|
|
|
JSONObject pref = (JSONObject) preferences.get(i);
|
|
|
|
String name = pref.getString("name");
|
|
|
|
|
2013-03-04 10:22:41 +00:00
|
|
|
if (name.equals(prefAbout)) {
|
|
|
|
if (aLocale.equals("en-US")) {
|
|
|
|
mAsserter.is(pref.getString("value"), "Test Partner", "check " + prefAbout);
|
|
|
|
} else if (aLocale.equals("es-MX")) {
|
|
|
|
mAsserter.is(pref.getString("value"), "Afiliado de Prueba", "check " + prefAbout);
|
|
|
|
}
|
|
|
|
} else if (name.equals(prefLocalizeable)) {
|
|
|
|
if (aLocale.equals("en-US")) {
|
|
|
|
mAsserter.is(pref.getString("value"), "http://test.org/en-US/en-US/", "check " + prefLocalizeable);
|
|
|
|
} else if (aLocale.equals("es-MX")) {
|
|
|
|
mAsserter.is(pref.getString("value"), "http://test.org/es-MX/es-MX/", "check " + prefLocalizeable);
|
|
|
|
}
|
|
|
|
} else if (name.equals(prefLocalizeableOverride)) {
|
|
|
|
if (aLocale.equals("en-US")) {
|
|
|
|
mAsserter.is(pref.getString("value"), "http://cheese.com", "check " + prefLocalizeableOverride);
|
|
|
|
} else if (aLocale.equals("es-MX")) {
|
|
|
|
mAsserter.is(pref.getString("value"), "http://test.org/es-MX/", "check " + prefLocalizeableOverride);
|
|
|
|
}
|
2013-02-19 18:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
mAsserter.ok(false, "exception getting preferences", e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copies the mock package to the data directory and returns the file path to it.
|
2013-03-04 10:22:41 +00:00
|
|
|
private String getMockPackagePath() {
|
|
|
|
String mockPackagePath = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
InputStream inStream = getAsset(MOCK_PACKAGE);
|
|
|
|
File dataDir = new File(mActivity.getApplicationInfo().dataDir);
|
|
|
|
File outFile = new File(dataDir, MOCK_PACKAGE);
|
|
|
|
|
|
|
|
OutputStream outStream = new FileOutputStream(outFile);
|
|
|
|
int b;
|
|
|
|
while ((b = inStream.read()) != -1) {
|
|
|
|
outStream.write(b);
|
|
|
|
}
|
|
|
|
inStream.close();
|
|
|
|
outStream.close();
|
|
|
|
|
|
|
|
mockPackagePath = outFile.getPath();
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
mAsserter.ok(false, "exception copying mock distribution package to data directory", e.toString());
|
2013-02-19 18:15:55 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 10:22:41 +00:00
|
|
|
return mockPackagePath;
|
2013-02-19 18:15:55 +00:00
|
|
|
}
|
|
|
|
|
2013-02-19 18:49:42 +00:00
|
|
|
// Clears the distribution pref to return distribution state to STATE_UNKNOWN
|
|
|
|
private void clearDistributionPref() {
|
|
|
|
SharedPreferences settings = mActivity.getSharedPreferences("GeckoApp", Activity.MODE_PRIVATE);
|
|
|
|
String keyName = mActivity.getPackageName() + ".distribution_state";
|
|
|
|
settings.edit().remove(keyName).commit();
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-19 18:15:55 +00:00
|
|
|
public void setUp() throws Exception {
|
|
|
|
// TODO: Set up the content provider after setting the distribution.
|
|
|
|
super.setUp("@ANDROID_PACKAGE_NAME@.db.BrowserProvider", "AUTHORITY");
|
|
|
|
}
|
|
|
|
|
2013-03-18 22:39:08 +00:00
|
|
|
private void delete(File file) throws Exception {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
File[] files = file.listFiles();
|
|
|
|
for (File f : files) {
|
|
|
|
delete(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mAsserter.ok(file.delete(), "clean up distribution files", "deleted " + file.getPath());
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-19 18:15:55 +00:00
|
|
|
public void tearDown() throws Exception {
|
|
|
|
File dataDir = new File(mActivity.getApplicationInfo().dataDir);
|
|
|
|
|
|
|
|
// Delete mock package from data directory.
|
|
|
|
File mockPackage = new File(dataDir, MOCK_PACKAGE);
|
|
|
|
mAsserter.ok(mockPackage.delete(), "clean up mock package", "deleted " + mockPackage.getPath());
|
|
|
|
|
2013-03-18 22:39:08 +00:00
|
|
|
// Recursively delete distribution files that Distribution.init copied to data directory.
|
2013-02-19 18:15:55 +00:00
|
|
|
File distDir = new File(dataDir, "distribution");
|
2013-03-18 22:39:08 +00:00
|
|
|
delete(distDir);
|
2013-02-19 18:15:55 +00:00
|
|
|
|
2013-02-19 18:49:42 +00:00
|
|
|
clearDistributionPref();
|
2013-02-19 18:15:55 +00:00
|
|
|
|
|
|
|
super.tearDown();
|
|
|
|
}
|
|
|
|
}
|