@Test public void testOnResponseReceived() { OnResponseListener onResponseListener = mock(OnResponseListener.class); OnResponseListener onResponseListener1 = mock(OnResponseListener.class); PersistentStatsHandler persistentStatsHandler = new PersistentStatsHandler(RuntimeEnvironment.application); persistentStatsHandler.addListener(onResponseListener); Assert.assertTrue(persistentStatsHandler.mOnResponseListeners.size() == 1); RequestStats requestStats = new RequestStats(1); persistentStatsHandler.onResponseReceived(requestStats); verify(onResponseListener, times(1)).onResponseSuccess(any(NetworkInfo.class), eq(requestStats)); reset(onResponseListener); persistentStatsHandler.addListener(onResponseListener1); Assert.assertTrue(persistentStatsHandler.mOnResponseListeners.size() == 2); persistentStatsHandler.onResponseReceived(requestStats); verify(onResponseListener, times(1)).onResponseSuccess(any(NetworkInfo.class), eq(requestStats)); verify(onResponseListener1, times(1)).onResponseSuccess(any(NetworkInfo.class), eq(requestStats)); }
@Override public void onResponseReceived(final RequestStats requestStats) { if (Utils.isLoggingEnabled) { Log.d("Response Received : ", requestStats + " "); } for (OnResponseListener onResponseListener : mOnResponseListeners) { if (onResponseListener != null) { onResponseListener.onResponseSuccess(getActiveNetworkInfo(), requestStats); } } synchronized (this) { mResponseCount += 1; if (mResponseCount >= MAX_SIZE) { double newAvgSpeed = mNetworkStat.mCurrentAvgSpeed; mCurrentAvgSpeed = (float) ((mCurrentAvgSpeed + newAvgSpeed) / 2); mConnectionQuality = ConnectionQuality.getConnectionQualityFromSpeed((int) mCurrentAvgSpeed); String networkKey = getNetworkKey(getActiveNetworkInfo()); mPreferenceManager.setAverageSpeed(networkKey, mCurrentAvgSpeed); mResponseCount = 0; } } mNetworkStat.addRequestStat(requestStats); }
PersistentStatsHandler implements NetworkRequestStatsHandler { @Override public void onResponseReceived(final RequestStats requestStats) { if (Utils.isLoggingEnabled) { Log.d("Response Received : ", requestStats + " "); } for (OnResponseListener onResponseListener : mOnResponseListeners) { if (onResponseListener != null) { onResponseListener.onResponseSuccess(getActiveNetworkInfo(), requestStats); } } synchronized (this) { mResponseCount += 1; if (mResponseCount >= MAX_SIZE) { double newAvgSpeed = mNetworkStat.mCurrentAvgSpeed; mCurrentAvgSpeed = (float) ((mCurrentAvgSpeed + newAvgSpeed) / 2); mConnectionQuality = ConnectionQuality.getConnectionQualityFromSpeed((int) mCurrentAvgSpeed); String networkKey = getNetworkKey(getActiveNetworkInfo()); mPreferenceManager.setAverageSpeed(networkKey, mCurrentAvgSpeed); mResponseCount = 0; } } mNetworkStat.addRequestStat(requestStats); } }
PersistentStatsHandler implements NetworkRequestStatsHandler { @Override public void onResponseReceived(final RequestStats requestStats) { if (Utils.isLoggingEnabled) { Log.d("Response Received : ", requestStats + " "); } for (OnResponseListener onResponseListener : mOnResponseListeners) { if (onResponseListener != null) { onResponseListener.onResponseSuccess(getActiveNetworkInfo(), requestStats); } } synchronized (this) { mResponseCount += 1; if (mResponseCount >= MAX_SIZE) { double newAvgSpeed = mNetworkStat.mCurrentAvgSpeed; mCurrentAvgSpeed = (float) ((mCurrentAvgSpeed + newAvgSpeed) / 2); mConnectionQuality = ConnectionQuality.getConnectionQualityFromSpeed((int) mCurrentAvgSpeed); String networkKey = getNetworkKey(getActiveNetworkInfo()); mPreferenceManager.setAverageSpeed(networkKey, mCurrentAvgSpeed); mResponseCount = 0; } } mNetworkStat.addRequestStat(requestStats); } PersistentStatsHandler(Context context); @VisibleForTesting  PersistentStatsHandler(Context context, PreferenceManager preferenceManager); }
PersistentStatsHandler implements NetworkRequestStatsHandler { @Override public void onResponseReceived(final RequestStats requestStats) { if (Utils.isLoggingEnabled) { Log.d("Response Received : ", requestStats + " "); } for (OnResponseListener onResponseListener : mOnResponseListeners) { if (onResponseListener != null) { onResponseListener.onResponseSuccess(getActiveNetworkInfo(), requestStats); } } synchronized (this) { mResponseCount += 1; if (mResponseCount >= MAX_SIZE) { double newAvgSpeed = mNetworkStat.mCurrentAvgSpeed; mCurrentAvgSpeed = (float) ((mCurrentAvgSpeed + newAvgSpeed) / 2); mConnectionQuality = ConnectionQuality.getConnectionQualityFromSpeed((int) mCurrentAvgSpeed); String networkKey = getNetworkKey(getActiveNetworkInfo()); mPreferenceManager.setAverageSpeed(networkKey, mCurrentAvgSpeed); mResponseCount = 0; } } mNetworkStat.addRequestStat(requestStats); } PersistentStatsHandler(Context context); @VisibleForTesting  PersistentStatsHandler(Context context, PreferenceManager preferenceManager); NetworkInfo getActiveNetworkInfo(); void addListener(OnResponseListener onResponseListener); void removeListener(OnResponseListener onResponseListener); void setMaxSizeForPersistence(int size); float getAverageNetworkSpeed(); ConnectionQuality getConnectionQuality(); @Override void onResponseReceived(final RequestStats requestStats); @Override void onHttpExchangeError(RequestStats requestStats, IOException e); @Override void onResponseInputStreamError(RequestStats requestStats, Exception e); }
PersistentStatsHandler implements NetworkRequestStatsHandler { @Override public void onResponseReceived(final RequestStats requestStats) { if (Utils.isLoggingEnabled) { Log.d("Response Received : ", requestStats + " "); } for (OnResponseListener onResponseListener : mOnResponseListeners) { if (onResponseListener != null) { onResponseListener.onResponseSuccess(getActiveNetworkInfo(), requestStats); } } synchronized (this) { mResponseCount += 1; if (mResponseCount >= MAX_SIZE) { double newAvgSpeed = mNetworkStat.mCurrentAvgSpeed; mCurrentAvgSpeed = (float) ((mCurrentAvgSpeed + newAvgSpeed) / 2); mConnectionQuality = ConnectionQuality.getConnectionQualityFromSpeed((int) mCurrentAvgSpeed); String networkKey = getNetworkKey(getActiveNetworkInfo()); mPreferenceManager.setAverageSpeed(networkKey, mCurrentAvgSpeed); mResponseCount = 0; } } mNetworkStat.addRequestStat(requestStats); } PersistentStatsHandler(Context context); @VisibleForTesting  PersistentStatsHandler(Context context, PreferenceManager preferenceManager); NetworkInfo getActiveNetworkInfo(); void addListener(OnResponseListener onResponseListener); void removeListener(OnResponseListener onResponseListener); void setMaxSizeForPersistence(int size); float getAverageNetworkSpeed(); ConnectionQuality getConnectionQuality(); @Override void onResponseReceived(final RequestStats requestStats); @Override void onHttpExchangeError(RequestStats requestStats, IOException e); @Override void onResponseInputStreamError(RequestStats requestStats, Exception e);  }