Niedzielski has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/320311

Change subject: Fix potential NPE & hygiene in DescriptionEditView
......................................................................

Fix potential NPE & hygiene in DescriptionEditView

• Allow for an original null description and add an @Nullable annotation

• Update test save button logic and refactor lightly

• Rename editText to pageDescriptionText

Bug: T148203
Change-Id: I73f6bc98f8b6bf3a1b82d16ea55ce7ac8583ca0e
---
M 
app/src/androidTest/java/org/wikipedia/descriptions/DescriptionEditViewTest.java
M app/src/androidTest/java/org/wikipedia/test/view/ViewTest.java
M app/src/main/java/org/wikipedia/descriptions/DescriptionEditView.java
3 files changed, 35 insertions(+), 29 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/11/320311/1

diff --git 
a/app/src/androidTest/java/org/wikipedia/descriptions/DescriptionEditViewTest.java
 
b/app/src/androidTest/java/org/wikipedia/descriptions/DescriptionEditViewTest.java
index 54e1630..7cce7bc 100644
--- 
a/app/src/androidTest/java/org/wikipedia/descriptions/DescriptionEditViewTest.java
+++ 
b/app/src/androidTest/java/org/wikipedia/descriptions/DescriptionEditViewTest.java
@@ -75,7 +75,7 @@
         assertThat(subject.getDescription(), is(expected.getDescription()));
     }
 
-    // todo: resolve why the button doesn't show up yet here or in the actual 
screenshots above
+    // todo: resolve why the button doesn't show
     // @Theory public void testSetSaveState(@TestedOnBool final boolean 
saving) {
     //     defaultSetUp();
     //     subject.setSaveState(saving);
@@ -100,7 +100,7 @@
         defaultSetUp();
         String expected = nul ? null : "text";
         subject.setDescription(expected);
-        assertThat(subject.editText.getText().toString(), 
is(emptyIfNull(expected)));
+        assertThat(subject.pageDescriptionText.getText().toString(), 
is(emptyIfNull(expected)));
     }
 
     private void defaultSetUp() {
@@ -117,5 +117,9 @@
         subject.setTitle(str(title));
         subject.setDescription(str(description));
         subject.setSaveState(saving);
+
+        // todo: resolve why the button doesn't show deterministically. the 
button appears either
+        //       correctly or in the upper left
+        subject.saveButton.hide();
     }
-}
\ No newline at end of file
+}
diff --git a/app/src/androidTest/java/org/wikipedia/test/view/ViewTest.java 
b/app/src/androidTest/java/org/wikipedia/test/view/ViewTest.java
index 43f3910..5681fbd 100644
--- a/app/src/androidTest/java/org/wikipedia/test/view/ViewTest.java
+++ b/app/src/androidTest/java/org/wikipedia/test/view/ViewTest.java
@@ -124,11 +124,11 @@
                 .build();
     }
 
-    protected String str(@NonNull TestStr str, Object... formatArgs) {
+    protected String str(@NonNull TestStr str, @Nullable Object... formatArgs) 
{
         return str(str.id(), formatArgs);
     }
 
-    protected String str(@StringRes int id, Object... formatArgs) {
+    protected String str(@StringRes int id, @Nullable Object... formatArgs) {
         return id == 0 ? null : ctx().getString(id, formatArgs);
     }
 
@@ -140,18 +140,21 @@
         Configuration cfg = new 
Configuration(ctx.getResources().getConfiguration());
         cfg.screenWidthDp = widthDp;
         cfg.fontScale = fontScale.multiplier();
-
-        if (android.os.Build.VERSION.SDK_INT >= 
android.os.Build.VERSION_CODES.N) {
-            cfg.setLocales(new LocaleList(locale));
-        } else if (Build.VERSION.SDK_INT >= 
Build.VERSION_CODES.JELLY_BEAN_MR1) {
-            cfg.setLocale(locale);
-            cfg.setLayoutDirection(locale);
-        } else {
-            //noinspection deprecation
-            cfg.locale = locale;
-        }
+        setConfigLocale(cfg, locale);
 
         ctx.getResources().updateConfiguration(cfg, null);
+    }
+
+    private void setConfigLocale(@NonNull Configuration config, @NonNull 
Locale locale) {
+        if (android.os.Build.VERSION.SDK_INT >= 
android.os.Build.VERSION_CODES.N) {
+            config.setLocales(new LocaleList(locale));
+        } else if (Build.VERSION.SDK_INT >= 
Build.VERSION_CODES.JELLY_BEAN_MR1) {
+            config.setLocale(locale);
+            config.setLayoutDirection(locale);
+        } else {
+            //noinspection deprecation
+            config.locale = locale;
+        }
     }
 
     // todo: identify method name by @Theory / @Test annotation instead of 
depth and remove repeated
@@ -167,4 +170,4 @@
 
         return name;
     }
