### Question:
AdsUtilitiesUserAgentProvider implements UserAgentProvider { @Override public String getUserAgent() { Set<AdsUtility> adsUtilities = adsUtilityRegistry.popRegisteredUtilities(); if (!adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent() || adsUtilities.isEmpty()) { return null; } return Joiner.on(", ").skipNulls().join( Sets.newTreeSet(Collections2.transform(adsUtilities, ADS_UTILITY_FUNCTION))); } @Inject  AdsUtilitiesUserAgentProvider(
      AdsUtilityRegistry adsUtilityRegistry, AdsLibConfiguration adsLibConfiguration); @Override String getUserAgent();  }### Answer:
@Test public void testGenerateUserAgent_utilityRegistered_includeUtilitiesTrue() { when(adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent()).thenReturn(true); adsUtilityRegistry.addUtility(AdsUtility.PRODUCT_PARTITION_TREE); String actualUserAgent = userAgentProvider.getUserAgent(); assertEquals("User agent should include registered utilities if include utilities is true", AdsUtility.PRODUCT_PARTITION_TREE.getUserAgentIdentifier(), actualUserAgent); }
@Test public void testGenerateUserAgent_multipleUtilitiesRegistered_includeUtilitiesTrue() { when(adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent()).thenReturn(true); List<AdsUtility> adsUtilities = Arrays.asList( AdsUtility.PRODUCT_PARTITION_TREE, AdsUtility.SELECTOR_BUILDER, AdsUtility.REPORT_DOWNLOADER); for (int i = 0; i < adsUtilities.size(); i++) { Collections.shuffle(adsUtilities); for (AdsUtility adsUtility : adsUtilities) { adsUtilityRegistry.addUtility(adsUtility); } String expectedUserAgent = "ProductPartitionTree, ReportDownloader, SelectorBuilder"; String actualUserAgent = userAgentProvider.getUserAgent(); assertEquals( "User agent should include registered utilities in alphabetical order if include " + "utilities is true", expectedUserAgent, actualUserAgent); assertTrue("User agent provider should clear utilities", adsUtilityRegistry.popRegisteredUtilities().isEmpty()); } }
@Test public void testGenerateUserAgent_noUtilitiesRegistered_includeUtilitiesTrue() { when(adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent()).thenReturn(true); String actualUserAgent = userAgentProvider.getUserAgent(); assertNull("User agent should be null if no utilities are registered", actualUserAgent); assertTrue("User agent provider should clear utilities", adsUtilityRegistry.popRegisteredUtilities().isEmpty()); }
@Test public void testGenerateUserAgent_utilitiesRegistered_includeUtilitiesFalse() { when(adsLibConfiguration.isIncludeAdsUtilitiesInUserAgent()).thenReturn(false); adsUtilityRegistry.addUtility(AdsUtility.PRODUCT_PARTITION_TREE); String actualUserAgent = userAgentProvider.getUserAgent(); assertNull("User agent should be null if include utilities is false", actualUserAgent); assertTrue("User agent provider should clear utilities", adsUtilityRegistry.popRegisteredUtilities().isEmpty()); }