TruckOptionsPanel extends OptionsPanel implements OptionItem.OnChangedListener { @SuppressWarnings({"PMD.CyclomaticComplexity"}) public void setRouteOptions(final RouteOptions routeOptions) { if (routeOptions == null) { throw new IllegalArgumentException(getContext().getString(R.string.msdkui_exception_route_options_null)); } mRouteOptions = routeOptions; for (final OptionItem item : mSubItem) { if (item.getItemId() == R.string.msdkui_height) { ((NumericOptionItem) item).setValue(mRouteOptions.getTruckHeight()); } else if (item.getItemId() == R.string.msdkui_length) { ((NumericOptionItem) item).setValue(mRouteOptions.getTruckLength()); } else if (item.getItemId() == R.string.msdkui_width) { ((NumericOptionItem) item).setValue(mRouteOptions.getTruckWidth()); } else if (item.getItemId() == R.string.msdkui_limited_weight) { ((NumericOptionItem) item).setValue(mRouteOptions.getTruckLimitedWeight()); } else if (item.getItemId() == R.string.msdkui_weight_per_axle) { ((NumericOptionItem) item).setValue(mRouteOptions.getTruckWeightPerAxle()); } else if (item.getItemId() == R.string.msdkui_number_of_trailers) { ((NumericOptionItem) item).setInputType(InputType.TYPE_CLASS_NUMBER) .setValue(mRouteOptions.getTruckTrailersCount()); } else if (item.getItemId() == R.string.msdkui_truck_type) { ((SingleChoiceOptionItem) item).selectLabel(getTruckTypeLabel(mRouteOptions.getTruckType())); } else if (item.getItemId() == R.string.msdkui_violate_truck_options) { setRestriction(item); } } } TruckOptionsPanel(final Context context);  TruckOptionsPanel(final Context context, final AttributeSet attrs);  TruckOptionsPanel(final Context context, final AttributeSet attrs, final int defStyleAttr); @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)  TruckOptionsPanel(final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes); RouteOptions getRouteOptions(); @SuppressWarnings({"PMD.CyclomaticComplexity"}) // This function is not complicated. void setRouteOptions(final RouteOptions routeOptions); @SuppressWarnings({"PMD.AvoidCatchingGenericException", "PMD.NcssCount", "PMD.CyclomaticComplexity", "PMD.NPathComplexity"}) @Override void onChanged(OptionItem item);  }
@Test public void testSetRouteOption() { RouteOptions options = new MockUtils.MockRouteOptionsBuilder().withTruckOptions() .build(); mTruckOptionsPanel.setRouteOptions(options); final List<OptionItem> items = mTruckOptionsPanel.getOptionItems(); int index = 0; for (OptionItem item : items) { if (index++ == 0 || index++ == 7) return; assertThat(item, instanceOf(NumericOptionItem.class)); final Number value = ((NumericOptionItem) item).getValue(); assertThat(value, equalTo(3)); } }
@Test public void testGettingRouteOptionsWhenMainItemIsUnselected() { final MockUtils.MockRouteOptionsBuilder builder = new MockUtils.MockRouteOptionsBuilder(); final RouteOptions mockRouteOptions = builder.withFullHazardousOptions() .build(); mTruckOptionsPanel.setRouteOptions(mockRouteOptions); doAnswer(new AnswerGetRouteOptions(RouteOptions.TruckRestrictionsMode.NO_VIOLATIONS)).when(mockRouteOptions) .setTruckRestrictionsMode(any(RouteOptions.TruckRestrictionsMode.class)); final List<OptionItem> items = mTruckOptionsPanel.getOptionItems(); BooleanOptionItem booleanOptionItem = (BooleanOptionItem) items.get(items.size() - 1); booleanOptionItem.setChecked(false); }
@Test public void testGettingRouteOptionsWhenMainItemIsSelected() { final MockUtils.MockRouteOptionsBuilder builder = new MockUtils.MockRouteOptionsBuilder(); final RouteOptions mockRouteOptions = builder.withFullHazardousOptions() .build(); mTruckOptionsPanel.setRouteOptions(mockRouteOptions); doAnswer(new AnswerGetRouteOptions(RouteOptions.TruckRestrictionsMode.PENALIZE_VIOLATIONS)).when(mockRouteOptions) .setTruckRestrictionsMode(any(RouteOptions.TruckRestrictionsMode.class)); final List<OptionItem> items = mTruckOptionsPanel.getOptionItems(); BooleanOptionItem booleanOptionItem = (BooleanOptionItem) items.get(items.size() - 1); booleanOptionItem.setChecked(true); }
@Test public void testChangeListener() { final MockUtils.MockRouteOptionsBuilder builder = new MockUtils.MockRouteOptionsBuilder(); final RouteOptions options = builder.withTruckOptions().build(); mTruckOptionsPanel.setRouteOptions(options); mTruckOptionsPanel.setListener(new OptionsPanel.Listener() { @Override public void onOptionCreated(List<OptionItem> item) { mCallbackCalled = false; } @Override public void onOptionChanged(OptionItem item) { mCallbackCalled = true; } }); when(options.getTruckHeight()).thenReturn(2.0f); mTruckOptionsPanel.setRouteOptions(options); assertThat(mCallbackCalled, is(true)); }