Index: modules/simpletest/drupal_web_test_case.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v
retrieving revision 1.103
diff -u -r1.103 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	3 May 2009 20:01:11 -0000	1.103
+++ modules/simpletest/drupal_web_test_case.php	11 May 2009 06:59:47 -0000
@@ -126,6 +126,10 @@
    */
   protected $httpauth_credentials = NULL;
 
+  /**
+   * An array of emails sent during this test case.
+   */
+  public static $capturedEmails = array();
 
   /**
    * Constructor for DrupalWebTestCase.
@@ -904,6 +908,10 @@
     variable_set('clean_url', $clean_url_original);
     variable_set('site_mail', 'simpletest@example.com');
 
+    // Make sure our drupal_mail_wrapper function is called instead of the
+    // default mail handler.
+    variable_set('smtp_library', drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php');
+
     // Use temporary files directory with the same prefix as database.
     variable_set('file_directory_path', $this->originalFileDirectory . '/' . $db_prefix);
     $directory = file_directory_path();
@@ -2144,5 +2152,71 @@
     $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
     return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
   }
+
+  /**
+   * Pass if the given subject matches the subject of the most recently sent email.
+   *
+   * @param $subject
+   *   The subject of the email that was sent.
+   * @param $message
+   *   Message to display.
+   * @param $group
+   *   The group this message belongs to.
+   * @return
+   *   TRUE on pass, FALSE on fail.
+   */
+  protected function assertMailSubject($subject, $message, $group = 'Other') {
+    $email = end(DrupalWebTestCase::$capturedEmails);
+    return $this->assertTrue($email['subject'] === $subject, $message, $group);
+  }
+
+  /**
+   * Pass if the text is found within the body of the most recently sent email.
+   *
+   * @param $text
+   *   Plain text to look for.
+   * @param $message
+   *   Message to display.
+   * @param $group
+   *   The group this message belongs to, defaults to 'Other'.
+   * @return
+   *   TRUE on pass, FALSE on fail.
+   */
+  protected function assertMailBodyText($text, $message = '', $group = 'Other') {
+    if (!$message) {
+      $message = t('"@text" found', array('@text' => $text));
+    }
+    $email = end(DrupalWebTestCase::$capturedEmails);
+    return $this->assert((strpos($email['body'], $text) !== FALSE), $message, $group);
+  }
+
+  /**
+   * Pass if the text is not found within the body of the most recently sent email.
+   *
+   * @param $text
+   *   Plain text to look for.
+   * @param $message
+   *   Message to display.
+   * @param $group
+   *   The group this message belongs to, defaults to 'Other'.
+   * @return
+   *   TRUE on pass, FALSE on fail.
+   */
+  protected function assertMailBodyNoText($text, $message = '', $group = 'Other') {
+  if (!$message) {
+      $message = t('"@text" not found', array('@text' => $text));
+    }
+    $email = end(DrupalWebTestCase::$capturedEmails);
+    return $this->assert((strpos($email['body'], $text) === FALSE), $message, $group);
+  }
 }
 
+/**
+ * Wrapper function to override the default mail handler function.
+ *
+ * @param array $message
+ * @return
+ */
+function drupal_mail_wrapper($message) {
+  DrupalWebTestCase::$capturedEmails[] = $message;
+}
\ No newline at end of file
Index: modules/simpletest/tests/drupal_mail.test
===================================================================
RCS file: modules/simpletest/tests/drupal_mail.test
diff -N modules/simpletest/tests/drupal_mail.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/drupal_mail.test	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,35 @@
+<?php
+class DrupalMailTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Mail'),
+      'description' => t('Test sending an email.'),
+      'group' => t('Mail'),
+    );
+  }
+
+  /**
+   * Test to see if the wrapper function is executed correctly.
+   */
+  function testMailSend() {
+    // Create a message.
+    $subject = $this->randomString(64);
+    $body = $this->randomString(128);
+    $message = array(
+      'headers' => array('Content-type'=> 'text/html'),
+      'subject' => $subject,
+      'to' => 'foobar@example.com',
+      'body' => $body,
+    );
+
+    // Send the email.
+    $response = drupal_mail_send( $message );
+
+    // Assert that the email was sent.
+    $this->assertMailSubject($subject, t('Confirm that the mail was sent.'), 'Mail');
+    $this->assertMailBodyText($body, t('Confirm the body of the email.'), 'Mail');
+  }
+}
