/*
 * Copyright (C) 2016 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.v7.widget;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.Collections;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class GapWorkerTest {

    private Context getContext() {
        return InstrumentationRegistry.getContext();
    }

    @Test
    public void taskOrderViewPresence() {
        ArrayList<GapWorker.Task> list = new ArrayList<>();
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());

        list.get(0).view = null;
        list.get(1).view = new RecyclerView(getContext());
        list.get(2).view = null;

        Collections.sort(list, GapWorker.sTaskComparator);

        assertNotNull(list.get(0).view);
        assertNull(list.get(1).view);
        assertNull(list.get(2).view);
    }
    @Test
    public void taskOrderImmediate() {
        ArrayList<GapWorker.Task> list = new ArrayList<>();
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());

        list.get(0).immediate = true;
        list.get(1).immediate = false;
        list.get(2).immediate = true;

        Collections.sort(list, GapWorker.sTaskComparator);

        assertTrue(list.get(0).immediate);
        assertTrue(list.get(1).immediate);
        assertFalse(list.get(2).immediate);
    }

    @Test
    public void taskOrderImmediateVelocity() {
        ArrayList<GapWorker.Task> list = new ArrayList<>();
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());

        list.get(0).immediate = true;
        list.get(0).viewVelocity = 10;

        list.get(1).immediate = false;
        list.get(1).viewVelocity = 99;

        list.get(2).immediate = true;
        list.get(2).viewVelocity = 20;

        Collections.sort(list, GapWorker.sTaskComparator);

        assertEquals(20, list.get(0).viewVelocity);
        assertEquals(10, list.get(1).viewVelocity);
        assertEquals(99, list.get(2).viewVelocity);
    }

    @Test
    public void taskOrderImmediateVelocityDistance() {
        ArrayList<GapWorker.Task> list = new ArrayList<>();
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());
        list.add(new GapWorker.Task());

        list.get(0).immediate = true;
        list.get(0).viewVelocity = 400;
        list.get(0).distanceToItem = 300;

        list.get(1).immediate = false;
        list.get(1).viewVelocity = 800;
        list.get(1).distanceToItem = 900;

        list.get(2).immediate = true;
        list.get(2).viewVelocity = 300;
        list.get(2).distanceToItem = 200;

        list.get(3).immediate = true;
        list.get(3).viewVelocity = 300;
        list.get(3).distanceToItem = 100;

        Collections.sort(list, GapWorker.sTaskComparator);

        assertEquals(300, list.get(0).distanceToItem);
        assertEquals(100, list.get(1).distanceToItem);
        assertEquals(200, list.get(2).distanceToItem);
        assertEquals(900, list.get(3).distanceToItem);
    }
}

@@ -903,16 +903,41 @@
 ontext;%0A
+import android.os.Build;%0A
 import a
@@ -981,16 +981,65 @@
 gistry;%0A
+import android.support.test.filters.SdkSuppress;%0A
 import a
@@ -4247,11 +4247,464 @@
 );%0A    %7D
+%0A%0A    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)%0A    @Test%0A    public void gapWorkerWithoutLayout() %7B%0A        RecyclerView recyclerView = new RecyclerView(getContext());%0A        try %7B%0A            assertFalse(recyclerView.mIsAttached);%0A            recyclerView.onAttachedToWindow();%0A            recyclerView.mGapWorker.prefetch(RecyclerView.FOREVER_NS);%0A        %7D finally %7B%0A            recyclerView.onDetachedFromWindow();%0A        %7D%0A    %7D
 %0A%7D%0A

Test for null LayoutManager in prefetch