diff --git includes/form.inc includes/form.inc
index 00d9061..5f84c54 100644
--- includes/form.inc
+++ includes/form.inc
@@ -1042,6 +1042,13 @@ function form_builder($form_id, $element, &$form_state) {
     else {
       $form_state['process_input'] = FALSE;
     }
+    // In addition to submitted form values,
+    // _form_builder_handle_input_elements() automatically takes over stored
+    // form values upon rebuild. If there was a validation error, prevent this
+    // from happening.
+    if (form_get_errors()) {
+      $form_state['values'] = array();
+    }
   }
 
   if (!isset($element['#id'])) {
@@ -1167,19 +1174,31 @@ function _form_builder_handle_input_element($form_id, &$element, &$form_state) {
     $value_callback = !empty($element['#value_callback']) ? $element['#value_callback'] : 'form_type_' . $element['#type'] . '_value';
 
     if ($form_state['programmed'] || ($form_state['process_input'] && (!isset($element['#access']) || $element['#access']))) {
+      // Check for a submitted value.
       $input = $form_state['input'];
       foreach ($element['#parents'] as $parent) {
         $input = isset($input[$parent]) ? $input[$parent] : NULL;
       }
+      // Check for a stored value.
+      $value = $form_state['values'];
+      foreach ($element['#parents'] as $parent) {
+        $value = isset($value[$parent]) ? $value[$parent] : NULL;
+      }
       // If we have input for the current element, assign it to the #value property.
-      if (!$form_state['programmed'] || isset($input)) {
-        // Call #type_value to set the form value;
-        if (function_exists($value_callback)) {
+      if (!$form_state['programmed'] || isset($input) || isset($value)) {
+        // Call the value callback to set the form element value.
+        if (isset($input) && function_exists($value_callback)) {
           $element['#value'] = $value_callback($element, $input, $form_state);
         }
+        // If we do not have a #value yet, check whether we have fresh input.
         if (!isset($element['#value']) && isset($input)) {
           $element['#value'] = $input;
         }
+        // If no #value was assigned through submitted values, check whether we
+        // have a value in $form_state.
+        if (!isset($element['#value']) && isset($value)) {
+          $element['#value'] = $value;
+        }
       }
       // Mark all posted values for validation.
       if (isset($element['#value']) || (!empty($element['#required']))) {
diff --git modules/simpletest/drupal_web_test_case.php modules/simpletest/drupal_web_test_case.php
index 57e95ed..71514fc 100644
--- modules/simpletest/drupal_web_test_case.php
+++ modules/simpletest/drupal_web_test_case.php
@@ -1500,6 +1500,84 @@ class DrupalWebTestCase extends DrupalTestCase {
     }
   }
 
+
+  /**
+   * Execute a POST request on an AJAX path (normally system/ajax).
+   *
+   * It will be done as usual POST request with SimpleBrowser.
+   *
+   * @param $path
+   *   Location of the post form. A Drupal path.
+   * @param  $edit
+   *   Field data in an associative array. Changes the current input fields
+   *   (where possible) to the values indicated. A checkbox can be set to
+   *   TRUE to be checked and FALSE to be unchecked. Note that when a form
+   *   contains file upload fields, other fields cannot start with the '@'
+   *   character.
+   *   Multiple select fields can be set using name[] and setting each of the
+   *   possible values. Example:
+   *   @code
+   *     $edit = array();
+   *     $edit['name[]'] = array('value1', 'value2');
+   *   @endcode
+   * @param $triggering_element
+   *   Name of the element that triggered the AJAX operation.
+   * @param $ajax_path
+   *   Path to which to post the AJAX request.
+   * @param $options
+   *   Options to be forwarded to url().
+   * @param $headers
+   *   An array containing additional HTTP request headers, each formatted as
+   *   "name: value".
+   */
+  protected function drupalAJAXPost($path, $edit, $triggering_element, $ajax_path = 'system/ajax', array $options = array(), array $headers = array()) {
+    $submit_matches = FALSE;
+    if (isset($path)) {
+      $html = $this->drupalGet($path, $options);
+    }
+
+    if ($this->parse()) {
+      $edit_save = $edit;
+      // Let's iterate over all the forms.
+      $forms = $this->xpath('//form');
+      foreach ($forms as $form) {
+        // We try to set the fields of this form as specified in $edit.
+        $edit = $edit_save;
+        $post = array();
+        $upload = array();
+        $this->handleForm($post, $edit, $upload, NULL, $form);
+        $action = $this->getAbsoluteUrl($ajax_path);
+        $post_array = $post;
+        foreach ($post as $key => $value) {
+          // Encode according to application/x-www-form-urlencoded
+          // Both names and values needs to be urlencoded, according to
+          // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
+          $post[$key] = urlencode($key) . '=' . urlencode($value);
+        }
+        $post['ajax_triggering_element'] = 'ajax_triggering_element=' . urlencode($triggering_element);
+        $post = implode('&', $post);
+        $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post, CURLOPT_HTTPHEADER => $headers));
+        // Ensure that any changes to variables in the other thread are picked up.
+        $this->refreshVariables();
+
+        // Replace original page output with new output from redirected page(s).
+        if (($new = $this->checkForMetaRefresh())) {
+          $out = $new;
+        }
+        $this->verbose('AJAX POST request to: ' . $ajax_path .
+                       '<hr />Ending URL: ' . $this->getUrl() .
+                       '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE) .
+                       '<hr />' . $out);
+        return json_decode($out, TRUE);
+
+      }
+      // We have not found a form which contained all fields of $edit.
+      foreach ($edit as $name => $value) {
+        $this->fail(t('Failed to set field @name to @value', array('@name' => $name, '@value' => $value)));
+      }
+    }
+  }
+
   /**
    * Runs cron in the Drupal installed by Simpletest.
    */
diff --git modules/simpletest/tests/ajax.test modules/simpletest/tests/ajax.test
index c70e7e4..f51848e 100644
--- modules/simpletest/tests/ajax.test
+++ modules/simpletest/tests/ajax.test
@@ -78,3 +78,46 @@ class AJAXCommandsTestCase extends AJAXTestCase {
   }
 }
 
+/**
+ * Test that $form_state['values'] is properly delivered to $ajax['callback'].
+ */
+class AJAXFormValuesTestCase extends AJAXTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'AJAX Form Values in command',
+      'description' => 'Creates a form, then POSTS to system/ajax to change the form via pseudo-ajax.',
+      'group' => 'AJAX',
+    );
+  }
+
+  /**
+   * Create a simple form with a select, then POST a change to it.
+   */
+  function testSimpleAJAXFormValue() {
+    $web_user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($web_user);
+
+    $form_path = 'ajax_forms_test_get_form';
+
+    $selects_to_test = array('red', 'green', 'blue');
+    foreach($selects_to_test as $item) {
+      $edit = array();
+      $edit['select'] = $item;
+      $commands = $this->drupalAJAXPost($form_path, $edit, 'select');
+      $data_command = $commands[2];
+      $this->assertEqual($data_command['value'], $edit['select']);
+    }
+
+    $values_to_test = array(FALSE, TRUE);
+    foreach($values_to_test as $item) {
+      $edit = array();
+      $edit['checkbox'] = $item;
+      $commands = $this->drupalAJAXPost($form_path, $edit, 'checkbox');
+      $data_command = $commands[2];
+      $value_expected = (int) $item;
+      $value_received = (int) $data_command['value'];
+      $this->assertEqual($value_received, $value_expected);
+    }
+  }
+}
+
diff --git modules/simpletest/tests/ajax_test.module modules/simpletest/tests/ajax_test.module
index d608464..f279e54 100644
--- modules/simpletest/tests/ajax_test.module
+++ modules/simpletest/tests/ajax_test.module
@@ -22,6 +22,14 @@ function ajax_test_menu() {
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
   );
