@@ -126,6 +126,11 @@ public class ConfigResolverTest extends FirebasePerformanceTestBase {
126
126
private static final String SESSIONS_MAX_DURATION_MIN_CACHE_KEY =
127
127
"com.google.firebase.perf.SessionsMaxDurationMinutes" ;
128
128
129
+ // Fragment trace sampling rate flags
130
+ private static final String FRAGMENT_SAMPLING_RATE_FRC_KEY = "fpr_vc_fragment_sampling_rate" ;
131
+ private static final String FRAGMENT_SAMPLING_RATE_CACHE_KEY =
132
+ "com.google.firebase.perf.FragmentSamplingRate" ;
133
+
129
134
private ConfigResolver testConfigResolver ;
130
135
131
136
@ Mock private RemoteConfigManager mockRemoteConfigManager ;
@@ -2482,4 +2487,237 @@ public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsCache() {
2482
2487
verify (mockDeviceCacheManager , times (1 ))
2483
2488
.setValue (eq ("com.google.firebase.perf.LogSourceName" ), eq ("FIREPERF_INTERNAL_LOW" ));
2484
2489
}
2490
+
2491
+ @ Test
2492
+ public void getFragmentSamplingRate_validMetadata_returnsMetadata () {
2493
+ // #1 pass: Validate that method returns Remote Config Value when there is no metadata value.
2494
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2495
+ .thenReturn (Optional .of (0.01f ));
2496
+
2497
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.01f );
2498
+
2499
+ // #2 pass: Validate that method returns Metadata value which takes higher precedence.
2500
+ Bundle bundle = new Bundle ();
2501
+ bundle .putFloat ("fragment_sampling_percentage" , 20.0f );
2502
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2503
+
2504
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.2f );
2505
+ }
2506
+
2507
+ @ Test
2508
+ public void getFragmentSamplingRate_validMetadata_notSaveMetadataInCache () {
2509
+ Bundle bundle = new Bundle ();
2510
+ bundle .putFloat ("fragment_sampling_percentage" , 20.0f );
2511
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2512
+
2513
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.2f );
2514
+
2515
+ verify (mockDeviceCacheManager , never ()).setValue (any (), any ());
2516
+ }
2517
+
2518
+ @ Test
2519
+ public void getFragmentSamplingRate_invalidAndroidMetadataBundle_returnDefaultValue () {
2520
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2521
+ .thenReturn (Optional .absent ());
2522
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2523
+ .thenReturn (Optional .absent ());
2524
+
2525
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2526
+
2527
+ // Case #1: Android Metadata bundle value is too high.
2528
+ Bundle bundle = new Bundle ();
2529
+ bundle .putFloat ("fragment_sampling_percentage" , 200.00f );
2530
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2531
+
2532
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2533
+
2534
+ // Case #2: Android Metadata bundle value is too low.
2535
+ bundle = new Bundle ();
2536
+ bundle .putFloat ("fragment_sampling_percentage" , -1.00f );
2537
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2538
+
2539
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2540
+ }
2541
+
2542
+ @ Test
2543
+ public void getFragmentSamplingRate_invalidAndroidMetadataBundle_returnRemoteConfigValue () {
2544
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2545
+ .thenReturn (Optional .of (0.25f ));
2546
+
2547
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.25f );
2548
+
2549
+ // Case #1: Android Metadata bundle value is too high.
2550
+ Bundle bundle = new Bundle ();
2551
+ bundle .putFloat ("fragment_sampling_percentage" , 200.00f );
2552
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2553
+
2554
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.25f );
2555
+
2556
+ // Case #2: Android Metadata bundle value is too low.
2557
+ bundle = new Bundle ();
2558
+ bundle .putFloat ("fragment_sampling_percentage" , -1.00f );
2559
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2560
+
2561
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.25f );
2562
+ }
2563
+
2564
+ @ Test
2565
+ public void getFragmentSamplingRate_invalidMetadataBundle_returnCacheValue () {
2566
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2567
+ .thenReturn (Optional .absent ());
2568
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2569
+ .thenReturn (Optional .of (1.0f ));
2570
+
2571
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2572
+
2573
+ // Case #1: Android Metadata bundle value is too high.
2574
+ Bundle bundle = new Bundle ();
2575
+ bundle .putFloat ("fragment_sampling_percentage" , 200.00f );
2576
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2577
+
2578
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2579
+
2580
+ // Case #2: Android Metadata bundle value is too low.
2581
+ bundle = new Bundle ();
2582
+ bundle .putFloat ("fragment_sampling_percentage" , -1.00f );
2583
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2584
+
2585
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2586
+ }
2587
+
2588
+ @ Test
2589
+ public void getFragmentSamplingRate_validRemoteConfig_returnRemoteConfigValue () {
2590
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2591
+ .thenReturn (Optional .of (0.25f ));
2592
+
2593
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.25f );
2594
+ verify (mockDeviceCacheManager , times (1 ))
2595
+ .setValue (eq (FRAGMENT_SAMPLING_RATE_CACHE_KEY ), eq (0.25f ));
2596
+
2597
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2598
+ .thenReturn (Optional .of (0.0f ));
2599
+
2600
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.0f );
2601
+ verify (mockDeviceCacheManager , times (1 ))
2602
+ .setValue (eq (FRAGMENT_SAMPLING_RATE_CACHE_KEY ), eq (0.0f ));
2603
+
2604
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2605
+ .thenReturn (Optional .of (0.00005f ));
2606
+
2607
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.00005f );
2608
+ verify (mockDeviceCacheManager , times (1 ))
2609
+ .setValue (eq (FRAGMENT_SAMPLING_RATE_CACHE_KEY ), eq (0.00005f ));
2610
+
2611
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2612
+ .thenReturn (Optional .of (0.0000000001f ));
2613
+
2614
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.0000000001f );
2615
+ verify (mockDeviceCacheManager , times (1 ))
2616
+ .setValue (eq (FRAGMENT_SAMPLING_RATE_CACHE_KEY ), eq (0.0000000001f ));
2617
+ }
2618
+
2619
+ @ Test
2620
+ public void getFragmentSamplingRate_invalidRemoteConfig_returnDefaultValue () {
2621
+ // Mock behavior that device cache doesn't have session sampling rate value.
2622
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2623
+ .thenReturn (Optional .absent ());
2624
+
2625
+ // Case #1: Firebase Remote Config value is too high.
2626
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2627
+ .thenReturn (Optional .of (1.01f ));
2628
+
2629
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2630
+ verify (mockDeviceCacheManager , never ()).setValue (any (), any ());
2631
+
2632
+ // Case #2: Firebase Remote Config value is too low.
2633
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2634
+ .thenReturn (Optional .of (-0.1f ));
2635
+
2636
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2637
+ verify (mockDeviceCacheManager , never ()).setValue (any (), any ());
2638
+ }
2639
+
2640
+ @ Test
2641
+ public void getFragmentSamplingRate_invalidRemoteConfig_returnCacheValue () {
2642
+ // Mock behavior that device cache doesn't have session sampling rate value.
2643
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2644
+ .thenReturn (Optional .of (0.25f ));
2645
+
2646
+ // Case #1: Firebase Remote Config value is too high.
2647
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2648
+ .thenReturn (Optional .of (1.01f ));
2649
+
2650
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.25f );
2651
+ verify (mockDeviceCacheManager , never ()).setValue (any (), any ());
2652
+
2653
+ // Case #2: Firebase Remote Config value is too low.
2654
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2655
+ .thenReturn (Optional .of (-0.1f ));
2656
+
2657
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.25f );
2658
+ verify (mockDeviceCacheManager , never ()).setValue (any (), any ());
2659
+ }
2660
+
2661
+ @ Test
2662
+ public void getFragmentSamplingRate_validCache_returnCacheValue () {
2663
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2664
+ .thenReturn (Optional .of (1.0f ));
2665
+
2666
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2667
+ .thenReturn (Optional .absent ());
2668
+
2669
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2670
+ }
2671
+
2672
+ @ Test
2673
+ public void getFragmentSamplingRate_invalidCache_returnDefaultValue () {
2674
+ // Mock behavior that remote config doesn't have session sampling rate value.
2675
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2676
+ .thenReturn (Optional .absent ());
2677
+
2678
+ // Case #1: Device Cache value is too high.
2679
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2680
+ .thenReturn (Optional .of (10.0f ));
2681
+
2682
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2683
+
2684
+ // Case #2: Device Cache value is too low.
2685
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2686
+ .thenReturn (Optional .of (-1.0f ));
2687
+
2688
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (1.0f );
2689
+ }
2690
+
2691
+ @ Test
2692
+ public void
2693
+ getFragmentSamplingRate_metadataAndRemoteConfigAndCacheAreSet_metadataHasHighestConfigPrecedence () {
2694
+ // Set cache value.
2695
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2696
+ .thenReturn (Optional .of (0.2f ));
2697
+
2698
+ // Set remote config value.
2699
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2700
+ .thenReturn (Optional .of (0.3f ));
2701
+
2702
+ // Set Android Manifest value.
2703
+ Bundle bundle = new Bundle ();
2704
+ bundle .putFloat ("fragment_sampling_percentage" , 4.0f );
2705
+ testConfigResolver .setMetadataBundle (new ImmutableBundle (bundle ));
2706
+
2707
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.04f );
2708
+ }
2709
+
2710
+ @ Test
2711
+ public void
2712
+ getFragmentSamplingRate_remoteConfigAndCacheAreSet_remoteConfigHasHighestConfigPrecedence () {
2713
+ // Set cache value.
2714
+ when (mockDeviceCacheManager .getFloat (FRAGMENT_SAMPLING_RATE_CACHE_KEY ))
2715
+ .thenReturn (Optional .of (0.2f ));
2716
+
2717
+ // Set remote config value.
2718
+ when (mockRemoteConfigManager .getFloat (FRAGMENT_SAMPLING_RATE_FRC_KEY ))
2719
+ .thenReturn (Optional .of (0.3f ));
2720
+
2721
+ assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.3f );
2722
+ }
2485
2723
}
0 commit comments