Android Open Source - AndroidPlaces Place Search Request Test






From Project

Back to project page AndroidPlaces.

License

The source code is released under:

Apache License

If you think the Android project AndroidPlaces 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

/*
 * Copyright (C) 2014 Brian Lee/* www  .  j av  a2 s  . c o  m*/
 *
 * 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.tigerpenguin.places.request;

import android.content.Context;
import com.tigerpenguin.places.model.Language;
import com.tigerpenguin.places.model.PlaceType;
import com.tigerpenguin.places.model.PriceLevel;
import com.tigerpenguin.places.response.PlaceSearchResponse;
import com.tigerpenguin.places.response.PlacesResponseFactory;
import com.tigerpenguin.places.responsehandler.PlacesResponseHandler;

public class PlaceSearchRequestTest extends RequestTest {

    public void testValidLatLong() {
        assertValidLatLong(90.0, 180.0);
        assertValidLatLong(90.0, -180.0);
        assertValidLatLong(-90.0, 180.0);
        assertValidLatLong(-90.0, -180.0);
        assertValidLatLong(0.0, 0.0);
    }

    public void testInvalidLatLong() {
        assertInvalidLatLong(90.001, 180.0);
        assertInvalidLatLong(90.001, -180.0);

        assertInvalidLatLong(90.0, 180.001);
        assertInvalidLatLong(-90.0, 180.001);

        assertInvalidLatLong(90.001, 180.001);
        assertInvalidLatLong(90.001, -180.001);

        assertInvalidLatLong(180.0, 90.0);
        assertInvalidLatLong(180.0, -90.0);
        assertInvalidLatLong(-180.0, 90.0);
        assertInvalidLatLong(-180.0, -90.0);
    }

    public void testValidRadius() {
        assertValidRadius(0);
        assertValidRadius(1);
        assertValidRadius(25000);
        assertValidRadius(50000);
    }

    public void testInvalidRadius() {
        assertInvalidRadius(-100);
        assertInvalidRadius(-1);
        assertInvalidRadius(50001);
    }

    public void testTypesToParameterNull() {
        String value = PlaceSearchRequest.typesToParameter(null);
        assertNotNull(value);
        assertTrue(value.isEmpty());
    }

    public void testTypesToParameterSingleType() {
        for (PlaceType placeType : PlaceType.values()) {
            PlaceType[] placeTypes = { placeType };
            assertEquals(placeType.getJsonValue(), PlaceSearchRequest.typesToParameter(placeTypes));
        }
    }

    public void testTypesToParameterMultipleType() {
        PlaceType[] placeTypes = PlaceType.values();
        int index = 0;
        while (index < placeTypes.length - 5) {
            PlaceType[] twoTypes = { placeTypes[index], placeTypes[++index] };
            assertEquals(twoTypes[0].getJsonValue()
                    + "|" + twoTypes[1].getJsonValue(),
                    PlaceSearchRequest.typesToParameter(twoTypes));

            PlaceType[] threeTypes = { placeTypes[++index], placeTypes[++index], placeTypes[++index] };
            assertEquals(threeTypes[0].getJsonValue()
                    + "|" + threeTypes[1].getJsonValue()
                    + "|" + threeTypes[2].getJsonValue(),
                    PlaceSearchRequest.typesToParameter(threeTypes));
        }
    }

    public void testNextPageSearch() throws Exception {
        final String pageToken = "foopage";
        TestPlaceSearchRequest testPlaceSearchRequest = new TestPlaceSearchRequest(getContext(), pageToken);
        String url = testPlaceSearchRequest.createUrl();
        url = checkAndRemoveBaseParams(getContext(), url, null);
        url = checkAndRemoveParam(url, PARAM_PAGE_TOKEN, pageToken);
        checkUrlEmpty(url);
        checkValidation(testPlaceSearchRequest);
    }

