User Info and Multi User Info Atom tests

Bug: 246735816
Test: atest UserInfoStatsTests && atest MultiUserInfoStatsTests
Change-Id: I92fa342e77c598fe24a6323be49ed49da1df0938
diff --git a/hostsidetests/statsdatom/apps/statsdapp/src/com/android/server/cts/device/statsdatom/Checkers.java b/hostsidetests/statsdatom/apps/statsdapp/src/com/android/server/cts/device/statsdatom/Checkers.java
index c13441a..ff37e40 100644
--- a/hostsidetests/statsdatom/apps/statsdapp/src/com/android/server/cts/device/statsdatom/Checkers.java
+++ b/hostsidetests/statsdatom/apps/statsdapp/src/com/android/server/cts/device/statsdatom/Checkers.java
@@ -21,6 +21,7 @@
 import android.Manifest;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
+import android.content.res.Resources;
 import android.net.wifi.WifiManager;
 import android.os.Vibrator;
 
@@ -62,6 +63,12 @@
                         Manifest.permission.BRIGHTNESS_SLIDER_USAGE, pm)).isGreaterThan(0);
     }
 
+    @Test
+    public void checkConfigShowUserSwitcher() {
+        assertThat(Resources.getSystem().getBoolean(com.android.internal
+                .R.bool.config_showUserSwitcherByDefault)).isTrue();
+    }
+
     /**
      * Returns the number of system apps with the given permission.
      */