-}
\ No newline at end of file
+}
diff --git 
a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditView.java 
b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditView.java
index 3e36696..0fcbbef 100644
--- a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditView.java
+++ b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditView.java
@@ -10,7 +10,6 @@
 import android.support.annotation.VisibleForTesting;
 import android.support.design.widget.FloatingActionButton;
 import android.support.v4.content.ContextCompat;
-import android.text.Editable;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.View;
@@ -19,9 +18,9 @@
 import android.widget.ProgressBar;
 import android.widget.TextView;
 
+import org.apache.commons.lang3.StringUtils;
 import org.wikipedia.R;
 import org.wikipedia.page.PageTitle;
-import org.wikipedia.util.StringUtil;
 
 import butterknife.BindView;
 import butterknife.ButterKnife;
@@ -31,12 +30,12 @@
 public class DescriptionEditView extends FrameLayout {
     @BindView(R.id.description_edit_page_title) TextView pageTitleText;
     @BindView(R.id.description_edit_save_button) FloatingActionButton 
saveButton;
-    @BindView(R.id.description_edit_text) EditText editText;
+    @BindView(R.id.description_edit_text) EditText pageDescriptionText;
     @BindView(R.id.description_edit_char_count) TextView charCountText;
     @BindView(R.id.description_edit_progress_bar) ProgressBar progressBar;
 
-    private PageTitle pageTitle;
-    private String originalDescription;
+    @Nullable private PageTitle pageTitle;
+    @Nullable private String originalDescription;
     @Nullable private Callback callback;
 
     public interface Callback {
@@ -71,7 +70,8 @@
     public void setPageTitle(@NonNull PageTitle pageTitle) {
         this.pageTitle = pageTitle;
         setTitle(pageTitle.getDisplayText());
-        setDescription(StringUtil.emptyIfNull(pageTitle.getDescription()));
+        originalDescription = pageTitle.getDescription();
+        setDescription(originalDescription);
     }
 
     public void setSaveState(boolean saving) {
@@ -84,7 +84,7 @@
     }
 
     @NonNull public String getDescription() {
-        return editText.getText().toString();
+        return pageDescriptionText.getText().toString();
     }
 
     @OnClick(R.id.description_edit_save_button) void onSaveClick() {
@@ -95,7 +95,7 @@
 
     @OnTextChanged(value = R.id.description_edit_text,
             callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
-    void descriptionTextChanged(Editable editable) {
+    void pageDescriptionTextChanged() {
         updateSaveButtonVisible();
         updateCharCount();
     }
@@ -105,8 +105,7 @@
     }
 
     @VisibleForTesting void setDescription(@Nullable String text) {
-        originalDescription = text;
-        editText.setText(text);
+        pageDescriptionText.setText(text);
     }
 
     private void init() {
@@ -115,8 +114,8 @@
     }
 
     private void updateSaveButtonVisible() {
-        if (!TextUtils.isEmpty(editText.getText())
-                && !originalDescription.equals(editText.getText().toString())) 
{
+        if (!TextUtils.isEmpty(pageDescriptionText.getText())
+                && !StringUtils.equals(originalDescription, 
pageDescriptionText.getText())) {
             saveButton.show();
         } else {
             saveButton.hide();
@@ -128,7 +127,7 @@
     }
 
     private void updateCharCount() {
-        int charCount = editText.getText().length();
+        int charCount = pageDescriptionText.getText().length();
         int maxChars = 
getResources().getInteger(R.integer.description_max_chars);
         charCountText.setText(String.format(getResources()
                 .getString(R.string.description_edit_char_count), charCount, 
maxChars));

-- 
To view, visit https://gerrit.wikimedia.org/r/320311
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I73f6bc98f8b6bf3a1b82d16ea55ce7ac8583ca0e
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to