1package com.android.server.twilight; 2 3/* 4 * Copyright (C) 2016 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19import android.app.AlarmManager; 20import android.content.Context; 21import android.location.Location; 22import android.test.AndroidTestCase; 23 24public class TwilightServiceTest extends AndroidTestCase { 25 26 private TwilightService mTwilightService; 27 private Location mInitialLocation; 28 29 @Override 30 protected void setUp() throws Exception { 31 final Context context = getContext(); 32 mTwilightService = new TwilightService(context); 33 mTwilightService.mAlarmManager = 34 (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 35 36 mInitialLocation = createMockLocation(10.0, 10.0); 37 mTwilightService.onLocationChanged(mInitialLocation); 38 } 39 40 @Override 41 protected void tearDown() throws Exception { 42 mTwilightService = null; 43 mInitialLocation = null; 44 } 45 46 public void testValidLocation_updatedLocation() { 47 final TwilightState priorState = mTwilightService.mLastTwilightState; 48 final Location validLocation = createMockLocation(35.0, 35.0); 49 mTwilightService.onLocationChanged(validLocation); 50 assertEquals(mTwilightService.mLastLocation, validLocation); 51 assertNotSame(priorState, mTwilightService.mLastTwilightState); 52 } 53 54 public void testInvalidLocation_ignoreLocationUpdate() { 55 final TwilightState priorState = mTwilightService.mLastTwilightState; 56 final Location invalidLocation = createMockLocation(0.0, 0.0); 57 mTwilightService.onLocationChanged(invalidLocation); 58 assertEquals(mTwilightService.mLastLocation, mInitialLocation); 59 assertEquals(priorState, mTwilightService.mLastTwilightState); 60 } 61 62 private Location createMockLocation(double latitude, double longitude) { 63 // There's no empty constructor, so we initialize with a string and quickly reset it. 64 final Location location = new Location(""); 65 location.reset(); 66 location.setLatitude(latitude); 67 location.setLongitude(longitude); 68 return location; 69 } 70 71} 72