If you think the Android project gasp-gcm-client listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.cloudbees.gasp.twitter;
//fromwww.java2s.comimport android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.test.ServiceTestCase;
import android.util.Base64;
import com.cloudbees.gasp.model.TwitterTokenResponse;
import com.cloudbees.gasp.service.RESTIntentService;
import com.google.gson.Gson;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Copyright (c) 2013 Mark Prichard, CloudBees
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
*/publicclass TwitterAuthServiceTest extends ServiceTestCase<RESTIntentService> {
private CountDownLatch signal = new CountDownLatch(1);
private ResultReceiver mResultReceiver;
public TwitterAuthServiceTest() {
super(RESTIntentService.class);
mResultReceiver = new ResultReceiver(new Handler()) {
@Override
protectedvoid onReceiveResult(int resultCode, Bundle resultData) {
if (resultData != null && resultData.containsKey(RESTIntentService.REST_RESULT)) {
onRESTResult(resultCode, resultData.getString(RESTIntentService.REST_RESULT));
}
else {
onRESTResult(resultCode, null);
}
}
};
}
publicvoid onRESTResult(int code, String result) {
if (code == 200 && result != null) {
TwitterTokenResponse twitterToken = new Gson().fromJson(result, TwitterTokenResponse.class);
assertNotNull(twitterToken);
assert (twitterToken.getToken_type().equalsIgnoreCase("bearer"));
}
else {
fail();
}
signal.countDown();
}
publicvoid testTwitterAuthUtils() {
assertNotNull(TwitterAuthentication.getEncodedBase64Credentials());
try{
String authEncodedBase64 =
TwitterAuthentication.getEncodedBase64Credentials().replaceFirst("Basic ", "");
Base64.decode(authEncodedBase64, Base64.NO_WRAP);
}
catch (Exception e) {
fail();
}
assertNotNull(TwitterAuthentication.getTwitterApiOAuthToken());
}
publicvoid testTwitterAuth() {
try {
Intent intent = new Intent(getContext(), RESTIntentService.class);
intent.setData(Uri.parse(TwitterAuthentication.getTwitterApiOAuthToken()));
Bundle params = new Bundle();
params.putString("grant_type", "client_credentials");
Bundle headers = new Bundle();
headers.putString("Authorization", TwitterAuthentication.getEncodedBase64Credentials());
intent.putExtra(RESTIntentService.EXTRA_HTTP_VERB, RESTIntentService.POST);
intent.putExtra(RESTIntentService.EXTRA_HEADERS, headers);
intent.putExtra(RESTIntentService.EXTRA_PARAMS, params);
intent.putExtra(RESTIntentService.EXTRA_RESULT_RECEIVER, mResultReceiver);
signal.await(10, TimeUnit.SECONDS);
}
catch (Exception e) {
fail();
}
}
}