diff --git a/hostsidetests/statsdatom/src/android/cts/statsdatom/users/MultiUserInfoStatsTests.java b/hostsidetests/statsdatom/src/android/cts/statsdatom/users/MultiUserInfoStatsTests.java
new file mode 100644
index 0000000..d6c15e9
--- /dev/null
+++ b/hostsidetests/statsdatom/src/android/cts/statsdatom/users/MultiUserInfoStatsTests.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.cts.statsdatom.users;
+
+import static android.cts.statsdatom.lib.AtomTestUtils.WAIT_TIME_LONG;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.cts.statsdatom.lib.AtomTestUtils;
+import android.cts.statsdatom.lib.ConfigUtils;
+import android.cts.statsdatom.lib.DeviceUtils;
+import android.cts.statsdatom.lib.ReportUtils;
+
+import com.android.os.AtomsProto;
+import com.android.os.AtomsProto.MultiUserInfo;
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.testtype.DeviceTestCase;
+import com.android.tradefed.testtype.IBuildReceiver;
+
+import java.util.List;
+
+public class MultiUserInfoStatsTests extends DeviceTestCase implements IBuildReceiver {
+    public static final int ATOM_ID = 10160;
+    protected IBuildInfo mCtsBuild;
+    static final String USER_SWITCHER_ENABLED = "user_switcher_enabled";
+    static final String GLOBAL_NAMESPACE = "global";
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        assertThat(mCtsBuild).isNotNull();
+        ConfigUtils.removeConfig(getDevice());
+        ReportUtils.clearReports(getDevice());
+        DeviceUtils.installStatsdTestApp(getDevice(), mCtsBuild);
+        Thread.sleep(WAIT_TIME_LONG);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        ConfigUtils.removeConfig(getDevice());
+        ReportUtils.clearReports(getDevice());
+        DeviceUtils.uninstallStatsdTestApp(getDevice());
+        super.tearDown();
+    }
+
+    @Override
+    public void setBuild(IBuildInfo buildInfo) {
+        mCtsBuild = buildInfo;
+    }
+
+    public void testMultiUserInfo() throws Exception {
+        int maxSupportedUsers = getDevice().getMaxNumberOfUsersSupported();
+        boolean isUserSwitcherEnabled = isUserSwitcherEnabled();
+        // Atoms are collected only if isUserSwitcher is enabled
+        uploadConfigForPulledAtom();
+        AtomTestUtils.sendAppBreadcrumbReportedAtom(getDevice());
+        Thread.sleep(AtomTestUtils.WAIT_TIME_LONG);
+        List<AtomsProto.Atom> data = ReportUtils.getGaugeMetricAtoms(getDevice());
+        if (isUserSwitcherEnabled) {
+            assertThat(data).isNotEmpty();
+            MultiUserInfo multiUserInfo = data.get(0).getMultiUserInfo();
+            assertThat(multiUserInfo.getMaxSupportedUsers()).isEqualTo(maxSupportedUsers);
+            assertThat(multiUserInfo.getMultiUserSettingOn()).isEqualTo(true);
+        } else {
+            data.isEmpty();
+        }
+    }
+
+    private boolean isUserSwitcherEnabled() throws Exception {
+        boolean config_showUserSwitcherByDefault = DeviceUtils
+                .checkDeviceFor(getDevice(), "checkConfigShowUserSwitcher");
+        String userSwitcherEnabled = getDevice()
+                .getSetting(GLOBAL_NAMESPACE, USER_SWITCHER_ENABLED);
+        boolean multiUserSettingOn = stringIsNullOrEmpty(userSwitcherEnabled)
+                ? config_showUserSwitcherByDefault
+                : Integer.parseInt(userSwitcherEnabled) == 1;
+
+        return getDevice().isMultiUserSupported() && multiUserSettingOn;
+    }
+
+    private boolean stringIsNullOrEmpty(String str) {
+        return str == null || str.equals("null") || str.isEmpty();
+    }
+
+    private void uploadConfigForPulledAtom() throws Exception {
+        ConfigUtils.uploadConfigForPulledAtom(getDevice(),
+                DeviceUtils.STATSD_ATOM_TEST_PKG,
+                ATOM_ID);
+    }
+
+}
diff --git a/hostsidetests/statsdatom/src/android/cts/statsdatom/users/UserInfoStatsTests.java b/hostsidetests/statsdatom/src/android/cts/statsdatom/users/UserInfoStatsTests.java
new file mode 100644
index 0000000..16d0656
--- /dev/null
+++ b/hostsidetests/statsdatom/src/android/cts/statsdatom/users/UserInfoStatsTests.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.cts.statsdatom.users;
+
+import static android.cts.statsdatom.lib.AtomTestUtils.WAIT_TIME_LONG;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.cts.statsdatom.lib.AtomTestUtils;
+import android.cts.statsdatom.lib.ConfigUtils;
+import android.cts.statsdatom.lib.DeviceUtils;
+import android.cts.statsdatom.lib.ReportUtils;
+
+import com.android.os.AtomsProto;
+import com.android.os.AtomsProto.UserInfo;
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.testtype.DeviceTestCase;
+import com.android.tradefed.testtype.IBuildReceiver;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class UserInfoStatsTests extends DeviceTestCase implements IBuildReceiver {
+    public static final int ATOM_ID = 10152;
+    protected IBuildInfo mCtsBuild;
+    protected List<Integer> mUsersToRemove = new ArrayList();
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        assertThat(mCtsBuild).isNotNull();
+        ConfigUtils.removeConfig(getDevice());
+        ReportUtils.clearReports(getDevice());
+        Thread.sleep(WAIT_TIME_LONG);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        getDevice().switchUser(0);
+        for (Integer userId : mUsersToRemove) {
+            getDevice().removeUser(userId);
+        }
+        ConfigUtils.removeConfig(getDevice());
+        ReportUtils.clearReports(getDevice());
+        super.tearDown();
+        Thread.sleep(WAIT_TIME_LONG);
+    }
+
+    @Override
+    public void setBuild(IBuildInfo buildInfo) {
+        mCtsBuild = buildInfo;
+    }
+
+    public void testGuestUserExists() throws Exception {
+        String userName = "TestUser_" + System.currentTimeMillis();
+        int userId = userCreate(userName, true);
+
+        uploadConfigForPulledAtom();
+        AtomTestUtils.sendAppBreadcrumbReportedAtom(getDevice());
+        Thread.sleep(AtomTestUtils.WAIT_TIME_LONG);
+        List<AtomsProto.Atom> data = ReportUtils.getGaugeMetricAtoms(getDevice());
+
+        assertThat(data).isNotEmpty();
+
+        UserInfo systemUser = data.get(0).getUserInfo();
+
+        assertThat(systemUser.getUserId()).isEqualTo(0);
+        assertThat(systemUser.getUserType().toString()).isEqualTo("FULL_SYSTEM");
+        assertThat(systemUser.getIsUserRunningUnlocked()).isTrue();
+        UserInfo guestUser = data.get(1).getUserInfo();
+        assertThat(guestUser.getUserId()).isEqualTo(userId);
+        assertThat(guestUser.getUserType().toString()).isEqualTo("FULL_GUEST");
+        assertThat(guestUser.getIsUserRunningUnlocked()).isFalse();
+    }
+
+
+    public void testSecondaryUserExists() throws Exception {
+        String userName = "TestUser_" + System.currentTimeMillis();
+        int userId = userCreate(userName, false);
+        uploadConfigForPulledAtom();
+
+        AtomTestUtils.sendAppBreadcrumbReportedAtom(getDevice());
+        Thread.sleep(AtomTestUtils.WAIT_TIME_LONG);
+
+        List<AtomsProto.Atom> data = ReportUtils.getGaugeMetricAtoms(getDevice());
+        assertThat(data).isNotEmpty();
+
+        UserInfo systemUser = data.get(0).getUserInfo();
+        assertThat(systemUser.getUserId()).isEqualTo(0);
+        assertThat(systemUser.getUserType().toString()).isEqualTo("FULL_SYSTEM");
+        assertThat(systemUser.getIsUserRunningUnlocked()).isTrue();
+        UserInfo secondaryUser = data.get(1).getUserInfo();
+        assertThat(secondaryUser.getUserId()).isEqualTo(userId);
+        assertThat(secondaryUser.getUserType().toString()).isEqualTo("FULL_SECONDARY");
+        assertThat(secondaryUser.getIsUserRunningUnlocked()).isFalse();
+    }
+
+    private int userCreate(String userName, boolean isGuest) throws Exception {
+        int userId = getDevice().createUser(userName, isGuest, false /* ephemeral */);
+        mUsersToRemove.add(userId);
+        Thread.sleep(WAIT_TIME_LONG);
+        return userId;
+    }
+
+    private void uploadConfigForPulledAtom() throws Exception {
+        ConfigUtils.uploadConfigForPulledAtom(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG,
+                ATOM_ID);
+    }
+
+}