Skip to content

Commit

Permalink
Merge pull request #290 from ArcBees/mmc_test_placeholder
Browse files Browse the repository at this point in the history
Add tests for Multiple select placeholder and selected text on init
  • Loading branch information
meriouma committed Jan 4, 2016
2 parents 6214fa6 + 7aa90da commit e2f5515
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import com.arcbees.chosen.integrationtest.client.testcases.AllowSingleDeselect;
import com.arcbees.chosen.integrationtest.client.testcases.ChooseOption;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelect;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxSingleSelectAddItems;
import com.arcbees.chosen.integrationtest.client.testcases.DisableSearchThreshold;
import com.arcbees.chosen.integrationtest.client.testcases.EnabledDisabled;
import com.arcbees.chosen.integrationtest.client.testcases.HideEmptyValues;
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.SearchContains;
import com.arcbees.chosen.integrationtest.client.testcases.ShowNonEmptyValues;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleMultiValueListBox;
Expand Down Expand Up @@ -76,6 +78,8 @@ public ChosenSampleIntegrationTests() {
registerTestCase(new SimpleMultiValueListBoxOnChange());
registerTestCase(new ChosenListBoxMultipleSelectAddItems());
registerTestCase(new ChosenListBoxSingleSelectAddItems());
registerTestCase(new MultiValueListBoxSelectedOptionsOnInit());
registerTestCase(new ChosenListBoxMultipleSelect());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 com.arcbees.chosen.client.gwt.ChosenListBox;
import com.arcbees.chosen.integrationtest.client.TestCase;
import com.google.gwt.user.client.ui.RootPanel;

public class ChosenListBoxMultipleSelect extends TestCase {
private ChosenListBox listBox;

@Override
public void run() {
listBox = new ChosenListBox(true);
listBox.setWidth("200px");
listBox.addItem("One");
listBox.addItem("Two");
listBox.addItem("Three");

RootPanel.get().add(listBox);
}

public ChosenListBox getListBox() {
return listBox;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,26 @@
package com.arcbees.chosen.integrationtest.client.testcases;

import com.arcbees.chosen.client.gwt.ChosenListBox;
import com.arcbees.chosen.integrationtest.client.TestCase;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class ChosenListBoxMultipleSelectAddItems extends TestCase {
public class ChosenListBoxMultipleSelectAddItems extends ChosenListBoxMultipleSelect {
public static final String BUTTON_ID = "ChosenListBoxMultipleSelectAddItems-button";
public static final String SELECTED_VALUE = "Six";

@Override
public void run() {
final ChosenListBox listBox = new ChosenListBox(true);
listBox.setWidth("200px");
listBox.addItem("One");
listBox.addItem("Two");
listBox.addItem("Three");
super.run();

Button button = new Button("Add items and select Six");
button.getElement().setId(BUTTON_ID);
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
ChosenListBox listBox = getListBox();

listBox.clear();
listBox.addItem("Four");
listBox.addItem("Five");
Expand All @@ -49,7 +46,6 @@ public void onClick(ClickEvent event) {
}
});

RootPanel.get().add(listBox);
RootPanel.get().add(button);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 com.arcbees.chosen.client.gwt.MultipleChosenValueListBox;
import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.google.common.collect.Lists;

public class MultiValueListBoxSelectedOptionsOnInit extends SimpleMultiValueListBox {
@Override
public void run() {
super.run();

MultipleChosenValueListBox<CarBrand> listBox = getListBox();
listBox.setValue(Lists.newArrayList(CarBrand.BENTLEY, CarBrand.CADILLAC));
}
}
36 changes: 35 additions & 1 deletion integration-test/src/test/java/ChosenIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.arcbees.chosen.client.ChosenImpl;
import com.arcbees.chosen.integrationtest.client.TestCase;
import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.arcbees.chosen.integrationtest.client.testcases.AllowSingleDeselect;
import com.arcbees.chosen.integrationtest.client.testcases.ChooseOption;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelect;
import com.arcbees.chosen.integrationtest.client.testcases.DisableSearchThreshold;
import com.arcbees.chosen.integrationtest.client.testcases.EnabledDisabled;
import com.arcbees.chosen.integrationtest.client.testcases.HideEmptyValues;
Expand All @@ -55,6 +58,7 @@
import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElement;

import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.AUDI;
import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.CADILLAC;
import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.FORD;
import static com.arcbees.chosen.integrationtest.client.domain.DefaultCarRenderer.RENDERER;

Expand Down Expand Up @@ -87,6 +91,34 @@ public void chooseOption() throws Throwable {
assertThat(getSelectedOptionText()).isEqualTo(fordRender);
}

/**
* Goal: ensure allowSingleDeselect is working properly.
*/
@Test
public void allowSingleDeselect() {
// Given
loadTestCase(new AllowSingleDeselect());
clickOption(CADILLAC, RENDERER);

// When
singleDeselect();

// Then
assertThat(getSelectedOptionText()).isEqualTo(AllowSingleDeselect.PLACEHOLDER);
}

/**
* Ensure default text is displayed on Multiple select.
*/
@Test
public void multipleListBox_displayPlaceHolder() {
// Given
loadTestCase(new ChosenListBoxMultipleSelect());

// Then
assertThat(getMultiplePlaceHolderText()).isEqualTo(ChosenImpl.MULTIPLE_DEFAULT_TEXT);
}

/**
* Tests the disableSearchThreshold options.
*/
Expand Down Expand Up @@ -311,12 +343,14 @@ protected WebElement getDropdown() {
By.className("com-arcbees-chosen-client-resources-ChosenCss-chzn-drop")));
}

protected WebElement getPlaceholder() {
protected WebElement getSinglePlaceholder() {
String xpath = "//div[@id='chosen_container__0_chzn']//a";

return webDriverWait().until(presenceOfElementLocated(By.xpath(xpath)));
}

protected abstract String getMultiplePlaceHolderText();

protected int getDropdownTop() {
WebElement dropdown = getDropdown();
String topString = dropdown.getCssValue("top");
Expand Down
30 changes: 9 additions & 21 deletions integration-test/src/test/java/DesktopChosenIT.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand All @@ -23,7 +23,6 @@
import org.openqa.selenium.WebElement;

import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.arcbees.chosen.integrationtest.client.testcases.AllowSingleDeselect;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxSingleSelectAddItems;
import com.arcbees.chosen.integrationtest.client.testcases.MaxSelectedOptions;
Expand All @@ -47,30 +46,12 @@

import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.AUDI;
import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.BMW;
import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.CADILLAC;
import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.HONDA;
import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.MERCEDES;
import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.TOYOTA;
import static com.arcbees.chosen.integrationtest.client.domain.DefaultCarRenderer.RENDERER;

public class DesktopChosenIT extends ChosenIT {

/**
* Goal: ensure allowSingleDeselect is working properly.
*/
@Test
public void allowSingleDeselect() {
// Given
loadTestCase(new AllowSingleDeselect());
clickOption(CADILLAC, RENDERER);

// When
singleDeselect();

// Then
assertThat(getSelectedOptionText()).isEqualTo(AllowSingleDeselect.PLACEHOLDER);
}

/**
* Tests that when
* - we deselect an option to a multiple chosen list box.
Expand Down Expand Up @@ -297,7 +278,7 @@ public void chosenListBox_updateAndSelect_removeDefaultStyle() {

// Then
assertThat(getSelectedOptionText()).isEqualTo(ChosenListBoxSingleSelectAddItems.SELECTED_VALUE);
assertThat(getPlaceholder().getAttribute("class")).doesNotContain("chzn-default");
assertThat(getSinglePlaceholder().getAttribute("class")).doesNotContain("chzn-default");
}

/**
Expand Down Expand Up @@ -349,6 +330,13 @@ public void maxSelectedOptions_optionsNotSelectableAnymore() {
assertDropdownIsClosed();
}

@Override
protected String getMultiplePlaceHolderText() {
String xpath = "//div[@id='chosen_container__0_chzn']//input";

return webDriverWait().until(presenceOfElementLocated(By.xpath(xpath))).getAttribute("value");
}

protected void openDropDown() {
WebElement btn;

Expand Down
20 changes: 19 additions & 1 deletion integration-test/src/test/java/MobileChosenIT.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand All @@ -25,12 +25,14 @@
import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
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.SimpleMultiValueListBox;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleValueListBox;
import com.google.common.base.Predicate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.HONDA;
import static com.arcbees.chosen.integrationtest.client.domain.CarBrand.MERCEDES;
Expand Down Expand Up @@ -118,6 +120,15 @@ public void maxSelectedOptions_optionsNotSelectableAnymore() {
assertDropdownIsOpenWithMobileLayout();
}

@Test
public void selectedValuesOnInit_setsItemsSelectedText() {
// Given
loadTestCase(new MultiValueListBoxSelectedOptionsOnInit());

// 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.
*/
Expand Down Expand Up @@ -166,6 +177,13 @@ public boolean apply(WebDriver input) {
assertThat(getDropdown().getAttribute("class")).doesNotContain(IS_OPEN);
}

@Override
protected String getMultiplePlaceHolderText() {
String xpath = "//div[@id='chosen_container__0_chzn']//span";

return webDriverWait().until(presenceOfElementLocated(By.xpath(xpath))).getText();
}

protected void openDropDown() {
String xpath = "//div[@id='chosen_container__0_chzn']";
WebElement btn = webDriverWait().until(elementToBeClickable(By.xpath(xpath)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
import static com.google.gwt.safehtml.shared.SafeHtmlUtils.fromTrustedString;

public abstract class ChosenImpl {
public static final String MULTIPLE_DEFAULT_TEXT = "Select Some Options";
public static final String SINGLE_DEFAULT_TEXT = "Select an Option";

static final int BACKSPACE = 8;
static final String TABINDEX_PROPERTY = "tabindex";

Expand Down Expand Up @@ -1246,15 +1249,15 @@ private void setDefaultText() {
} else if (options.getPlaceholderText() != null) {
defaultText = options.getPlaceholderText();
} else {
defaultText = "Select Some Options";
defaultText = MULTIPLE_DEFAULT_TEXT;
}
} else {
if (options.getPlaceholderTextSingle() != null) {
defaultText = options.getPlaceholderTextSingle();
} else if (options.getPlaceholderText() != null) {
defaultText = options.getPlaceholderText();
} else {
defaultText = "Select an Option";
defaultText = SINGLE_DEFAULT_TEXT;
}
}

Expand Down

0 comments on commit e2f5515

Please sign in to comment.