AOD: Add wakelock for charging text while dozing

Also refactors the WakeLocks in SystemUI.

Bug: 30876804
Bug: 35850304
Test: runtest systemui
Change-Id: Ie17eedfd266deb3aa46dabd701bc784330b2e030
diff --git a/packages/SystemUI/tests/AndroidManifest.xml b/packages/SystemUI/tests/AndroidManifest.xml
index 41b75ff..612a54a 100644
--- a/packages/SystemUI/tests/AndroidManifest.xml
+++ b/packages/SystemUI/tests/AndroidManifest.xml
@@ -36,6 +36,7 @@
     <uses-permission android:name="android.permission.MANAGE_NETWORK_POLICY" />
     <uses-permission android:name="android.permission.REQUEST_NETWORK_SCORES" />
     <uses-permission android:name="android.permission.CONTROL_VPN" />
+    <uses-permission android:name="android.permission.WAKE_LOCK" />
 
     <application>
         <uses-library android:name="android.test.runner" />
diff --git a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java
index 3abd955..ba39671 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java
@@ -44,6 +44,7 @@
 import com.android.systemui.SysUIRunner;
 import com.android.systemui.UiThreadTest;
 import com.android.internal.hardware.AmbientDisplayConfiguration;
+import com.android.systemui.util.wakelock.WakeLockFake;
 
 import org.junit.Before;
 import org.junit.Test;
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java
index 7335af3..6424a0a 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java
@@ -16,19 +16,26 @@
 
 package com.android.systemui.statusbar;
 
+import static android.support.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
+import android.app.Instrumentation;
 import android.app.admin.DevicePolicyManager;
 import android.app.trust.TrustManager;
 import android.content.Context;
 import android.hardware.fingerprint.FingerprintManager;
 import android.os.Looper;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.internal.runner.junit4.statement.UiThreadStatement;
 import android.support.test.runner.AndroidJUnit4;
-import android.test.suitebuilder.annotation.SmallTest;
 import android.view.View;
 import android.view.ViewGroup;
 
@@ -36,6 +43,7 @@
 import com.android.systemui.R;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.statusbar.phone.KeyguardIndicationTextView;
+import com.android.systemui.util.wakelock.WakeLockFake;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -54,9 +62,13 @@
     private KeyguardIndicationTextView mDisclosure = mock(KeyguardIndicationTextView.class);
 
     private KeyguardIndicationController mController;
+    private WakeLockFake mWakeLock;
+    private Instrumentation mInstrumentation;
 
     @Before
     public void setUp() throws Exception {
+        mInstrumentation = InstrumentationRegistry.getInstrumentation();
+
         mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager);
         mContext.addMockSystemService(Context.TRUST_SERVICE, mock(TrustManager.class));
         mContext.addMockSystemService(Context.FINGERPRINT_SERVICE, mock(FingerprintManager.class));
@@ -65,13 +77,15 @@
 
         when(mIndicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure))
                 .thenReturn(mDisclosure);
+
+        mWakeLock = new WakeLockFake();
     }
 
     private void createController() {
         if (Looper.myLooper() == null) {
             Looper.prepare();
         }
-        mController = new KeyguardIndicationController(mContext, mIndicationArea, null);
+        mController = new KeyguardIndicationController(mContext, mIndicationArea, null, mWakeLock);
     }
 
     @Test
@@ -139,4 +153,45 @@
         verify(mDisclosure).setVisibility(View.GONE);
         verifyNoMoreInteractions(mDisclosure);
     }