    public void testValidatePageToken() {
        final String pageToken = "barpage";
        TestPlaceSearchRequest testPlaceSearchRequest = new TestPlaceSearchRequest(getContext(), pageToken);
        testPlaceSearchRequest.removeParameter(PARAM_PAGE_TOKEN);
        checkExecutionValidated(testPlaceSearchRequest);
    }

    public void testSetMinPrice() throws Exception {
        TestPlaceSearchRequest testPlaceSearchRequest = new TestPlaceSearchRequest(getContext());
        PriceLevel minPrice = PriceLevel.MODERATE;
        testPlaceSearchRequest.setMinPrice(minPrice);
        String url = testPlaceSearchRequest.createUrl();
        url = checkAndRemoveBaseParams(getContext(), url, null);
        url = checkAndRemoveParam(url, PARAM_MIN_PRICE, minPrice);
        checkUrlEmpty(url);
        checkValidation(testPlaceSearchRequest);
    }

    public void testSetMaxPrice() throws Exception {
        TestPlaceSearchRequest testPlaceSearchRequest = new TestPlaceSearchRequest(getContext());
        PriceLevel maxPrice = PriceLevel.EXPENSIVE;
        testPlaceSearchRequest.setMaxPrice(maxPrice);
        String url = testPlaceSearchRequest.createUrl();
        url = checkAndRemoveBaseParams(getContext(), url, null);
        url = checkAndRemoveParam(url, PARAM_MAX_PRICE, maxPrice);
        checkUrlEmpty(url);
        checkValidation(testPlaceSearchRequest);
    }

    public void testSetOpenNow() throws Exception {
        TestPlaceSearchRequest testPlaceSearchRequest = new TestPlaceSearchRequest(getContext());
        final boolean openNow = true;
        testPlaceSearchRequest.setOpenNow(openNow);
        String url = testPlaceSearchRequest.createUrl();
        url = checkAndRemoveBaseParams(getContext(), url, null);
        url = checkAndRemoveParam(url, PARAM_OPEN_NOW, openNow);
        checkUrlEmpty(url);
        checkValidation(testPlaceSearchRequest);
    }

    public void testSetLanguage() throws Exception {
        TestPlaceSearchRequest testPlaceSearchRequest = new TestPlaceSearchRequest(getContext());

        testPlaceSearchRequest.setLanguage(Language.ENGLISH);
        String url = testPlaceSearchRequest.createUrl();
        url = checkAndRemoveBaseParams(getContext(), url, null);
        url = checkAndRemoveParam(url, PARAM_LANGUAGE, Language.ENGLISH);
        checkUrlEmpty(url);
        checkValidation(testPlaceSearchRequest);
    }

    private void assertValidLatLong(double latitude, double longitude) {
        boolean exceptionCaught = false;
        try {
            PlaceSearchRequest.checkLatLong(latitude, longitude);
        } catch (IllegalArgumentException iae) {
            exceptionCaught = true;
        }
        assertFalse("Valid lat/long was not allowed. Lat: " + latitude + " Long: " + longitude, exceptionCaught);
    }

    private void assertInvalidLatLong(double latitude, double longitude) {
        boolean exceptionCaught = false;
        try {
            PlaceSearchRequest.checkLatLong(latitude, longitude);
        } catch (IllegalArgumentException iae) {
            exceptionCaught = true;
        }
        assertTrue("Invalid lat/long was allowed. Lat: " + latitude + " Long: " + longitude, exceptionCaught);
    }

    private void assertValidRadius(int radius) {
        boolean exceptionCaught = false;
        try {
            PlaceSearchRequest.checkRadius(radius);
        } catch (IllegalArgumentException iae) {
            exceptionCaught = true;
        }
        assertFalse("Valid radius was not allowed: " + radius, exceptionCaught);
    }

    private void assertInvalidRadius(int radius) {
        boolean exceptionCaught = false;
        try {
            PlaceSearchRequest.checkRadius(radius);
        } catch (IllegalArgumentException iae) {
            exceptionCaught = true;
        }
        assertTrue("Invalid radius was allowed: " + radius, exceptionCaught);
    }

