Bug 1341527 - Move UserManager mocking into helper class r=sebastian
authorAndrzej Hunt <ahunt@mozilla.com>
Thu, 23 Feb 2017 14:08:51 -0800
changeset 392050 9f67d8a507479b15022bce420752b6d7c456a01e
parent 392049 093c7e787c9b1523b3364fdaac7a4481106002ca
child 392051 a0d02ea2773152926486f5b39f6fe83954e5c400
push id7198
push userjlorenzo@mozilla.com
push dateTue, 18 Apr 2017 12:07:49 +0000
treeherdermozilla-beta@d57aa49c3948 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1341527
milestone54.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1341527 - Move UserManager mocking into helper class r=sebastian We need this for more than one test, let's have one copy we can call from wherever it's needed. MozReview-Commit-ID: Bd0o38KcqQc
mobile/android/tests/background/junit4/src/org/mozilla/gecko/helpers/MockUserManager.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/icons/loader/TestSuggestedSiteLoader.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/helpers/MockUserManager.java
@@ -0,0 +1,42 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * 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.helpers;
+
+import android.annotation.TargetApi;
+import android.content.Context;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.UserManager;
+
+import org.mozilla.gecko.AppConstants;
+import org.robolectric.RuntimeEnvironment;
+
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+public class MockUserManager {
+
+    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
+    public static Context getContextWithMockedUserManager() {
+        // This seems to be done automatically in newer versions of RoboElectric, we need our own
+        // version for now:
+        final Context context;
+
+        if (!AppConstants.Versions.preJBMR2) {
+            context = spy(RuntimeEnvironment.application);
+
+            final UserManager userManager = spy((UserManager) context.getSystemService(Context.USER_SERVICE));
+            doReturn(new Bundle()).when(userManager).getApplicationRestrictions(anyString());
+            doReturn(new Bundle()).when(userManager).getUserRestrictions();
+
+            doReturn(userManager).when(context).getSystemService(Context.USER_SERVICE);
+        } else {
+            context = RuntimeEnvironment.application;
+        }
+
+        return context;
+    }
+}
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/icons/loader/TestSuggestedSiteLoader.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/icons/loader/TestSuggestedSiteLoader.java
@@ -5,37 +5,31 @@
 package org.mozilla.gecko.icons.loader;
 
 import android.annotation.TargetApi;
 import android.content.Context;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.Color;
 import android.os.Build;
-import android.os.Bundle;
-import android.os.UserManager;
-
-import com.squareup.picasso.RequestCreator;
 
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mozilla.gecko.AppConstants;
-import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.SuggestedSites;
+import org.mozilla.gecko.helpers.MockUserManager;
 import org.mozilla.gecko.icons.IconDescriptor;
 import org.mozilla.gecko.icons.IconRequest;
 import org.mozilla.gecko.icons.IconRequestBuilder;
 import org.mozilla.gecko.icons.IconResponse;
 import org.mozilla.gecko.icons.Icons;
-import org.mozilla.gecko.restrictions.Restrictions;
 import org.robolectric.RuntimeEnvironment;
 
 import java.io.IOException;
 
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
@@ -51,29 +45,17 @@ public class TestSuggestedSiteLoader {
             "https://www.amazon.com/",
             "https://www.wikipedia.org/",
             "https://mobile.twitter.com/"
     };
 
     @Before
     @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
     public void setUp() throws IOException {
-        // This seems to be done automatically in newer versions of RoboElectric, we need our own
-        // version for now:
-        if (!AppConstants.Versions.preJBMR2) {
-            context = spy(RuntimeEnvironment.application);
-
-            UserManager userManager = spy((UserManager) context.getSystemService(Context.USER_SERVICE));
-            doReturn(new Bundle()).when(userManager).getApplicationRestrictions(anyString());
-            doReturn(new Bundle()).when(userManager).getUserRestrictions();
-
-            doReturn(userManager).when(context).getSystemService(Context.USER_SERVICE);
-        } else {
-            context = RuntimeEnvironment.application;
-        }
+        context = MockUserManager.getContextWithMockedUserManager();
 
         final SuggestedSites sites = new SuggestedSites(context);
         BrowserDB.from(context).setSuggestedSites(sites);
 
         // Force loading sites, so that SuggestedSites is correctly initialised. Without this, we
         // won't be able to retrieve site data (i.e. icon locations and colours).
         final Cursor sitesCursor = sites.get(200);
         sitesCursor.close();