Bug: 22979576 CTS: RingtoneManagerTest failing
Added a utils file to turn on/off appops. Also added
calls to use the utility method to turn on appops for write_settings
for RingtoneManagerTest. This should stop the tests from failing.
Change-Id: I9d787fc7dbb48c95c57d0199a3b6122a488db07f
diff --git a/tests/tests/media/src/android/media/cts/RingtoneManagerTest.java b/tests/tests/media/src/android/media/cts/RingtoneManagerTest.java
index bf47a27..4693036 100644
--- a/tests/tests/media/src/android/media/cts/RingtoneManagerTest.java
+++ b/tests/tests/media/src/android/media/cts/RingtoneManagerTest.java
@@ -55,6 +55,7 @@
mActivity = getActivity();
mInstrumentation = getInstrumentation();
mContext = mInstrumentation.getContext();
+ Utils.enableAppOps(mContext.getPackageName(), "android:write_settings", mInstrumentation);
mRingtoneManager = new RingtoneManager(mActivity);
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
// backup ringer settings
@@ -74,6 +75,7 @@
}
RingtoneManager.setActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_RINGTONE,
mDefaultUri);
+ Utils.disableAppOps(mContext.getPackageName(), "android:write_settings", mInstrumentation);
super.tearDown();
}
diff --git a/tests/tests/media/src/android/media/cts/Utils.java b/tests/tests/media/src/android/media/cts/Utils.java
new file mode 100644
index 0000000..60a4462
--- /dev/null
+++ b/tests/tests/media/src/android/media/cts/Utils.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2015 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.media.cts;
+
+import android.app.Instrumentation;
+import android.app.UiAutomation;
+import android.os.ParcelFileDescriptor;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.Scanner;
+
+public class Utils {
+ public static void enableAppOps(String packageName, String operation,
+ Instrumentation instrumentation) {
+ setAppOps(packageName, operation, instrumentation, true);
+ }
+
+ public static void disableAppOps(String packageName, String operation,
+ Instrumentation instrumentation) {
+ setAppOps(packageName, operation, instrumentation, false);
+ }
+
+ public static String convertStreamToString(InputStream is) {
+ try (Scanner scanner = new Scanner(is).useDelimiter("\\A")) {
+ return scanner.hasNext() ? scanner.next() : "";
+ }
+ }
+
+ private static void setAppOps(String packageName, String operation,
+ Instrumentation instrumentation, boolean enable) {
+ StringBuilder cmd = new StringBuilder();
+ cmd.append("appops set ");
+ cmd.append(packageName);
+ cmd.append(" ");
+ cmd.append(operation);
+ cmd.append(enable ? " allow" : " deny");
+ instrumentation.getUiAutomation().executeShellCommand(cmd.toString());
+
+ StringBuilder query = new StringBuilder();
+ query.append("appops get ");
+ query.append(packageName);
+ query.append(" ");
+ query.append(operation);
+ String queryStr = query.toString();
+
+ String result = enable ? "allow" : "deny";
+ while(!result.contains(result)) {
+ ParcelFileDescriptor pfd = instrumentation.getUiAutomation().executeShellCommand(
+ queryStr);
+ InputStream inputStream = new FileInputStream(pfd.getFileDescriptor());
+ result = convertStreamToString(inputStream);
+ }
+ }
+}