Provide default parameter names

This commit is contained in:
serso 2016-02-26 15:07:49 +01:00
parent c437c2d0c9
commit b03a0df74e
2 changed files with 35 additions and 1 deletions

View File

@ -1,15 +1,21 @@
package org.solovyev.android.calculator.plot; package org.solovyev.android.calculator.plot;
import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.functions.CppFunction; import org.solovyev.android.calculator.functions.CppFunction;
import org.solovyev.android.calculator.functions.FunctionParamsView;
import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeTextIntoFocusedView;
import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.hasFocus;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withTagValue;
import static android.support.test.espresso.matcher.ViewMatchers.withText; import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
public class PlotEditFunctionFragmentTest extends BasePlotTest { public class PlotEditFunctionFragmentTest extends BasePlotTest {
@ -35,4 +41,22 @@ public class PlotEditFunctionFragmentTest extends BasePlotTest {
onView(withId(R.id.function_params_add)).check(matches(not(isDisplayed()))); onView(withId(R.id.function_params_add)).check(matches(not(isDisplayed())));
} }
@Test
public void testShouldProvideDefaultParamNames() throws Exception {
openFunctionEditor();
onView(withId(R.id.function_params_add)).perform(click());
onView(allOf(hasFocus(), withTagValue(Matchers.<Object>equalTo(FunctionParamsView.PARAM_VIEW_TAG)))).check(matches(withText("x")));
}
@Test
public void testShouldSelectParamOnFocus() throws Exception {
openFunctionEditor();
onView(withId(R.id.function_params_add)).perform(click());
// check "select-on-focus" attribute
onView(allOf(hasFocus(), withTagValue(Matchers.<Object>equalTo(FunctionParamsView.PARAM_VIEW_TAG)))).perform(typeTextIntoFocusedView("y"));
onView(allOf(hasFocus(), withTagValue(Matchers.<Object>equalTo(FunctionParamsView.PARAM_VIEW_TAG)))).check(matches(withText("y")));
}
} }

View File

@ -48,6 +48,7 @@ import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -60,6 +61,7 @@ public class FunctionParamsView extends LinearLayout {
@Nonnull @Nonnull
public static final String PARAM_VIEW_TAG = "param-view"; public static final String PARAM_VIEW_TAG = "param-view";
private static final List<String> PARAM_NAMES = Arrays.asList("x", "y", "z", "t", "a", "b", "c");
private static final int FOOTERS = 1; private static final int FOOTERS = 1;
private static final int PARAM_VIEW_INDEX = 3; private static final int PARAM_VIEW_INDEX = 3;
private static final int START_ROW_ID = App.generateViewId(); private static final int START_ROW_ID = App.generateViewId();
@ -106,7 +108,7 @@ public class FunctionParamsView extends LinearLayout {
addButton.setOnClickListener(new View.OnClickListener() { addButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final LinearLayout rowView = addParam(null); final LinearLayout rowView = addParam(generateParamName());
final EditText paramView = getParamView(rowView); final EditText paramView = getParamView(rowView);
paramView.requestFocus(); paramView.requestFocus();
} }
@ -116,6 +118,13 @@ public class FunctionParamsView extends LinearLayout {
addView(headerView, new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT)); addView(headerView, new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
} }
@Nullable
private String generateParamName() {
final List<String> available = new ArrayList<>(PARAM_NAMES);
available.removeAll(getParams());
return available.size() > 0 ? available.get(0) : null;
}
@NonNull @NonNull
private ImageButton makeButton(int icon) { private ImageButton makeButton(int icon) {
final ImageButton addButton = new ImageButton(getContext()); final ImageButton addButton = new ImageButton(getContext());
@ -187,6 +196,7 @@ public class FunctionParamsView extends LinearLayout {
paramView.setText(param); paramView.setText(param);
} }
paramView.setOnFocusChangeListener(getOnFocusChangeListener()); paramView.setOnFocusChangeListener(getOnFocusChangeListener());
paramView.setSelectAllOnFocus(true);
paramView.setInputType(EditorInfo.TYPE_CLASS_TEXT); paramView.setInputType(EditorInfo.TYPE_CLASS_TEXT);
paramView.setId(id); paramView.setId(id);
paramView.setTag(PARAM_VIEW_TAG); paramView.setTag(PARAM_VIEW_TAG);