Implement new BS warning / notification flow (1/2)

- Show the battery saver confirmation dialog only for the first time.
- Start counting # of manual activations, which will be used in a
follow-up CL.

Bug: 74120126
Test: Manual tests with ./vendor/google_experimental/users/omakoto/android-battery-tester
Test: m -j ROBOTEST_FILTER=BatterySaverUtilsTest RunSettingsLibRoboTests
Test: cd frameworks/base/packages/SystemUI/tests && \
        atest src/com/android/systemui/power/PowerUITest.java src/com/android/systemui/power/PowerNotificationWarningsTest.java
Change-Id: If6a081a6222e6a87c4cd332364c89856e7648a36
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/fuelgauge/BatterySaverUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/fuelgauge/BatterySaverUtilsTest.java
new file mode 100644
index 0000000..b33df30
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/fuelgauge/BatterySaverUtilsTest.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2018 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 com.android.settingslib.fuelgauge;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.PowerManager;
+import android.provider.Settings.Secure;
+
+import com.android.settingslib.SettingsLibRobolectricTestRunner;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+@RunWith(SettingsLibRobolectricTestRunner.class)
+public class BatterySaverUtilsTest {
+    @Mock
+    Context mMockContext;
+
+    @Mock
+    ContentResolver mMockResolver;
+
+    @Mock
+    PowerManager mMockPowerManager;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        when(mMockContext.getContentResolver()).thenReturn(mMockResolver);
+        when(mMockContext.getSystemService(eq(PowerManager.class))).thenReturn(mMockPowerManager);
+        when(mMockPowerManager.setPowerSaveMode(anyBoolean())).thenReturn(true);
+    }
+
+    @Test
+    public void testSetPowerSaveMode_enable_firstCall_needWarning() throws Exception {
+        Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
+        Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
+
+        assertEquals(false, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true));
+
+        verify(mMockContext, times(1)).sendBroadcast(any(Intent.class));
+        verify(mMockPowerManager, times(0)).setPowerSaveMode(anyBoolean());
+
+        // They shouldn't have changed.
+        assertEquals(-1,
+                Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
+        assertEquals(-2,
+                Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
+    }
+
+    @Test
+    public void testSetPowerSaveMode_enable_secondCall_needWarning() throws Exception {
+        Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1); // Already acked.
+        Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
+
+        assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true));
+
+        verify(mMockContext, times(0)).sendBroadcast(any(Intent.class));
+        verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true));
+
+        assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
+        assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
+    }
+
+    @Test
+    public void testSetPowerSaveMode_enable_thridCall_needWarning() throws Exception {
+        Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1); // Already acked.
+        Secure.putInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, 1);
+
+        assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true));
+
+        verify(mMockContext, times(0)).sendBroadcast(any(Intent.class));
+        verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true));
+
+        assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
+        assertEquals(2, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
+    }
+
+    @Test
+    public void testSetPowerSaveMode_enable_firstCall_noWarning() throws Exception {
+        Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
+        Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
+
+        assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, false));
+
+        verify(mMockContext, times(0)).sendBroadcast(any(Intent.class));
+        verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true));
+
+        assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
+        assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
+    }
+
+    @Test
+    public void testSetPowerSaveMode_disable_firstCall_noWarning() throws Exception {
+        Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
+        Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
+
+        // When disabling, needFirstTimeWarning doesn't matter.
+        assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, false, false));
+
+        verify(mMockContext, times(0)).sendBroadcast(any(Intent.class));
+        verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(false));
+
+        assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
+        assertEquals(-2,
+                Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
+    }
+
+    @Test
+    public void testSetPowerSaveMode_disable_firstCall_needWarning() throws Exception {
+        Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
+        Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
+
+        // When disabling, needFirstTimeWarning doesn't matter.
+        assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, false, true));
+
+        verify(mMockContext, times(0)).sendBroadcast(any(Intent.class));
+        verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(false));
+
+        assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
+        assertEquals(-2,
+                Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
+    }
+}