Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce9a8792 authored by Ben Kwa's avatar Ben Kwa
Browse files

Clean up unit tests.

- Rework unit tests to eliminate some null checks from production code.
- Fix some flakiness in the tests resulting from a coupling between
  DirectoryFragment.Model and the application View.

Change-Id: I6b23774a5b4897f33d8641e677bda9e66d86cb96
parent 0f7078f0
Loading
Loading
Loading
Loading
+19 −21
Original line number Original line Diff line number Diff line
@@ -832,8 +832,18 @@ public class DirectoryFragment extends Fragment {
                                if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
                                if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
                                    mModel.undoDeletion();
                                    mModel.undoDeletion();
                                } else {
                                } else {
                                    // TODO: Use a listener rather than pushing the view.
                                    mModel.finalizeDeletion(
                                    mModel.finalizeDeletion(DirectoryFragment.this.getView());
                                            new Runnable() {
                                                @Override
                                                public void run() {
                                                    Snackbar.make(
                                                            DirectoryFragment.this.getView(),
                                                            R.string.toast_failed_delete,
                                                            Snackbar.LENGTH_LONG)
                                                            .show();

                                                }
                                            });
                                }
                                }
                            }
                            }
                        })
                        })
@@ -1814,13 +1824,13 @@ public class DirectoryFragment extends Fragment {
                info = null;
                info = null;
                error = null;
                error = null;
                mIsLoading = false;
                mIsLoading = false;
                if (mUpdateListener != null)  mUpdateListener.onModelUpdate(this);
                mUpdateListener.onModelUpdate(this);
                return;
                return;
            }
            }


            if (result.exception != null) {
            if (result.exception != null) {
                Log.e(TAG, "Error while loading directory contents", result.exception);
                Log.e(TAG, "Error while loading directory contents", result.exception);
                if (mUpdateListener != null)  mUpdateListener.onModelUpdateFailed(result.exception);
                mUpdateListener.onModelUpdateFailed(result.exception);
                return;
                return;
            }
            }


@@ -1834,7 +1844,7 @@ public class DirectoryFragment extends Fragment {
                mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
                mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
            }
            }


            if (mUpdateListener != null)  mUpdateListener.onModelUpdate(this);
            mUpdateListener.onModelUpdate(this);
        }
        }


        int getItemCount() {
        int getItemCount() {
@@ -1935,7 +1945,7 @@ public class DirectoryFragment extends Fragment {
                int position = selected.get(i);
                int position = selected.get(i);
                if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
                if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
                mMarkedForDeletion.append(position, true);
                mMarkedForDeletion.append(position, true);
                if (mUpdateListener != null)  mUpdateListener.notifyItemRemoved(position);
                mUpdateListener.notifyItemRemoved(position);
            }
            }
        }
        }


@@ -1950,7 +1960,7 @@ public class DirectoryFragment extends Fragment {
            for (int i = 0; i < size; ++i) {
            for (int i = 0; i < size; ++i) {
                final int position = mMarkedForDeletion.keyAt(i);
                final int position = mMarkedForDeletion.keyAt(i);
                mMarkedForDeletion.put(position, false);
                mMarkedForDeletion.put(position, false);
                if (mUpdateListener != null)  mUpdateListener.notifyItemInserted(position);
                mUpdateListener.notifyItemInserted(position);
            }
            }


            // Then, clear the deletion list.
            // Then, clear the deletion list.
@@ -1964,21 +1974,9 @@ public class DirectoryFragment extends Fragment {
         * @param view The view which will be used to interact with the user (e.g. surfacing
         * @param view The view which will be used to interact with the user (e.g. surfacing
         * snackbars) for errors, info, etc.
         * snackbars) for errors, info, etc.
         */
         */
        void finalizeDeletion(final View view) {
        void finalizeDeletion(Runnable errorCallback) {
            final ContentResolver resolver = mContext.getContentResolver();
            final ContentResolver resolver = mContext.getContentResolver();
            DeleteFilesTask task = new DeleteFilesTask(
            DeleteFilesTask task = new DeleteFilesTask(resolver, errorCallback);
                    resolver,
                    new Runnable() {
                        @Override
                        public void run() {
                            Snackbar.make(
                                    view,
                                    R.string.toast_failed_delete,
                                    Snackbar.LENGTH_LONG)
                                    .show();

                        }
                    });
            task.execute();
            task.execute();
        }
        }


+15 −2
Original line number Original line Diff line number Diff line
@@ -58,6 +58,7 @@ public class DirectoryFragmentModelTest extends AndroidTestCase {
        r.cursor = cursor;
        r.cursor = cursor;


        model = new Model(mContext, null);
        model = new Model(mContext, null);
        model.addUpdateListener(new DummyListener());
        model.update(r);
        model.update(r);
    }
    }


@@ -73,8 +74,12 @@ public class DirectoryFragmentModelTest extends AndroidTestCase {


        assertEquals(ITEM_COUNT - 2, model.getItemCount());
        assertEquals(ITEM_COUNT - 2, model.getItemCount());


        // Finalize the deletion
        // Finalize the deletion.  Provide a callback that just ignores errors.
        model.finalizeDeletion(null);
        model.finalizeDeletion(
              new Runnable() {
                  @Override
                  public void run() {}
              });
        assertEquals(ITEM_COUNT - 2, model.getItemCount());
        assertEquals(ITEM_COUNT - 2, model.getItemCount());
    }
    }


@@ -154,4 +159,12 @@ public class DirectoryFragmentModelTest extends AndroidTestCase {
        }
        }
        return model.getDocuments(sel);
        return model.getDocuments(sel);
    }
    }

    private static class DummyListener implements Model.UpdateListener {
        public void onModelUpdate(Model model) {}
        public void onModelUpdateFailed(Exception e) {}
        public void notifyItemRemoved(int position) {}
        public void notifyItemInserted(int position) {}
    }

}
}