Skip to content

Commit

Permalink
Fix issue #101.
Browse files Browse the repository at this point in the history
  • Loading branch information
ragunathjawahar committed Aug 2, 2015
1 parent cdf0859 commit 7907d40
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2015 Mobs & Geeks
*
* 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.
*/

package com.mobsandgeeks.saripaar.tests;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

import com.mobsandgeeks.saripaar.tests.ui.QuickRuleOnlyControllerActivity;

public class QuickRuleOnlyControllerTest
extends ActivityInstrumentationTestCase2<QuickRuleOnlyControllerActivity> {

private TextView mResultTextView;

@Override
protected void setUp() throws Exception {
super.setUp();
mResultTextView = (TextView) getActivity().findViewById(R.id.resultTextView);
}

public void testInputNot1_failure() {
EspressoHelper.type(R.id.oneOnlyEditText, "100");
EspressoHelper.clickView(R.id.saripaarButton);
EspressoHelper.checkForText(Constants.STATE_FAILURE, mResultTextView);
}

public void testInput1_success() {
EspressoHelper.type(R.id.oneOnlyEditText, "1");
EspressoHelper.clickView(R.id.saripaarButton);
EspressoHelper.checkForText(Constants.STATE_SUCCESS, mResultTextView);
}

public QuickRuleOnlyControllerTest() {
super(QuickRuleOnlyControllerActivity.class);
}
}
5 changes: 4 additions & 1 deletion saripaar-tests/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@
android:label="@string/title_activity_quick_rule" />
<activity
android:name=".ui.QuickRuleOnlyActivity"
android:label="@string/title_activity_quick_rule" />
android:label="@string/title_activity_quick_rule_only" />
<activity
android:name=".ui.QuickRuleOnlyControllerActivity"
android:label="@string/title_activity_quick_rule_only_controller" />
<activity
android:name=".ui.ConfirmPasswordWithPasswordActivity"
android:label="@string/title_activity_confirm_password" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (C) 2015 Mobs & Geeks
*
* 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.
*/

package com.mobsandgeeks.saripaar.tests.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mobsandgeeks.saripaar.QuickRule;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.tests.R;

import java.util.List;


public class QuickRuleOnlyControllerActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set content view
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.activity_quick_rule_only, null);
setContentView(view);

// Controller
new Controller(view);
}

static class Controller implements View.OnClickListener, Validator.ValidationListener {
// UI
private EditText mOneOnlyEditText;
private TextView mResultTextView;
private Button mSaripaarButton;

// Attributes
private Validator mValidator;

Controller(final View rootView) {
// UI
mOneOnlyEditText = (EditText) rootView.findViewById(R.id.oneOnlyEditText);
mResultTextView = (TextView) rootView.findViewById(R.id.resultTextView);
mSaripaarButton = (Button) rootView.findViewById(R.id.saripaarButton);

// Validation
mValidator = new Validator(this);
mValidator.setValidationListener(this);

// Add a quick rule
mValidator.put(mOneOnlyEditText, new QuickRule<TextView>() {

@Override
public boolean isValid(TextView textView) {
return "1".equals(textView.getText().toString());
}

@Override
public String getMessage(Context context) {
return "Enter 1, nothing else.";
}
});

// Event listeners
mSaripaarButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
try {
mValidator.validate();
} catch (IllegalStateException e) {
mResultTextView.setText(R.string.crash);
}
}

@Override
public void onValidationSucceeded() {
mResultTextView.setText(R.string.success);
}

@Override
public void onValidationFailed(List<ValidationError> errors) {
mResultTextView.setText(R.string.failure);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/root">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Saripaar!"
android:id="@+id/saripaarButton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>

<EditText
android:id="@+id/oneOnlyEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="1 Only"
android:ems="10"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/oneOnlyEditText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/saripaarButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center"/>

</RelativeLayout>
2 changes: 2 additions & 0 deletions saripaar-tests/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<string name="title_activity_custom_annotation">Custom Annotation</string>
<string name="title_activity_custom_annotation_with_adapter">Custom Annotation with Adapter</string>
<string name="title_activity_quick_rule">Quick Rule</string>
<string name="title_activity_quick_rule_only">Quick Rule Only</string>
<string name="title_activity_quick_rule_only_controller">Quick Rule Controller</string>
<string name="title_activity_confirm_password">Confirm Password</string>
<string name="title_activity_remove_rules">Remove Rules</string>

Expand Down
3 changes: 3 additions & 0 deletions saripaar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ android {
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 8
targetSdkVersion 21
Expand Down
19 changes: 19 additions & 0 deletions saripaar/src/main/java/com/mobsandgeeks/saripaar/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

package com.mobsandgeeks.saripaar;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Pair;
Expand Down Expand Up @@ -157,6 +160,18 @@ public Validator(final Object controller) {
mValidationMode = Mode.BURST;
mSequenceComparator = new SequenceComparator();
mViewValidatedAction = new DefaultViewValidatedAction();

// Instantiate a ValidationContext
if (controller instanceof Activity) {
mValidationContext = new ValidationContext((Activity) controller);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
&& controller instanceof Fragment) {
Activity activity = ((Fragment) controller).getActivity();
mValidationContext = new ValidationContext(activity);
}
// Else, lazy init ValidationContext in #getRuleAdapterPair(Annotation, Field)
// or void #put(VIEW, QuickRule<VIEW>) by obtaining a Context from one of the
// View instances.
}

/**
Expand Down Expand Up @@ -397,6 +412,10 @@ public <VIEW extends View> void put(final VIEW view, final QuickRule<VIEW>... qu
throw new IllegalArgumentException("'quickRules' cannot be empty.");
}

if (mValidationContext == null) {
mValidationContext = new ValidationContext(view.getContext());
}

// Create rules
createRulesSafelyAndLazily(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* Copyright (C) 2015 Mobs & Geeks
*
* 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.
*/

package com.mobsandgeeks.saripaar.adapter;

import android.view.View;
Expand Down

0 comments on commit 7907d40

Please sign in to comment.