Bug 1246816 - Add ContextUtils.getPackageInstallTime. r=sebastian a=ritu

MozReview-Commit-ID: JjGcoSwahUF
This commit is contained in:
Michael Comella 2016-03-31 14:32:12 -07:00
parent bb7ca40347
commit 5e42c38afb
3 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,27 @@
/*
* 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.util;
import android.content.Context;
import android.content.pm.PackageManager;
public class ContextUtils {
private ContextUtils() {}
/**
* @return {@link android.content.pm.PackageInfo#firstInstallTime} for the context's package.
* @throws PackageManager.NameNotFoundException Unexpected - we get the package name from the context so
* it's expected to be found.
*/
public static long getPackageInstallTime(final Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime;
} catch (final PackageManager.NameNotFoundException e) {
throw new AssertionError("Should not happen: could not get package info for own package");
}
}
}

View File

@ -97,6 +97,7 @@ gujar.sources += ['java/org/mozilla/gecko/' + x for x in [
'util/BundleEventListener.java',
'util/Clipboard.java',
'util/ColorUtils.java',
'util/ContextUtils.java',
'util/DrawableUtil.java',
'util/EventCallback.java',
'util/FileUtils.java',

View File

@ -0,0 +1,38 @@
/*
* 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.util;
import android.content.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mozilla.gecko.background.testhelpers.TestRunner;
import org.robolectric.RuntimeEnvironment;
import static org.junit.Assert.*;
/**
* Unit test methods of the ContextUtils class.
*/
@RunWith(TestRunner.class)
public class TestContextUtils {
private Context context;
@Before
public void setUp() {
context = RuntimeEnvironment.application;
}
@Test
public void testGetPackageInstallTimeReturnsReasonableValue() throws Exception {
// At the time of writing, Robolectric's value is 0, which is reasonable.
final long installTime = ContextUtils.getPackageInstallTime(context);
assertTrue("Package install time is positive", installTime >= 0);
assertTrue("Package install time is less than current time", installTime < System.currentTimeMillis());
}
}