Networking { @NonNull public static String getUserAgent(@NonNull Context context) { Preconditions.checkNotNull(context); String userAgent = sUserAgent; if (userAgent == null) { synchronized (Networking.class) { userAgent = sUserAgent; if (userAgent == null) { if (Looper.myLooper() == Looper.getMainLooper()) { userAgent = new WebView(context).getSettings().getUserAgentString(); } else { userAgent = System.getProperty("http.agent"); } sUserAgent = userAgent; } } } return userAgent; }  @Nullable static MoPubRequestQueue getRequestQueue(); @NonNull static MoPubRequestQueue getRequestQueue(@NonNull Context context); @NonNull static ImageLoader getImageLoader(@NonNull Context context); @NonNull static String getUserAgent(@NonNull Context context); @VisibleForTesting static synchronized void clearForTesting(); @VisibleForTesting static synchronized void setRequestQueueForTesting(MoPubRequestQueue queue); @VisibleForTesting static synchronized void setImageLoaderForTesting(MaxWidthImageLoader imageLoader); @VisibleForTesting static synchronized void setUserAgentForTesting(String userAgent); static void useHttps(boolean useHttps); static boolean useHttps(); static boolean sUseHttps; }
@Test public void getUserAgent_fromMainThread_shouldIncludeAndroid() throws InterruptedException { context.runOnUiThread(new Runnable() { @Override public void run() { String userAgent = Networking.getUserAgent(context); assertThat(userAgent).containsIgnoringCase("android"); } }); }
@Test public void getUserAgent_fromBackgroundThread_shouldIncludeAndroid() throws InterruptedException { System.setProperty("http.agent", "system level user agent"); final Semaphore semaphore = new Semaphore(0); new Thread(new Runnable() { @Override public void run() { sUserAgent = Networking.getUserAgent(context); semaphore.release(); } }).start(); semaphore.acquire(); assertThat(sUserAgent).isEqualTo("system level user agent"); }