|
32 | 32 | import android.content.Context;
|
33 | 33 | import android.os.Bundle;
|
34 | 34 | import android.view.WindowManager.LayoutParams;
|
| 35 | +import com.google.firebase.perf.FirebasePerformanceInitializer; |
35 | 36 | import com.google.firebase.perf.FirebasePerformanceTestBase;
|
36 | 37 | import com.google.firebase.perf.config.ConfigResolver;
|
37 | 38 | import com.google.firebase.perf.config.DeviceCacheManager;
|
@@ -675,51 +676,106 @@ public void backgroundTrace_perfMonDeactivated_traceCreated() {
|
675 | 676 | }
|
676 | 677 |
|
677 | 678 | @Test
|
678 |
| - public void activityStateChanges_singleClient_callbackIsCalled() { |
| 679 | + public void activityStateChanges_singleSubscriber_callbackIsCalled() { |
679 | 680 | AppStateMonitor monitor = new AppStateMonitor(transportManager, mClock);
|
680 |
| - Map<Integer, ApplicationProcessState> clientState = new HashMap<>(); |
| 681 | + Map<Integer, ApplicationProcessState> subscriberState = new HashMap<>(); |
681 | 682 |
|
682 |
| - final int client1 = 1; |
| 683 | + final int subscriber1 = 1; |
683 | 684 | monitor.registerForAppState(
|
684 |
| - new WeakReference<>(newState -> clientState.put(client1, newState))); |
| 685 | + new WeakReference<>(newState -> subscriberState.put(subscriber1, newState))); |
685 | 686 |
|
686 | 687 | // Activity comes to Foreground
|
687 | 688 | monitor.onActivityResumed(activity1);
|
688 |
| - assertThat(clientState.get(client1)).isEqualTo(ApplicationProcessState.FOREGROUND); |
| 689 | + assertThat(subscriberState.get(subscriber1)).isEqualTo(ApplicationProcessState.FOREGROUND); |
689 | 690 |
|
690 | 691 | // Activity goes to Background
|
691 | 692 | monitor.onActivityStopped(activity1);
|
692 |
| - assertThat(clientState.get(client1)).isEqualTo(ApplicationProcessState.BACKGROUND); |
| 693 | + assertThat(subscriberState.get(subscriber1)).isEqualTo(ApplicationProcessState.BACKGROUND); |
693 | 694 | }
|
694 | 695 |
|
695 | 696 | @Test
|
696 |
| - public void activityStateChanges_multipleClients_callbackCalledOnEachClient() { |
| 697 | + public void activityStateChanges_multipleSubscribers_callbackCalledOnEachSubscriber() { |
697 | 698 | AppStateMonitor monitor = new AppStateMonitor(transportManager, mClock);
|
698 |
| - Map<Integer, ApplicationProcessState> clientState = new HashMap<>(); |
| 699 | + Map<Integer, ApplicationProcessState> subscriberState = new HashMap<>(); |
699 | 700 |
|
700 |
| - final int client1 = 1; |
| 701 | + final int subscriber1 = 1; |
701 | 702 | monitor.registerForAppState(
|
702 |
| - new WeakReference<>(newState -> clientState.put(client1, newState))); |
| 703 | + new WeakReference<>(newState -> subscriberState.put(subscriber1, newState))); |
703 | 704 |
|
704 |
| - final int client2 = 2; |
| 705 | + final int subscriber2 = 2; |
705 | 706 | monitor.registerForAppState(
|
706 |
| - new WeakReference<>(newState -> clientState.put(client2, newState))); |
| 707 | + new WeakReference<>(newState -> subscriberState.put(subscriber2, newState))); |
707 | 708 |
|
708 |
| - final int client3 = 3; |
| 709 | + final int subscriber3 = 3; |
709 | 710 | monitor.registerForAppState(
|
710 |
| - new WeakReference<>(newState -> clientState.put(client3, newState))); |
| 711 | + new WeakReference<>(newState -> subscriberState.put(subscriber3, newState))); |
711 | 712 |
|
712 | 713 | // Activity comes to Foreground
|
713 | 714 | monitor.onActivityResumed(activity1);
|
714 |
| - assertThat(clientState.get(client1)).isEqualTo(ApplicationProcessState.FOREGROUND); |
715 |
| - assertThat(clientState.get(client2)).isEqualTo(ApplicationProcessState.FOREGROUND); |
716 |
| - assertThat(clientState.get(client3)).isEqualTo(ApplicationProcessState.FOREGROUND); |
| 715 | + assertThat(subscriberState.get(subscriber1)).isEqualTo(ApplicationProcessState.FOREGROUND); |
| 716 | + assertThat(subscriberState.get(subscriber2)).isEqualTo(ApplicationProcessState.FOREGROUND); |
| 717 | + assertThat(subscriberState.get(subscriber3)).isEqualTo(ApplicationProcessState.FOREGROUND); |
717 | 718 |
|
718 | 719 | // Activity goes to Background
|
719 | 720 | monitor.onActivityStopped(activity1);
|
720 |
| - assertThat(clientState.get(client1)).isEqualTo(ApplicationProcessState.BACKGROUND); |
721 |
| - assertThat(clientState.get(client2)).isEqualTo(ApplicationProcessState.BACKGROUND); |
722 |
| - assertThat(clientState.get(client3)).isEqualTo(ApplicationProcessState.BACKGROUND); |
| 721 | + assertThat(subscriberState.get(subscriber1)).isEqualTo(ApplicationProcessState.BACKGROUND); |
| 722 | + assertThat(subscriberState.get(subscriber2)).isEqualTo(ApplicationProcessState.BACKGROUND); |
| 723 | + assertThat(subscriberState.get(subscriber3)).isEqualTo(ApplicationProcessState.BACKGROUND); |
| 724 | + } |
| 725 | + |
| 726 | + @Test |
| 727 | + public void appColdStart_singleSubscriber_callbackIsCalled() { |
| 728 | + AppStateMonitor monitor = new AppStateMonitor(transportManager, mClock); |
| 729 | + FirebasePerformanceInitializer mockInitializer = mock(FirebasePerformanceInitializer.class); |
| 730 | + monitor.registerForAppColdStart(mockInitializer); |
| 731 | + |
| 732 | + // Activity comes to Foreground |
| 733 | + monitor.onActivityResumed(activity1); |
| 734 | + verify(mockInitializer, times(1)).onAppColdStart(); |
| 735 | + } |
| 736 | + |
| 737 | + @Test |
| 738 | + public void appHotStart_singleSubscriber_callbackIsNotCalled() { |
| 739 | + AppStateMonitor monitor = new AppStateMonitor(transportManager, mClock); |
| 740 | + FirebasePerformanceInitializer mockInitializer = mock(FirebasePerformanceInitializer.class); |
| 741 | + monitor.registerForAppColdStart(mockInitializer); |
| 742 | + |
| 743 | + // Activity comes to Foreground |
| 744 | + monitor.onActivityResumed(activity1); |
| 745 | + verify(mockInitializer, times(1)).onAppColdStart(); |
| 746 | + |
| 747 | + // Activity goes to Background |
| 748 | + monitor.onActivityStopped(activity1); |
| 749 | + |
| 750 | + // Activity comes to Foreground |
| 751 | + monitor.onActivityResumed(activity1); |
| 752 | + verify(mockInitializer, times(1)).onAppColdStart(); |
| 753 | + } |
| 754 | + |
| 755 | + @Test |
| 756 | + public void appColdStart_multipleSubscriber_callbackIsCalled() { |
| 757 | + AppStateMonitor monitor = new AppStateMonitor(transportManager, mClock); |
| 758 | + FirebasePerformanceInitializer mockInitializer1 = mock(FirebasePerformanceInitializer.class); |
| 759 | + FirebasePerformanceInitializer mockInitializer2 = mock(FirebasePerformanceInitializer.class); |
| 760 | + monitor.registerForAppColdStart(mockInitializer1); |
| 761 | + monitor.registerForAppColdStart(mockInitializer2); |
| 762 | + |
| 763 | + // Activity comes to Foreground |
| 764 | + monitor.onActivityResumed(activity1); |
| 765 | + verify(mockInitializer1, times(1)).onAppColdStart(); |
| 766 | + verify(mockInitializer2, times(1)).onAppColdStart(); |
| 767 | + } |
| 768 | + |
| 769 | + @Test |
| 770 | + public void appColdStart_singleSubscriberRegistersForMultipleTimes_oneCallbackIsCalled() { |
| 771 | + AppStateMonitor monitor = new AppStateMonitor(transportManager, mClock); |
| 772 | + FirebasePerformanceInitializer mockInitializer1 = mock(FirebasePerformanceInitializer.class); |
| 773 | + monitor.registerForAppColdStart(mockInitializer1); |
| 774 | + monitor.registerForAppColdStart(mockInitializer1); |
| 775 | + |
| 776 | + // Activity comes to Foreground |
| 777 | + monitor.onActivityResumed(activity1); |
| 778 | + verify(mockInitializer1, times(1)).onAppColdStart(); |
723 | 779 | }
|
724 | 780 |
|
725 | 781 | private static Activity createFakeActivity(boolean isHardwareAccelerated) {
|
|
0 commit comments