diff --git a/core/lib/Drupal/Core/Extension/ThemeHandler.php b/core/lib/Drupal/Core/Extension/ThemeHandler.php
index fbd42c5..99e9d4e 100644
--- a/core/lib/Drupal/Core/Extension/ThemeHandler.php
+++ b/core/lib/Drupal/Core/Extension/ThemeHandler.php
@@ -125,10 +125,32 @@ class ThemeHandler implements ThemeHandlerInterface {
   /**
    * {@inheritdoc}
    */
+  public function getDefault() {
+    $this->configFactory->get('system.theme')->get('default');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setDefault($theme) {
+    $available_themes = $this->listInfo();
+    if (!isset($available_themes[$key])) {
+      throw new \Exception("Theme $key does not exists.");
+    }
+    $this->configFactory
+        ->get('system.theme')
+        ->set('default', $theme)
+        ->save();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function enable(array $theme_list) {
     $this->clearCssCache();
     $theme_config = $this->configFactory->get('system.theme');
     $disabled_themes = $this->configFactory->get('system.theme.disabled');
+    $available_themes = $this->listInfo();
     foreach ($theme_list as $key) {
       // Throw an exception if the theme name is too long.
       if (strlen($key) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) {
@@ -137,6 +159,9 @@ class ThemeHandler implements ThemeHandlerInterface {
           '@max' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
         )));
       }
+      if (!isset($available_themes[$key])) {
+        throw new \Exception("Theme $key does not exists.");
+      }
 
       // The value is not used; the weight is ignored for themes currently.
       $theme_config->set("enabled.$key", 0)->save();
@@ -160,7 +185,7 @@ class ThemeHandler implements ThemeHandlerInterface {
    */
   public function disable(array $theme_list) {
     // Don't disable the default theme.
-    if ($pos = array_search($this->configFactory->get('system.theme')->get('default'), $theme_list) !== FALSE) {
+    if ($pos = array_search($this->getDefault(), $theme_list) !== FALSE) {
       unset($theme_list[$pos]);
       if (empty($theme_list)) {
         return;
@@ -171,7 +196,11 @@ class ThemeHandler implements ThemeHandlerInterface {
 
     $theme_config = $this->configFactory->get('system.theme');
     $disabled_themes = $this->configFactory->get('system.theme.disabled');
+    $available_themes = $this->listInfo();
     foreach ($theme_list as $key) {
+      if (!isset($available_themes[$key])) {
+        throw new \Exception("Theme $key does not exists.");
+      }
       // The value is not used; the weight is ignored for themes currently.
       $theme_config->clear("enabled.$key");
       $disabled_themes->set($key, 0);
diff --git a/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php
index eef3381..7bf972e 100644
--- a/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php
+++ b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php
@@ -13,6 +13,18 @@
 interface ThemeHandlerInterface {
 
   /**
+   * Get the default theme key.
+   */
+  public function getDefault();
+
+  /**
+   * Set the default theme to the given key.
+   *
+   * @throws \Exception
+   */
+  public function setDefault($theme);
+
+  /**
    * Enables a given list of themes.
    *
    * @param array $theme_list
@@ -28,6 +40,8 @@
    *
    * @param array $theme_list
    *   An array of theme names.
+   *
+   * @throws \Exception
    */
   public function disable(array $theme_list);
 
diff --git a/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php b/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php
index 4066b16..fdd3289 100644
--- a/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php
+++ b/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php
@@ -81,7 +81,7 @@ class AjaxBasePageNegotiator implements ThemeNegotiatorInterface {
       // allowed to see the default theme, token validation isn't required for
       // that, and bypassing it allows most use-cases to work even when accessed
       // from the page cache.
-      if ($theme === $this->configFactory->get('system.theme')->get('default') || $this->csrfGenerator->validate($token, $theme)) {
+      if ($theme === \Drupal::service('theme_handler')->getDefault() || $this->csrfGenerator->validate($token, $theme)) {
         return $theme;
       }
     }
diff --git a/core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php b/core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php
index 717cee5..957007b 100644
--- a/core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php
+++ b/core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php
@@ -123,7 +123,10 @@ class ThemeHandlerTest extends UnitTestCase {
    * @see \Drupal\Core\Extension\ThemeHandler::enable()
    */
   public function testEnableSingleTheme() {
-    $theme_list = array('theme_test');
+    $name = 'theme_test';
+    $mock = $this->getMockTheme($name);
+    $this->themeHandler->systemList[$name] = $mock;
+    $theme_list = array($name);
 
     $this->configFactory->get('system.theme')
       ->expects($this->once())
@@ -188,24 +191,21 @@ class ThemeHandlerTest extends UnitTestCase {
       ->method('scan')
       ->will($this->returnValue(array()));
 
-    $this->themeHandler->enable(array('bartik'));
-    $this->themeHandler->systemList['bartik'] = (object) array(
-      'name' => 'bartik',
-      'info' => array(
-        'stylesheets' => array(
-          'all' => array(
-            'css/layout.css',
-            'css/style.css',
-            'css/colors.css',
-          ),
-        ),
-        'scripts' => array(
-          'example' => 'theme.js',
-        ),
-        'engine' => 'twig',
-        'base theme' => 'stark',
+    // Setup a mocked bartik theme.
+    $mock = $this->getMockTheme('bartik', 'stark');
+    $mock->info['stylesheets'] = array(
+      'all' => array(
+        'css/layout.css',
+        'css/style.css',
+        'css/colors.css',
       ),
     );
+    $mock->info['scripts'] = array(
+      'example' => 'theme.js',
+    );
+    $mock->info['engine'] = 'twig';
+    $this->themeHandler->systemList['bartik'] = $mock;
+    $this->themeHandler->enable(array('bartik'));
 
     $list_info = $this->themeHandler->listInfo();
     $this->assertCount(1, $list_info);
@@ -216,18 +216,9 @@ class ThemeHandlerTest extends UnitTestCase {
     $this->assertEquals('stark', $list_info['bartik']->base_theme);
     $this->assertEquals(0, $list_info['bartik']->status);
 
-    $this->themeHandler->systemList['seven'] = (object) array(
-      'name' => 'seven',
-      'info' => array(
-        'stylesheets' => array(
-          'screen' => array(
-            'style.css',
-          ),
-        ),
-        'scripts' => array(),
-      ),
-      'status' => 1,
-    );
+    $mock = $this->getMockTheme('seven');
+    $mock->info['status'] = 1;
+    $this->themeHandler->systemList['seven'] = $mock;
 
     $this->themeHandler->enable(array('seven'));
 
@@ -307,6 +298,32 @@ class ThemeHandlerTest extends UnitTestCase {
   }
 
   /**
+   * Creates a Theme mock opject.
+   *
+   * @param string $name
+   *   Theme name.
+   * @param string $base_theme
+   *   Base theme name.
+   * @return object
+   *   Info mock for a theme.
+   */
+  public function getMockTheme($name, $base_theme = NULL) {
+    $theme = array(
+      'name' => $name,
+      'info' => array(
+        'name' => $name,
+        'stylesheets' => array(),
+        'scripts' => array(),
+      ),
+      'status' => 0,
+    );
+    if (!is_null($base_theme)) {
+      $theme['info']['base theme'] = $base_theme;
+    }
+    return (object) $theme;
+  }
+
+  /**
    * Provides test data for testGetBaseThemes.
    *
    * @return array
@@ -316,67 +333,40 @@ class ThemeHandlerTest extends UnitTestCase {
     $data = array();
 
     // Tests a theme without any base theme.
-    $themes = array();
-    $themes['test_1'] = (object) array(
-      'name' => 'test_1',
-      'info' => array(
-        'name' => 'test_1',
+    $data[] = array(
+      array(
+        'test_1' => $this->getMockTheme('test_1'),
       ),
+      'test_1',
+      array()
     );
-    $data[] = array($themes, 'test_1', array());
 
     // Tests a theme with a non existing base theme.
-    $themes = array();
-    $themes['test_1'] = (object) array(
-      'name' => 'test_1',
-      'info' => array(
-        'name' => 'test_1',
-        'base theme' => 'test_2',
+    $data[] = array(
+      array(
+        'test_1' => $this->getMockTheme('test_1', 'test_2'),
       ),
+      'test_1',
+      array('test_2' => NULL)
     );
-    $data[] = array($themes, 'test_1', array('test_2' => NULL));
 
     // Tests a theme with a single existing base theme.
-    $themes = array();
-    $themes['test_1'] = (object) array(
-      'name' => 'test_1',
-      'info' => array(
-        'name' => 'test_1',
-        'base theme' => 'test_2',
-      ),
-    );
-    $themes['test_2'] = (object) array(
-      'name' => 'test_2',
-      'info' => array(
-        'name' => 'test_2',
+    $data[] = array(
+      array(
+        'test_1' => $this->getMockTheme('test_1', 'test_2'),
+        'test_2' => $this->getMockTheme('test_2'),
       ),
+      'test_1',
+      array('test_2' => 'test_2')
     );
-    $data[] = array($themes, 'test_1', array('test_2' => 'test_2'));
 
     // Tests a theme with multiple base themes.
-    $themes = array();
-    $themes['test_1'] = (object) array(
-      'name' => 'test_1',
-      'info' => array(
-        'name' => 'test_1',
-        'base theme' => 'test_2',
-      ),
-    );
-    $themes['test_2'] = (object) array(
-      'name' => 'test_2',
-      'info' => array(
-        'name' => 'test_2',
-        'base theme' => 'test_3',
-      ),
-    );
-    $themes['test_3'] = (object) array(
-      'name' => 'test_3',
-      'info' => array(
-        'name' => 'test_3',
-      ),
-    );
     $data[] = array(
-      $themes,
+      array(
+        'test_1' => $this->getMockTheme('test_1', 'test_2'),
+        'test_2' => $this->getMockTheme('test_2', 'test_3'),
+        'test_3' => $this->getMockTheme('test_3'),
+      ),
       'test_1',
       array('test_2' => 'test_2', 'test_3' => 'test_3'),
     );
@@ -416,6 +406,7 @@ class TestThemeHandler extends ThemeHandler {
    * {@inheritdoc}
    */
   protected function systemListReset() {
+
   }
 
 }
