8080import org .easymock .Capture ;
8181import org .easymock .EasyMock ;
8282import org .easymock .IAnswer ;
83- import org .easymock .IExpectationSetters ;
8483
8584import java .io .File ;
8685import java .util .LinkedHashSet ;
@@ -537,6 +536,7 @@ public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
537536 .addIfaceValues (TEST_IFACE , 256L , 2L , 256L , 2L );
538537 expect (mStatsService .getSummaryForNetwork (sTemplateWifi , TIME_FEB_15 , TIME_MAR_10 ))
539538 .andReturn (stats ).atLeastOnce ();
539+ expectPolicyDataEnable (TYPE_WIFI , true );
540540
541541 // TODO: consider making strongly ordered mock
542542 expectRemoveInterfaceQuota (TEST_IFACE );
@@ -580,7 +580,7 @@ public void testOverWarningLimitNotification() throws Exception {
580580 NetworkState [] state = null ;
581581 NetworkStats stats = null ;
582582 Future <Void > future ;
583- Capture <String > tag ;
583+ Future <String > tagFuture ;
584584
585585 final long TIME_FEB_15 = 1171497600000L ;
586586 final long TIME_MAR_10 = 1173484800000L ;
@@ -598,6 +598,7 @@ public void testOverWarningLimitNotification() throws Exception {
598598 expect (mConnManager .getAllNetworkState ()).andReturn (state ).atLeastOnce ();
599599 expect (mStatsService .getSummaryForNetwork (sTemplateWifi , TIME_FEB_15 , currentTimeMillis ()))
600600 .andReturn (stats ).atLeastOnce ();
601+ expectPolicyDataEnable (TYPE_WIFI , true );
601602
602603 expectClearNotifications ();
603604 future = expectMeteredIfacesChanged ();
@@ -620,6 +621,7 @@ public void testOverWarningLimitNotification() throws Exception {
620621 expect (mConnManager .getAllNetworkState ()).andReturn (state ).atLeastOnce ();
621622 expect (mStatsService .getSummaryForNetwork (sTemplateWifi , TIME_FEB_15 , currentTimeMillis ()))
622623 .andReturn (stats ).atLeastOnce ();
624+ expectPolicyDataEnable (TYPE_WIFI , true );
623625
624626 expectRemoveInterfaceQuota (TEST_IFACE );
625627 expectSetInterfaceQuota (TEST_IFACE , 2 * MB_IN_BYTES );
@@ -642,14 +644,15 @@ public void testOverWarningLimitNotification() throws Exception {
642644 expectCurrentTime ();
643645 expect (mStatsService .getSummaryForNetwork (sTemplateWifi , TIME_FEB_15 , currentTimeMillis ()))
644646 .andReturn (stats ).atLeastOnce ();
647+ expectPolicyDataEnable (TYPE_WIFI , true );
645648
646649 expectForceUpdate ();
647650 expectClearNotifications ();
648- tag = expectEnqueueNotification ();
651+ tagFuture = expectEnqueueNotification ();
649652
650653 replay ();
651654 mNetworkObserver .limitReached (null , TEST_IFACE );
652- assertNotificationType (TYPE_WARNING , tag . getValue ());
655+ assertNotificationType (TYPE_WARNING , tagFuture . get ());
653656 verifyAndReset ();
654657 }
655658
@@ -662,15 +665,15 @@ public void testOverWarningLimitNotification() throws Exception {
662665 expectCurrentTime ();
663666 expect (mStatsService .getSummaryForNetwork (sTemplateWifi , TIME_FEB_15 , currentTimeMillis ()))
664667 .andReturn (stats ).atLeastOnce ();
665- expectPolicyDataEnable (TYPE_WIFI , false ). atLeastOnce () ;
668+ expectPolicyDataEnable (TYPE_WIFI , false );
666669
667670 expectForceUpdate ();
668671 expectClearNotifications ();
669- tag = expectEnqueueNotification ();
672+ tagFuture = expectEnqueueNotification ();
670673
671674 replay ();
672675 mNetworkObserver .limitReached (null , TEST_IFACE );
673- assertNotificationType (TYPE_LIMIT , tag . getValue ());
676+ assertNotificationType (TYPE_LIMIT , tagFuture . get ());
674677 verifyAndReset ();
675678 }
676679
@@ -682,21 +685,20 @@ public void testOverWarningLimitNotification() throws Exception {
682685 expect (mConnManager .getAllNetworkState ()).andReturn (state ).atLeastOnce ();
683686 expect (mStatsService .getSummaryForNetwork (sTemplateWifi , TIME_FEB_15 , currentTimeMillis ()))
684687 .andReturn (stats ).atLeastOnce ();
685- expectPolicyDataEnable (TYPE_WIFI , true ). atLeastOnce () ;
688+ expectPolicyDataEnable (TYPE_WIFI , true );
686689
687690 // snoozed interface still has high quota so background data is
688691 // still restricted.
689692 expectRemoveInterfaceQuota (TEST_IFACE );
690693 expectSetInterfaceQuota (TEST_IFACE , Long .MAX_VALUE );
694+ expectMeteredIfacesChanged (TEST_IFACE );
691695
692696 expectClearNotifications ();
693- tag = expectEnqueueNotification ();
694- future = expectMeteredIfacesChanged (TEST_IFACE );
697+ tagFuture = expectEnqueueNotification ();
695698
696699 replay ();
697700 mService .snoozePolicy (sTemplateWifi );
698- future .get ();
699- assertNotificationType (TYPE_LIMIT_SNOOZED , tag .getValue ());
701+ assertNotificationType (TYPE_LIMIT_SNOOZED , tagFuture .get ());
700702 verifyAndReset ();
701703 }
702704 }
@@ -737,9 +739,9 @@ private void expectClearNotifications() throws Exception {
737739 expectLastCall ().anyTimes ();
738740 }
739741
740- private Capture <String > expectEnqueueNotification () throws Exception {
741- final Capture <String > tag = new Capture <String >();
742- mNotifManager .enqueueNotificationWithTag (isA (String .class ), capture (tag ), anyInt (),
742+ private Future <String > expectEnqueueNotification () throws Exception {
743+ final FutureCapture <String > tag = new FutureCapture <String >();
744+ mNotifManager .enqueueNotificationWithTag (isA (String .class ), capture (tag . capture ), anyInt (),
743745 isA (Notification .class ), isA (int [].class ));
744746 return tag ;
745747 }
@@ -789,29 +791,42 @@ private Future<Void> expectMeteredIfacesChanged(String... ifaces) throws Excepti
789791 return future ;
790792 }
791793
792- private < T > IExpectationSetters < T > expectPolicyDataEnable (int type , boolean enabled )
793- throws Exception {
794+ private Future < Void > expectPolicyDataEnable (int type , boolean enabled ) throws Exception {
795+ final FutureAnswer future = new FutureAnswer ();
794796 mConnManager .setPolicyDataEnable (type , enabled );
795- return expectLastCall ();
797+ expectLastCall ().andAnswer (future );
798+ return future ;
796799 }
797800
798- private static class FutureAnswer extends AbstractFuture <Void > implements IAnswer < Void > {
801+ private static class TestAbstractFuture < T > extends AbstractFuture <T > {
799802 @ Override
800- public Void get () throws InterruptedException , ExecutionException {
803+ public T get () throws InterruptedException , ExecutionException {
801804 try {
802805 return get (5 , TimeUnit .SECONDS );
803806 } catch (TimeoutException e ) {
804807 throw new RuntimeException (e );
805808 }
806809 }
810+ }
807811
812+ private static class FutureAnswer extends TestAbstractFuture <Void > implements IAnswer <Void > {
808813 @ Override
809814 public Void answer () {
810815 set (null );
811816 return null ;
812817 }
813818 }
814819
820+ private static class FutureCapture <T > extends TestAbstractFuture <T > {
821+ public Capture <T > capture = new Capture <T >() {
822+ @ Override
823+ public void setValue (T value ) {
824+ super .setValue (value );
825+ set (value );
826+ }
827+ };
828+ }
829+
815830 private static class IdleFuture extends AbstractFuture <Void > implements IdleHandler {
816831 @ Override
817832 public Void get () throws InterruptedException , ExecutionException {
0 commit comments