diff --git a/auth/README.md b/auth/README.md index efa9e1171..e64b115ea 100644 --- a/auth/README.md +++ b/auth/README.md @@ -357,6 +357,11 @@ The country code selector will exclude all countries with a country code of +1 a Note: You can't provide both a list of countries to whitelist and blacklist. If you do, a runtime exception will be thrown. +This change is purely UI based. We do not restrict users from signing in with their phone number. +They will simply be unable to choose their country in the selector, but there may be another country +sharing the same country code (e.g. US and CA are +1). + + ##### ### Handling the sign-in response diff --git a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java index 6c3db66ef..a8251cebc 100644 --- a/auth/src/main/java/com/firebase/ui/auth/AuthUI.java +++ b/auth/src/main/java/com/firebase/ui/auth/AuthUI.java @@ -668,16 +668,24 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) { * https://en.wikipedia.org/wiki/ISO_3166-1 * and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes * - * @param whitelistedCountries a case insensitive list of country codes and/or - * isos to be whitelisted + * @param whitelistedCountries a non empty case insensitive list of country codes + * and/or isos to be whitelisted + * @throws IllegalArgumentException if an empty whitelist is provided. + * @throws NullPointerException if a null whitelist is provided. */ public PhoneBuilder setWhitelistedCountries( @NonNull List whitelistedCountries) { if (getParams().containsKey(ExtraConstants.BLACKLISTED_COUNTRIES)) { - throw new RuntimeException( + throw new IllegalStateException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } + + String message = "Invalid argument: Only non-%s whitelists are valid. " + + "To specify no whitelist, do not call this method."; + Preconditions.checkNotNull(whitelistedCountries, String.format(message, "null")); + Preconditions.checkArgument(!whitelistedCountries.isEmpty(), String.format(message, "empty")); + addCountriesToBundle(whitelistedCountries, ExtraConstants.WHITELISTED_COUNTRIES); return this; } @@ -695,16 +703,25 @@ public PhoneBuilder setWhitelistedCountries( * For a list of country iso or codes, see Alpha-2 codes here: * https://en.wikipedia.org/wiki/ISO_3166-1 * and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes - * @param blacklistedCountries a case insensitive list of country codes and/or isos - * to be blacklisted + * + * @param blacklistedCountries a non empty case insensitive list of country codes + * and/or isos to be blacklisted + * @throws IllegalArgumentException if an empty blacklist is provided. + * @throws NullPointerException if a null blacklist is provided. */ public PhoneBuilder setBlacklistedCountries( @NonNull List blacklistedCountries) { if (getParams().containsKey(ExtraConstants.WHITELISTED_COUNTRIES)) { - throw new RuntimeException( + throw new IllegalStateException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } + + String message = "Invalid argument: Only non-%s blacklists are valid. " + + "To specify no blacklist, do not call this method."; + Preconditions.checkNotNull(blacklistedCountries, String.format(message, "null")); + Preconditions.checkArgument(!blacklistedCountries.isEmpty(), String.format(message, "empty")); + addCountriesToBundle(blacklistedCountries, ExtraConstants.BLACKLISTED_COUNTRIES); return this; } @@ -732,41 +749,70 @@ private void validateInputs() { if (whitelistedCountries != null && blacklistedCountries != null) { - throw new RuntimeException( + throw new IllegalStateException( "You can either whitelist or blacklist country codes for phone " + "authentication."); } else if (whitelistedCountries != null) { - validateCountries(whitelistedCountries); - validateDefaultCountryIso(whitelistedCountries, true); + validateInputs(whitelistedCountries, true); } else if (blacklistedCountries != null) { - validateCountries(blacklistedCountries); - validateDefaultCountryIso(blacklistedCountries, false); + validateInputs(blacklistedCountries, false); } } - private void validateCountries(List codes) { + private void validateInputs(List countries, boolean whitelisted) { + validateCountryInput(countries); + validateDefaultCountryInput(countries, whitelisted); + } + + private void validateCountryInput(List codes) { for (String code : codes) { if (!PhoneNumberUtils.isValidIso(code) && !PhoneNumberUtils.isValid(code)) { - throw new RuntimeException("Invalid input: You must provide a valid " + - "country iso (alpha-2) or code (e-164). e.g. 'us' or '+1'."); + throw new IllegalArgumentException("Invalid input: You must provide a " + + "valid country iso (alpha-2) or code (e-164). e.g. 'us' or '+1'."); } } } - private void validateDefaultCountryIso(List codes, boolean whitelisted) { - if (getParams().containsKey(ExtraConstants.COUNTRY_ISO) && codes != null) { - String defaultIso = getParams().getString(ExtraConstants.COUNTRY_ISO); - boolean containsIso = containsCountryIso(codes, defaultIso); - if (!containsIso && whitelisted || containsIso && !whitelisted) { - throw new RuntimeException("Invalid default country iso. Make sure it " + - "is either part of the whitelisted list or that you " + - "haven't blacklisted it."); + private void validateDefaultCountryInput(List codes, boolean whitelisted) { + // A default iso/code can be set via #setDefaultCountryIso() or #setDefaultNumber() + if (getParams().containsKey(ExtraConstants.COUNTRY_ISO) || + getParams().containsKey(ExtraConstants.PHONE)) { + + if (!validateDefaultCountryIso(codes, whitelisted) + || !validateDefaultPhoneIsos(codes, whitelisted)) { + throw new IllegalArgumentException("Invalid default country iso. Make " + + "sure it is either part of the whitelisted list or that you " + + "haven't blacklisted it."); } } + + } + + private boolean validateDefaultCountryIso(List codes, boolean whitelisted) { + String defaultIso = getDefaultIso(); + return isValidDefaultIso(codes, defaultIso, whitelisted); + } + + private boolean validateDefaultPhoneIsos(List codes, boolean whitelisted) { + List phoneIsos = getPhoneIsosFromCode(); + for (String iso : phoneIsos) { + if (isValidDefaultIso(codes, iso, whitelisted)) { + return true; + } + } + return phoneIsos.isEmpty(); + } + + private boolean isValidDefaultIso(List codes, String iso, boolean whitelisted) { + if (iso == null) return true; + boolean containsIso = containsCountryIso(codes, iso); + return containsIso && whitelisted || !containsIso && !whitelisted; + } private boolean containsCountryIso(List codes, String iso) { + iso = iso.toUpperCase(Locale.getDefault()); for (String code : codes) { if (PhoneNumberUtils.isValidIso(code)) { if (code.equals(iso)) { @@ -781,6 +827,26 @@ private boolean containsCountryIso(List codes, String iso) { } return false; } + + private List getPhoneIsosFromCode() { + List isos = new ArrayList<>(); + String phone = getParams().getString(ExtraConstants.PHONE); + if (phone != null && phone.startsWith("+")) { + String countryCode = "+" + PhoneNumberUtils.getPhoneNumber(phone) + .getCountryCode(); + List isosToAdd = PhoneNumberUtils. + getCountryIsosFromCountryCode(countryCode); + if (isosToAdd != null) { + isos.addAll(isosToAdd); + } + } + return isos; + } + + private String getDefaultIso() { + return getParams().containsKey(ExtraConstants.COUNTRY_ISO) ? + getParams().getString(ExtraConstants.COUNTRY_ISO) : null; + } } /** @@ -943,8 +1009,8 @@ public T setLogo(@DrawableRes int logo) { /** * Specifies the terms-of-service URL for the application. * - * @deprecated Please use {@link #setTosAndPrivacyPolicyUrls(String, String)} - * For the Tos link to be displayed a Privacy Policy url must also be provided. + * @deprecated Please use {@link #setTosAndPrivacyPolicyUrls(String, String)} For the Tos + * link to be displayed a Privacy Policy url must also be provided. */ @NonNull @Deprecated @@ -956,8 +1022,8 @@ public T setTosUrl(@Nullable String tosUrl) { /** * Specifies the privacy policy URL for the application. * - * @deprecated Please use {@link #setTosAndPrivacyPolicyUrls(String, String)} - * For the Privacy Policy link to be displayed a Tos url must also be provided. + * @deprecated Please use {@link #setTosAndPrivacyPolicyUrls(String, String)} For the + * Privacy Policy link to be displayed a Tos url must also be provided. */ @NonNull @Deprecated diff --git a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java b/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java deleted file mode 100644 index 7e5976ca7..000000000 --- a/auth/src/main/java/com/firebase/ui/auth/data/client/CountryListLoadTask.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (C) 2015 Twitter, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Modifications copyright (C) 2017 Google Inc - */ - -package com.firebase.ui.auth.data.client; - -import android.os.AsyncTask; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.annotation.RestrictTo; - -import com.firebase.ui.auth.data.model.CountryInfo; -import com.firebase.ui.auth.util.data.PhoneNumberUtils; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -// TODO We need to move away from ListView and AsyncTask in the future and use (say) -// RecyclerView and Task/ThreadPoolExecutor . -@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) -public final class CountryListLoadTask extends AsyncTask> { - - @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) - public interface Listener { - void onLoadComplete(List result); - } - - private final Listener mListener; - - - Set whitelistedCountryIsos; - Set blacklistedCountryIsos; - - public CountryListLoadTask(@Nullable List whitelistedCountries, - @Nullable List blacklistedCountries, - Listener listener) { - mListener = listener; - - if (whitelistedCountries != null) { - this.whitelistedCountryIsos = convertCodesToIsos(new HashSet<>(whitelistedCountries)); - } else if (blacklistedCountries != null) { - this.blacklistedCountryIsos = convertCodesToIsos(new HashSet<>(blacklistedCountries)); - } - } - - private Set convertCodesToIsos(@NonNull Set codes) { - Set isos = new HashSet<>(); - for (String code : codes) { - if (PhoneNumberUtils.isValid(code)) { - isos.addAll(PhoneNumberUtils.getCountryIsosFromCountryCode(code)); - } else { - isos.add(code); - } - } - return isos; - } - - @Override - protected List doInBackground(Void... params) { - Map countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap(); - List countryInfoList = getAvailableCountryIsos(countryInfoMap); - Collections.sort(countryInfoList); - return countryInfoList; - } - - public List getAvailableCountryIsos(Map countryInfoMap) { - // We consider all countries to be whitelisted if there are no whitelisted - // or blacklisted countries given as input. - if (whitelistedCountryIsos == null && blacklistedCountryIsos == null) { - this.whitelistedCountryIsos = new HashSet<>(countryInfoMap.keySet()); - } - - List countryInfoList = new ArrayList<>(); - - // At this point either whitelistedCountryIsos or blacklistedCountryIsos is null. - // We assume no countries are to be excluded. Here, we correct this assumption based on the - // contents of either lists. - Set excludedCountries = new HashSet<>(); - if (whitelistedCountryIsos == null) { - // Exclude all countries in the blacklistedCountryIsos list. - excludedCountries.addAll(blacklistedCountryIsos); - } else { - // Exclude all countries that are not present in the whitelistedCountryIsos list. - excludedCountries.addAll(countryInfoMap.keySet()); - excludedCountries.removeAll(whitelistedCountryIsos); - } - - // Once we know which countries need to be excluded, we loop through the country isos, - // skipping those that have been excluded. - for (String countryIso : countryInfoMap.keySet()) { - if (!excludedCountries.contains(countryIso)) { - countryInfoList.add(new CountryInfo(new Locale("", countryIso), - countryInfoMap.get(countryIso))); - } - } - - return countryInfoList; - } - - @Override - public void onPostExecute(List result) { - if (mListener != null) { - mListener.onLoadComplete(result); - } - } -} diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CheckPhoneNumberFragment.java b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CheckPhoneNumberFragment.java index 86a2df770..93ed3a9fc 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CheckPhoneNumberFragment.java +++ b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CheckPhoneNumberFragment.java @@ -47,6 +47,7 @@ public class CheckPhoneNumberFragment extends FragmentBase implements View.OnCli private EditText mPhoneEditText; private TextView mSmsTermsText; + public static CheckPhoneNumberFragment newInstance(Bundle params) { CheckPhoneNumberFragment fragment = new CheckPhoneNumberFragment(); Bundle args = new Bundle(); @@ -95,12 +96,6 @@ public void onDonePressed() { } }); mSubmitButton.setOnClickListener(this); - mCountryListSpinner.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - mPhoneInputLayout.setError(null); - } - }); setupPrivacyDisclosures(view.findViewById(R.id.email_footer_tos_and_pp_text)); } @@ -125,35 +120,7 @@ protected void onFailure(@NonNull Exception e) { // destroyed so its state isn't saved and we have to rely on an instance field. Sigh. mCalled = true; - // Check for phone - // It is assumed that the phone number that are being wired in via Credential Selector - // are e164 since we store it. - Bundle params = getArguments().getBundle(ExtraConstants.PARAMS); - String phone = null; - String countryIso = null; - String nationalNumber = null; - if (params != null) { - phone = params.getString(ExtraConstants.PHONE); - countryIso = params.getString(ExtraConstants.COUNTRY_ISO); - nationalNumber = params.getString(ExtraConstants.NATIONAL_NUMBER); - } - - // We can receive the phone number in one of two formats: split between the ISO or fully - // processed. If it's complete, we use it directly. Otherwise, we parse the ISO and national - // number combination or we just set the default ISO if there's no default number. If there - // are no defaults at all, we prompt the user for a phone number through Smart Lock. - if (!TextUtils.isEmpty(phone)) { - start(PhoneNumberUtils.getPhoneNumber(phone)); - } else if (!TextUtils.isEmpty(countryIso) && !TextUtils.isEmpty(nationalNumber)) { - start(PhoneNumberUtils.getPhoneNumber(countryIso, nationalNumber)); - } else if (!TextUtils.isEmpty(countryIso)) { - setCountryCode(new PhoneNumber( - "", - countryIso, - String.valueOf(PhoneNumberUtils.getCountryCode(countryIso)))); - } else if (getFlowParams().enableHints) { - mCheckPhoneHandler.fetchCredential(); - } + setupCountrySpinner(); } @Override @@ -174,10 +141,12 @@ private void start(PhoneNumber number) { mPhoneEditText.setText(number.getPhoneNumber()); mPhoneEditText.setSelection(number.getPhoneNumber().length()); - if (PhoneNumber.isCountryValid(number)) { + String iso = number.getCountryIso(); + + if (PhoneNumber.isCountryValid(number) && mCountryListSpinner.isValidIso(iso)) { setCountryCode(number); + onNext(); } - onNext(); } private void onNext() { @@ -223,6 +192,53 @@ private void setCountryCode(PhoneNumber number) { new Locale("", number.getCountryIso()), number.getCountryCode()); } + private void setupCountrySpinner() { + Bundle params = getArguments().getBundle(ExtraConstants.PARAMS); + mCountryListSpinner.init(params); + + setDefaultCountryForSpinner(); + + // Clear error when spinner is clicked on + mCountryListSpinner.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mPhoneInputLayout.setError(null); + } + }); + } + + private void setDefaultCountryForSpinner() { + // Check for phone + // It is assumed that the phone number that are being wired in via Credential Selector + // are e164 since we store it. + Bundle params = getArguments().getBundle(ExtraConstants.PARAMS); + String phone = null; + String countryIso = null; + String nationalNumber = null; + if (params != null) { + phone = params.getString(ExtraConstants.PHONE); + countryIso = params.getString(ExtraConstants.COUNTRY_ISO); + nationalNumber = params.getString(ExtraConstants.NATIONAL_NUMBER); + } + + // We can receive the phone number in one of two formats: split between the ISO or fully + // processed. If it's complete, we use it directly. Otherwise, we parse the ISO and national + // number combination or we just set the default ISO if there's no default number. If there + // are no defaults at all, we prompt the user for a phone number through Smart Lock. + if (!TextUtils.isEmpty(phone)) { + start(PhoneNumberUtils.getPhoneNumber(phone)); + } else if (!TextUtils.isEmpty(countryIso) && !TextUtils.isEmpty(nationalNumber)) { + start(PhoneNumberUtils.getPhoneNumber(countryIso, nationalNumber)); + } else if (!TextUtils.isEmpty(countryIso)) { + setCountryCode(new PhoneNumber( + "", + countryIso, + String.valueOf(PhoneNumberUtils.getCountryCode(countryIso)))); + } else if (getFlowParams().enableHints) { + mCheckPhoneHandler.fetchCredential(); + } + } + @Override public void showProgress(int message) { mSubmitButton.setEnabled(false); diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java index 166a1e7ad..20c69ade4 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java +++ b/auth/src/main/java/com/firebase/ui/auth/ui/phone/CountryListSpinner.java @@ -20,9 +20,9 @@ import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Parcelable; +import android.support.annotation.NonNull; import android.support.v7.widget.AppCompatEditText; import android.text.TextUtils; import android.util.AttributeSet; @@ -30,15 +30,19 @@ import android.view.inputmethod.InputMethodManager; import android.widget.ListView; -import com.firebase.ui.auth.data.client.CountryListLoadTask; import com.firebase.ui.auth.data.model.CountryInfo; +import com.firebase.ui.auth.util.ExtraConstants; import com.firebase.ui.auth.util.data.PhoneNumberUtils; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Set; -public final class CountryListSpinner extends AppCompatEditText implements - View.OnClickListener, CountryListLoadTask.Listener { +public final class CountryListSpinner extends AppCompatEditText implements View.OnClickListener { private static final String KEY_SUPER_STATE = "KEY_SUPER_STATE"; private static final String KEY_COUNTRY_INFO = "KEY_COUNTRY_INFO"; @@ -50,8 +54,8 @@ public final class CountryListSpinner extends AppCompatEditText implements private String mSelectedCountryName; private CountryInfo mSelectedCountryInfo; - private List whitelistedCountries; - private List blacklistedCountries; + private Set mWhitelistedCountryIsos; + private Set mBlacklistedCountryIsos; public CountryListSpinner(Context context) { this(context, null, android.R.attr.spinnerStyle); @@ -69,8 +73,99 @@ public CountryListSpinner(Context context, AttributeSet attrs, int defStyle) { mDialogPopup = new DialogPopup(mCountryListAdapter); mTextFormat = "%1$s +%2$d"; mSelectedCountryName = ""; + } + + public void init(Bundle params) { + if (params != null) { + List countries = getCountriesToDisplayInSpinner(params); + setCountriesToDisplay(countries); + setDefaultCountryForSpinner(countries); + } + } + + private List getCountriesToDisplayInSpinner(Bundle params) { + initCountrySpinnerIsosFromParams(params); + + Map countryInfoMap = PhoneNumberUtils.getImmutableCountryIsoMap(); + // We consider all countries to be whitelisted if there are no whitelisted + // or blacklisted countries given as input. + if (mWhitelistedCountryIsos == null && mBlacklistedCountryIsos == null) { + this.mWhitelistedCountryIsos = new HashSet<>(countryInfoMap.keySet()); + } + + List countryInfoList = new ArrayList<>(); + + // At this point either mWhitelistedCountryIsos or mBlacklistedCountryIsos is null. + // We assume no countries are to be excluded. Here, we correct this assumption based on the + // contents of either lists. + Set excludedCountries = new HashSet<>(); + if (mWhitelistedCountryIsos == null) { + // Exclude all countries in the mBlacklistedCountryIsos list. + excludedCountries.addAll(mBlacklistedCountryIsos); + } else { + // Exclude all countries that are not present in the mWhitelistedCountryIsos list. + excludedCountries.addAll(countryInfoMap.keySet()); + excludedCountries.removeAll(mWhitelistedCountryIsos); + } + + // Once we know which countries need to be excluded, we loop through the country isos, + // skipping those that have been excluded. + for (String countryIso : countryInfoMap.keySet()) { + if (!excludedCountries.contains(countryIso)) { + countryInfoList.add(new CountryInfo(new Locale("", countryIso), + countryInfoMap.get(countryIso))); + } + } + Collections.sort(countryInfoList); + return countryInfoList; + } + + private void initCountrySpinnerIsosFromParams(@NonNull Bundle params) { + List whitelistedCountries = + params.getStringArrayList(ExtraConstants.WHITELISTED_COUNTRIES); + List blacklistedCountries = + params.getStringArrayList(ExtraConstants.BLACKLISTED_COUNTRIES); + + if (whitelistedCountries != null) { + mWhitelistedCountryIsos = convertCodesToIsos(whitelistedCountries); + } else if (blacklistedCountries != null) { + mBlacklistedCountryIsos = convertCodesToIsos(blacklistedCountries); + } + } + + private Set convertCodesToIsos(@NonNull List codes) { + Set isos = new HashSet<>(); + for (String code : codes) { + if (PhoneNumberUtils.isValid(code)) { + isos.addAll(PhoneNumberUtils.getCountryIsosFromCountryCode(code)); + } else { + isos.add(code); + } + } + return isos; + } + + public void setCountriesToDisplay(List countries) { + mCountryListAdapter.setData(countries); + } + + private void setDefaultCountryForSpinner(List countries) { CountryInfo countryInfo = PhoneNumberUtils.getCurrentCountryInfo(getContext()); - setSelectedForCountry(countryInfo.getCountryCode(), countryInfo.getLocale()); + if (isValidIso(countryInfo.getLocale().getCountry())) { + setSelectedForCountry(countryInfo.getCountryCode(), + countryInfo.getLocale()); + } else if (countries.iterator().hasNext()) { + countryInfo = countries.iterator().next(); + setSelectedForCountry(countryInfo.getCountryCode(), + countryInfo.getLocale()); + } + } + + public boolean isValidIso(String iso) { + iso = iso.toUpperCase(Locale.getDefault()); + return ((mWhitelistedCountryIsos == null && mBlacklistedCountryIsos == null) + || (mWhitelistedCountryIsos != null && mWhitelistedCountryIsos.contains(iso)) + || (mBlacklistedCountryIsos != null && !mBlacklistedCountryIsos.contains(iso))); } @Override @@ -105,16 +200,18 @@ private static void hideKeyboard(Context context, View view) { } } - private void setSelectedForCountry(int countryCode, Locale locale) { + public void setSelectedForCountry(int countryCode, Locale locale) { setText(String.format(mTextFormat, CountryInfo.localeToEmoji(locale), countryCode)); mSelectedCountryInfo = new CountryInfo(locale, countryCode); } public void setSelectedForCountry(final Locale locale, String countryCode) { - final String countryName = locale.getDisplayName(); - if (!TextUtils.isEmpty(countryName) && !TextUtils.isEmpty(countryCode)) { - mSelectedCountryName = countryName; - setSelectedForCountry(Integer.parseInt(countryCode), locale); + if (isValidIso(locale.getCountry())) { + final String countryName = locale.getDisplayName(); + if (!TextUtils.isEmpty(countryName) && !TextUtils.isEmpty(countryCode)) { + mSelectedCountryName = countryName; + setSelectedForCountry(Integer.parseInt(countryCode), locale); + } } } @@ -138,40 +235,17 @@ public void setOnClickListener(OnClickListener l) { @Override public void onClick(View view) { - if (mCountryListAdapter.getCount() == 0) { - loadCountryList(); - } else { - mDialogPopup.show(mCountryListAdapter.getPositionForCountry(mSelectedCountryName)); - } + mDialogPopup.show(mCountryListAdapter.getPositionForCountry(mSelectedCountryName)); hideKeyboard(getContext(), this); executeUserClickListener(view); } - private void loadCountryList() { - new CountryListLoadTask(whitelistedCountries, blacklistedCountries, this - ).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - private void executeUserClickListener(View view) { if (mListener != null) { mListener.onClick(view); } } - public void setWhitelistedCountries(List whitelistedCountries) { - this.whitelistedCountries = whitelistedCountries; - } - - public void setBlacklistedCountries(List blacklistedCountries) { - this.blacklistedCountries = blacklistedCountries; - } - - @Override - public void onLoadComplete(List result) { - mCountryListAdapter.setData(result); - mDialogPopup.show(mCountryListAdapter.getPositionForCountry(mSelectedCountryName)); - } - public class DialogPopup implements DialogInterface.OnClickListener { //Delay for postDelayed to set selection without showing the scroll animation private static final long DELAY_MILLIS = 10L; diff --git a/auth/src/main/java/com/firebase/ui/auth/util/Preconditions.java b/auth/src/main/java/com/firebase/ui/auth/util/Preconditions.java index f594655d8..465341e5c 100644 --- a/auth/src/main/java/com/firebase/ui/auth/util/Preconditions.java +++ b/auth/src/main/java/com/firebase/ui/auth/util/Preconditions.java @@ -91,4 +91,17 @@ public static void checkConfigured(@NonNull Context context, } } } + + /** + * Ensures the truth of an expression involving parameters to the calling method. + * + * @param expression a boolean expression + * @param errorMessage the exception message to use if the check fails + * @throws IllegalArgumentException if {@code expression} is false + */ + public static void checkArgument(boolean expression, String errorMessage) { + if (!expression) { + throw new IllegalArgumentException(errorMessage); + } + } } diff --git a/auth/src/test/java/com/firebase/ui/auth/AuthUITest.java b/auth/src/test/java/com/firebase/ui/auth/AuthUITest.java index fafb8bb06..c828e2119 100644 --- a/auth/src/test/java/com/firebase/ui/auth/AuthUITest.java +++ b/auth/src/test/java/com/firebase/ui/auth/AuthUITest.java @@ -29,7 +29,9 @@ import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import static junit.framework.Assert.assertEquals; import static org.mockito.Mockito.mock; @@ -92,4 +94,114 @@ public void testCreatingStartIntent_withNullPp_expectEnforcesNonNullPpUrl() { SignInIntentBuilder startIntent = AuthUI.getInstance().createSignInIntentBuilder(); startIntent.setTosAndPrivacyPolicyUrls(TestConstants.TOS_URL, null); } + + @Test(expected = IllegalArgumentException.class) + public void testPhoneBuilder_withBlacklistedDefaultNumberCode_expectIllegalArgumentException() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("+1123456789") + .setBlacklistedCountries(Arrays.asList("+1")) + .build(); + } + + @Test(expected = IllegalArgumentException.class) + public void testPhoneBuilder_withBlacklistedDefaultIso_expectIllegalArgumentException() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("us", "123456789") + .setBlacklistedCountries(Arrays.asList("us")) + .build(); + } + + @Test + public void testPhoneBuilder_withWhitelistedDefaultIso_expectSuccess() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("us", "123456789") + .setWhitelistedCountries(Arrays.asList("us")) + .build(); + } + + @Test + public void testPhoneBuilder_withWhitelistedDefaultNumberCode_expectSuccess() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("+1123456789") + .setWhitelistedCountries(Arrays.asList("+1")) + .build(); + } + + @Test(expected = IllegalArgumentException.class) + public void testPhoneBuilder_whiteInvalidDefaultNumberCode_expectIllegalArgumentException() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("+1123456789") + .setWhitelistedCountries(Arrays.asList("gr")) + .build(); + } + + @Test + public void testPhoneBuilder_withValidDefaultNumberCode_expectSuccess() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("+1123456789") + .setWhitelistedCountries(Arrays.asList("ca")) + .build(); + } + + @Test + public void testPhoneBuilder_withBlacklistedCountryWithSameCountryCode_expectSucess() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("+1123456789") + .setBlacklistedCountries(Arrays.asList("ca")) + .build(); + } + + @Test(expected = IllegalArgumentException.class) + public void testPhoneBuilder_withInvalidDefaultIso_expectIllegalArgumentException() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("us", "123456789") + .setWhitelistedCountries(Arrays.asList("ca")) + .build(); + } + + @Test + public void testPhoneBuilder_withValidDefaultIso_expectSucess() { + new IdpConfig.PhoneBuilder() + .setDefaultNumber("us", "123456789") + .setBlacklistedCountries(Arrays.asList("ca")) + .build(); + } + + @Test(expected = IllegalStateException.class) + public void testPhoneBuilder_setBothBlacklistedAndWhitelistedCountries_expectIllegalStateException() { + List countries = Arrays.asList("ca"); + new IdpConfig.PhoneBuilder() + .setBlacklistedCountries(countries) + .setWhitelistedCountries(countries) + .build(); + } + + @Test(expected = IllegalArgumentException.class) + public void testPhoneBuilder_passEmptyListForWhitelistedCountries_expectIllegalArgumentException() { + new IdpConfig.PhoneBuilder() + .setWhitelistedCountries(new ArrayList()) + .build(); + } + + @Test(expected = NullPointerException.class) + public void testPhoneBuilder_passNullForWhitelistedCountries_expectNullPointerException() { + new IdpConfig.PhoneBuilder() + .setWhitelistedCountries(null) + .build(); + } + + + @Test(expected = IllegalArgumentException.class) + public void testPhoneBuilder_passEmptyListForBlacklistedCountries_expectIllegalArgumentException() { + new IdpConfig.PhoneBuilder() + .setBlacklistedCountries(new ArrayList()) + .build(); + } + + @Test(expected = NullPointerException.class) + public void testPhoneBuilder_passNullForBlacklistedCountries_expectNullPointerException() { + new IdpConfig.PhoneBuilder() + .setBlacklistedCountries(null) + .build(); + } } diff --git a/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java b/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java deleted file mode 100644 index b2a49e821..000000000 --- a/auth/src/test/java/com/firebase/ui/auth/ui/phone/CountryListLoadTaskTests.java +++ /dev/null @@ -1,436 +0,0 @@ -/* - * Copyright (C) 2015 Twitter, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Modifications copyright (C) 2017 Google Inc - */ - -package com.firebase.ui.auth.ui.phone; - -import com.firebase.ui.auth.data.client.CountryListLoadTask; -import com.firebase.ui.auth.data.model.CountryInfo; -import com.firebase.ui.auth.util.data.PhoneNumberUtils; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.concurrent.ExecutionException; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -@RunWith(RobolectricTestRunner.class) -public class CountryListLoadTaskTests { - private static final ArrayList COUNTRY_LIST = new ArrayList<>(); - - static { - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AF"), 93)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AX"), 358)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AL"), 355)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "DZ"), 213)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AS"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AD"), 376)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AO"), 244)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AI"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AG"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AR"), 54)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AM"), 374)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AW"), 297)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AC"), 247)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AU"), 61)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AT"), 43)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AZ"), 994)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BS"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BH"), 973)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BD"), 880)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BB"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BY"), 375)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BE"), 32)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BZ"), 501)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BJ"), 229)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BM"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BT"), 975)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BO"), 591)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BA"), 387)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BW"), 267)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BR"), 55)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IO"), 246)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "VG"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BN"), 673)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BG"), 359)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BF"), 226)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BI"), 257)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KH"), 855)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CM"), 237)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CA"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CV"), 238)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BQ"), 599)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KY"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CF"), 236)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TD"), 235)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CL"), 56)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CN"), 86)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CX"), 61)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CC"), 61)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CO"), 57)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KM"), 269)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CD"), 243)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CG"), 242)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CK"), 682)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CR"), 506)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CI"), 225)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "HR"), 385)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CU"), 53)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CW"), 599)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CY"), 357)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CZ"), 420)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "DK"), 45)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "DJ"), 253)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "DM"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "DO"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TL"), 670)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "EC"), 593)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "EG"), 20)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SV"), 503)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GQ"), 240)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ER"), 291)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "EE"), 372)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ET"), 251)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "FK"), 500)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "FO"), 298)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "FJ"), 679)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "FI"), 358)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "FR"), 33)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GF"), 594)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PF"), 689)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GA"), 241)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GM"), 220)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GE"), 995)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "DE"), 49)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GH"), 233)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GI"), 350)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GR"), 30)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GL"), 299)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GD"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GP"), 590)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GU"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GT"), 502)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GG"), 44)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GN"), 224)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GW"), 245)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GY"), 592)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "HT"), 509)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "HM"), 672)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "HN"), 504)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "HK"), 852)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "HU"), 36)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IS"), 354)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IN"), 91)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ID"), 62)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IR"), 98)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IQ"), 964)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IE"), 353)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IM"), 44)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IL"), 972)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "IT"), 39)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "JM"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "JP"), 81)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "JE"), 44)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "JO"), 962)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KZ"), 7)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KE"), 254)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KI"), 686)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "XK"), 381)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KW"), 965)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KG"), 996)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LA"), 856)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LV"), 371)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LB"), 961)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LS"), 266)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LR"), 231)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LY"), 218)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LI"), 423)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LT"), 370)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LU"), 352)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MO"), 853)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MK"), 389)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MG"), 261)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MW"), 265)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MY"), 60)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MV"), 960)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ML"), 223)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MT"), 356)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MH"), 692)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MQ"), 596)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MR"), 222)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MU"), 230)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "YT"), 262)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MX"), 52)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "FM"), 691)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MD"), 373)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MC"), 377)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MN"), 976)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ME"), 382)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MS"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MA"), 212)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MZ"), 258)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MM"), 95)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NA"), 264)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NR"), 674)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NP"), 977)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NL"), 31)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NC"), 687)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NZ"), 64)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NI"), 505)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NE"), 227)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NG"), 234)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NU"), 683)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NF"), 672)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KP"), 850)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MP"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "NO"), 47)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "OM"), 968)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PK"), 92)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PW"), 680)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PS"), 970)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PA"), 507)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PG"), 675)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PY"), 595)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PE"), 51)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PH"), 63)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PL"), 48)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PT"), 351)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PR"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "QA"), 974)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "RE"), 262)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "RO"), 40)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "RU"), 7)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "RW"), 250)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "BL"), 590)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SH"), 290)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KN"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LC"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "MF"), 590)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "PM"), 508)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "VC"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "WS"), 685)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SM"), 378)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ST"), 239)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SA"), 966)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SN"), 221)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "RS"), 381)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SC"), 248)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SL"), 232)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SG"), 65)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SX"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SK"), 421)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SI"), 386)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SB"), 677)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SO"), 252)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ZA"), 27)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GS"), 500)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "KR"), 82)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SS"), 211)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ES"), 34)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "LK"), 94)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SD"), 249)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SR"), 597)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SJ"), 47)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SZ"), 268)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SE"), 46)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "CH"), 41)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "SY"), 963)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TW"), 886)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TJ"), 992)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TZ"), 255)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TH"), 66)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TG"), 228)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TK"), 690)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TO"), 676)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TT"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TN"), 216)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TR"), 90)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TM"), 993)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TC"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "TV"), 688)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "VI"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "UG"), 256)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "UA"), 380)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "AE"), 971)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "GB"), 44)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "US"), 1)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "UY"), 598)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "UZ"), 998)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "VU"), 678)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "VA"), 379)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "VE"), 58)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "VN"), 84)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "WF"), 681)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "EH"), 212)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "YE"), 967)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ZM"), 260)); - COUNTRY_LIST.add(new CountryInfo(new Locale("", "ZW"), 263)); - } - - private CountryListLoadTask mTask; - private CountryListLoadTask.Listener mListener; - - @Before - public void setUp() { - mListener = mock(CountryListLoadTask.Listener.class); - } - - @Test - public void testExecute_withoutUserInput_expectAllCountriesAdded() { - mTask = new CountryListLoadTask(null, null, mListener); - - mTask.execute(); - - try { - final List result = mTask.get(); - Collections.sort(COUNTRY_LIST); - assertEquals(COUNTRY_LIST, result); - } catch (InterruptedException e) { - fail("Should not throw InterruptedException"); - } catch (ExecutionException e) { - fail("Should not throw ExecutionException"); - } - } - - - @Test - public void testExecute_withUserWhitelistInput_expectUserCountriesAddedOnly() { - List whitelistedCountryIsos = new ArrayList<>(); - whitelistedCountryIsos.add("US"); - - List expectedOutput = new ArrayList<>(); - expectedOutput.add(new CountryInfo(new Locale("", "US"), 1)); - - mTask = new CountryListLoadTask(whitelistedCountryIsos, null, mListener); - - mTask.execute(); - - try { - final List result = mTask.get(); - Collections.sort(expectedOutput); - assertEquals(expectedOutput, result); - } catch (InterruptedException e) { - fail("Should not throw InterruptedException"); - } catch (ExecutionException e) { - fail("Should not throw ExecutionException"); - } - } - - @Test - public void testExecute_withUserBlacklistInput_expectUserCountriesRemoved() { - List blacklistedCountryIsos = new ArrayList<>(); - blacklistedCountryIsos.add("US"); - - List expectedOutput = new ArrayList<>(COUNTRY_LIST); - expectedOutput.remove(new CountryInfo(new Locale("", "US"), 1)); - - mTask = new CountryListLoadTask(null, blacklistedCountryIsos, mListener); - - mTask.execute(); - - try { - final List result = mTask.get(); - Collections.sort(expectedOutput); - assertEquals(expectedOutput, result); - } catch (InterruptedException e) { - fail("Should not throw InterruptedException"); - } catch (ExecutionException e) { - fail("Should not throw ExecutionException"); - } - } - - @Test - public void testExecute_withCodeWhitelistInput_expectUserCodeCountriesAddedOnly() { - List whitelistedCountries = new ArrayList<>(); - whitelistedCountries.add("+1"); - - List expectedOutput = new ArrayList<>(); - for (String iso : PhoneNumberUtils.getCountryIsosFromCountryCode("+1")) { - expectedOutput.add(new CountryInfo(new Locale("", iso), 1)); - } - - mTask = new CountryListLoadTask(whitelistedCountries, null, mListener); - - mTask.execute(); - - try { - final List result = mTask.get(); - Collections.sort(expectedOutput); - assertEquals(expectedOutput, result); - } catch (InterruptedException e) { - fail("Should not throw InterruptedException"); - } catch (ExecutionException e) { - fail("Should not throw ExecutionException"); - } - } - - @Test - public void testExecute_withCodeBlacklistInput_expectUserCodeCountriesRemoved() { - List blacklistedCountries = new ArrayList<>(); - blacklistedCountries.add("+1"); - - List excludedCountries = new ArrayList<>(); - for (String iso : PhoneNumberUtils.getCountryIsosFromCountryCode("+1")) { - excludedCountries.add(new CountryInfo(new Locale("", iso), 1)); - } - - List expectedOutput = new ArrayList<>(COUNTRY_LIST); - expectedOutput.removeAll(excludedCountries); - - mTask = new CountryListLoadTask(null, blacklistedCountries, mListener); - - mTask.execute(); - - try { - final List result = mTask.get(); - Collections.sort(expectedOutput); - assertEquals(expectedOutput, result); - } catch (InterruptedException e) { - fail("Should not throw InterruptedException"); - } catch (ExecutionException e) { - fail("Should not throw ExecutionException"); - } - } - - - @Test - public void testOnPostExecute_nullListener() { - mTask = new CountryListLoadTask(null, null, null); - try { - mTask.onPostExecute(COUNTRY_LIST); - } catch (NullPointerException ex) { - fail("Should not throw NullPointerException"); - } - } - - @Test - public void testOnPostExecute() { - mTask = new CountryListLoadTask(null, null, mListener); - mTask.onPostExecute(COUNTRY_LIST); - verify(mListener).onLoadComplete(COUNTRY_LIST); - } -}