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	21 May 2009 08:05:04 -0000
@@ -126,7 +126,6 @@
    */
   protected $httpauth_credentials = NULL;
 
-
   /**
    * Constructor for DrupalWebTestCase.
    *
@@ -607,7 +606,7 @@
 
     // Make sure type is valid.
     if (in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'))) {
-     // Use original file directory instead of one created during setUp().
+      // Use original file directory instead of one created during setUp().
       $path = $this->originalFileDirectory . '/simpletest';
       $files = file_scan_directory($path, '/' . $type . '\-.*/');
 
@@ -904,6 +903,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();
@@ -947,6 +950,13 @@
    */
   protected function tearDown() {
     global $db_prefix, $user;
+
+    $emailCount = count(variable_get('simpletest_emails', array()));
+    if ($emailCount) {
+      $message = format_plural($emailCount, t('!count e-mail was sent during this test case.'), t('!count e-mails were sent during this test case.'), array('!count' => $emailCount));
+      $this->pass($message, t('E-mail'));
+    }
+
     if (preg_match('/simpletest\d+/', $db_prefix)) {
       // Delete temporary files directory and reset files directory path.
       file_unmanaged_delete_recursive(file_directory_path());
@@ -1647,6 +1657,13 @@
   }
 
   /**
+   * Gets an array containing all e-mails sent during this test case.
+   */
+  protected function drupalGetMails() {
+    return variable_get('simpletest_emails', array());
+  }
+
+  /**
    * Sets the raw HTML content. This can be useful when a page has been fetched
    * outside of the internal browser and assertions need to be made on the
    * returned page.
@@ -2144,5 +2161,35 @@
     $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'));
   }
+
+  /**
+   * Assert that the most recently sent e-mail message has a field with the given value.
+   *
+   * @param $name
+   *   Name of field or message property to assert. Examples: subject, body, id, ...
+   * @param $value
+   *   Value of the field to assert.
+   * @param $message
+   *   Message to display.
+   * @return
+   *   TRUE on pass, FALSE on fail.
+   */
+  protected function assertMail($name, $value = '', $message = '') {
+    $email = end(variable_get('simpletest_emails', array()));
+    return $this->assertTrue(isset($email[$name]) && $email[$name] == $value, $message, t('E-mail'));
+  }
 }
 
+/**
+ * Wrapper function to override the default mail handler function.
+ *
+ * @param array $message
+ * @return
+ */
+function drupal_mail_wrapper($message) {
+  $captured_emails = variable_get('simpletest_emails', array());
+  $captured_emails[] = $message;
+  variable_set('simpletest_emails', $captured_emails);
+
+  return TRUE;
+}
\ 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,36 @@
+<?php
+class DrupalMailWrapperTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('E-mail'),
+      'description' => t('Test drupal_mail_wrapper.'),
+      'group' => t('E-mail'),
+    );
+  }
+
+  /**
+   * Test to see if the wrapper function is executed correctly.
+   */
+  function testMailSend() {
+    // Create an e-mail.
+    $subject = $this->randomString(64);
+    $body = $this->randomString(128);
+    $message = array(
+      'id' => 'drupal_mail_text',
+      'headers' => array('Content-type'=> 'text/html'),
+      'subject' => $subject,
+      'to' => 'foobar@example.com',
+      'body' => $body,
+    );
+
+    // Send the e-mail.
+    $response = drupal_mail_send($message);
+
+    // Assert that the e-mail was sent.
+    $this->assertMail('subject', $subject, t('Confirm that the e-mail was sent.'));
+    $this->assertMail('body', $body, t('Confirm the body of the e-mail.'));
+  }
+}
