Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1603)

Unified Diff: chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebApkInfoTest.java

Issue 2948653002: Allow localization of WebApk name. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebApkInfoTest.java
diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebApkInfoTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebApkInfoTest.java
index ecaf1ae1140a3361a9aa0deed2d8d8e9d155d46f..fe4c8d76a29b6ea6b933797effd82726aadeeb98 100644
--- a/chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebApkInfoTest.java
+++ b/chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebApkInfoTest.java
@@ -5,6 +5,8 @@
package org.chromium.chrome.browser.webapps;
import android.content.Intent;
+import android.content.res.AssetManager;
+import android.content.res.Resources;
import android.os.Bundle;
import org.junit.Assert;
@@ -13,6 +15,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
+import org.robolectric.res.builder.DefaultPackageManager;
import org.chromium.base.ContextUtils;
import org.chromium.blink_public.platform.WebDisplayMode;
@@ -24,6 +27,7 @@ import org.chromium.webapk.lib.common.WebApkConstants;
import org.chromium.webapk.lib.common.WebApkMetaDataKeys;
import org.chromium.webapk.test.WebApkTestHelper;
+import java.util.HashMap;
import java.util.Map;
/**
@@ -49,6 +53,66 @@ public class WebApkInfoTest {
private static final String ICON_MURMUR2_HASH = "5";
private static final int SOURCE = ShortcutSource.NOTIFICATION;
+ /** FakePackageManager allows setting up Resources for installed packages. */
+ private static class FakePackageManager extends DefaultPackageManager {
+ private final HashMap<String, Resources> mResourceMap;
+
+ public FakePackageManager() {
+ super();
pkotwicz 2017/06/20 19:39:48 Is this super call necessary?
Xi Han 2017/06/21 14:46:36 Removed.
+ mResourceMap = new HashMap<String, Resources>();
+ }
+
+ @Override
+ public Resources getResourcesForApplication(String appPackageName)
+ throws NameNotFoundException {
+ Resources result = mResourceMap.get(appPackageName);
+ if (result == null) throw new NameNotFoundException(appPackageName);
+
+ return result;
+ }
+
+ public void setResourcesForTest(String packageName, Resources resources) {
+ mResourceMap.put(packageName, resources);
+ }
+ }
+
+ /** Fakes the Resources object, allowing lookup of String value. */
+ private static class FakeResources extends Resources {
+ private final Map<String, Integer> mStringIdMap;
+ private final Map<Integer, String> mIdValueMap;
+
+ // Do not warn about deprecated call to Resources(); the documentation says code is not
+ // supposed to create its own Resources object, but we are using it to fake out the
+ // Resources, and there is no other way to do that.
+ @SuppressWarnings("deprecation")
+ public FakeResources() {
+ super(new AssetManager(), null, null);
+ mStringIdMap = new HashMap<String, Integer>();
+ mIdValueMap = new HashMap<Integer, String>();
+ }
+
+ @Override
+ public int getIdentifier(String name, String defType, String defPackage) {
+ if (name == null) throw new NullPointerException();
pkotwicz 2017/06/20 19:39:48 Nit: You don't need to check for null here since |
Xi Han 2017/06/21 14:46:36 Done.
+
+ return mStringIdMap.containsKey(name) ? mStringIdMap.get(name) : 0;
+ }
+
+ @Override
+ public String getString(int id) {
+ if (!mIdValueMap.containsKey(id)) {
+ throw new Resources.NotFoundException("id 0x" + Integer.toHexString(id));
+ }
+
+ return mIdValueMap.get(id);
+ }
+
+ public void addStringForTesting(String string, int identifier, String value) {
pkotwicz 2017/06/20 19:39:48 Nit: 'string' -> 'identifierName'
Xi Han 2017/06/21 14:46:36 Done.
+ mStringIdMap.put(string, identifier);
+ mIdValueMap.put(identifier, value);
+ }
+ }
+
@Before
public void setUp() {
ContextUtils.initApplicationContextForTests(RuntimeEnvironment.application);
@@ -248,4 +312,33 @@ public class WebApkInfoTest {
WebApkInfo info = WebApkInfo.create(intent);
Assert.assertEquals(ShortcutSource.UNKNOWN, info.source());
}
+
+ /**
+ * Test that {@link WebApkInfo#name()} and {@link WebApkInfo#shortName()} return the string
+ * values from the WebAPK resources if exist.
+ */
+ @Test
+ public void testNameAndShortNameFromWebApkStrings() {
+ FakePackageManager packageManager = new FakePackageManager();
+ RuntimeEnvironment.setRobolectricPackageManager(packageManager);
+
+ Bundle bundle = new Bundle();
+ bundle.putString(WebApkMetaDataKeys.START_URL, START_URL);
+ WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, bundle);
+
+ String name = "@string/name";
pkotwicz 2017/06/20 19:39:48 Shouldn't |name| and |shortName| have the value of
Xi Han 2017/06/21 14:46:36 Yes, I will use some other values.
+ String shortName = "@string/short_name";
+ FakeResources res = new FakeResources();
+ res.addStringForTesting(WEBAPK_PACKAGE_NAME + WebApkInfo.STRING_NAME, 1, name);
+ res.addStringForTesting(WEBAPK_PACKAGE_NAME + WebApkInfo.STRING_SHORT_NAME, 2, shortName);
+ packageManager.setResourcesForTest(WEBAPK_PACKAGE_NAME, res);
+
+ Intent intent = new Intent();
+ intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
+ intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
+
+ WebApkInfo info = WebApkInfo.create(intent);
+ Assert.assertEquals(name, info.name());
+ Assert.assertEquals(shortName, info.shortName());
+ }
}

Powered by Google App Engine
This is Rietveld 408576698