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 17 package com.android.retaildemo; 18 19 import android.content.Context; 20 import android.support.test.filters.SmallTest; 21 import android.support.test.runner.AndroidJUnit4; 22 23 import java.io.File; 24 import org.junit.After; 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.mockito.Mock; 29 import org.mockito.MockitoAnnotations; 30 31 import static android.support.test.InstrumentationRegistry.getTargetContext; 32 33 import static org.junit.Assert.assertEquals; 34 import static org.mockito.Mockito.when; 35 36 @RunWith(AndroidJUnit4.class) 37 @SmallTest 38 public class DataReaderWriterTest { 39 40 private @Mock Context mContext; 41 42 private File mDataFile; 43 44 @Before setUp()45 public void setUp() { 46 MockitoAnnotations.initMocks(this); 47 48 when(mContext.getObbDir()).thenReturn(getTargetContext().getCacheDir()); 49 mDataFile = new File(DataReaderWriter.getFilePath(mContext)); 50 deleteDataFile(); 51 } 52 53 @After tearDown()54 public void tearDown() { 55 deleteDataFile(); 56 } 57 58 @Test testWriteReadLastBootCount()59 public void testWriteReadLastBootCount() { 60 final int testBootCount = 11; 61 DataReaderWriter.writeLastBootCount(mContext, testBootCount); 62 assertEquals(testBootCount, DataReaderWriter.readLastBootCount(mContext)); 63 } 64 65 @Test testWriteReadLastBootCount_fileAlreadyExists()66 public void testWriteReadLastBootCount_fileAlreadyExists() throws Exception { 67 mDataFile.createNewFile(); 68 69 final int testBootCount = 11; 70 DataReaderWriter.writeLastBootCount(mContext, testBootCount); 71 assertEquals(testBootCount, DataReaderWriter.readLastBootCount(mContext)); 72 } 73 74 @Test testReadLastBootCount_firstTime()75 public void testReadLastBootCount_firstTime() { 76 assertEquals(0, DataReaderWriter.readLastBootCount(mContext)); 77 } 78 79 @Test testReadLastBootCount_error()80 public void testReadLastBootCount_error() throws Exception { 81 mDataFile.createNewFile(); 82 assertEquals(-1, DataReaderWriter.readLastBootCount(mContext)); 83 } 84 deleteDataFile()85 private void deleteDataFile() { 86 if (mDataFile.exists()) { 87 mDataFile.delete(); 88 } 89 } 90 }