Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Feb 11, 2014
1 parent 77b9898 commit c4b2596
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
Expand Up @@ -21,7 +21,9 @@
package org.zanata.page.projects;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -125,18 +127,11 @@ public CreateVersionPage clickEditVersion() {
}

public String getStatisticsForLocale(String locale) {
List<WebElement> localeTableRows = getLocaleTableRows();
for (int rowIndex = 0; rowIndex < localeTableRows.size(); rowIndex++) {
WebElement tableRow = localeTableRows.get(rowIndex);
WebElement firstLink = tableRow.findElement(By.tagName("a"));
if (getLocaleLinkText(firstLink).equals(locale)) {
WebElement stats =
getDriver().findElement(
By.id(String.format(STATS_TEMPLATE, rowIndex)));
return stats.getText();
}
}
throw new IllegalArgumentException(
"can not find statistics for locale:" + locale);
int rowIndex = getTranslatableLocales().indexOf(locale);
Preconditions.checkState(rowIndex >= 0, "can not find statistics for locale: %s", locale);
WebElement stats =
getDriver().findElement(
By.id(String.format(STATS_TEMPLATE, rowIndex)));
return stats.getText();
}
}
Expand Up @@ -31,7 +31,6 @@
import org.zanata.util.WebElementUtil;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -44,15 +43,15 @@ public class EditorPage extends BasePage {
// first %d is row index, second %d is plural form index (i.e. 0 or 1)
private static final String SOURCE_ID_FMT =
"gwt-debug-%d-source-panel-%d-container";
private static final int SINGULAR = 0;
private static final int PLURAL = 1;

// first %d is row index, second %d is plural form index (i.e. 0-6)
private static final String TARGET_ID_FMT = "gwt-debug-%d-target-%d";

// buttons id format
private static final String APPROVE_FMT = "gwt-debug-target-%d-save-approve";
private static final String FUZZY_FMT = "gwt-debug-target-%d-save-fuzzy";
private static final String APPROVE_BUTTON_ID_FMT =
"gwt-debug-target-%d-save-approve";
private static final String FUZZY_BUTTON_ID_FMT =
"gwt-debug-target-%d-save-fuzzy";

private final By glossaryTableBy = By.id("gwt-debug-glossaryResultTable");
private final By glossaryNoResultBy = By.id("gwt-debug-glossaryNoResult");
Expand Down Expand Up @@ -115,14 +114,11 @@ public List<List<String>> apply(WebDriver input) {
* @return content of the source
*/
public String getMessageSourceAtRowIndex(final int rowIndex) {
return getCodeMirrorContent(rowIndex, SOURCE_ID_FMT, SINGULAR);
return getCodeMirrorContent(rowIndex, SOURCE_ID_FMT, Plurals.SourceSingular);
}

public String getMessageSourceAtRowIndex(int rowIndex, int pluralIndex) {
Preconditions.checkArgument(
pluralIndex == SINGULAR || pluralIndex == PLURAL,
"plural index must be 0 or 1");
return getCodeMirrorContent(rowIndex, SOURCE_ID_FMT, pluralIndex);
public String getMessageSourceAtRowIndex(int rowIndex, Plurals plural) {
return getCodeMirrorContent(rowIndex, SOURCE_ID_FMT, plural);
}

/**
Expand All @@ -135,17 +131,17 @@ public String getMessageSourceAtRowIndex(int rowIndex, int pluralIndex) {
* @return content of the target
*/
public String getMessageTargetAtRowIndex(final int rowIndex) {
return getCodeMirrorContent(rowIndex, TARGET_ID_FMT, SINGULAR);
return getCodeMirrorContent(rowIndex, TARGET_ID_FMT, Plurals.TargetSingular);
}

private String getCodeMirrorContent(final long rowIndex,
final String idFormat, final int pluralIndex) {
final String idFormat, final Plurals plurals) {
return waitForTenSec().until(new Function<WebDriver, String>() {
@Override
public String apply(WebDriver input) {
// code mirror will turn text into list of <pre>.
List<WebElement> cmTextLines = input.findElement(
By.id(String.format(idFormat, rowIndex, pluralIndex)))
By.id(String.format(idFormat, rowIndex, plurals.index())))
.findElements(By.tagName("pre"));
List<String> contents =
WebElementUtil.elementsToText(cmTextLines);
Expand Down Expand Up @@ -176,7 +172,7 @@ public Boolean apply(WebDriver input) {
}

public String getBasicTranslationTargetAtRowIndex(final int rowIndex) {
return getContentAtRowIndex(rowIndex, TARGET_ID_FMT, SINGULAR);
return getContentAtRowIndex(rowIndex, TARGET_ID_FMT, Plurals.TargetSingular);
}

/**
Expand All @@ -186,12 +182,12 @@ public String getBasicTranslationTargetAtRowIndex(final int rowIndex) {
*/
private String getContentAtRowIndex(final long rowIndex,
final String idFormat,
final int pluralIndex) {
final Plurals plural) {
return waitForTenSec().until(new Function<WebDriver, String>() {
@Override
public String apply(WebDriver input) {
return input.findElement(
By.id(String.format(idFormat, rowIndex, pluralIndex)))
By.id(String.format(idFormat, rowIndex, plural.index())))
.getAttribute("value");
}
});
Expand All @@ -204,17 +200,17 @@ public String apply(WebDriver input) {
* @return updated EditorPage
*/
public EditorPage translateTargetAtRowIndex(final int rowIndex, String text) {
setTargetContent(rowIndex, text, TARGET_ID_FMT, SINGULAR);
setTargetContent(rowIndex, text, TARGET_ID_FMT, Plurals.SourceSingular);
return new EditorPage(getDriver());
}

private void setTargetContent(final long rowIndex, final String text,
final String idFormat, final int pluralIndex) {
final String idFormat, final Plurals plural) {
WebElement we = waitForTenSec().until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver input) {
return input.findElement(
By.id(String.format(idFormat, rowIndex, pluralIndex)));
By.id(String.format(idFormat, rowIndex, plural.index())));
}
});
we.click();
Expand All @@ -224,22 +220,20 @@ public WebElement apply(WebDriver input) {

public EditorPage approveTranslationAtRow(int rowIndex) {
WebElement button = getDriver()
.findElement(By.id(String.format(APPROVE_FMT, rowIndex)));
.findElement(By.id(String.format(APPROVE_BUTTON_ID_FMT, rowIndex)));
button.click();
return this;
}

public EditorPage saveAsFuzzyAtRow(int rowIndex) {
WebElement button = getDriver()
.findElement(By.id(String.format(FUZZY_FMT, rowIndex)));
.findElement(By.id(String.format(FUZZY_BUTTON_ID_FMT, rowIndex)));
button.click();
return this;
}

public String getMessageTargetAtRowIndex(int rowIndex, int pluralIndex) {
Preconditions.checkArgument(pluralIndex >= 0 && pluralIndex <= 6,
"plural index must be in range [0,6]");
return getCodeMirrorContent(rowIndex, TARGET_ID_FMT, pluralIndex);
public String getMessageTargetAtRowIndex(int rowIndex, Plurals plurals) {
return getCodeMirrorContent(rowIndex, TARGET_ID_FMT, plurals);
}

public String getStatistics() {
Expand Down
@@ -0,0 +1,18 @@
package org.zanata.page.webtrans;

public enum Plurals {
// source plural forms
SourceSingular(0), SourcePlural(1),
// target plural forms
TargetSingular(0), TargetPluralOne(1), TargetPluralTwo(2),
TargetPluralThree(3), TargetPluralFour(4), TargetPluralFive(5);
private final int index;

Plurals(int index) {
this.index = index;
}

int index() {
return index;
}
}
Expand Up @@ -16,6 +16,7 @@
import org.zanata.common.LocaleId;
import org.zanata.feature.DetailedTest;
import org.zanata.page.webtrans.EditorPage;
import org.zanata.page.webtrans.Plurals;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.util.SampleProjectRule;
Expand Down Expand Up @@ -124,16 +125,16 @@ private static EditorPage verifyPluralPushedToEditor() {
BasicWorkFlow.EDITOR_TEMPLATE, "plurals", "master",
"pl", "test"), EditorPage.class);

assertThat(editorPage.getMessageSourceAtRowIndex(0, 0),
assertThat(editorPage.getMessageSourceAtRowIndex(0, Plurals.SourceSingular),
Matchers.equalTo("One file removed"));
assertThat(editorPage.getMessageSourceAtRowIndex(0, 1),
assertThat(editorPage.getMessageSourceAtRowIndex(0, Plurals.SourcePlural),
Matchers.equalTo("%d files removed"));
// nplural for Polish is 3
assertThat(editorPage.getMessageTargetAtRowIndex(0, 0),
assertThat(editorPage.getMessageTargetAtRowIndex(0, Plurals.TargetSingular),
Matchers.equalTo("1 aoeuaouaou"));
assertThat(editorPage.getMessageTargetAtRowIndex(0, 1),
assertThat(editorPage.getMessageTargetAtRowIndex(0, Plurals.TargetPluralOne),
Matchers.equalTo("%d aoeuaouao"));
assertThat(editorPage.getMessageTargetAtRowIndex(0, 2),
assertThat(editorPage.getMessageTargetAtRowIndex(0, Plurals.TargetPluralTwo),
Matchers.equalTo(" "));

return editorPage;
Expand Down

0 comments on commit c4b2596

Please sign in to comment.