+
+    @Test
+    public void transientIndication_holdsWakeLock_whenDozing() {
+        createController();
+
+        mController.setDozing(true);
+        mController.showTransientIndication("Test");
+
+        assertTrue(mWakeLock.isHeld());
+    }
+
+    @Test
+    public void transientIndication_releasesWakeLock_afterHiding() {
+        createController();
+
+        mController.setDozing(true);
+        mController.showTransientIndication("Test");
+        mController.hideTransientIndication();
+
+        assertFalse(mWakeLock.isHeld());
+    }
+
+    @Test
+    public void transientIndication_releasesWakeLock_afterHidingDelayed() throws Throwable {
+        mInstrumentation.runOnMainSync(() -> {
+            createController();
+
+            mController.setDozing(true);
+            mController.showTransientIndication("Test");
+            mController.hideTransientIndicationDelayed(0);
+        });
+        mInstrumentation.waitForIdleSync();
+
+        boolean[] held = new boolean[2];
+        mInstrumentation.runOnMainSync(() -> {
+            held[0] = mWakeLock.isHeld();
+            held[1] = true;
+        });
+        assertFalse("wake lock still held", held[0]);
+        assertTrue("held was not written yet", held[1]);
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/SettableWakeLockTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/SettableWakeLockTest.java
new file mode 100644
index 0000000..f6692eb
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/SettableWakeLockTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2017 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.systemui.util.wakelock;
+
+import static junit.framework.TestCase.assertTrue;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class SettableWakeLockTest {
+
+    private WakeLockFake mFake;
+    private SettableWakeLock mSettable;
+
+    @Before
+    public void setup() {
+        mFake = new WakeLockFake();
+        mSettable = new SettableWakeLock(mFake);
+    }
+
+    @Test
+    public void setAcquire_true_acquires() throws Exception {
+        mSettable.setAcquired(true);
+        assertTrue(mFake.isHeld());
+        assertEquals(mFake.isHeld(), mSettable.isAcquired());
+    }
+
+    @Test
+    public void setAcquire_false_releases() throws Exception {
+        mSettable.setAcquired(true);
+        mSettable.setAcquired(false);
+        assertFalse(mFake.isHeld());
+        assertEquals(mFake.isHeld(), mSettable.isAcquired());
+    }
+
+    @Test
+    public void setAcquire_true_multipleTimes_isIdempotent() throws Exception {
+        mSettable.setAcquired(true);
+        mSettable.setAcquired(true);
+        mSettable.setAcquired(true);
+        mSettable.setAcquired(false);
+        assertFalse(mFake.isHeld());
+        assertEquals(mFake.isHeld(), mSettable.isAcquired());
+    }
+
+    @Test
+    public void setAcquire_false_multipleTimes_idempotent() throws Exception {
+        mSettable.setAcquired(true);
+        mSettable.setAcquired(false);
+        mSettable.setAcquired(false);
+        assertFalse(mFake.isHeld());
+        assertEquals(mFake.isHeld(), mSettable.isAcquired());
+    }
+
+    @Test
+    public void setAcquire_false_multipleTimes_idempotent_again() throws Exception {
+        mSettable.setAcquired(true);
+        mSettable.setAcquired(false);
+        mSettable.setAcquired(false);
+        mSettable.setAcquired(true);
+        assertTrue(mFake.isHeld());
+        assertEquals(mFake.isHeld(), mSettable.isAcquired());
+    }
+
+
+}
\ No newline at end of file
diff --git a/packages/SystemUI/tests/src/com/android/systemui/doze/WakeLockFake.java b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockFake.java
similarity index 82%
rename from packages/SystemUI/tests/src/com/android/systemui/doze/WakeLockFake.java
rename to packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockFake.java
index 7c04fe2..4cefb99 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/doze/WakeLockFake.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockFake.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 The Android Open Source Project
+ * Copyright (C) 2017 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.
@@ -11,21 +11,17 @@
  * 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.
+ * limitations under the License
  */
 
-package com.android.systemui.doze;
+package com.android.systemui.util.wakelock;
 
 import com.android.internal.util.Preconditions;
 
-public class WakeLockFake extends DozeFactory.WakeLock {
+public class WakeLockFake implements WakeLock {
 
     private int mAcquired = 0;
 
-    public WakeLockFake() {
-        super(null);
-    }
-
     @Override
     public void acquire() {
         mAcquired++;
diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockTest.java
new file mode 100644
index 0000000..5394499
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/util/wakelock/WakeLockTest.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2017 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.systemui.util.wakelock;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.content.Context;
+import android.os.PowerManager;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class WakeLockTest {
+
+    WakeLock mWakeLock;
+    PowerManager.WakeLock mInner;
+
+    @Before
+    public void setUp() {
+        Context context = InstrumentationRegistry.getContext();
+
+        mInner = WakeLock.createPartialInner(context, WakeLockTest.class.getName());
+        mWakeLock = WakeLock.wrap(mInner);
+    }
+
+    @After
+    public void tearDown() {
+        mInner.setReferenceCounted(false);
+        mInner.release();
+    }
+
+    @Test
+    public void createPartialInner_notHeldYet() {
+        assertFalse(mInner.isHeld());
+    }
+
+    @Test
+    public void wakeLock_acquire() {
+        mWakeLock.acquire();
+        assertTrue(mInner.isHeld());
+    }
+
+    @Test
+    public void wakeLock_release() {
+        mWakeLock.acquire();
+        mWakeLock.release();
+        assertFalse(mInner.isHeld());
+    }
+
+    @Test
+    public void wakeLock_refCounted() {
+        mWakeLock.acquire();
+        mWakeLock.acquire();
+        mWakeLock.release();
+        assertTrue(mInner.isHeld());
+    }
+
+    @Test
+    public void wakeLock_wrap() {
+        boolean[] ran = new boolean[1];
+
+        Runnable wrapped = mWakeLock.wrap(() -> {
+            ran[0] = true;
+        });
+
+        assertTrue(mInner.isHeld());
+        assertFalse(ran[0]);
+
+        wrapped.run();
+
+        assertTrue(ran[0]);
+        assertFalse(mInner.isHeld());
+    }
+}
\ No newline at end of file