in unit/BasicSample/app/src/main/java/com/example/android/testing/unittesting/BasicSample/MainActivity.java [95:121]
public void onSaveClick(View view) {
// Don't save if the fields do not validate.
if (!mEmailValidator.isValid()) {
mEmailText.setError("Invalid email");
Log.w(TAG, "Not saving personal information: Invalid email");
return;
}
// Get the text from the input fields.
String name = mNameText.getText().toString();
Calendar dateOfBirth = Calendar.getInstance();
dateOfBirth.set(mDobPicker.getYear(), mDobPicker.getMonth(), mDobPicker.getDayOfMonth());
String email = mEmailText.getText().toString();
// Create a Setting model class to persist.
SharedPreferenceEntry sharedPreferenceEntry =
new SharedPreferenceEntry(name, dateOfBirth, email);
// Persist the personal information.
boolean isSuccess = mSharedPreferencesHelper.savePersonalInfo(sharedPreferenceEntry);
if (isSuccess) {
Toast.makeText(this, "Personal information saved", Toast.LENGTH_LONG).show();
Log.i(TAG, "Personal information saved");
} else {
Log.e(TAG, "Failed to write personal information to SharedPreferences");
}
}