@Test public void testAddOrUpdateDiscoveredPeer_Update2() throws Exception { HashMap<PeerProperties, Timestamp> mDiscoveredPeers = new HashMap<>(); mDiscoveredPeers.put(mMockPeerProperties, new Timestamp(1000L)); Field mDiscoveredPeersField = mPeerModel.getClass().getDeclaredField("mDiscoveredPeers"); mDiscoveredPeersField.setAccessible(true); mDiscoveredPeersField.set(mPeerModel, mDiscoveredPeers); doNothing().when(mMockListener).onPeerUpdated(isA(PeerProperties.class)); when(mMockDiscoveryManagerSettings.getPeerExpiration()).thenReturn(500L); when(mMockPeerProperties.hasMoreInformation(isA(PeerProperties.class))).thenReturn(false); mPeerModel.addOrUpdateDiscoveredPeer(mMockPeerProperties); verify(mMockListener, never()).onPeerUpdated(isA(PeerProperties.class)); verify(mMockListener, never()).onPeerAdded(isA(PeerProperties.class)); verify(mMockDiscoveryManagerSettings, times(1)).getPeerExpiration(); Field discoveredPeersField = mPeerModel.getClass().getDeclaredField("mDiscoveredPeers"); discoveredPeersField.setAccessible(true); @SuppressWarnings("unchecked") HashMap<PeerProperties, Timestamp> peers = (HashMap<PeerProperties, Timestamp>) discoveredPeersField.get(mPeerModel); assertThat("The peer is added", peers.size(), is(1)); }
public void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate) { synchronized (this) { if (peerPropertiesToAddOrUpdate != null) { Log.v(TAG, "addOrUpdateDiscoveredPeer: " + peerPropertiesToAddOrUpdate.toString()); PeerProperties oldPeerProperties = removePeer(peerPropertiesToAddOrUpdate); if (oldPeerProperties != null) { boolean extraInformationDiffers = (peerPropertiesToAddOrUpdate.getExtraInformation() != oldPeerProperties.getExtraInformation()); PeerProperties.copyMissingValuesFromOldPeer(oldPeerProperties, peerPropertiesToAddOrUpdate); boolean hasMoreInfo = peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties); Log.d(TAG, "has more info: " + hasMoreInfo + ", extra info differs: " + extraInformationDiffers); if (peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties) || extraInformationDiffers) { Log.d(TAG, "Want to call onPeerUpdated. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerUpdated(peerPropertiesToAddOrUpdate); } } } else { Log.d(TAG, "Want to call onPeerAdded. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerAdded(peerPropertiesToAddOrUpdate); } } mDiscoveredPeers.put(peerPropertiesToAddOrUpdate, new Timestamp(new Date().getTime())); Log.v(TAG, "addOrUpdateDiscoveredPeer: " + ((oldPeerProperties == null) ? ("New peer, " + peerPropertiesToAddOrUpdate.toString() + ", added") : ("Timestamp of peer " + peerPropertiesToAddOrUpdate.toString() + " updated")) + " - the peer count is " + mDiscoveredPeers.size()); if (mCheckExpiredPeersTimer == null) { createCheckPeerExpirationTimer(); mCheckExpiredPeersTimer.start(); } } } }
PeerModel { public void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate) { synchronized (this) { if (peerPropertiesToAddOrUpdate != null) { Log.v(TAG, "addOrUpdateDiscoveredPeer: " + peerPropertiesToAddOrUpdate.toString()); PeerProperties oldPeerProperties = removePeer(peerPropertiesToAddOrUpdate); if (oldPeerProperties != null) { boolean extraInformationDiffers = (peerPropertiesToAddOrUpdate.getExtraInformation() != oldPeerProperties.getExtraInformation()); PeerProperties.copyMissingValuesFromOldPeer(oldPeerProperties, peerPropertiesToAddOrUpdate); boolean hasMoreInfo = peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties); Log.d(TAG, "has more info: " + hasMoreInfo + ", extra info differs: " + extraInformationDiffers); if (peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties) || extraInformationDiffers) { Log.d(TAG, "Want to call onPeerUpdated. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerUpdated(peerPropertiesToAddOrUpdate); } } } else { Log.d(TAG, "Want to call onPeerAdded. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerAdded(peerPropertiesToAddOrUpdate); } } mDiscoveredPeers.put(peerPropertiesToAddOrUpdate, new Timestamp(new Date().getTime())); Log.v(TAG, "addOrUpdateDiscoveredPeer: " + ((oldPeerProperties == null) ? ("New peer, " + peerPropertiesToAddOrUpdate.toString() + ", added") : ("Timestamp of peer " + peerPropertiesToAddOrUpdate.toString() + " updated")) + " - the peer count is " + mDiscoveredPeers.size()); if (mCheckExpiredPeersTimer == null) { createCheckPeerExpirationTimer(); mCheckExpiredPeersTimer.start(); } } } } }
PeerModel { public void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate) { synchronized (this) { if (peerPropertiesToAddOrUpdate != null) { Log.v(TAG, "addOrUpdateDiscoveredPeer: " + peerPropertiesToAddOrUpdate.toString()); PeerProperties oldPeerProperties = removePeer(peerPropertiesToAddOrUpdate); if (oldPeerProperties != null) { boolean extraInformationDiffers = (peerPropertiesToAddOrUpdate.getExtraInformation() != oldPeerProperties.getExtraInformation()); PeerProperties.copyMissingValuesFromOldPeer(oldPeerProperties, peerPropertiesToAddOrUpdate); boolean hasMoreInfo = peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties); Log.d(TAG, "has more info: " + hasMoreInfo + ", extra info differs: " + extraInformationDiffers); if (peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties) || extraInformationDiffers) { Log.d(TAG, "Want to call onPeerUpdated. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerUpdated(peerPropertiesToAddOrUpdate); } } } else { Log.d(TAG, "Want to call onPeerAdded. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerAdded(peerPropertiesToAddOrUpdate); } } mDiscoveredPeers.put(peerPropertiesToAddOrUpdate, new Timestamp(new Date().getTime())); Log.v(TAG, "addOrUpdateDiscoveredPeer: " + ((oldPeerProperties == null) ? ("New peer, " + peerPropertiesToAddOrUpdate.toString() + ", added") : ("Timestamp of peer " + peerPropertiesToAddOrUpdate.toString() + " updated")) + " - the peer count is " + mDiscoveredPeers.size()); if (mCheckExpiredPeersTimer == null) { createCheckPeerExpirationTimer(); mCheckExpiredPeersTimer.start(); } } } } PeerModel(Listener listener, DiscoveryManagerSettings settings); }
PeerModel { public void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate) { synchronized (this) { if (peerPropertiesToAddOrUpdate != null) { Log.v(TAG, "addOrUpdateDiscoveredPeer: " + peerPropertiesToAddOrUpdate.toString()); PeerProperties oldPeerProperties = removePeer(peerPropertiesToAddOrUpdate); if (oldPeerProperties != null) { boolean extraInformationDiffers = (peerPropertiesToAddOrUpdate.getExtraInformation() != oldPeerProperties.getExtraInformation()); PeerProperties.copyMissingValuesFromOldPeer(oldPeerProperties, peerPropertiesToAddOrUpdate); boolean hasMoreInfo = peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties); Log.d(TAG, "has more info: " + hasMoreInfo + ", extra info differs: " + extraInformationDiffers); if (peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties) || extraInformationDiffers) { Log.d(TAG, "Want to call onPeerUpdated. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerUpdated(peerPropertiesToAddOrUpdate); } } } else { Log.d(TAG, "Want to call onPeerAdded. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerAdded(peerPropertiesToAddOrUpdate); } } mDiscoveredPeers.put(peerPropertiesToAddOrUpdate, new Timestamp(new Date().getTime())); Log.v(TAG, "addOrUpdateDiscoveredPeer: " + ((oldPeerProperties == null) ? ("New peer, " + peerPropertiesToAddOrUpdate.toString() + ", added") : ("Timestamp of peer " + peerPropertiesToAddOrUpdate.toString() + " updated")) + " - the peer count is " + mDiscoveredPeers.size()); if (mCheckExpiredPeersTimer == null) { createCheckPeerExpirationTimer(); mCheckExpiredPeersTimer.start(); } } } } PeerModel(Listener listener, DiscoveryManagerSettings settings); void addListener(Listener listener); void removeListener(Listener listener); void clear(); void onPeerExpirationTimeChanged(); synchronized PeerProperties getDiscoveredPeerByBluetoothMacAddress(final String bluetoothMacAddress); synchronized PeerProperties getDiscoveredPeerByDeviceAddress(final String deviceAddress); synchronized PeerProperties removePeer(PeerProperties peerPropertiesToRemove); void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate); synchronized void checkListForExpiredPeers(); }
PeerModel { public void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate) { synchronized (this) { if (peerPropertiesToAddOrUpdate != null) { Log.v(TAG, "addOrUpdateDiscoveredPeer: " + peerPropertiesToAddOrUpdate.toString()); PeerProperties oldPeerProperties = removePeer(peerPropertiesToAddOrUpdate); if (oldPeerProperties != null) { boolean extraInformationDiffers = (peerPropertiesToAddOrUpdate.getExtraInformation() != oldPeerProperties.getExtraInformation()); PeerProperties.copyMissingValuesFromOldPeer(oldPeerProperties, peerPropertiesToAddOrUpdate); boolean hasMoreInfo = peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties); Log.d(TAG, "has more info: " + hasMoreInfo + ", extra info differs: " + extraInformationDiffers); if (peerPropertiesToAddOrUpdate.hasMoreInformation(oldPeerProperties) || extraInformationDiffers) { Log.d(TAG, "Want to call onPeerUpdated. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerUpdated(peerPropertiesToAddOrUpdate); } } } else { Log.d(TAG, "Want to call onPeerAdded. Listeners size = " + mListeners.size()); for (Listener listener : mListeners) { listener.onPeerAdded(peerPropertiesToAddOrUpdate); } } mDiscoveredPeers.put(peerPropertiesToAddOrUpdate, new Timestamp(new Date().getTime())); Log.v(TAG, "addOrUpdateDiscoveredPeer: " + ((oldPeerProperties == null) ? ("New peer, " + peerPropertiesToAddOrUpdate.toString() + ", added") : ("Timestamp of peer " + peerPropertiesToAddOrUpdate.toString() + " updated")) + " - the peer count is " + mDiscoveredPeers.size()); if (mCheckExpiredPeersTimer == null) { createCheckPeerExpirationTimer(); mCheckExpiredPeersTimer.start(); } } } } PeerModel(Listener listener, DiscoveryManagerSettings settings); void addListener(Listener listener); void removeListener(Listener listener); void clear(); void onPeerExpirationTimeChanged(); synchronized PeerProperties getDiscoveredPeerByBluetoothMacAddress(final String bluetoothMacAddress); synchronized PeerProperties getDiscoveredPeerByDeviceAddress(final String deviceAddress); synchronized PeerProperties removePeer(PeerProperties peerPropertiesToRemove); void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate); synchronized void checkListForExpiredPeers();  }