Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2017 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.contentpager.content;
     18 
     19 import static androidx.contentpager.content.ContentPager.createArgs;
     20 import static androidx.contentpager.content.TestContentProvider.UNPAGED_URI;
     21 
     22 import android.app.Activity;
     23 import android.database.Cursor;
     24 import android.support.test.filters.MediumTest;
     25 import android.support.test.rule.ActivityTestRule;
     26 import android.support.test.runner.AndroidJUnit4;
     27 
     28 import androidx.contentpager.content.ContentPager.ContentCallback;
     29 import androidx.contentpager.content.ContentPager.QueryRunner;
     30 
     31 import org.junit.Before;
     32 import org.junit.Rule;
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 @MediumTest
     37 @RunWith(AndroidJUnit4.class)
     38 public class LoaderQueryRunnerTest {
     39 
     40     @Rule
     41     public ActivityTestRule<Activity> mActivityRule = new ActivityTestRule(TestActivity.class);
     42 
     43     private Activity mActivity;
     44     private QueryRunner mRunner;
     45     private TestQueryCallback mCallback;
     46 
     47     @Before
     48     public void setUp() {
     49         mActivity = mActivityRule.getActivity();
     50         mRunner = new LoaderQueryRunner(mActivity, mActivity.getLoaderManager());
     51         mCallback = new TestQueryCallback();
     52     }
     53 
     54     @Test
     55     public void testRunsQuery() throws Throwable {
     56         int offset = 0;
     57         int limit = 10;
     58 
     59         ContentCallback dummyContentCallback = new ContentCallback() {
     60             @Override
     61             public void onCursorReady(Query query, Cursor cursor) {
     62                 // Nothing to see here. Move along.
     63             }
     64         };
     65         final Query query = new Query(
     66                 UNPAGED_URI,
     67                 null,
     68                 createArgs(offset, limit),
     69                 null,
     70                 dummyContentCallback);
     71 
     72         mCallback.reset(1);
     73         mActivityRule.runOnUiThread(new Runnable() {
     74             @Override
     75             public void run() {
     76                 // This calls through to LoaderManager.restartLoader
     77                 // which must always be called on the main thread
     78                 mRunner.query(query, mCallback);
     79             }
     80         });
     81 
     82         mCallback.waitFor(10);
     83         mCallback.assertQueried(query.getId());
     84         mCallback.assertReceivedContent(UNPAGED_URI, query.getId());
     85     }
     86 }
     87