Last active
January 2, 2016 07:22
-
-
Save 75py/4d7ab2000c9d38111ef5 to your computer and use it in GitHub Desktop.
UIAutomatorで、対象パッケージのランタイムパーミッションを全て許可するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2015 75py | |
* | |
* 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.nagopy.android; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.provider.Settings; | |
import android.support.test.InstrumentationRegistry; | |
import android.support.test.uiautomator.By; | |
import android.support.test.uiautomator.UiDevice; | |
import android.support.test.uiautomator.UiObject2; | |
import android.util.Log; | |
import java.util.List; | |
import java.util.regex.Pattern; | |
/** | |
* androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' | |
*/ | |
public class UiTestUtil { | |
private static final long TIMEOUT_MS = 5000; | |
private static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; | |
private static final String TAG = "UiTestUtil"; | |
private static final Context context = InstrumentationRegistry.getContext(); | |
private static final UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); | |
private static final Pattern PATTERN_PERMISSION = Pattern.compile("Permissions|許可"); | |
private UiTestUtil() { | |
throw new AssertionError(); | |
} | |
public static void grantAllPermissions(String packageName) { | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | |
return; | |
} | |
try { | |
Intent i = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + packageName)) | |
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | |
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
context.startActivity(i); | |
uiDevice.waitForWindowUpdate(SETTINGS_PACKAGE_NAME, TIMEOUT_MS); | |
uiDevice.findObject(By.text(PATTERN_PERMISSION)).click(); | |
uiDevice.waitForWindowUpdate(SETTINGS_PACKAGE_NAME, TIMEOUT_MS); | |
List<UiObject2> checkableObjects = uiDevice.findObjects(By.checkable(true)); | |
for (UiObject2 checkableObj : checkableObjects) { | |
if (!checkableObj.isChecked()) { | |
checkableObj.click(); | |
uiDevice.waitForIdle(TIMEOUT_MS); | |
} | |
} | |
} catch (Throwable t) { | |
Log.e(TAG, "Failed", t); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment