### Question:
AlertClickListener implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { LowStockAlert alert = adapter.getItem(position); if (!alert.isDisabled()) { Intent intent = new Intent(context, OrderActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Bundle data = new Bundle(); data.putString(ORDER_TYPE, OrderType.EMERGENCY); intent.putExtras(data); context.startActivity(intent); } } AlertClickListener(AlertsAdapter adapter, Context context); @Override void onItemClick(AdapterView<?> parent, View view, int position, long id); static final String ORDER_TYPE; }### Answer:
@Test public void shouldOnlyStartIntentForEnabledLowStockAlerts() throws Exception { AlertClickListener listener = new AlertClickListener(setupAdapter(false), activity); listener.onItemClick(null, mock(View.class), 1, 1); Intent startedIntent = shadowOf(activity).getNextStartedActivity(); assertThat(startedIntent, is(notNullValue())); assertThat(startedIntent.getComponent().getClassName(), equalTo(OrderActivity.class.getName())); }
@Test public void shouldNotStartIntentForDisabledLowStockAlerts() throws Exception { AlertClickListener listener = new AlertClickListener(setupAdapter(true), activity); listener.onItemClick(null, mock(View.class), 1, 1); Intent startedIntent = shadowOf(activity).getNextStartedActivity(); assertThat(startedIntent, is(nullValue())); }
@Test public void shouldSetOrderTypeToEmergency() throws Exception { AlertClickListener listener = new AlertClickListener(setupAdapter(false), activity); listener.onItemClick(null, mock(View.class), 1, 1); Intent startedIntent = shadowOf(activity).getNextStartedActivity(); assertThat(startedIntent, is(notNullValue())); assertThat(startedIntent.getStringExtra(AlertClickListener.ORDER_TYPE), is(OrderType.EMERGENCY)); }