+
+  $items['ajax_forms_test_get_form'] = array(
+    'title' => 'Simple AJAX form test',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ajax_forms_test_simple_form'),
+    'access callback' => TRUE,
+  );
+
   return $items;
 }
 
@@ -57,3 +65,51 @@ function ajax_test_render_error() {
   ajax_render_error($message);
 }
 
+/**
+ * A basic form used to test form_state['values'] during callback.
+ */
+function ajax_forms_test_simple_form($form, &$form_state) {
+  $form['select'] = array(
+    '#type' => 'select',
+    '#options' => array('red' => 'red', 'green' => 'green', 'blue' => 'blue'),
+    '#ajax' => array(
+      'callback' => 'ajax_forms_test_simple_form_select_callback',
+    ),
+    '#suffix' => "<div id='ajax_selected_color'>No color yet selected</div>",
+  );
+
+  $form['checkbox'] = array(
+    '#type' => 'checkbox',
+    '#title' => 'Test checkbox',
+    '#ajax' => array(
+       'callback' => 'ajax_forms_test_simple_form_checkbox_callback',
+    ),
+    '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Save',
+  );
+  return $form;
+}
+
+/**
+ * Form element value callback for 'select' element in ajax_forms_test_simple_form().
+ */
+function ajax_forms_test_simple_form_select_callback($form, $form_state) {
+  $commands = array();
+  $commands[] = ajax_command_html('#ajax_selected_color', $form_state['values']['select']);
+  $commands[] = ajax_command_data('#ajax_selected_color', 'form_state_value_select', $form_state['values']['select']);
+  return array('#type' => 'ajax_commands', '#ajax_commands' => $commands);
+}
+
+/**
+ * Form element value callback for 'checkbox' element in ajax_forms_test_simple_form().
+ */
+function ajax_forms_test_simple_form_checkbox_callback($form, $form_state) {
+  $commands = array();
+  $commands[] = ajax_command_html('#ajax_checkbox_value', (int) $form_state['values']['checkbox']);
+  $commands[] = ajax_command_data('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']);
+  return array('#type' => 'ajax_commands', '#ajax_commands' => $commands);
+}
+
