Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright 2018 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 androidx.recyclerview.widget;
     18 
     19 import static org.hamcrest.CoreMatchers.is;
     20 import static org.hamcrest.CoreMatchers.nullValue;
     21 import static org.hamcrest.CoreMatchers.sameInstance;
     22 import static org.hamcrest.MatcherAssert.assertThat;
     23 
     24 import android.support.test.filters.SmallTest;
     25 import android.support.test.runner.AndroidJUnit4;
     26 
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 
     31 @RunWith(AndroidJUnit4.class)
     32 @SmallTest
     33 public class MessageQueueTest {
     34     MessageThreadUtil.MessageQueue mQueue;
     35 
     36     @Before
     37     public void setUp() throws Exception {
     38         mQueue = new MessageThreadUtil.MessageQueue();
     39     }
     40 
     41     @Test
     42     public void allArguments() {
     43         String data = "data";
     44         mQueue.sendMessage(MessageThreadUtil.SyncQueueItem.obtainMessage(
     45                 0, 1, 2, 3, 4, 5, data));
     46 
     47         MessageThreadUtil.SyncQueueItem msg = mQueue.next();
     48         assertThat(msg.what, is(0));
     49         assertThat(msg.arg1, is(1));
     50         assertThat(msg.arg2, is(2));
     51         assertThat(msg.arg3, is(3));
     52         assertThat(msg.arg4, is(4));
     53         assertThat(msg.arg5, is(5));
     54         assertThat((String)msg.data, sameInstance(data));
     55     }
     56 
     57     @Test
     58     public void sendInOrder() {
     59         mQueue.sendMessage(obtainMessage(1, 2));
     60         mQueue.sendMessage(obtainMessage(3, 4));
     61         mQueue.sendMessage(obtainMessage(5, 6));
     62 
     63         MessageThreadUtil.SyncQueueItem msg = mQueue.next();
     64         assertThat(msg.what, is(1));
     65         assertThat(msg.arg1, is(2));
     66 
     67         msg = mQueue.next();
     68         assertThat(msg.what, is(3));
     69         assertThat(msg.arg1, is(4));
     70 
     71         msg = mQueue.next();
     72         assertThat(msg.what, is(5));
     73         assertThat(msg.arg1, is(6));
     74 
     75         msg = mQueue.next();
     76         assertThat(msg, nullValue());
     77     }
     78 
     79     @Test
     80     public void sendAtFront() {
     81         mQueue.sendMessage(obtainMessage(1, 2));
     82         mQueue.sendMessageAtFrontOfQueue(obtainMessage(3, 4));
     83         mQueue.sendMessage(obtainMessage(5, 6));
     84 
     85         MessageThreadUtil.SyncQueueItem msg = mQueue.next();
     86         assertThat(msg.what, is(3));
     87         assertThat(msg.arg1, is(4));
     88 
     89         msg = mQueue.next();
     90         assertThat(msg.what, is(1));
     91         assertThat(msg.arg1, is(2));
     92 
     93         msg = mQueue.next();
     94         assertThat(msg.what, is(5));
     95         assertThat(msg.arg1, is(6));
     96 
     97         msg = mQueue.next();
     98         assertThat(msg, nullValue());
     99     }
    100 
    101     @Test
    102     public void remove() {
    103         mQueue.sendMessage(obtainMessage(1, 0));
    104         mQueue.sendMessage(obtainMessage(2, 0));
    105         mQueue.sendMessage(obtainMessage(1, 0));
    106         mQueue.sendMessage(obtainMessage(2, 1));
    107         mQueue.sendMessage(obtainMessage(3, 0));
    108         mQueue.sendMessage(obtainMessage(1, 0));
    109 
    110         mQueue.removeMessages(1);
    111 
    112         MessageThreadUtil.SyncQueueItem msg = mQueue.next();
    113         assertThat(msg.what, is(2));
    114         assertThat(msg.arg1, is(0));
    115 
    116         msg = mQueue.next();
    117         assertThat(msg.what, is(2));
    118         assertThat(msg.arg1, is(1));
    119 
    120         msg = mQueue.next();
    121         assertThat(msg.what, is(3));
    122         assertThat(msg.arg1, is(0));
    123 
    124         msg = mQueue.next();
    125         assertThat(msg, nullValue());
    126     }
    127 
    128     private MessageThreadUtil.SyncQueueItem obtainMessage(int what, int arg) {
    129         return MessageThreadUtil.SyncQueueItem.obtainMessage(what, arg, null);
    130     }
    131 }
    132