/chrome/android/junit/src/org/chromium/chrome/browser/firstrun/ChildAccountStatusSupplierTest.java

https://github.com/chromium/chromium · Java · 178 lines · 137 code · 28 blank · 13 comment · 0 complexity · 8a84cad76ada349fe3aeb2e4237ba5c6 MD5 · raw file

  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. package org.chromium.chrome.browser.firstrun;
  5. import static org.junit.Assert.assertEquals;
  6. import static org.junit.Assert.assertFalse;
  7. import static org.junit.Assert.assertNull;
  8. import static org.junit.Assert.assertTrue;
  9. import static org.mockito.ArgumentMatchers.any;
  10. import static org.mockito.Mockito.doAnswer;
  11. import static org.mockito.Mockito.doNothing;
  12. import static org.robolectric.Shadows.shadowOf;
  13. import android.os.Looper;
  14. import org.junit.After;
  15. import org.junit.Rule;
  16. import org.junit.Test;
  17. import org.junit.runner.RunWith;
  18. import org.mockito.ArgumentCaptor;
  19. import org.mockito.Captor;
  20. import org.mockito.Mock;
  21. import org.mockito.junit.MockitoJUnit;
  22. import org.mockito.junit.MockitoRule;
  23. import org.robolectric.annotation.Config;
  24. import org.chromium.base.Callback;
  25. import org.chromium.base.metrics.test.ShadowRecordHistogram;
  26. import org.chromium.base.test.BaseRobolectricTestRunner;
  27. import org.chromium.chrome.test.util.browser.signin.AccountManagerTestRule;
  28. import org.chromium.components.signin.test.util.FakeAccountManagerFacade;
  29. /**
  30. * Tests for {@link ChildAccountStatusSupplier}.
  31. */
  32. @RunWith(BaseRobolectricTestRunner.class)
  33. @Config(manifest = Config.NONE, shadows = {ShadowRecordHistogram.class})
  34. public class ChildAccountStatusSupplierTest {
  35. private static final String ADULT_ACCOUNT_EMAIL = "adult.account@gmail.com";
  36. private static final String CHILD_ACCOUNT_EMAIL =
  37. AccountManagerTestRule.generateChildEmail(/*baseName=*/"account@gmail.com");
  38. FakeAccountManagerFacade mAccountManagerFacade = new FakeAccountManagerFacade();
  39. @Rule
  40. public final AccountManagerTestRule mAccountManagerTestRule =
  41. new AccountManagerTestRule(mAccountManagerFacade);
  42. @Rule
  43. public MockitoRule mMockitoRule = MockitoJUnit.rule();
  44. @Captor
  45. public ArgumentCaptor<Callback<Boolean>> mCallbackCaptor;
  46. @Mock
  47. private FirstRunAppRestrictionInfo mFirstRunAppRestrictionInfoMock;
  48. @After
  49. public void tearDown() {
  50. ShadowRecordHistogram.reset();
  51. }
  52. @Test
  53. public void testNoAccounts() {
  54. mAccountManagerFacade.blockGetAccounts();
  55. ChildAccountStatusSupplier supplier = new ChildAccountStatusSupplier(
  56. mAccountManagerFacade, mFirstRunAppRestrictionInfoMock);
  57. shadowOf(Looper.getMainLooper()).idle();
  58. // Supplier shouldn't be set and should not record any histograms until it can obtain the
  59. // list of accounts from AccountManagerFacade.
  60. assertNull(supplier.get());
  61. assertEquals(0,
  62. ShadowRecordHistogram.getHistogramTotalCountForTesting(
  63. "MobileFre.ChildAccountStatusDuration"));
  64. mAccountManagerFacade.unblockGetAccounts();
  65. shadowOf(Looper.getMainLooper()).idle();
  66. assertFalse(supplier.get());
  67. assertEquals(1,
  68. ShadowRecordHistogram.getHistogramTotalCountForTesting(
  69. "MobileFre.ChildAccountStatusDuration"));
  70. }
  71. @Test
  72. public void testOneChildAccount() {
  73. mAccountManagerTestRule.addAccount(CHILD_ACCOUNT_EMAIL);
  74. ChildAccountStatusSupplier supplier = new ChildAccountStatusSupplier(
  75. mAccountManagerFacade, mFirstRunAppRestrictionInfoMock);
  76. shadowOf(Looper.getMainLooper()).idle();
  77. assertTrue(supplier.get());
  78. assertEquals(1,
  79. ShadowRecordHistogram.getHistogramTotalCountForTesting(
  80. "MobileFre.ChildAccountStatusDuration"));
  81. }
  82. @Test
  83. public void testNonChildAccount() {
  84. mAccountManagerTestRule.addAccount(ADULT_ACCOUNT_EMAIL);
  85. ChildAccountStatusSupplier supplier = new ChildAccountStatusSupplier(
  86. mAccountManagerFacade, mFirstRunAppRestrictionInfoMock);
  87. shadowOf(Looper.getMainLooper()).idle();
  88. assertFalse(supplier.get());
  89. assertEquals(1,
  90. ShadowRecordHistogram.getHistogramTotalCountForTesting(
  91. "MobileFre.ChildAccountStatusDuration"));
  92. }
  93. @Test
  94. public void testOneChildAccountWithNonChildAccounts() {
  95. mAccountManagerTestRule.addAccount(CHILD_ACCOUNT_EMAIL);
  96. mAccountManagerTestRule.addAccount(ADULT_ACCOUNT_EMAIL);
  97. ChildAccountStatusSupplier supplier = new ChildAccountStatusSupplier(
  98. mAccountManagerFacade, mFirstRunAppRestrictionInfoMock);
  99. shadowOf(Looper.getMainLooper()).idle();
  100. assertTrue(supplier.get());
  101. assertEquals(1,
  102. ShadowRecordHistogram.getHistogramTotalCountForTesting(
  103. "MobileFre.ChildAccountStatusDuration"));
  104. }
  105. @Test
  106. public void testNonChildWhenNoAppRestrictions() {
  107. mAccountManagerTestRule.addAccount(ADULT_ACCOUNT_EMAIL);
  108. // Block getAccounts call to make sure ChildAccountStatusSupplier checks app restrictions.
  109. mAccountManagerFacade.blockGetAccounts();
  110. doNothing()
  111. .when(mFirstRunAppRestrictionInfoMock)
  112. .getHasAppRestriction(mCallbackCaptor.capture());
  113. ChildAccountStatusSupplier supplier = new ChildAccountStatusSupplier(
  114. mAccountManagerFacade, mFirstRunAppRestrictionInfoMock);
  115. shadowOf(Looper.getMainLooper()).idle();
  116. assertNull(supplier.get());
  117. Callback<Boolean> getHasAppRestrictionsCallback = mCallbackCaptor.getValue();
  118. getHasAppRestrictionsCallback.onResult(false);
  119. // No app restrictions should mean that the child account status is false.
  120. assertFalse(supplier.get());
  121. assertEquals(1,
  122. ShadowRecordHistogram.getHistogramTotalCountForTesting(
  123. "MobileFre.ChildAccountStatusDuration"));
  124. }
  125. @Test
  126. public void testWaitsForAccountManagerFacadeWhenAppRestrictionsFound() {
  127. mAccountManagerTestRule.addAccount(CHILD_ACCOUNT_EMAIL);
  128. // Block getAccounts call to make sure ChildAccountStatusSupplier checks app restrictions.
  129. mAccountManagerFacade.blockGetAccounts();
  130. doAnswer(invocation -> {
  131. Callback<Boolean> callback = invocation.getArgument(0);
  132. callback.onResult(true);
  133. return null;
  134. })
  135. .when(mFirstRunAppRestrictionInfoMock)
  136. .getHasAppRestriction(any());
  137. ChildAccountStatusSupplier supplier = new ChildAccountStatusSupplier(
  138. mAccountManagerFacade, mFirstRunAppRestrictionInfoMock);
  139. shadowOf(Looper.getMainLooper()).idle();
  140. // Since app restrictions were found - ChildAccountSupplier should wait for status from
  141. // AccountManagerFacade, so the status shouldn't be available yet.
  142. assertNull(supplier.get());
  143. mAccountManagerFacade.unblockGetAccounts();
  144. shadowOf(Looper.getMainLooper()).idle();
  145. assertTrue(supplier.get());
  146. assertEquals(1,
  147. ShadowRecordHistogram.getHistogramTotalCountForTesting(
  148. "MobileFre.ChildAccountStatusDuration"));
  149. }
  150. }