public void testLoadObjects() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { assertNull(e); assertEquals(TOTAL_THINGS, objects.size()); done.release(); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithGenericParseObjects() throws Exception { final ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<>(activity, Thing.class); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() { @Override public void onLoading() { } @Override public void onLoaded(List<ParseObject> objects, Exception e) { assertNull(e); assertEquals(TOTAL_THINGS, objects.size()); done.release(); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadNextPageBeforeLoadObjects() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { assertNull(e); assertEquals(TOTAL_THINGS, objects.size()); done.release(); } }); adapter.loadNextPage(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithNoPagination() throws Exception { final int additional = 16; for (int i = 0; i < additional; i++) { ParseObject thing = ParseObject.create(Thing.class); thing.put("name", "Additional Thing " + i); savedThings.add(thing); } TOTAL_THINGS += additional; final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); adapter.setPaginationEnabled(false); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { assertNull(e); assertEquals(TOTAL_THINGS, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); done.release(); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testClear() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); final Semaphore done = new Semaphore(0); final Capture<Integer> counter = new Capture<>(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { if (e != null) { return; } switch (counter.get()) { case 0: assertEquals(TOTAL_THINGS, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); adapter.clear(); assertEquals(0, adapter.getCount()); adapter.loadObjects(); break; default: assertEquals(TOTAL_THINGS, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); done.release(); } counter.set(counter.get() + 1); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithOnLoadingAndOnLoadedCallback() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); adapter.setObjectsPerPage(5); final Capture<Boolean> flag = new Capture<>(false); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { assertFalse(flag.get()); flag.set(true); assertEquals(0, adapter.getCount()); } @Override public void onLoaded(List<Thing> objects, Exception e) { assertTrue(flag.get()); assertEquals(5, objects.size()); done.release(); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testIncomingQueryResultAfterClearing() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); final int pageSize = 4; adapter.setObjectsPerPage(pageSize); final Semaphore done = new Semaphore(0); final Capture<Integer> timesThrough = new Capture<>(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() {} @Override public void onLoaded(List<Thing> objects, Exception e) { switch (timesThrough.get()) { case 0: adapter.loadNextPage(); adapter.clear(); case 1: done.release(); } timesThrough.set(timesThrough.get()+1); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithOverrideSetPageOnQuery() throws Exception { final int arbitraryLimit = 3; final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<Thing>(activity, Thing.class) { @Override public void setPageOnQuery(int page, ParseQuery<Thing> query) { // Make sure that this method is being used + respected. query.setLimit(arbitraryLimit); } }; final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { }; @Override public void onLoaded(List<Thing> objects, Exception e) { assertEquals(arbitraryLimit, objects.size()); done.release(); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithtAutoload() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); final Capture<Boolean> flag = new Capture<>(false); // Make sure that the Adapter doesn't start trying to load objects until AFTER we set this flag // to true (= triggered by calling setAutoload, NOT registerDataSetObserver, if autoload is // false). adapter.setAutoload(false); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { assertEquals(0, adapter.getCount()); assertTrue(flag.get()); } @Override public void onLoaded(List<Thing> objects, Exception e) { assertEquals(TOTAL_THINGS, adapter.getCount()); done.release(); } }); DataSetObserver observer = new DataSetObserver() { }; adapter.registerDataSetObserver(observer); flag.set(true); adapter.setAutoload(true); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithLimitsObjectsPerPage() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); final int pageSize = 4; adapter.setObjectsPerPage(pageSize); final Capture<Integer> timesThrough = new Capture<>(0); final Semaphore done = new Semaphore(0); final OnQueryLoadListener<Thing> listener = new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { if (e != null) { return; } switch (timesThrough.get()) { case 0: // first time through, should have one page of results + "Load more" assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); adapter.loadNextPage(); break; case 1: // second time through, should have two pages of results + "Load more" assertEquals(pageSize, objects.size()); assertEquals(2 * pageSize + 1, adapter.getCount()); adapter.loadNextPage(); break; case 2: // last time through, no "Load more" necessary. assertEquals(TOTAL_THINGS - 2 * pageSize, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); done.release(); } timesThrough.set(timesThrough.get() + 1); } }; adapter.addOnQueryLoadListener(listener); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithLimitsObjectsPerPageAndNoRemainder() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); final int pageSize = 5; adapter.setObjectsPerPage(pageSize); final Capture<Integer> timesThrough = new Capture<>(0); final Semaphore done = new Semaphore(0); final OnQueryLoadListener<Thing> listener = new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { if (e != null) { return; } switch (timesThrough.get()) { case 0: // first time through, should have one page of results + "Load more" cell assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); adapter.loadNextPage(); break; case 1: // second time through, should have two pages' worth of results. It should realize that an // additional "Load more" link isn't necessary, since this second page covers all of the // results. assertEquals(TOTAL_THINGS - pageSize, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); done.release(); } timesThrough.set(timesThrough.get() + 1); } }; adapter.addOnQueryLoadListener(listener); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithPaginationNextPageView() throws Exception { final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, Thing.class); final int pageSize = 5; adapter.setObjectsPerPage(pageSize); final Capture<Integer> timesThrough = new Capture<>(0); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { if (e != null) { return; } switch (timesThrough.get()) { case 0: assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); // Get Next Page view by passing in pageSize as the index View view = adapter.getView(pageSize, null, null); TextView textView = (TextView) view.findViewById(android.R.id.text1); assertEquals("Load more...", textView.getText()); // View should have OnClickListener attached. In API level 15+, we could call // view.hasOnClickListeners() instead. assertTrue(view.performClick()); break; case 1: // Triggered by the performClick() call done.release(); } timesThrough.set(timesThrough.get() + 1); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
public void testLoadObjectsWithCacheThenNetworkQueryAndPagination() throws Exception { QueryFactory<Thing> factory = new QueryFactory<Thing>() { @Override public ParseQuery<Thing> create() { ParseQuery<Thing> query = new ParseQuery<Thing>(Thing.class); query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK); return query; } }; final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, factory); final int pageSize = 5; adapter.setObjectsPerPage(pageSize); adapter.setPaginationEnabled(true); final Capture<Integer> timesThrough = new Capture<>(0); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { if (e != null) { return; } switch (timesThrough.get()) { case 0: // Network callback for first page assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); adapter.loadNextPage(); break; case 1: // Network callback for second page assertEquals(TOTAL_THINGS - pageSize, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); adapter.loadObjects(); break; case 2: // Cache callback for first page assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); break; case 3: // Network callback for first page assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); adapter.loadNextPage(); break; case 4: // Cache callback for second page assertEquals(TOTAL_THINGS - pageSize, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); break; case 5: // Network callback for second page assertEquals(TOTAL_THINGS - pageSize, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); done.release(); break; } timesThrough.set(timesThrough.get() + 1); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ParseAnalytics.trackAppOpened(getIntent()); setContentView(R.layout.main); mLastHour = new Date(); mLastHour.setHours(mLastHour.getHours() - 2); // Instantiate a QueryFactory to define the ParseQuery to be used for fetching items in this // Adapter. ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() { public ParseQuery<ParseObject> create() { ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Image"); // query.whereGreaterThan("createdAt", mLastHour); query.orderByDescending("createdAt"); return query; } }; mAdapter = new ParseQueryAdapter<ParseObject>(this, factory) { @Override public View getItemView(ParseObject object, View v, ViewGroup parent) { if (v == null) { v = View.inflate(FotoFlickActivity.this, R.layout.photo, null); } super.getItemView(object, v, parent); final ImageView imgView = (ImageView)v; ImageLoader.getInstance() .loadImage(object.getString("imageURLString") , mTargetSize , new SimpleImageLoadingListener() { @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { imgView.setImageBitmap(loadedImage); } }); imgView.setOnClickListener(mListener); return v; } }; // Perhaps set a callback to be fired upon successful loading of a new set of ParseObjects. mAdapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() { @Override public void onLoading() { mProgressBar.setVisibility(View.VISIBLE); } @Override public void onLoaded(List<ParseObject> objects, Exception e) { mProgressBar.setVisibility(View.GONE); android.util.Log.d("Foto", "onLoaded"); if (objects != null) { android.util.Log.d("Foto", "parsing objects: " + objects.size()); } } }); mGridView = (GridView) findViewById(R.id.photo_grid); mProgressBar = (ProgressBar) findViewById(R.id.progress); mGridView.setAdapter(mAdapter); }