/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.support.media.instantvideo.preload;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

import android.net.Uri;
import android.support.media.instantvideo.preload.InstantVideoPreloadManager.VideoPreloader;
import android.support.media.instantvideo.preload.InstantVideoPreloadManager.VideoPreloaderFactory;
import android.support.test.filters.SmallTest;
import android.test.AndroidTestCase;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/**
 * Tests for IntentVideoPreloadManager.
 */
@SmallTest
public class InstantVideoPreloadManagerTest extends AndroidTestCase {
    private static final Uri PRELOAD_VIDEO_URI_1 = Uri.parse("http://test/test1.mp4");
    private static final Uri PRELOAD_VIDEO_URI_2 = Uri.parse("http://test/test2.mp4");

    private InstantVideoPreloadManager mPreloadManager;
    @Mock private VideoPreloaderFactory mMockVideoPreloaderFactory;
    @Mock private VideoPreloader mMockVideoPreloader;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        MockitoAnnotations.initMocks(this);
        when(mMockVideoPreloaderFactory.createVideoPreloader(any(Uri.class)))
                .thenReturn(mMockVideoPreloader);
        mPreloadManager = new InstantVideoPreloadManager(getContext(), mMockVideoPreloaderFactory);
    }

    @Override
    public void tearDown() throws Exception {
        mPreloadManager.clearCache();
        super.tearDown();
    }

    public void testPreload() {
        mPreloadManager.preload(PRELOAD_VIDEO_URI_1);
        assertCacheSize(1);
        mPreloadManager.preload(PRELOAD_VIDEO_URI_2);
        assertCacheSize(2);
    }

    public void testPreload_duplicate() {
        mPreloadManager.preload(PRELOAD_VIDEO_URI_1);
        mPreloadManager.preload(PRELOAD_VIDEO_URI_1);
        assertCacheSize(1);
    }

    public void testMaxPreloadVideoCount() {
        mPreloadManager.setMaxPreloadVideoCount(1);
        mPreloadManager.preload(PRELOAD_VIDEO_URI_1);
        assertCacheSize(1);
        mPreloadManager.preload(PRELOAD_VIDEO_URI_2);
        assertCacheSize(1);
    }

    public void testClearCache() {
        mPreloadManager.preload(PRELOAD_VIDEO_URI_1);
        mPreloadManager.preload(PRELOAD_VIDEO_URI_2);
        mPreloadManager.clearCache();
        assertCacheSize(0);
    }

    private void assertCacheSize(int expected) {
        int cacheSize = mPreloadManager.getCacheSize();
        assertEquals("The cache size should be " + expected + ", but was " + cacheSize, expected,
                cacheSize);
    }
}

@@ -666,16 +666,133 @@
 eload;%0A%0A
+import static android.support.test.InstrumentationRegistry.getContext;%0A%0Aimport static org.junit.Assert.assertEquals;%0A
 import s
@@ -1146,30 +1146,148 @@
 oid.
-test.AndroidTestCase;%0A
+support.test.runner.AndroidJUnit4;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;
 %0Aimp
@@ -1407,16 +1407,46 @@
 allTest%0A
+@RunWith(AndroidJUnit4.class)%0A
 public c
@@ -1485,32 +1485,8 @@
 est 
-extends AndroidTestCase 
 %7B%0A  
@@ -1715,32 +1715,36 @@
 nager;%0A    @Mock
+%0A   
  private VideoPr
@@ -1795,16 +1795,20 @@
    @Mock
+%0A   
  private
@@ -1846,31 +1846,29 @@
 ader;%0A%0A    @
-Overrid
+Befor
 e%0A    public
@@ -1885,49 +1885,9 @@
 p() 
-throws Exception %7B%0A        super.setUp();
+%7B
 %0A   
@@ -2171,16 +2171,13 @@
    @
-Override
+After
 %0A   
@@ -2203,25 +2203,8 @@
 wn()
- throws Exception
  %7B%0A 
@@ -2248,36 +2248,20 @@
     
+%7D%0A%0A
     
-super.tearDown();%0A    %7D%0A
+@Test
 %0A   
@@ -2269,29 +2269,25 @@
 public void 
-testP
+p
 reload() %7B%0A 
@@ -2448,32 +2448,42 @@
 Size(2);%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -2486,13 +2486,9 @@
 oid 
-testP
+p
 relo
@@ -2639,32 +2639,42 @@
 Size(1);%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -2673,19 +2673,18 @@
 ic void 
-te
 s
+e
 tMaxPrel
@@ -2920,24 +2920,34 @@
 (1);%0A    %7D%0A%0A
+    @Test%0A
     public v
@@ -2954,13 +2954,9 @@
 oid 
-testC
+c
 lear

Migrate test to JUnit4