1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.wifi.hotspot2; 18 19import static org.junit.Assert.assertNotNull; 20import static org.junit.Assert.assertNull; 21import static org.junit.Assert.assertTrue; 22import static org.mockito.Mockito.when; 23import static org.mockito.MockitoAnnotations.initMocks; 24 25import android.test.suitebuilder.annotation.SmallTest; 26 27import com.android.server.wifi.Clock; 28import com.android.server.wifi.hotspot2.ANQPData; 29import com.android.server.wifi.hotspot2.AnqpCache; 30 31import org.junit.Before; 32import org.junit.Test; 33import org.mockito.Mock; 34 35/** 36 * Unit tests for {@link com.android.server.wifi.hotspot2.AnqpCache}. 37 * 38 * TODO(b/33000864): add more test once the ANQP elements cleanup are completed, which will 39 * allow easy construction of ANQP elements for testing. 40 */ 41@SmallTest 42public class AnqpCacheTest { 43 private static final ANQPNetworkKey ENTRY_KEY = new ANQPNetworkKey("test", 0L, 0L, 1); 44 45 @Mock Clock mClock; 46 AnqpCache mCache; 47 48 /** 49 * Sets up test. 50 */ 51 @Before 52 public void setUp() throws Exception { 53 initMocks(this); 54 // Returning the initial timestamp. 55 when(mClock.getElapsedSinceBootMillis()).thenReturn(0L); 56 mCache = new AnqpCache(mClock); 57 } 58 59 /** 60 * Verify expectation for addEntry and getEntry. 61 * 62 * @throws Exception 63 */ 64 @Test 65 public void addAndGetEntry() throws Exception { 66 mCache.addEntry(ENTRY_KEY, null); 67 ANQPData data = mCache.getEntry(ENTRY_KEY); 68 assertNotNull(data); 69 assertTrue(data.getElements().isEmpty()); 70 } 71 72 /** 73 * Verify that getting a non-existing entry will return a null. 74 * 75 * @throws Exception 76 */ 77 @Test 78 public void getNonExistEntry() throws Exception { 79 assertNull(mCache.getEntry(ENTRY_KEY)); 80 } 81 82 /** 83 * Verify the expectation for the sweep function (expired entries will be removed). 84 * 85 * @throws Exception 86 */ 87 @Test 88 public void sweepRemoveExpiredEntry() throws Exception { 89 mCache.addEntry(ENTRY_KEY, null); 90 91 // Sweep the cache when the entry is not expired. 92 when(mClock.getElapsedSinceBootMillis()) 93 .thenReturn(AnqpCache.CACHE_SWEEP_INTERVAL_MILLISECONDS); 94 mCache.sweep(); 95 assertNotNull(mCache.getEntry(ENTRY_KEY)); 96 97 // Sweep the cache when the entry is expired. 98 when(mClock.getElapsedSinceBootMillis()).thenReturn(ANQPData.DATA_LIFETIME_MILLISECONDS); 99 mCache.sweep(); 100 assertNull(mCache.getEntry(ENTRY_KEY)); 101 } 102} 103