diff --git a/entity_view_mode.info b/entity_view_mode.info
index 51cd2df..1d77cc5 100644
--- a/entity_view_mode.info
+++ b/entity_view_mode.info
@@ -3,3 +3,4 @@ description = Allows administrators to define custom view modes for entities via
 core = 7.x
 recommends[] = field_ui
 configure = admin/config/system/entity-view-modes
+files[] = entity_view_mode.test
diff --git a/entity_view_mode.module b/entity_view_mode.module
index 58548d4..4be530c 100644
--- a/entity_view_mode.module
+++ b/entity_view_mode.module
@@ -174,6 +174,29 @@ function field_entity_view_mode_update($view_mode, $entity_type) {
   //  $view_mode['enabled_bundles'] = array_fill_keys(array_keys($entity_info['bundles']), 0);
   //}
 
+  // If the view mode machine name has changed, first fix all the bundle
+  // settings to the new machine name.
+  if (!empty($view_mode['original']) && $view_mode['machine_name'] != $view_mode['original']['machine_name']) {
+    $original_view_mode_name = $view_mode['original']['machine_name'];
+    foreach (array_keys($entity_info['bundles']) as $bundle) {
+      $settings = field_bundle_settings($entity_type, $bundle);
+      $changed = FALSE;
+      if (isset($settings['view_modes'][$original_view_mode_name])) {
+        $settings['view_modes'][$view_mode_name] = $settings['view_modes'][$original_view_mode_name];
+        unset($settings['view_modes'][$original_view_mode_name]);
+        $changed = TRUE;
+      }
+      if (isset($settings['extra_fields']['display'][$original_view_mode_name])) {
+        $settings['extra_fields']['display'][$view_mode_name] = $settings['extra_fields']['display'][$original_view_mode_name];
+        unset($settings['extra_fields']['display'][$original_view_mode_name]);
+        $changed = TRUE;
+      }
+      if ($changed) {
+        field_bundle_settings($entity_type, $bundle, $settings);
+      }
+    }
+  }
+
   if (!empty($view_mode['enabled_bundles'])) {
     foreach ($view_mode['enabled_bundles'] as $bundle => $value) {
       $bundle_settings = field_bundle_settings($entity_type, $bundle);
diff --git a/entity_view_mode.test b/entity_view_mode.test
new file mode 100644
index 0000000..0247330
--- /dev/null
+++ b/entity_view_mode.test
@@ -0,0 +1,151 @@
+<?php
+
+/**
+ * @file
+ * Test integration for the entity_view_mode module.
+ */
+
+class EntityViewModeTestHelper extends DrupalWebTestCase {
+
+  /**
+   * Overrides DrupalWebTestCase::setUp().
+   */
+  public function setUp() {
+    parent::setUp(array('entity_view_mode'));
+  }
+
+  /**
+   * Overrides DrupalWebTestCase::refreshVariables().
+   *
+   * Ensures that the entity and field view mode caches are cleared so they
+   * can be reliably checked in the test.
+   */
+  protected function refreshVariables() {
+    parent::refreshVariables();
+
+    // Clear the entity and display caches.
+    drupal_static_reset('field_view_mode_settings');
+    entity_info_cache_clear();
+  }
+
+  public function assertViewModeExists($entity_type, $view_mode) {
+    $info = entity_get_info($entity_type);
+    return $this->assertTrue(!empty($info['view modes'][$view_mode]), "View mode $view_mode found for entity type $entity_type.");
+  }
+
+  public function assertNoViewModeExists($entity_type, $view_mode) {
+    $info = entity_get_info($entity_type);
+    return $this->assertTrue(!isset($info['view modes'][$view_mode]), "View mode $view_mode not found for entity type $entity_type.");
+  }
+
+  public function getActualViewMode($entity_type, $bundle, $view_mode) {
+    $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
+    return (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
+  }
+
+  public function assertActualViewMode($entity_type, $bundle, $view_mode, $expected_view_mode) {
+    $actual_view_mode = $this->getActualViewMode($entity_type, $bundle, $view_mode);
+    return $this->assertIdentical($expected_view_mode, $actual_view_mode, "View mode $view_mode for entity type $entity_type and bundle $bundle actually uses view mode $actual_view_mode, expected $expected_view_mode.");
+  }
+
+  public function assertViewModeDefaultDisplay($entity_type, $bundle, $view_mode) {
+    return $this->assertActualViewMode($entity_type, $bundle, $view_mode, 'default');
+  }
+
+  public function assertViewModeCustomDisplay($entity_type, $bundle, $view_mode) {
+    return $this->assertActualViewMode($entity_type, $bundle, $view_mode, $view_mode);
+  }
+}
+
+class EntityViewModeFunctionalTest extends EntityViewModeTestHelper {
+  public static function getInfo() {
+    return array(
+      'name' => 'Entity view mode functionality',
+      'description' => 'Tests entity view mode module functionality.',
+      'group' => 'Entity view mode',
+    );
+  }
+
+  public function setUp() {
+    parent::setUp();
+
+    // Create an administrative user.
+    $this->admin_user = $this->drupalCreateUser(array(
+      'administer entity view modes',
+      'administer content types',
+    ));
+  }
+
+  public function testEntityViewModes() {
+    $this->drupalLogin($this->admin_user);
+    $this->drupalGet('admin/config/system/entity-view-modes');
+
+    $this->assertLinkByHref('admin/config/system/entity-view-modes/add/node');
+
+    $this->drupalGet('admin/config/system/entity-view-modes/add/node');
+
+    // Attempt to create a view mode that already is provided by core.
+    $edit = array(
+      'label' => 'Custom teaser',
+      'machine_name' => 'teaser',
+    );
+    $this->drupalPost(NULL, $edit, 'Save');
+    $this->assertText('The machine-readable name is already in use. It must be unique.');
+
+    // Save a new view mode for real.
+    $edit['label'] = 'Custom 1';
+    $edit['machine_name'] = 'custom_1';
+    $edit['enabled_bundles[article]'] = TRUE;
+    $edit['enabled_bundles[page]'] = FALSE;
+    $this->drupalPost(NULL, $edit, 'Save');
+    $this->assertViewModeExists('node', 'custom_1');
+    $this->assertViewModeCustomDisplay('node', 'article', 'custom_1');
+    $this->assertViewModeDefaultDisplay('node', 'page', 'custom_1');
+
+    // Switch the view mode from articles to pages.
+    $edit = array(
+      'enabled_bundles[article]' => FALSE,
+      'enabled_bundles[page]' => TRUE,
+    );
+    $this->drupalPost('admin/config/system/entity-view-modes/edit/node/custom_1', $edit, 'Save');
+    $this->assertText('Saved the Custom 1 node view mode.');
+    $this->assertViewModeExists('node', 'custom_1');
+    $this->assertViewModeDefaultDisplay('node', 'article', 'custom_1');
+    $this->assertViewModeCustomDisplay('node', 'page', 'custom_1');
+
+    // Save a custom value into the view mode settings to check that when a
+    // view mode's machine name is changed, that the display settings are
+    // changed as well.
+    $settings = field_bundle_settings('node', 'page');
+    $settings['view_modes']['custom_1']['testing'] = TRUE;
+    $settings['extra_fields']['display']['custom_1']['testing'] = TRUE;
+    field_bundle_settings('node', 'page', $settings);
+
+    // Rename the view mode's label and machine name.
+    $edit = array(
+      'label' => 'Custom 2',
+      'machine_name' => 'custom_2',
+    );
+    $this->drupalPost('admin/config/system/entity-view-modes/edit/node/custom_1', $edit, 'Save');
+    $this->assertText('Saved the Custom 2 node view mode.');
+    $this->assertNoViewModeExists('node', 'custom_1');
+    $this->assertViewModeDefaultDisplay('node', 'article', 'custom_1');
+    $this->assertViewModeDefaultDisplay('node', 'page', 'custom_1');
+    $this->assertViewModeExists('node', 'custom_2');
+    $this->assertViewModeDefaultDisplay('node', 'article', 'custom_2');
+    $this->assertViewModeCustomDisplay('node', 'page', 'custom_2');
+
+    // Check that our custom value was transferred to the new view mode
+    // settings.
+    $settings = field_bundle_settings('node', 'page');
+    $this->assertTrue(!isset($settings['view_modes']['custom_1']) && !isset($settings['extra_fields']['display']['custom_1']));
+    $this->assertTrue(!empty($settings['view_modes']['custom_2']['testing']) && !empty($settings['extra_fields']['display']['custom_2']['testing']));
+
+    // Delete the view mode.
+    $this->drupalPost('admin/config/system/entity-view-modes/delete/node/custom_2', array(), 'Delete');
+    $this->assertText('Deleted the Custom 2 node view mode.');
+    $this->assertNoViewModeExists('node', 'custom_2');
+    $this->assertViewModeDefaultDisplay('node', 'article', 'custom_2');
+    $this->assertViewModeDefaultDisplay('node', 'page', 'custom_2');
+  }
+}
