Merge "Add CTS Tests for QS Tiles" into nyc-dev
diff --git a/CtsTestCaseList.mk b/CtsTestCaseList.mk
index 3bcc80d..64bbf2b 100644
--- a/CtsTestCaseList.mk
+++ b/CtsTestCaseList.mk
@@ -114,6 +114,7 @@
     CtsSimpleApp \
     CtsSimplePreMApp \
     CtsSomeAccessibilityServices \
+    CtsSystemUiDeviceApp \
     CtsThemeDeviceApp \
     TestDeviceSetup \
     CtsUiAutomatorTestApp \
@@ -257,7 +258,8 @@
     CtsServicesHostTestCases \
     CtsThemeHostTestCases \
     CtsUiHostTestCases \
-    CtsUsbTests
+    CtsUsbTests \
+    CtsSystemUiHostTestCases \
 
 # List of native tests. For 32 bit targets, assumes that there will be
 # one test executable, and it will end in 32. For 64 bit targets, assumes
diff --git a/hostsidetests/systemui/Android.mk b/hostsidetests/systemui/Android.mk
new file mode 100644
index 0000000..a8704bf
--- /dev/null
+++ b/hostsidetests/systemui/Android.mk
@@ -0,0 +1,38 @@
+# Copyright (C) 2016 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.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_MODULE := CtsSystemUiHostTestCases
+
+LOCAL_JAVA_LIBRARIES := cts-tradefed tradefed-prebuilt
+
+LOCAL_STATIC_JAVA_LIBRARIES := cts-migration-lib
+
+LOCAL_CTS_TEST_PACKAGE := android.host.systemui
+
+LOCAL_SDK_VERSION := current
+
+# Tag this module as a cts_v2 test artifact
+LOCAL_COMPATIBILITY_SUITE := cts_v2
+
+include $(BUILD_CTS_HOST_JAVA_LIBRARY)
+
+include $(call all-subdir-makefiles)
diff --git a/hostsidetests/systemui/AndroidTest.xml b/hostsidetests/systemui/AndroidTest.xml
new file mode 100644
index 0000000..c8b1883
--- /dev/null
+++ b/hostsidetests/systemui/AndroidTest.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 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.
+-->
+<configuration description="Config for CTS System UI host test cases">
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.ApkInstaller">
+        <option name="cleanup-apks" value="true" />
+        <option name="test-file-name" value="CtsSystemUiDeviceApp.apk" />
+    </target_preparer>
+    <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
+        <option name="jar" value="CtsSystemUiHostTestCases.jar" />
+    </test>
+</configuration>
diff --git a/hostsidetests/systemui/app/Android.mk b/hostsidetests/systemui/app/Android.mk
new file mode 100644
index 0000000..556756b
--- /dev/null
+++ b/hostsidetests/systemui/app/Android.mk
@@ -0,0 +1,37 @@
+# Copyright (C) 2016 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# Don't include this package in any target
+LOCAL_MODULE_TAGS := tests
+# When built, explicitly put it in the data partition.
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_DEX_PREOPT := false
+
+LOCAL_PROGUARD_ENABLED := disabled
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+# tag this module as a cts_v2 test artifact
+LOCAL_COMPATIBILITY_SUITE := cts_v2
+
+LOCAL_PACKAGE_NAME := CtsSystemUiDeviceApp
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_CTS_SUPPORT_PACKAGE)
diff --git a/hostsidetests/systemui/app/AndroidManifest.xml b/hostsidetests/systemui/app/AndroidManifest.xml
new file mode 100755
index 0000000..5296111
--- /dev/null
+++ b/hostsidetests/systemui/app/AndroidManifest.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2016 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.systemui.cts">
+
+    <application>
+        <service android:name=".TestTileService"
+            android:icon="@android:drawable/ic_delete"
+            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
+            <intent-filter>
+                <action android:name="android.service.quicksettings.action.QS_TILE" />
+            </intent-filter>
+        </service>
+
+        <service android:name=".TestActiveTileService"
+            android:icon="@android:drawable/ic_delete"
+            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
+            <intent-filter>
+                <action android:name="android.service.quicksettings.action.QS_TILE" />
+            </intent-filter>
+        </service>
+
+        <receiver
+            android:name=".TestActiveTileService$Receiver">
+            <intent-filter>
+                <action android:name="android.sysui.testtile.REQUEST_LISTENING" />
+            </intent-filter>
+        </receiver>
+
+    </application>
+
+</manifest>
+
diff --git a/hostsidetests/systemui/app/src/android/systemui/cts/TestActiveTileService.java b/hostsidetests/systemui/app/src/android/systemui/cts/TestActiveTileService.java
new file mode 100644
index 0000000..013a21b
--- /dev/null
+++ b/hostsidetests/systemui/app/src/android/systemui/cts/TestActiveTileService.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2016 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.systemui.cts;
+
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+public class TestActiveTileService extends TestTileService {
+
+    @Override
+    public int onTileAdded() {
+        Log.i(TAG, TEST_PREFIX + "onTileAdded");
+        return TILE_MODE_ACTIVE;
+    }
+
+    public static class Receiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            Log.i(TestActiveTileService.class.getSimpleName(),
+                    TEST_PREFIX + "requestListeningState");
+            requestListeningState(context, new ComponentName(context, TestActiveTileService.class));
+        }
+    }
+}
diff --git a/hostsidetests/systemui/app/src/android/systemui/cts/TestTileService.java b/hostsidetests/systemui/app/src/android/systemui/cts/TestTileService.java
new file mode 100644
index 0000000..f2b50f7
--- /dev/null
+++ b/hostsidetests/systemui/app/src/android/systemui/cts/TestTileService.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2016 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.systemui.cts;
+
+import android.app.Dialog;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.graphics.drawable.Icon;
+import android.provider.Settings;
+import android.service.quicksettings.Tile;
+import android.service.quicksettings.TileService;
+import android.util.Log;
+import android.view.View;
+
+/**
+ * A test tile that logs everything that happens to it.
+ * The tests will manipulate the state of the QS tile through ADB and verify
+ * the correct callbacks actually happened.
+ */
+public class TestTileService extends TileService {
+    protected final String TAG = getClass().getSimpleName();
+
+    public static final String SHOW_DIALOG = "android.sysui.testtile.action.SHOW_DIALOG";
+    public static final String START_ACTIVITY = "android.sysui.testtile.action.START_ACTIVITY";
+
+    public static final String TEST_PREFIX = "TileTest_";
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        Log.i(TAG, TEST_PREFIX + "onCreate");
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        Log.i(TAG, TEST_PREFIX + "onDestroy");
+    }
+
+    @Override
+    public int onTileAdded() {
+        Log.i(TAG, TEST_PREFIX + "onTileAdded");
+        return super.onTileAdded();
+    }
+
+    @Override
+    public void onTileRemoved() {
+        Log.i(TAG, TEST_PREFIX + "onTileRemoved");
+        super.onTileRemoved();
+    }
+
+    @Override
+    public void onStartListening() {
+        super.onStartListening();
+        Log.i(TAG, TEST_PREFIX + "onStartListening");
+        IntentFilter filter = new IntentFilter(SHOW_DIALOG);
+        filter.addAction(START_ACTIVITY);
+        registerReceiver(mReceiver, filter);
+
+        // Set up some initial good state.
+        getQsTile().setLabel(TAG);
+        getQsTile().setContentDescription("CTS Test Tile");
+        getQsTile().setIcon(Icon.createWithResource(this, android.R.drawable.ic_secure));
+        getQsTile().setState(Tile.STATE_ACTIVE);
+        getQsTile().updateTile();
+    }
+
+    @Override
+    public void onStopListening() {
+        super.onStopListening();
+        Log.i(TAG, TEST_PREFIX + "onStopListening");
+        unregisterReceiver(mReceiver);
+    }
+
+    @Override
+    public void onClick() {
+        super.onClick();
+        Log.i(TAG, TEST_PREFIX + "onClick");
+        Log.i(TAG, TEST_PREFIX + "is_secure_" + isSecure());
+        Log.i(TAG, TEST_PREFIX + "is_locked_" + isLocked());
+        unlockAndRun(new Runnable() {
+            @Override
+            public void run() {
+                Log.i(TAG, TEST_PREFIX + "unlockAndRunRun");
+            }
+        });
+    }
+
+    private void handleStartActivity() {
+        startActivityAndCollapse(new Intent(Settings.ACTION_SETTINGS)
+                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
+    }
+
+    private void handleShowDialog() {
+        Log.i(TAG, TEST_PREFIX + "handleShowDialog");
+        final Dialog dialog = new Dialog(this);
+        dialog.setContentView(new FocusView(this, dialog));
+        try {
+            showDialog(dialog);
+        } catch (Exception e) {
+            Log.i(TAG, TEST_PREFIX + "onWindowAddFailed", e);
+        }
+    }
+
+    private class FocusView extends View {
+        private final Dialog mDialog;
+
+        public FocusView(Context context, Dialog dialog) {
+            super(context);
+            mDialog = dialog;
+        }
+
+        @Override
+        public void onWindowFocusChanged(boolean hasWindowFocus) {
+            Log.i(TAG, TEST_PREFIX + "onWindowFocusChanged_" + hasWindowFocus);
+            if (hasWindowFocus) {
+                post(new Runnable() {
+                    @Override
+                    public void run() {
+                        mDialog.dismiss();
+                    }
+                });
+            }
+            super.onWindowFocusChanged(hasWindowFocus);
+        }
+    }
+
+    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (intent.getAction().equals(SHOW_DIALOG)) {
+                handleShowDialog();
+            } else {
+                handleStartActivity();
+            }
+        }
+    };
+}
diff --git a/hostsidetests/systemui/src/android/host/systemui/ActiveTileServiceTest.java b/hostsidetests/systemui/src/android/host/systemui/ActiveTileServiceTest.java
new file mode 100644
index 0000000..5325b29
--- /dev/null
+++ b/hostsidetests/systemui/src/android/host/systemui/ActiveTileServiceTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2016 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.host.systemui;
+
+/**
+ * Tests the differences in behavior between tiles in TileService#TILE_MODE_PASSIVE
+ * and TileService#TILE_MODE_ACTIVE.
+ */
+public class ActiveTileServiceTest extends BaseTileServiceTest {
+    // Constants for generating commands below.
+    private static final String SERVICE = "TestActiveTileService";
+
+    private static final String ACTION_REQUEST_LISTENING =
+            "android.sysui.testtile.REQUEST_LISTENING";
+
+    private static final String REQUEST_LISTENING = "am broadcast -a " + ACTION_REQUEST_LISTENING;
+
+    public ActiveTileServiceTest() {
+        super(SERVICE);
+    }
+
+    public void testNotListening() throws Exception {
+        addTile();
+        assertTrue(waitFor("onDestroy"));
+
+        // Open quick settings and verify that this service doesn't get put in
+        // a listening state since its an active tile.
+        openSettings();
+        assertFalse(waitFor("onStartListening"));
+    }
+
+    public void testRequestListening() throws Exception {
+        addTile();
+        assertTrue(waitFor("onDestroy"));
+
+        // Request the listening state and verify that it gets an onStartListening.
+        getDevice().executeShellCommand(REQUEST_LISTENING);
+        assertTrue(waitFor("requestListeningState"));
+        assertTrue(waitFor("onStartListening"));
+    }
+
+    public void testClick() throws Exception {
+        addTile();
+        assertTrue(waitFor("onDestroy"));
+
+        // Open the quick settings.
+        openSettings();
+
+        // Click on the tile and verify it happens.
+        clickTile();
+        assertTrue(waitFor("onStartListening"));
+        assertTrue(waitFor("onClick"));
+    }
+
+}
diff --git a/hostsidetests/systemui/src/android/host/systemui/BaseTileServiceTest.java b/hostsidetests/systemui/src/android/host/systemui/BaseTileServiceTest.java
new file mode 100644
index 0000000..368cff9
--- /dev/null
+++ b/hostsidetests/systemui/src/android/host/systemui/BaseTileServiceTest.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2016 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.host.systemui;
+
+import com.android.compatibility.common.util.AbiUtils;
+import com.android.cts.migration.MigrationHelper;
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.testtype.DeviceTestCase;
+import com.android.tradefed.testtype.IAbi;
+import com.android.tradefed.testtype.IAbiReceiver;
+import com.android.tradefed.testtype.IBuildReceiver;
+
+import java.io.File;
+
+public class BaseTileServiceTest extends DeviceTestCase implements IBuildReceiver {
+    // Constants for generating commands below.
+    private static final String PACKAGE = "android.systemui.cts";
+    private static final String ACTION_SHOW_DIALOG = "android.sysui.testtile.action.SHOW_DIALOG";
+
+    private static final String APK_NAME = "CtsSystemUiDeviceApp";
+
+    // Commands used on the device.
+    private static final String ADD_TILE   = "cmd statusbar add-tile ";
+    private static final String REM_TILE   = "cmd statusbar remove-tile ";
+    private static final String CLICK_TILE = "cmd statusbar click-tile ";
+
+    private static final String OPEN_NOTIFICATIONS = "cmd statusbar expand-notifications";
+    private static final String OPEN_SETTINGS      = "cmd statusbar expand-settings";
+    private static final String COLLAPSE           = "cmd statusbar collapse";
+
+    private static final String SHOW_DIALOG = "am broadcast -a " + ACTION_SHOW_DIALOG;
+
+    public static final String TEST_PREFIX = "TileTest_";
+
+    // Time between checks for logs we expect.
+    private static final long CHECK_DELAY = 500;
+    // Number of times to check before failing.
+    private static final long CHECK_RETRIES = 15;
+
+    private final String mService;
+    private final String mComponent;
+
+    /** A reference to the build. */
+    private IBuildInfo mBuildInfo;
+
+    public BaseTileServiceTest(String service) {
+        mService = service;
+        mComponent = PACKAGE + "/." + mService;
+    }
+
+    @Override
+    public void setBuild(IBuildInfo buildInfo) {
+        mBuildInfo = buildInfo;
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        assertNotNull(mBuildInfo);  // ensure build has been set before test is run.
+        // Get the APK from the build.
+        final File app = MigrationHelper.getTestFile(mBuildInfo, String.format("%s.apk", APK_NAME));
+
+        getDevice().installPackage(app, true);
+
+        clearLogcat();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+
+        collapse();
+        remTile();
+        // Try to wait for a onTileRemoved.
+        waitFor("onTileRemoved");
+
+        ITestDevice device = getDevice();
+        device.uninstallPackage(PACKAGE);
+    }
+
+    protected void showDialog() throws Exception {
+        execute(SHOW_DIALOG);
+    }
+
+    protected void addTile() throws Exception {
+        execute(ADD_TILE + mComponent);
+    }
+
+    protected void remTile() throws Exception {
+        execute(REM_TILE + mComponent);
+    }
+
+    protected void clickTile() throws Exception {
+        execute(CLICK_TILE + mComponent);
+    }
+
+    protected void openNotifications() throws Exception {
+        execute(OPEN_NOTIFICATIONS);
+    }
+
+    protected void openSettings() throws Exception {
+        execute(OPEN_SETTINGS);
+    }
+
+    protected void collapse() throws Exception {
+        execute(COLLAPSE);
+    }
+
+    private void execute(String cmd) throws Exception {
+        getDevice().executeShellCommand(cmd);
+        // All of the status bar commands tend to have animations associated
+        // everything seems to be happier if you give them time to finish.
+        Thread.sleep(100);
+    }
+
+    protected boolean waitFor(String str) throws DeviceNotAvailableException, InterruptedException {
+        final String searchStr = TEST_PREFIX + str;
+        int ct = 0;
+        while (!hasLog(searchStr) && (ct++ < CHECK_RETRIES)) {
+            Thread.sleep(CHECK_DELAY);
+        }
+        return hasLog(searchStr);
+    }
+
+    protected boolean hasLog(String str) throws DeviceNotAvailableException {
+        String logs = getDevice().executeAdbCommand("logcat", "-v", "brief", "-d", mService + ":I",
+                "*:S");
+        return logs.contains(str);
+    }
+
+    private void clearLogcat() throws DeviceNotAvailableException {
+        getDevice().executeAdbCommand("logcat", "-c");
+    }
+}
diff --git a/hostsidetests/systemui/src/android/host/systemui/TileServiceTest.java b/hostsidetests/systemui/src/android/host/systemui/TileServiceTest.java
new file mode 100644
index 0000000..3c92918
--- /dev/null
+++ b/hostsidetests/systemui/src/android/host/systemui/TileServiceTest.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2016 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.host.systemui;
+
+/**
+ * Tests the lifecycle of a TileService by adding/removing it and opening/closing
+ * the notification/settings shade through adb commands.
+ */
+public class TileServiceTest extends BaseTileServiceTest {
+
+    private static final String SERVICE = "TestTileService";
+
+    public static final String ACTION_START_ACTIVITY =
+            "android.sysui.testtile.action.START_ACTIVITY";
+
+    public static final String START_ACTIVITY_AND_COLLAPSE =
+            "am broadcast -a " + ACTION_START_ACTIVITY;
+
+    public TileServiceTest() {
+        super(SERVICE);
+    }
+
+    public void testAddTile() throws Exception {
+        addTile();
+        // Verify that the service starts up and gets a onTileAdded callback.
+        assertTrue(waitFor("onCreate"));
+        assertTrue(waitFor("onTileAdded"));
+        assertTrue(waitFor("onDestroy"));
+    }
+
+    public void testRemoveTile() throws Exception {
+        addTile();
+        // Verify that the service starts up and gets a onTileAdded callback.
+        assertTrue(waitFor("onCreate"));
+        assertTrue(waitFor("onTileAdded"));
+        assertTrue(waitFor("onDestroy"));
+
+        remTile();
+        assertTrue(waitFor("onTileRemoved"));
+    }
+
+    public void testListeningNotifications() throws Exception {
+        addTile();
+        assertTrue(waitFor("onDestroy"));
+
+        // Open the notification shade and make sure the tile gets a chance to listen.
+        openNotifications();
+        assertTrue(waitFor("onStartListening"));
+        // Collapse the shade and make sure the listening ends.
+        collapse();
+        assertTrue(waitFor("onStopListening"));
+    }
+
+    public void testListeningSettings() throws Exception {
+        addTile();
+        assertTrue(waitFor("onDestroy"));
+
+        // Open the quick settings and make sure the tile gets a chance to listen.
+        openSettings();
+        assertTrue(waitFor("onStartListening"));
+        // Collapse the shade and make sure the listening ends.
+        collapse();
+        assertTrue(waitFor("onStopListening"));
+    }
+
+    public void testCantAddDialog() throws Exception {
+        addTile();
+        assertTrue(waitFor("onDestroy"));
+
+        // Wait for the tile to be added.
+        assertTrue(waitFor("onTileAdded"));
+
+        // Open the quick settings and make sure the tile gets a chance to listen.
+        openSettings();
+        assertTrue(waitFor("onStartListening"));
+
+        // Try to open a dialog, verify it doesn't happen.
+        showDialog();
+        assertTrue(waitFor("handleShowDialog"));
+        assertTrue(waitFor("onWindowAddFailed"));
+    }
+
+    public void testClick() throws Exception {
+        addTile();
+        // Wait for the tile to be added.
+        assertTrue(waitFor("onTileAdded"));
+
+        // Open the quick settings and make sure the tile gets a chance to listen.
+        openSettings();
+        assertTrue(waitFor("onStartListening"));
+
+        // Click on the tile and verify it happens.
+        clickTile();
+        assertTrue(waitFor("onClick"));
+
+        // Verify the state that gets dumped during a click.
+        // Device is expected to be unlocked and unsecure during CTS.
+        // The unlock callback should be triggered immediately.
+        assertTrue(waitFor("is_secure_false"));
+        assertTrue(waitFor("is_locked_false"));
+        assertTrue(waitFor("unlockAndRunRun"));
+    }
+
+    public void testClickAndShowDialog() throws Exception {
+        addTile();
+        assertTrue(waitFor("onDestroy"));
+
+        // Open the quick settings and make sure the tile gets a chance to listen.
+        openSettings();
+        assertTrue(waitFor("onStartListening"));
+
+        // Click on the tile and verify it happens.
+        clickTile();
+        assertTrue(waitFor("onClick"));
+
+        // Try to open a dialog, verify it doesn't happen.
+        showDialog();
+        assertTrue(waitFor("handleShowDialog"));
+        assertTrue(waitFor("onWindowFocusChanged_true"));
+    }
+
+    public void testStartActivity() throws Exception {
+        addTile();
+        // Wait for the tile to be added.
+        assertTrue(waitFor("onTileAdded"));
+
+        // Open the quick settings and make sure the tile gets a chance to listen.
+        openSettings();
+        assertTrue(waitFor("onStartListening"));
+
+        // Trigger the startActivityAndCollapse call and verify we get taken out of listening
+        // because the shade gets collapsed.
+        getDevice().executeShellCommand(START_ACTIVITY_AND_COLLAPSE);
+        assertTrue(waitFor("onStopListening"));
+    }
+
+}