Skip to content
Permalink
Browse files

Merge pull request #298 from timeu/master

Bugfix: Make sure to use the keyProvider when adding an item to the list
  • Loading branch information...
olafleur committed Jul 27, 2016
2 parents 28b7157 + dbdf3b2 commit e8b64dbb0ff2fed4337435598a99939822568fb7
@@ -30,6 +30,7 @@
import com.arcbees.chosen.integrationtest.client.testcases.IsAcceptedValueListBox;
import com.arcbees.chosen.integrationtest.client.testcases.MaxSelectedOptions;
import com.arcbees.chosen.integrationtest.client.testcases.MultiValueListBoxSelectedOptionsOnInit;
import com.arcbees.chosen.integrationtest.client.testcases.MultiValueListBoxWithCustomKeyProvider;
import com.arcbees.chosen.integrationtest.client.testcases.SearchContains;
import com.arcbees.chosen.integrationtest.client.testcases.ShowNonEmptyValues;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleMultiValueListBox;
@@ -80,6 +81,7 @@ public ChosenSampleIntegrationTests() {
registerTestCase(new ChosenListBoxSingleSelectAddItems());
registerTestCase(new MultiValueListBoxSelectedOptionsOnInit());
registerTestCase(new ChosenListBoxMultipleSelect());
registerTestCase(new MultiValueListBoxWithCustomKeyProvider());
}

@Override
@@ -0,0 +1,35 @@
/**
* Copyright 2015 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.arcbees.chosen.integrationtest.client.domain;

public class Car {
private final Integer id;
private final CarBrand name;

public Car(Integer id, CarBrand name) {
this.id = id;
this.name = name;
}

public Integer getId() {
return id;
}

public CarBrand getName() {
return name;
}
}
@@ -0,0 +1,84 @@
/**
* Copyright 2015 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.arcbees.chosen.integrationtest.client.testcases;

import java.util.List;

import javax.annotation.Nullable;

import com.arcbees.chosen.client.ChosenOptions;
import com.arcbees.chosen.client.gwt.MultipleChosenValueListBox;
import com.arcbees.chosen.integrationtest.client.TestCase;
import com.arcbees.chosen.integrationtest.client.domain.Car;
import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.google.common.collect.Lists;
import com.google.gwt.dom.client.Style;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.ProvidesKey;

public class MultiValueListBoxWithCustomKeyProvider extends TestCase {

public static class CarKeyProvider implements ProvidesKey<Car> {
@Override
public Object getKey(Car item) {
return item.getId();
}
}

private final ChosenOptions options;
private MultipleChosenValueListBox<Car> listBox;

public MultiValueListBoxWithCustomKeyProvider() {
this(new ChosenOptions());
}

protected MultiValueListBoxWithCustomKeyProvider(ChosenOptions options) {
this.options = options;
}

@Override
public void run() {
listBox = new MultipleChosenValueListBox<Car>(new AbstractRenderer<Car>() {
@Override
public String render(Car object) {
return object.getName().name();
}
}, new CarKeyProvider(),options);

listBox.getElement().getStyle().setWidth(500, Style.Unit.PX);
List<Car> cars = getAvailableCars();
listBox.setAcceptableValues(cars);
List<Car> selectedCars = Lists.newArrayList(cars.get(5),cars.get(6));
RootPanel.get().add(listBox);
listBox.setValue(selectedCars);
}

@Nullable
protected MultipleChosenValueListBox<Car> getListBox() {
return listBox;
}

protected List<Car> getAvailableCars() {
List<Car> cars = Lists.newArrayList();
CarBrand[] carBrands = CarBrand.values();
for (int i = 0; i < carBrands.length; i++) {
cars.add(new Car(i, CarBrand.values()[i]));
}
return cars;
}
}
@@ -26,6 +26,7 @@
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems;
import com.arcbees.chosen.integrationtest.client.testcases.MaxSelectedOptions;
import com.arcbees.chosen.integrationtest.client.testcases.MultiValueListBoxSelectedOptionsOnInit;
import com.arcbees.chosen.integrationtest.client.testcases.MultiValueListBoxWithCustomKeyProvider;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleMultiValueListBox;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleValueListBox;
import com.google.common.base.Predicate;
@@ -129,6 +130,15 @@ public void selectedValuesOnInit_setsItemsSelectedText() {
assertThat(getSelectedOptionText()).isEqualTo("2 items selected");
}

@Test
public void multiValueListBoxWithCustomProvider_setsItemsSelectedText() {
// Given
loadTestCase(new MultiValueListBoxWithCustomKeyProvider());

// Then
assertThat(getSelectedOptionText()).isEqualTo("2 items selected");
}

/**
* On mobile, we should be able to close the dropdown by clicking on the icon net to the input.
*/
@@ -157,7 +157,7 @@ public void unselectAll() {

@Override
protected void addItemToChosenListBox(T value) {
int index = valueKeyToIndex.get(value);
int index = valueKeyToIndex.get(keyProvider.getKey(value));

getChosenListBox().addItem(renderer.render(value), "" + index);
}

0 comments on commit e8b64db

Please sign in to comment.
You can’t perform that action at this time.