DO NOT MERGE: Test validity of port numbers in account setup

Bug: 1712475
Change-Id: Iced4875379a804b5072e4df0af25db7bf9473131
This commit is contained in:
Marc Blank 2010-09-21 16:50:46 -07:00
parent ccfc1c29c2
commit d46952e7b0
4 changed files with 42 additions and 2 deletions

View File

@ -129,6 +129,20 @@ public class Utility {
return s != null && s.length() > 0;
}
public static boolean isPortFieldValid(TextView view) {
CharSequence chars = view.getText();
if (TextUtils.isEmpty(chars)) return false;
Integer port;
// In theory, we can't get an illegal value here, since the field is monitored for valid
// numeric input. But this might be used elsewhere without such a check.
try {
port = Integer.parseInt(chars.toString());
} catch (NumberFormatException e) {
return false;
}
return port > 0 && port < 65536;
}
/**
* Ensures that the given string starts and ends with the double quote character. The string is not modified in any way except to add the
* double quote character to start and end if it's not already there.

View File

@ -308,7 +308,7 @@ public class AccountSetupIncoming extends Activity implements OnClickListener {
boolean enabled = Utility.requiredFieldValid(mUsernameView)
&& Utility.requiredFieldValid(mPasswordView)
&& Utility.requiredFieldValid(mServerView)
&& Utility.requiredFieldValid(mPortView);
&& Utility.isPortFieldValid(mPortView);
if (enabled) {
try {
URI uri = getUri();

View File

@ -223,7 +223,8 @@ public class AccountSetupOutgoing extends Activity implements OnClickListener,
*/
private void validateFields() {
boolean enabled =
Utility.requiredFieldValid(mServerView) && Utility.requiredFieldValid(mPortView);
Utility.requiredFieldValid(mServerView)
&& Utility.isPortFieldValid(mPortView);
if (enabled && mRequireLoginView.isChecked()) {
enabled = (Utility.requiredFieldValid(mUsernameView)

View File

@ -25,6 +25,7 @@ import android.test.AndroidTestCase;
import android.test.MoreAsserts;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import android.widget.TextView;
import java.util.HashSet;
import java.util.Set;
@ -223,4 +224,28 @@ public class UtilityUnitTests extends AndroidTestCase {
assertEquals("Thu, 10 Dec 09 15:08:08 -0700",
Utility.cleanUpMimeDate("Thu, 10 Dec 09 15:08:08 -0700"));
}
public void testIsPortFieldValid() {
TextView view = new TextView(getContext());
// null, empty, negative, and non integer strings aren't valid
view.setText(null);
assertFalse(Utility.isPortFieldValid(view));
view.setText("");
assertFalse(Utility.isPortFieldValid(view));
view.setText("-1");
assertFalse(Utility.isPortFieldValid(view));
view.setText("1403.75");
assertFalse(Utility.isPortFieldValid(view));
view.setText("0");
assertFalse(Utility.isPortFieldValid(view));
view.setText("65536");
assertFalse(Utility.isPortFieldValid(view));
view.setText("i'm not valid");
assertFalse(Utility.isPortFieldValid(view));
// These next values are valid
view.setText("1");
assertTrue(Utility.isPortFieldValid(view));
view.setText("65535");
assertTrue(Utility.isPortFieldValid(view));
}
}