如何使用 Google Drive Android SDK 自动授权以进行仪器测试?
How to auth automatically with Google Drive Android SDK for instrumentation testing?
为了提高应用质量,我正在进行测试:单元测试和 UI 测试。因为我在应用程序中有 Google Drive 支持 Android SDK 我想测试它,我需要在测试前授权 Google Drive 帐户(在我的 android 应用程序,用户可以保存文件、读取文件、重命名等 - 基本文件例程)。
在 auth example 中显示了身份验证是如何完成的:
// 1. try connect
private void startAuthGoogleDrive() {
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
GoogleApiClientHolder.get().setClient(mGoogleApiClient);
mGoogleApiClient.connect();
}
// 2. if failed - Google Drive auth intent is sent to Google Drive
// if already authorized - go to #4
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
Toast.makeText(this, R.string.Google_signin_required, Toast.LENGTH_LONG)
.show();
try {
connectionResult.startResolutionForResult(this, ACTION_SIGNIN_GOOGLE_DRIVE); // sending intent to auth
} catch (IntentSender.SendIntentException e) {
// Unable to resolve, message user appropriately
}
} else {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
}
}
// 3. check result intent
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
// Google Drive signed in
if (requestCode == ACTION_SIGNIN_GOOGLE_DRIVE && resultCode == RESULT_OK) {
// Google Drive does not provide token, just try to connect()
// and get callback onConnected() instead
GoogleApiClientHolder.get().getClient().connect();
}
// 4. got connected callback
@Override
public void onConnected(Bundle bundle) {
... // ready
}
问题是:
- 没有返回访问令牌(不可能以其他方式进行身份验证(例如,在应用程序中手动 activity)并且只是在测试中对访问令牌进行硬编码
- 无法在测试中进行正确的授权,因为无法与 UI 交互(Google Drive Auth activity)
我该怎么做才能对 Google 驱动器进行自动化单元测试?
我想避免将测试移动到 UI 使用 Espresso 的测试并将其保留在仪器测试中。
您可能需要检查 Preparing to make an authorized API call 其中指出,如果您需要 运行 在其他环境中使用您的应用程序,例如在本地测试您的应用程序,您必须检测到这种情况并使用不同的凭证机制。
您可以选择使用这个:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.sqladmin.SQLAdminScopes;
// ...
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json"))
.createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN));
有关详细信息,另请参阅 Using OAuth 2.0 for Server to Server Applications。
我必须创建应用程序并且 运行 它至少要手动登录一次(所以我有 android-library
代码和 android-app
应用程序代码(src/main/java
) 用于手动记录和 android 测试 (src/androidTest/java
))。由于过期 token/session,测试有时会失败,但通常它允许执行自动测试。
为了提高应用质量,我正在进行测试:单元测试和 UI 测试。因为我在应用程序中有 Google Drive 支持 Android SDK 我想测试它,我需要在测试前授权 Google Drive 帐户(在我的 android 应用程序,用户可以保存文件、读取文件、重命名等 - 基本文件例程)。
在 auth example 中显示了身份验证是如何完成的:
// 1. try connect
private void startAuthGoogleDrive() {
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
GoogleApiClientHolder.get().setClient(mGoogleApiClient);
mGoogleApiClient.connect();
}
// 2. if failed - Google Drive auth intent is sent to Google Drive
// if already authorized - go to #4
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
Toast.makeText(this, R.string.Google_signin_required, Toast.LENGTH_LONG)
.show();
try {
connectionResult.startResolutionForResult(this, ACTION_SIGNIN_GOOGLE_DRIVE); // sending intent to auth
} catch (IntentSender.SendIntentException e) {
// Unable to resolve, message user appropriately
}
} else {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
}
}
// 3. check result intent
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
// Google Drive signed in
if (requestCode == ACTION_SIGNIN_GOOGLE_DRIVE && resultCode == RESULT_OK) {
// Google Drive does not provide token, just try to connect()
// and get callback onConnected() instead
GoogleApiClientHolder.get().getClient().connect();
}
// 4. got connected callback
@Override
public void onConnected(Bundle bundle) {
... // ready
}
问题是:
- 没有返回访问令牌(不可能以其他方式进行身份验证(例如,在应用程序中手动 activity)并且只是在测试中对访问令牌进行硬编码
- 无法在测试中进行正确的授权,因为无法与 UI 交互(Google Drive Auth activity)
我该怎么做才能对 Google 驱动器进行自动化单元测试?
我想避免将测试移动到 UI 使用 Espresso 的测试并将其保留在仪器测试中。
您可能需要检查 Preparing to make an authorized API call 其中指出,如果您需要 运行 在其他环境中使用您的应用程序,例如在本地测试您的应用程序,您必须检测到这种情况并使用不同的凭证机制。
您可以选择使用这个:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.sqladmin.SQLAdminScopes;
// ...
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json"))
.createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN));
有关详细信息,另请参阅 Using OAuth 2.0 for Server to Server Applications。
我必须创建应用程序并且 运行 它至少要手动登录一次(所以我有 android-library
代码和 android-app
应用程序代码(src/main/java
) 用于手动记录和 android 测试 (src/androidTest/java
))。由于过期 token/session,测试有时会失败,但通常它允许执行自动测试。