• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.eq;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.content.SharedPreferences;
28 
29 import androidx.test.InstrumentationRegistry;
30 import androidx.test.filters.SmallTest;
31 
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 
38 @SmallTest
39 public class ManagedProvisioningSharedPreferencesTest {
40 
41     private static final String KEY_TEST_SHARED_PREFERENCE =
42             "ManagedProvisioningSharedPreferencesTest";
43 
44     @Mock
45     private Context mContext;
46     private SharedPreferences mSharedPreferences;
47     private ManagedProvisioningSharedPreferences mManagedProvisioningSharedPreferences;
48 
49     @Before
setUp()50     public void setUp() {
51         MockitoAnnotations.initMocks(this);
52         Context targetContext = InstrumentationRegistry.getTargetContext();
53         mSharedPreferences = targetContext.getSharedPreferences(KEY_TEST_SHARED_PREFERENCE,
54                 Context.MODE_PRIVATE);
55         cleanUp();
56 
57         when(mContext.getSharedPreferences(eq(SHARED_PREFERENCE), eq(Context.MODE_PRIVATE)))
58                 .thenReturn(mSharedPreferences);
59         mManagedProvisioningSharedPreferences = new ManagedProvisioningSharedPreferences(mContext);
60     }
61 
62     @After
tearDown()63     public void tearDown() {
64         cleanUp();
65     }
66 
cleanUp()67     private void cleanUp() {
68         mSharedPreferences.edit().clear().commit();
69     }
70 
71     @Test
testGetAndIncrementProvisioningId()72     public void testGetAndIncrementProvisioningId() {
73         assertEquals(mManagedProvisioningSharedPreferences.incrementAndGetProvisioningId(), 1L);
74         assertEquals(mManagedProvisioningSharedPreferences.getProvisioningId(), 1L);
75 
76         assertEquals(mManagedProvisioningSharedPreferences.incrementAndGetProvisioningId(), 2L);
77         assertEquals(mManagedProvisioningSharedPreferences.getProvisioningId(), 2L);
78     }
79 
80     @Test
testProvisioningStartedTimestamp()81     public void testProvisioningStartedTimestamp() {
82         mManagedProvisioningSharedPreferences.writeProvisioningStartedTimestamp(1234);
83 
84         assertThat(mManagedProvisioningSharedPreferences.getProvisioningStartedTimestamp())
85                 .isEqualTo(1234);
86     }
87 }
88