    private static class TestPlaceSearchRequest extends PlaceSearchRequest<PlaceSearchRequest, PlaceSearchResponse> {

        protected TestPlaceSearchRequest(Context context) {
            super(context, new TestResponseHandler());
        }

        protected TestPlaceSearchRequest(Context context, String nextPageToken) {
            super(context, nextPageToken, new TestResponseHandler());
        }

        @Override
        protected PlacesResponseFactory<PlaceSearchRequest, PlaceSearchResponse> getResponseFactory() {
            return null;
        }

        @Override
        protected String getRequestPath() {
            return "";
        }

        @Override
        protected PlaceSearchRequest getThis() {
            return this;
        }
    }

    private static class TestResponseHandler implements PlacesResponseHandler<PlaceSearchResponse> {
        @Override
        public void handleResponse(PlaceSearchResponse response) {

        }
    }
}




Java Source Code List

com.tigerpenguin.demo.places.PlaceDetailActivity.java
com.tigerpenguin.demo.places.PlaceResultAdapter.java
com.tigerpenguin.demo.places.PlaceResultsFragment.java
com.tigerpenguin.demo.places.PlaceReviewAdapter.java
com.tigerpenguin.demo.places.PlaceReviewsFragment.java
com.tigerpenguin.demo.places.PlaceThumbnailAdapter.java
com.tigerpenguin.demo.places.PlaceThumbnailsFragment.java
com.tigerpenguin.demo.places.PlacesDemo.java
com.tigerpenguin.places.PlacesTest.java
com.tigerpenguin.places.deserializer.HtmlStringDeserializer.java
com.tigerpenguin.places.deserializer.PlaceTypeDeserializer.java
com.tigerpenguin.places.model.AddressComponent.java
com.tigerpenguin.places.model.AspectRating.java
com.tigerpenguin.places.model.AspectType.java
com.tigerpenguin.places.model.DayOfWeek.java
com.tigerpenguin.places.model.DayTime.java
com.tigerpenguin.places.model.Geometry.java
com.tigerpenguin.places.model.JsonModel.java
com.tigerpenguin.places.model.Language.java
com.tigerpenguin.places.model.OpeningHours.java
com.tigerpenguin.places.model.Period.java
com.tigerpenguin.places.model.Photo.java
com.tigerpenguin.places.model.PlaceDetail.java
com.tigerpenguin.places.model.PlaceLocation.java
com.tigerpenguin.places.model.PlaceType.java
com.tigerpenguin.places.model.Place.java
com.tigerpenguin.places.model.PriceLevel.java
com.tigerpenguin.places.model.RankBy.java
com.tigerpenguin.places.model.Review.java
com.tigerpenguin.places.model.StatusCode.java
com.tigerpenguin.places.request.InvalidRequestException.java
com.tigerpenguin.places.request.NearbySearchRequestTest.java
com.tigerpenguin.places.request.NearbySearchRequest.java
com.tigerpenguin.places.request.PlaceDetailRequestTest.java
com.tigerpenguin.places.request.PlaceDetailRequest.java
com.tigerpenguin.places.request.PlaceSearchRequestTest.java
com.tigerpenguin.places.request.PlaceSearchRequest.java
com.tigerpenguin.places.request.PlacesRequestTest.java
com.tigerpenguin.places.request.PlacesRequest.java
com.tigerpenguin.places.request.RequestTest.java
com.tigerpenguin.places.response.NearbySearchResponseFactory.java
com.tigerpenguin.places.response.NearbySearchResponse.java
com.tigerpenguin.places.response.PlaceDetailResponseFactory.java
com.tigerpenguin.places.response.PlaceDetailResponse.java
com.tigerpenguin.places.response.PlaceSearchResponse.java
com.tigerpenguin.places.response.PlacesResponseFactory.java
com.tigerpenguin.places.response.PlacesResponse.java
com.tigerpenguin.places.responsehandler.NearbySearchHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlaceDetailResponseHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlacesResponseHandler.java
com.tigerpenguin.widget.simpleratingbar.MockAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarTest.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBar.java