Add tests for guest user events

Also, move ACTION_USER_GUEST_EXIT_CONFIRMED so it is only logged after
safety-check

Test: ROBOTEST_FILTER="com.android.settings.users.UserSettingsTest" m RunSettingsRoboTests
Test: ROBOTEST_FILTER="com.android.settings.users.UserDetailsSettingsTest" m RunSettingsRoboTests
Bug: 184200796
Change-Id: Id18aa914bbb262984d7c7cf4b8a4ffdde278fd23
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index 5f14399..e95fed7 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -829,13 +829,14 @@
     /**
      * Erase the current user (guest) and switch to another user.
      */
-    private void exitGuest() {
+    @VisibleForTesting
+    void exitGuest() {
         // Just to be safe
-        mMetricsFeatureProvider.action(getActivity(),
-                SettingsEnums.ACTION_USER_GUEST_EXIT_CONFIRMED);
         if (!isCurrentUserGuest()) {
             return;
         }
+        mMetricsFeatureProvider.action(getActivity(),
+                SettingsEnums.ACTION_USER_GUEST_EXIT_CONFIRMED);
         removeThisUser();
     }
 
diff --git a/tests/robotests/src/com/android/settings/users/UserDetailsSettingsTest.java b/tests/robotests/src/com/android/settings/users/UserDetailsSettingsTest.java
index 90c9172..0915000 100644
--- a/tests/robotests/src/com/android/settings/users/UserDetailsSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/users/UserDetailsSettingsTest.java
@@ -25,6 +25,7 @@
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -35,6 +36,7 @@
 import static org.robolectric.Shadows.shadowOf;
 
 import android.app.admin.DevicePolicyManager;
+import android.app.settings.SettingsEnums;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -56,6 +58,7 @@
 import com.android.settings.testutils.shadow.ShadowUserManager;
 import com.android.settingslib.RestrictedLockUtils;
 import com.android.settingslib.RestrictedPreference;
+import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
 
 import org.junit.After;
 import org.junit.Before;
@@ -105,6 +108,8 @@
     private FragmentActivity mActivity;
     private Context mContext;
     private UserCapabilities mUserCapabilities;
+    @Mock
+    private MetricsFeatureProvider mMetricsFeatureProvider;
     private UserDetailsSettings mFragment;
     private Bundle mArguments;
     private UserInfo mUserInfo;
@@ -128,6 +133,7 @@
 
         ReflectionHelpers.setField(mFragment, "mUserManager", userManager);
         ReflectionHelpers.setField(mFragment, "mUserCaps", mUserCapabilities);
+        ReflectionHelpers.setField(mFragment, "mMetricsFeatureProvider", mMetricsFeatureProvider);
         doReturn(mActivity).when(mFragment).getActivity();
         doReturn(mActivity).when(mFragment).getContext();
 
@@ -472,6 +478,23 @@
         mFragment.onPreferenceClick(mSwitchUserPref);
 
         verify(mFragment).switchUser();
+        verify(mMetricsFeatureProvider, never()).action(any(),
+                eq(SettingsEnums.ACTION_SWITCH_TO_GUEST));
+    }
+
+    @Test
+    public void onPreferenceClick_switchToGuestClicked_canSwitch_shouldSwitch() {
+        setupSelectedGuest();
+        mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_OK);
+        mFragment.mSwitchUserPref = mSwitchUserPref;
+        mFragment.mRemoveUserPref = mRemoveUserPref;
+        mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
+        mFragment.mUserInfo = mUserInfo;
+
+        mFragment.onPreferenceClick(mSwitchUserPref);
+
+        verify(mFragment).switchUser();
+        verify(mMetricsFeatureProvider).action(any(), eq(SettingsEnums.ACTION_SWITCH_TO_GUEST));
     }
 
     @Test
diff --git a/tests/robotests/src/com/android/settings/users/UserSettingsTest.java b/tests/robotests/src/com/android/settings/users/UserSettingsTest.java
index c542c55..299e054 100644
--- a/tests/robotests/src/com/android/settings/users/UserSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/users/UserSettingsTest.java
@@ -177,6 +177,22 @@
     }
 
     @Test
+    public void testExitGuest_ShouldLogAction() {
+        mUserCapabilities.mIsGuest = true;
+        mFragment.exitGuest();
+        verify(mMetricsFeatureProvider).action(any(),
+                eq(SettingsEnums.ACTION_USER_GUEST_EXIT_CONFIRMED));
+    }
+
+    @Test
+    public void testExitGuestWhenNotGuest_ShouldNotLogAction() {
+        mUserCapabilities.mIsGuest = false;
+        mFragment.exitGuest();
+        verify(mMetricsFeatureProvider, never()).action(any(),
+                eq(SettingsEnums.ACTION_USER_GUEST_EXIT_CONFIRMED));
+    }
+
+    @Test
     public void withDisallowRemoveUser_ShouldDisableRemoveUser() {
         // TODO(b/115781615): Tidy robolectric tests
         // Arrange