@Test public void onOptionsItemSelected_forSync_whenDeviceIsOffline_showsErrorToastAndDoesNotSync() { when(networkStateProvider.isDeviceOnline()).thenReturn(false); BlankFormListMenuDelegate menuDelegate = new BlankFormListMenuDelegate(activity, viewModel, networkStateProvider); menuDelegate.onOptionsItemSelected(new RoboMenuItem(R.id.menu_refresh)); assertThat(ShadowToast.getTextOfLatestToast(), is(activity.getString(R.string.no_connection))); verify(viewModel, never()).syncWithServer(); }
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_refresh) { if (networkStateProvider.isDeviceOnline()) { blankFormsListViewModel.syncWithServer().observe(activity, success -> { if (success) { Toast.makeText( activity, activity.getString(R.string.form_update_succeeded), Toast.LENGTH_SHORT ).show(); } }); } else { Toast.makeText(activity, R.string.no_connection, Toast.LENGTH_SHORT).show(); } return true; } else { return false; } }
BlankFormListMenuDelegate implements MenuDelegate { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_refresh) { if (networkStateProvider.isDeviceOnline()) { blankFormsListViewModel.syncWithServer().observe(activity, success -> { if (success) { Toast.makeText( activity, activity.getString(R.string.form_update_succeeded), Toast.LENGTH_SHORT ).show(); } }); } else { Toast.makeText(activity, R.string.no_connection, Toast.LENGTH_SHORT).show(); } return true; } else { return false; } } }
BlankFormListMenuDelegate implements MenuDelegate { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_refresh) { if (networkStateProvider.isDeviceOnline()) { blankFormsListViewModel.syncWithServer().observe(activity, success -> { if (success) { Toast.makeText( activity, activity.getString(R.string.form_update_succeeded), Toast.LENGTH_SHORT ).show(); } }); } else { Toast.makeText(activity, R.string.no_connection, Toast.LENGTH_SHORT).show(); } return true; } else { return false; } } BlankFormListMenuDelegate(FragmentActivity activity, BlankFormsListViewModel blankFormsListViewModel, NetworkStateProvider networkStateProvider); }
BlankFormListMenuDelegate implements MenuDelegate { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_refresh) { if (networkStateProvider.isDeviceOnline()) { blankFormsListViewModel.syncWithServer().observe(activity, success -> { if (success) { Toast.makeText( activity, activity.getString(R.string.form_update_succeeded), Toast.LENGTH_SHORT ).show(); } }); } else { Toast.makeText(activity, R.string.no_connection, Toast.LENGTH_SHORT).show(); } return true; } else { return false; } } BlankFormListMenuDelegate(FragmentActivity activity, BlankFormsListViewModel blankFormsListViewModel, NetworkStateProvider networkStateProvider); @Override void onCreateOptionsMenu(MenuInflater menuInflater, Menu menu); @Override void onPrepareOptionsMenu(Menu menu); @Override boolean onOptionsItemSelected(MenuItem item); }
BlankFormListMenuDelegate implements MenuDelegate { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_refresh) { if (networkStateProvider.isDeviceOnline()) { blankFormsListViewModel.syncWithServer().observe(activity, success -> { if (success) { Toast.makeText( activity, activity.getString(R.string.form_update_succeeded), Toast.LENGTH_SHORT ).show(); } }); } else { Toast.makeText(activity, R.string.no_connection, Toast.LENGTH_SHORT).show(); } return true; } else { return false; } } BlankFormListMenuDelegate(FragmentActivity activity, BlankFormsListViewModel blankFormsListViewModel, NetworkStateProvider networkStateProvider); @Override void onCreateOptionsMenu(MenuInflater menuInflater, Menu menu); @Override void onPrepareOptionsMenu(Menu menu); @Override boolean onOptionsItemSelected(MenuItem item);  }