From b03a0df74e02aee649121b830f9b8443e9138990 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 26 Feb 2016 15:07:49 +0100 Subject: [PATCH] Provide default parameter names --- .../plot/PlotEditFunctionFragmentTest.java | 24 +++++++++++++++++++ .../functions/FunctionParamsView.java | 12 +++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/src/androidTest/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragmentTest.java b/app/src/androidTest/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragmentTest.java index c49ea058..daeaa1dc 100644 --- a/app/src/androidTest/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragmentTest.java +++ b/app/src/androidTest/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragmentTest.java @@ -1,15 +1,21 @@ package org.solovyev.android.calculator.plot; +import org.hamcrest.Matchers; import org.junit.Test; import org.solovyev.android.calculator.R; 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.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.matcher.ViewMatchers.hasFocus; 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.withTagValue; import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.not; 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()))); } + + @Test + public void testShouldProvideDefaultParamNames() throws Exception { + openFunctionEditor(); + + onView(withId(R.id.function_params_add)).perform(click()); + onView(allOf(hasFocus(), withTagValue(Matchers.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.equalTo(FunctionParamsView.PARAM_VIEW_TAG)))).perform(typeTextIntoFocusedView("y")); + onView(allOf(hasFocus(), withTagValue(Matchers.equalTo(FunctionParamsView.PARAM_VIEW_TAG)))).check(matches(withText("y"))); + } } \ No newline at end of file diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionParamsView.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionParamsView.java index 7177b416..0f58905f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionParamsView.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionParamsView.java @@ -48,6 +48,7 @@ import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.R; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import javax.annotation.Nonnull; @@ -60,6 +61,7 @@ public class FunctionParamsView extends LinearLayout { @Nonnull public static final String PARAM_VIEW_TAG = "param-view"; + private static final List PARAM_NAMES = Arrays.asList("x", "y", "z", "t", "a", "b", "c"); private static final int FOOTERS = 1; private static final int PARAM_VIEW_INDEX = 3; private static final int START_ROW_ID = App.generateViewId(); @@ -106,7 +108,7 @@ public class FunctionParamsView extends LinearLayout { addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - final LinearLayout rowView = addParam(null); + final LinearLayout rowView = addParam(generateParamName()); final EditText paramView = getParamView(rowView); paramView.requestFocus(); } @@ -116,6 +118,13 @@ public class FunctionParamsView extends LinearLayout { addView(headerView, new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT)); } + @Nullable + private String generateParamName() { + final List available = new ArrayList<>(PARAM_NAMES); + available.removeAll(getParams()); + return available.size() > 0 ? available.get(0) : null; + } + @NonNull private ImageButton makeButton(int icon) { final ImageButton addButton = new ImageButton(getContext()); @@ -187,6 +196,7 @@ public class FunctionParamsView extends LinearLayout { paramView.setText(param); } paramView.setOnFocusChangeListener(getOnFocusChangeListener()); + paramView.setSelectAllOnFocus(true); paramView.setInputType(EditorInfo.TYPE_CLASS_TEXT); paramView.setId(id); paramView.setTag(PARAM_VIEW_TAG);