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 */
16package com.android.managedprovisioning.common;
17
18import static com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences.SHARED_PREFERENCE;
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Mockito.eq;
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import android.content.Context;
25import android.content.SharedPreferences;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31import org.mockito.Mock;
32import org.mockito.MockitoAnnotations;
33
34@SmallTest
35public 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
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
60    public void tearDown() {
61        cleanUp();
62    }
63
64    private void cleanUp() {
65        mSharedPreferences.edit().clear().commit();
66    }
67
68    @Test
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