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 package com.android.managedprovisioning.common; 17 18 import static com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences.SHARED_PREFERENCE; 19 import static org.junit.Assert.assertEquals; 20 import static org.mockito.Mockito.eq; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.SharedPreferences; 26 import android.support.test.InstrumentationRegistry; 27 import android.support.test.filters.SmallTest; 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 34 @SmallTest 35 public class ManagedProvisioningSharedPreferencesTest { 36 37 private static final String KEY_TEST_SHARED_PREFERENCE = 38 "ManagedProvisioningSharedPreferencesTest"; 39 40 @Mock 41 Context mContext; 42 SharedPreferences mSharedPreferences; 43 44 ManagedProvisioningSharedPreferences mManagedProvisioningSharedPreferences; 45 46 @Before setUp()47 public void setUp() { 48 MockitoAnnotations.initMocks(this); 49 Context targetContext = InstrumentationRegistry.getTargetContext(); 50 mSharedPreferences = targetContext.getSharedPreferences(KEY_TEST_SHARED_PREFERENCE, 51 Context.MODE_PRIVATE); 52 cleanUp(); 53 54 when(mContext.getSharedPreferences(eq(SHARED_PREFERENCE), eq(Context.MODE_PRIVATE))) 55 .thenReturn(mSharedPreferences); 56 mManagedProvisioningSharedPreferences = new ManagedProvisioningSharedPreferences(mContext); 57 } 58 59 @After tearDown()60 public void tearDown() { 61 cleanUp(); 62 } 63 cleanUp()64 private void cleanUp() { 65 mSharedPreferences.edit().clear().commit(); 66 } 67 68 @Test testGetAndIncrementProvisioningId()69 public void testGetAndIncrementProvisioningId() { 70 assertEquals(mManagedProvisioningSharedPreferences.incrementAndGetProvisioningId(), 1L); 71 assertEquals(mManagedProvisioningSharedPreferences.getProvisioningId(), 1L); 72 73 assertEquals(mManagedProvisioningSharedPreferences.incrementAndGetProvisioningId(), 2L); 74 assertEquals(mManagedProvisioningSharedPreferences.getProvisioningId(), 2L); 75 } 76 } 77