Back to project page AndroidPlaces.
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.
/* * 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) { } } }