org.deviceconnect.android.profile.restful.test.NormalMediaPlayerProfileTestCase.java Source code

Java tutorial

Introduction

Here is the source code for org.deviceconnect.android.profile.restful.test.NormalMediaPlayerProfileTestCase.java

Source

/*
 NormalMediaPlayerProfileTestCase.java
 Copyright (c) 2014 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */
package org.deviceconnect.android.profile.restful.test;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.deviceconnect.android.test.plugin.profile.TestMediaPlayerProfileConstants;
import org.deviceconnect.message.DConnectMessage;
import org.deviceconnect.profile.AuthorizationProfileConstants;
import org.deviceconnect.profile.DConnectProfileConstants;
import org.deviceconnect.profile.MediaPlayerProfileConstants;
import org.deviceconnect.utils.URIBuilder;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * MediaPlayer?.
 * @author NTT DOCOMO, INC.
 */
public class NormalMediaPlayerProfileTestCase extends RESTfulDConnectTestCase
        implements TestMediaPlayerProfileConstants {

    /**
     * .
     * 
     * @param tag 
     */
    public NormalMediaPlayerProfileTestCase(final String tag) {
        super(tag);
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/media?deviceId=xxxx&mediaId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutMedia() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_MEDIA);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        builder.addParameter(MediaPlayerProfileConstants.PARAM_MEDIA_ID, TEST_MEDIA_ID);
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ??????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /media_player/media?deviceId=xxxx&mediaId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testGetMedia() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_MEDIA);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        builder.addParameter(MediaPlayerProfileConstants.PARAM_MEDIA_ID, TEST_MEDIA_ID);
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ??????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /media_player/media_list?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testGetMediaList() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_MEDIA_LIST);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ??????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /media_player/play_status?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testGetPlayStatus() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_PLAY_STATUS);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
            assertEquals(MediaPlayerProfileConstants.PlayStatus.PLAY.getValue(),
                    response.getString(MediaPlayerProfileConstants.PARAM_STATUS));
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/play?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutPlay() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_PLAY);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/stop?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutStop() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_STOP);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/pause?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutPause() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_PAUSE);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/resume?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutResume() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_RESUME);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ??????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/seek?deviceId=xxxx&pos=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutSeek() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_SEEK);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        builder.addParameter(MediaPlayerProfileConstants.PARAM_POS, String.valueOf(0));
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ???????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /media_player/seek?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testGetSeek() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_SEEK);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
            assertEquals(0, response.getInt(MediaPlayerProfileConstants.PARAM_POS));
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ??????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/volume?deviceId=xxxx&volume=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutVolume() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_VOLUME);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        builder.addParameter(MediaPlayerProfileConstants.PARAM_VOLUME, String.valueOf(TEST_VOLUME));
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ???????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /media_player/volume?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testGetVolume() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_VOLUME);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
            assertEquals(0.5, response.getDouble(MediaPlayerProfileConstants.PARAM_VOLUME));
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/mute?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutMute() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_MUTE);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /media_player/mute?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testDeleteMute() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_MUTE);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpDelete(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: GET
     * Path: /media_player/mute?deviceId=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testGetMute() {
        URIBuilder builder = TestURIBuilder.createURIBuilder();
        builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
        builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_MUTE);
        builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
        builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
        try {
            HttpUriRequest request = new HttpGet(builder.toString());
            JSONObject response = sendRequest(request);
            assertResultOK(response);
            assertEquals(true, response.getBoolean(MediaPlayerProfileConstants.PARAM_MUTE));
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ????.
     * <pre>
     * ?HTTP
     * Method: PUT
     * Path: /media_player/onstatuschange?deviceid=xxxx&session_key=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testPutOnStatusChangePlay() {
        StringBuilder builder = new StringBuilder();
        builder.append(DCONNECT_MANAGER_URI);
        builder.append("/" + MediaPlayerProfileConstants.PROFILE_NAME);
        builder.append("/" + MediaPlayerProfileConstants.ATTRIBUTE_ON_STATUS_CHANGE);
        builder.append("?");
        builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
        builder.append("&");
        builder.append(DConnectProfileConstants.PARAM_SESSION_KEY + "=" + getClientId());
        builder.append("&");
        builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
        try {
            HttpUriRequest request = new HttpPut(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultOK(root);
            // ???
            JSONObject event = waitForEvent();
            assertNotNull(event);
            assertEquals(MediaPlayerProfileConstants.ATTRIBUTE_ON_STATUS_CHANGE,
                    event.getString(DConnectMessage.EXTRA_ATTRIBUTE));
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

    /**
     * ?????.
     * <pre>
     * ?HTTP
     * Method: DELETE
     * Path: /media_player/onstatuschange?deviceid=xxxx&session_key=xxxx
     * </pre>
     * <pre>
     * ??
     * result?0???????
     * </pre>
     */
    public void testDeleteOnPlayStatusChange() {
        StringBuilder builder = new StringBuilder();
        builder.append(DCONNECT_MANAGER_URI);
        builder.append("/" + MediaPlayerProfileConstants.PROFILE_NAME);
        builder.append("/" + MediaPlayerProfileConstants.ATTRIBUTE_ON_STATUS_CHANGE);
        builder.append("?");
        builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
        builder.append("&");
        builder.append(DConnectProfileConstants.PARAM_SESSION_KEY + "=" + getClientId());
        builder.append("&");
        builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
        try {
            HttpUriRequest request = new HttpDelete(builder.toString());
            JSONObject root = sendRequest(request);
            assertResultOK(root);
        } catch (JSONException e) {
            fail("Exception in JSONObject." + e.getMessage());
        }
    }

}