diff --git a/core/core.services.yml b/core/core.services.yml
index c3021b6..0b029cb 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -732,5 +732,8 @@ services:
     class: Drupal\Core\Asset\JsCollectionGrouper
   asset.js.dumper:
     class: Drupal\Core\Asset\AssetDumper
+  library.provider:
+    class: Drupal\Core\Asset\LibraryDiscovery
+    arguments: ['@cache.cache', '@module_handler', '@theme_handler']
   info_parser:
     class: Drupal\Core\Extension\InfoParser
diff --git a/core/includes/common.inc b/core/includes/common.inc
index 1a98d8a..7a1d9f8 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -2759,161 +2759,16 @@ function drupal_add_library($module, $name, $every_page = NULL) {
  *   requisite API functions; find and use a different name.
  */
 function drupal_get_library($extension, $name = NULL) {
-  $libraries = &drupal_static(__FUNCTION__, array());
-
-  if (!isset($libraries[$extension]) && ($cache = \Drupal::cache()->get('library:info:' . $extension))) {
-    $libraries[$extension] = $cache->data;
-  }
-
-  if (!isset($libraries[$extension])) {
-    $libraries[$extension] = array();
-    if ($extension === 'core') {
-      $path = 'core';
-      $extension_type = 'core';
-    }
-    else {
-      // @todo Add a $type argument OR automatically figure out the type based
-      //   on current extension data, possibly using a module->theme fallback.
-      $path = drupal_get_path('module', $extension);
-      $extension_type = 'module';
-      if (!$path) {
-        $path = drupal_get_path('theme', $extension);
-        $extension_type = 'theme';
-      }
-    }
-    $library_file = $path . '/' . $extension . '.libraries.yml';
-
-    if ($library_file && file_exists(DRUPAL_ROOT . '/' . $library_file)) {
-      $libraries[$extension] = array();
-      $parser = new Parser();
-      try {
-        $libraries[$extension] = $parser->parse(file_get_contents(DRUPAL_ROOT . '/' . $library_file));
-      }
-      catch (ParseException $e) {
-        // Rethrow a more helpful exception, since ParseException lacks context.
-        throw new \RuntimeException(sprintf('Invalid library definition in %s: %s', $library_file, $e->getMessage()), 0, $e);
-      }
-      // Allow modules to alter the module's registered libraries.
-      \Drupal::moduleHandler()->alter('library_info', $libraries[$extension], $extension);
-    }
-
-    foreach ($libraries[$extension] as $id => &$library) {
-      if (!isset($library['js']) && !isset($library['css']) && !isset($library['settings'])) {
-        throw new \RuntimeException(sprintf("Incomplete library definition for '%s' in %s", $id, $library_file));
-      }
-      $library += array('dependencies' => array(), 'js' => array(), 'css' => array());
-
-      if (isset($library['version'])) {
-        // @todo Retrieve version of a non-core extension.
-        if ($library['version'] === 'VERSION') {
-          $library['version'] = \Drupal::VERSION;
-        }
-        // Remove 'v' prefix from external library versions.
-        elseif ($library['version'][0] === 'v') {
-          $library['version'] = substr($library['version'], 1);
-        }
-      }
-
-      foreach (array('js', 'css') as $type) {
-        // Prepare (flatten) the SMACSS-categorized definitions.
-        // @todo After Asset(ic) changes, retain the definitions as-is and
-        //   properly resolve dependencies for all (css) libraries per category,
-        //   and only once prior to rendering out an HTML page.
-        if ($type == 'css' && !empty($library[$type])) {
-          foreach ($library[$type] as $category => $files) {
-            foreach ($files as $source => $options) {
-              if (!isset($options['weight'])) {
-                $options['weight'] = 0;
-              }
-              // Apply the corresponding weight defined by CSS_* constants.
-              $options['weight'] += constant('CSS_' . strtoupper($category == 'theme' ? 'skin' : $category));
-              $library[$type][$source] = $options;
-            }
-            unset($library[$type][$category]);
-          }
-        }
-        foreach ($library[$type] as $source => $options) {
-          unset($library[$type][$source]);
-          // Allow to omit the options hashmap in YAML declarations.
-          if (!is_array($options)) {
-            $options = array();
-          }
-          if ($type == 'js' && isset($options['weight']) && $options['weight'] > 0) {
-            throw new \UnexpectedValueException("The $extension/$id library defines a positive weight for '$source'. Only negative weights are allowed (but should be avoided). Instead of a positive weight, specify accurate dependencies for this library.");
-          }
-          // Unconditionally apply default groups for the defined asset files.
-          // The library system is a dependency management system. Each library
-          // properly specifies its dependencies instead of relying on a custom
-          // processing order.
-          if ($type == 'js') {
-            $options['group'] = JS_LIBRARY;
-          }
-          elseif ($type == 'css') {
-            $options['group'] = $extension_type == 'theme' ? CSS_AGGREGATE_THEME : CSS_AGGREGATE_DEFAULT;
-          }
-          // By default, all library assets are files.
-          if (!isset($options['type'])) {
-            $options['type'] = 'file';
-          }
-          if ($options['type'] == 'external') {
-            $options['data'] = $source;
-          }
-          // Determine the file asset URI.
-          else {
-            if ($source[0] === '/') {
-              // An absolute path maps to DRUPAL_ROOT / base_path().
-              if ($source[1] !== '/') {
-                $options['data'] = substr($source, 1);
-              }
-              // A protocol-free URI (e.g., //cdn.com/example.js) is external.
-              else {
-                $options['type'] = 'external';
-                $options['data'] = $source;
-              }
-            }
-            // A stream wrapper URI (e.g., public://generated_js/example.js).
-            elseif (file_valid_uri($source)) {
-              $options['data'] = $source;
-            }
-            // By default, file paths are relative to the registering extension.
-            else {
-              $options['data'] = $path . '/' . $source;
-            }
-          }
-          $options['version'] = $library['version'];
-
-          $library[$type][] = $options;
-        }
-      }
-      // @todo Introduce drupal_add_settings().
-      if (isset($library['settings'])) {
-        $library['js'][] = array(
-          'type' => 'setting',
-          'data' => $library['settings'],
-        );
-        unset($library['settings']);
-      }
-      // @todo Convert all uses of #attached[library][]=array('provider','name')
-      //   into #attached[library][]='provider/name' and remove this.
-      foreach ($library['dependencies'] as $i => $dependency) {
-        if (!is_array($dependency)) {
-          $library['dependencies'][$i] = explode('/', $dependency, 2);
-        }
-      }
-    }
-    \Drupal::cache()->set('library:info:' . $extension, $libraries[$extension], Cache::PERMANENT, array(
-      'extension' => array(TRUE, $extension),
-      'library_info' => array(TRUE),
-    ));
+  /**
+   * @var $provider \Drupal\Core\Asset\LibraryDiscoveryInterface;
+   */
+  $provider = \Drupal::service('library.provider');
+  if ($name) {
+    return $provider->getLibraryByName($extension, $name);
   }
-
-  if (isset($name)) {
-    if (!isset($libraries[$extension][$name])) {
-      $libraries[$extension][$name] = FALSE;
-    }
-    return $libraries[$extension][$name];
+  else {
+    return $provider->getLibrariesByExtension($extension);
   }
-  return $libraries[$extension];
 }
 
 /**
diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 1f88d4a..09848bd 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -469,6 +469,11 @@ function install_begin_request(&$install_state) {
       ->addArgument(new Reference('cache.cache'))
       ->addArgument(new Reference('info_parser'));
 
+    $container->register('library.provider', 'Drupal\Core\Asset\LibraryProvider')
+      ->addArgument(new Reference('cache.cache'))
+      ->addArgument(new Reference('module_handler'))
+      ->addArgument(new Reference('theme_handler'));
+
     // Overrides can not work at this point since this would cause the
     // ConfigFactory to try to load language override configuration which is not
     // supported by \Drupal\Core\Config\InstallStorage since loading a
diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php
new file mode 100644
index 0000000..9025788
--- /dev/null
+++ b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php
@@ -0,0 +1,273 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Asset\LibraryDiscovery.
+ */
+
+namespace Drupal\Core\Asset;
+
+use Drupal\Core\Cache\Cache;
+use Drupal\Core\Cache\CacheBackendInterface;
+use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drupal\Core\Extension\ThemeHandlerInterface;
+use Symfony\Component\Yaml\Exception\ParseException;
+use Symfony\Component\Yaml\Parser;
+
+
+/**
+ * Discovers available libraries in drupal.
+ */
+class LibraryDiscovery implements LibraryDiscoveryInterface {
+
+  /**
+   * Stores the library information keyed by extension
+   *
+   * @var array
+   */
+  protected $libraries;
+
+  /**
+   * The cache backend.
+   *
+   * @var \Drupal\Core\Cache\CacheBackendInterface
+   */
+  protected $cache;
+
+  /**
+   * The module handler.
+   *
+   * @var \Drupal\Core\Extension\ModuleHandlerInterface
+   */
+  protected $moduleHandler;
+
+  /**
+   * Constructs a new LibraryDiscovery instance.
+   *
+   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
+   *   The cache backend.
+   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
+   *   The module handler.
+   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
+   *   The theme handler.
+   */
+  public function __construct(CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
+    $this->cache = $cache_backend;
+    $this->moduleHandler = $module_handler;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getLibrariesByExtension($extension) {
+    $this->ensureLibraryInformation($extension);
+    return $this->libraries[$extension];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getLibraryByName($extension, $name) {
+    $this->ensureLibraryInformation($extension);
+    return isset($this->libraries[$extension][$name]) ? $this->libraries[$extension][$name] : FALSE;
+  }
+
+  protected function ensureLibraryInformation($extension) {
+    $this->retrieveFromCache($extension);
+    if (!isset($this->libraries[$extension])) {
+      if ($information = $this->buildLibrariesByExtension($extension)) {
+        $this->libraries[$extension] = $information;
+      }
+      else {
+        $this->libraries[$extension] = FALSE;
+      }
+    }
+  }
+
+  protected function retrieveFromCache($extension) {
+    if (!isset($this->libraries[$extension])) {
+      if ($cache = $this->cache->get('library:info:' . $extension)) {
+        $this->libraries[$extension] = $cache->data;
+      }
+    }
+  }
+
+  protected function setCache($extension, array $information) {
+    $this->cache->set('library:info:' . $extension, $information, Cache::PERMANENT, array(
+      'extension' => array(TRUE, $extension),
+      'library_info' => array(TRUE),
+    ));
+  }
+
+  protected function buildLibrariesByExtension($extension) {
+    $this->libraries[$extension] = array();
+    if ($extension === 'core') {
+      $path = 'core';
+      $extension_type = 'core';
+    }
+    else {
+      if ($this->moduleHandler->moduleExists($extension)) {
+        $extension_type = 'module';
+      }
+      else {
+        $extension_type = 'theme';
+      }
+      $path = $this->drupalGetPath($extension_type, $extension);
+    }
+    $library_file = $path . '/' . $extension . '.libraries.yml';
+
+    if ($library_file && file_exists(DRUPAL_ROOT . '/' . $library_file)) {
+      $this->libraries[$extension] = array();
+      $this->parseLibraryInfo($extension, $library_file);
+    }
+
+    foreach ($this->libraries[$extension] as $id => &$library) {
+      if (!isset($library['js']) && !isset($library['css']) && !isset($library['settings'])) {
+        throw new \RuntimeException(sprintf("Incomplete library definition for '%s' in %s", $id, $library_file));
+      }
+      $library += array('dependencies' => array(), 'js' => array(), 'css' => array());
+
+      if (isset($library['version'])) {
+        // @todo Retrieve version of a non-core extension.
+        if ($library['version'] === 'VERSION') {
+          $library['version'] = \Drupal::VERSION;
+        }
+        // Remove 'v' prefix from external library versions.
+        elseif ($library['version'][0] === 'v') {
+          $library['version'] = substr($library['version'], 1);
+        }
+      }
+
+      foreach (array('js', 'css') as $type) {
+        // Prepare (flatten) the SMACSS-categorized definitions.
+        // @todo After Asset(ic) changes, retain the definitions as-is and
+        //   properly resolve dependencies for all (css) libraries per category,
+        //   and only once prior to rendering out an HTML page.
+        if ($type == 'css' && !empty($library[$type])) {
+          foreach ($library[$type] as $category => $files) {
+            foreach ($files as $source => $options) {
+              if (!isset($options['weight'])) {
+                $options['weight'] = 0;
+              }
+              // Apply the corresponding weight defined by CSS_* constants.
+              $options['weight'] += constant('CSS_' . strtoupper($category == 'theme' ? 'skin' : $category));
+              $library[$type][$source] = $options;
+            }
+            unset($library[$type][$category]);
+          }
+        }
+        foreach ($library[$type] as $source => $options) {
+          unset($library[$type][$source]);
+          // Allow to omit the options hashmap in YAML declarations.
+          if (!is_array($options)) {
+            $options = array();
+          }
+          if ($type == 'js' && isset($options['weight']) && $options['weight'] > 0) {
+            throw new \UnexpectedValueException("The $extension/$id library defines a positive weight for '$source'. Only negative weights are allowed (but should be avoided). Instead of a positive weight, specify accurate dependencies for this library.");
+          }
+          // Unconditionally apply default groups for the defined asset files.
+          // The library system is a dependency management system. Each library
+          // properly specifies its dependencies instead of relying on a custom
+          // processing order.
+          if ($type == 'js') {
+            $options['group'] = JS_LIBRARY;
+          }
+          elseif ($type == 'css') {
+            $options['group'] = $extension_type == 'theme' ? CSS_AGGREGATE_THEME : CSS_AGGREGATE_DEFAULT;
+          }
+          // By default, all library assets are files.
+          if (!isset($options['type'])) {
+            $options['type'] = 'file';
+          }
+          if ($options['type'] == 'external') {
+            $options['data'] = $source;
+          }
+          // Determine the file asset URI.
+          else {
+            if ($source[0] === '/') {
+              // An absolute path maps to DRUPAL_ROOT / base_path().
+              if ($source[1] !== '/') {
+                $options['data'] = substr($source, 1);
+              }
+              // A protocol-free URI (e.g., //cdn.com/example.js) is external.
+              else {
+                $options['type'] = 'external';
+                $options['data'] = $source;
+              }
+            }
+            // A stream wrapper URI (e.g., public://generated_js/example.js).
+            elseif ($this->fileValidUri($source)) {
+              $options['data'] = $source;
+            }
+            // By default, file paths are relative to the registering extension.
+            else {
+              $options['data'] = $path . '/' . $source;
+            }
+          }
+
+          if (!isset($library['version'])) {
+            // @todo Get the information from the extension.
+            $options['version'] = -1;
+          }
+          else {
+            $options['version'] = $library['version'];
+          }
+
+          $library[$type][] = $options;
+        }
+      }
+
+      // @todo Introduce drupal_add_settings().
+      if (isset($library['settings'])) {
+        $library['js'][] = array(
+          'type' => 'setting',
+          'data' => $library['settings'],
+        );
+        unset($library['settings']);
+      }
+      // @todo Convert all uses of #attached[library][]=array('provider','name')
+      //   into #attached[library][]='provider/name' and remove this.
+      foreach ($library['dependencies'] as $i => $dependency) {
+        if (!is_array($dependency)) {
+          $library['dependencies'][$i] = explode('/', $dependency, 2);
+        }
+      }
+    }
+    return $this->libraries[$extension];
+  }
+
+  /**
+   * Wraps drupal_get_path().
+   */
+  protected function drupalGetPath($type, $name) {
+    return drupal_get_path($type, $name);
+  }
+
+  /**
+   * @param $source
+   * @return bool
+   */
+  protected function fileValidUri($source) {
+    return file_valid_uri($source);
+  }
+
+  /**
+   * @param $extension
+   * @param $library_file
+   * @return mixed
+   * @throws \RuntimeException
+   */
+  protected function parseLibraryInfo($extension, $library_file) {
+    $parser = new Parser();
+    try {
+      $this->libraries[$extension] = $parser->parse(file_get_contents(DRUPAL_ROOT . '/' . $library_file));
+    }
+    catch (ParseException $e) {
+      // Rethrow a more helpful exception, since ParseException lacks context.
+      throw new \RuntimeException(sprintf('Invalid library definition in %s: %s', $library_file, $e->getMessage()), 0, $e);
+    }
+    // Allow modules to alter the module's registered libraries.
+    $this->moduleHandler->alter('library_info', $this->libraries[$extension], $extension);
+  }
+
+}
diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscoveryInterface.php b/core/lib/Drupal/Core/Asset/LibraryDiscoveryInterface.php
new file mode 100644
index 0000000..da4dc50
--- /dev/null
+++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryInterface.php
@@ -0,0 +1,53 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Asset\LibraryDiscoveryInterface.
+ */
+
+namespace Drupal\Core\Asset;
+
+/**
+ * Discovers information for javascript/CSS libraries.
+ *
+ * Library information is statically cached. Libraries are keyed by module for
+ * several reasons:
+ * - Libraries are not unique. Multiple modules might ship with the same library
+ *   in a different version or variant. This registry cannot (and does not
+ *   attempt to) prevent library conflicts.
+ * - Modules implementing and thereby depending on a library that is registered
+ *   by another module can only rely on that module's library.
+ * - Two (or more) modules can still register the same library and use it
+ *   without conflicts in case the libraries are loaded on certain pages only.
+ */
+interface LibraryDiscoveryInterface {
+
+  /**
+   * Gets all libraries defined by an extension.
+   *
+   * @param string $extension
+   *   The name of the extension that registered a library.
+   *
+   * @return array
+   *   An associative array of libraries registered by $extension is returned
+   *   (which may be empty).
+   *
+   * @see self::getLibraryByName
+   */
+  public function getLibrariesByExtension($extension);
+
+  /**
+   * Gets a single library defined by an extension by name.
+   *
+   * @param string $extension
+   *   The name of the extension that registered a library.
+   * @param string $name
+   *   The name of a registered library to retrieve.
+   *
+   * @return array|FALSE
+   *   The definition of the requested library, if $name was passed and it
+   *   exists, otherwise FALSE.
+   */
+  public function getLibraryByName($extension, $name);
+
+}
diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php
new file mode 100644
index 0000000..a13fc1b
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php
@@ -0,0 +1,408 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\Core\Asset\LibraryDiscoveryTest.
+ */
+
+namespace Drupal\Tests\Core\Asset;
+
+use Drupal\Core\Asset\LibraryDiscovery;
+use Drupal\Tests\UnitTestCase;
+
+if (!defined('DRUPAL_ROOT')) {
+  define('DRUPAL_ROOT', dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)))));
+}
+
+if (!defined('CSS_AGGREGATE_DEFAULT')) {
+  define('CSS_AGGREGATE_DEFAULT', 0);
+  define('CSS_AGGREGATE_THEME', 100);
+  define('CSS_BASE', -200);
+  define('CSS_LAYOUT', -100);
+  define('CSS_COMPONENT', 0);
+  define('CSS_STATE', 100);
+  define('CSS_SKIN', 200);
+  define('JS_SETTING', -200);
+  define('JS_LIBRARY', -100);
+  define('JS_DEFAULT', 0);
+  define('JS_THEME', 100);
+}
+
+/**
+ * Tests the library discovery.
+ *
+ * @coversDefaultClass \Drupal\Core\Asset\LibraryDiscovery
+ */
+class LibraryDiscoveryTest extends UnitTestCase {
+
+  /**
+   * The tested library provider.
+   *
+   * @var \Drupal\Core\Asset\LibraryDiscovery|\Drupal\Tests\Core\Asset\TestLibraryDiscovery
+   */
+  protected $libraryDiscovery;
+
+  /**
+   * The mocked cache backend.
+   *
+   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $cache;
+
+  /**
+   * The mocked module handler.
+   *
+   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $moduleHandler;
+
+  /**
+   * The mocked theme handler.
+   *
+   * @var \Drupal\Core\Extension\ThemeHandlerInterface
+   */
+  protected $themeHandler;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Tests \Drupal\Core\Asset\LibraryProvider',
+      'description' => '',
+      'group' => 'Asset handling',
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
+    $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
+    $this->themeHandler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
+    $this->libraryDiscovery = new TestLibraryDiscovery($this->cache, $this->moduleHandler, $this->themeHandler);
+  }
+
+  /**
+   * Ensures that basic functionality works for getLibraryByName.
+   *
+   * @covers ::getLibraryByName()
+   */
+  public function testGetLibraryByNameSimple() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('example_module')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'example_module', $path);
+
+    $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example');
+    $this->assertCount(0, $library['js']);
+    $this->assertCount(1, $library['css']);
+    $this->assertCount(0, $library['dependencies']);
+    $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']);
+
+    // Ensures that VERSION is replaced by the current core version.
+    $this->assertEquals(\Drupal::VERSION, $library['version']);
+  }
+
+  /**
+   * Ensures that basic functionality works for getLibrariesByExtension.
+   *
+   * @covers ::getLibrariesByExtension()
+   */
+  public function testGetLibrariesByExtensionSimple() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('example_module')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'example_module', $path);
+
+    $libraries = $this->libraryDiscovery->getLibrariesByExtension('example_module', 'example');
+    $this->assertCount(1, $libraries);
+    $this->assertEquals($path . '/css/example.css', $libraries['example']['css'][0]['data']);
+  }
+
+  /**
+   * Ensures that a theme can be used instead of a module.
+   */
+  public function testGetLibraryByNameWithTheme() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('example_theme')
+      ->will($this->returnValue(FALSE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('theme', 'example_theme', $path);
+
+    $library = $this->libraryDiscovery->getLibraryByName('example_theme', 'example');
+    $this->assertCount(0, $library['js']);
+    $this->assertCount(1, $library['css']);
+    $this->assertCount(0, $library['dependencies']);
+    $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']);
+  }
+
+  /**
+   * Ensures that a module with a missing library file results in an empty lib.
+   */
+  public function testGetLibraryWithMissingLibraryFile() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('example_module')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files_not_existing';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'example_module', $path);
+
+    $this->assertFalse($this->libraryDiscovery->getLibraryByName('example_module', 'example'));
+    $this->assertFalse($this->libraryDiscovery->getLibrariesByExtension('example_module'));
+  }
+
+  /**
+   * Ensures that an exception is thrown when a file could not be parsed.
+   *
+   * @expectedException \RuntimeException
+   */
+  public function testInvalidLibrariesFile() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('invalid_file')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'invalid_file', $path);
+
+    $this->libraryDiscovery->getLibrariesByExtension('invalid_file');
+  }
+
+  /**
+   * Ensures that an exception is thrown when no css/js/setting is specified.
+   *
+   * @expectedException \RuntimeException
+   * @expectedExceptionMessage Incomplete library definition for 'example' in core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module_missing_information.libraries.yml
+   */
+  public function testGetLibraryWithMissingInformation() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('example_module_missing_information')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'example_module_missing_information', $path);
+
+    $this->libraryDiscovery->getLibrariesByExtension('example_module_missing_information');
+  }
+
+  /**
+   * Ensures that version of external libraries gets handled.
+   */
+  public function testExternalLibraries() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('external')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'external', $path);
+
+    $library = $this->libraryDiscovery->getLibraryByName('external', 'example_external');
+    $this->assertEquals($path . '/css/example_external.css', $library['css'][0]['data']);
+    $this->assertEquals('3.14', $library['version']);
+  }
+
+  /**
+   * Ensures that CSS weights is taken into account properly.
+   */
+  public function testDefaultCssWeights() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('css_weights')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'css_weights', $path);
+
+    $library = $this->libraryDiscovery->getLibraryByName('css_weights', 'example');
+    $css = $library['css'];
+    $this->assertCount(10, $css);
+
+    // The following default weights are tested:
+    // - CSS_BASE: -200
+    // - CSS_LAYOUT: -100
+    // - CSS_COMPONENT: 0
+    // - CSS_STATE: 100
+    // - CSS_SKIN: 200
+    $this->assertEquals(200, $css[0]['weight']);
+    $this->assertEquals(200 + 29, $css[1]['weight']);
+    $this->assertEquals(-200, $css[2]['weight']);
+    $this->assertEquals(-200 + 97, $css[3]['weight']);
+    $this->assertEquals(-100, $css[4]['weight']);
+    $this->assertEquals(-100 + 92, $css[5]['weight']);
+    $this->assertEquals(0, $css[6]['weight']);
+    $this->assertEquals(45, $css[7]['weight']);
+    $this->assertEquals(100, $css[8]['weight']);
+    $this->assertEquals(100 + 8, $css[9]['weight']);
+  }
+
+  /**
+   * Ensures that you cannot provide positive weights for css libraries.
+   *
+   * @expectedException \UnexpectedValueException
+   */
+  public function testJsWithPositiveWeight() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('js_positive_weight')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'js_positive_weight', $path);
+
+    $this->libraryDiscovery->getLibrariesByExtension('js_positive_weight');
+  }
+
+  /**
+   * Tests a library with CSS/JS and a setting.
+   */
+  public function testLibraryWithCssJsSetting() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('css_js_settings')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'css_js_settings', $path);
+
+    $library = $this->libraryDiscovery->getLibraryByName('css_js_settings', 'example');
+
+    // Ensures that the group and type is set automatically.
+    $this->assertEquals(-100, $library['js'][0]['group']);
+    $this->assertEquals('file', $library['js'][0]['type']);
+    $this->assertEquals($path . '/js/example.js', $library['js'][0]['data']);
+
+    $this->assertEquals(0, $library['css'][0]['group']);
+    $this->assertEquals('file', $library['css'][0]['type']);
+    $this->assertEquals($path . '/css/base.css', $library['css'][0]['data']);
+
+    $this->assertEquals('setting', $library['js'][1]['type']);
+    $this->assertEquals(array('key' => 'value'), $library['js'][1]['data']);
+  }
+
+  /**
+   * Tests a library with dependencies.
+   */
+  public function testLibraryWithDependencies() {
+     $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('dependencies')
+      ->will($this->returnValue(TRUE));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'dependencies', $path);
+
+    $library = $this->libraryDiscovery->getLibraryByName('dependencies', 'example');
+    $this->assertCount(3, $library['dependencies']);
+    $this->assertEquals(array('external', 'example_external'), $library['dependencies'][0]);
+    $this->assertEquals(array('example_module', 'example'), $library['dependencies'][1]);
+    $this->assertEquals(array('example_theme', 'example'), $library['dependencies'][2]);
+  }
+
+  /**
+   * Tests the internal static cache.
+   */
+  public function testStaticCache() {
+    $this->moduleHandler->expects($this->once())
+      ->method('moduleExists')
+      ->with('example_module')
+      ->will($this->returnValue(TRUE));
+    $this->cache->expects($this->once())
+      ->method('get')
+      ->with('library:info:' . 'example_module')
+      ->will($this->returnValue(NULL));
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+    $this->libraryDiscovery->setPaths('module', 'example_module', $path);
+
+    $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example');
+    $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']);
+
+    $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example');
+    $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']);
+  }
+
+  /**
+   * Tests the external cache.
+   */
+  public function testExternalCache() {
+    // Ensure that the module handler does not need to be touched.
+    $this->moduleHandler->expects($this->never())
+      ->method('moduleExists');
+
+    $path = __DIR__ . '/library_test_files';
+    $path = substr($path, strlen(DRUPAL_ROOT) + 1);
+
+    // Setup a cache entry which will be retrieved, but just once, so the static
+    // cache still works.
+    $this->cache->expects($this->once())
+      ->method('get')
+      ->with('library:info:' . 'example_module')
+      ->will($this->returnValue((object) array(
+        'data' => array(
+          'example' => array(
+            'css' => array(
+              array(
+                'data' => $path . '/css/example.css',
+              ),
+            ),
+          ),
+        )
+      )));
+
+    $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example');
+    $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']);
+
+    $library = $this->libraryDiscovery->getLibraryByName('example_module', 'example');
+    $this->assertEquals($path . '/css/example.css', $library['css'][0]['data']);
+  }
+
+}
+
+class TestLibraryDiscovery extends LibraryDiscovery {
+
+  protected $paths;
+
+  protected $validUris;
+
+  protected function drupalGetPath($type, $name) {
+    return isset($this->paths[$type][$name]) ? $this->paths[$type][$name] : NULL;
+  }
+
+  public function setPaths($type, $name, $path) {
+    $this->paths[$type][$name] = $path;
+  }
+
+  protected function fileValidUri($source) {
+    return isset($this->validUris[$source]) ? $this->validUris[$source] : FALSE;
+  }
+
+  public function setFileValidUri($source, $valid) {
+    $this->validUris[$source] = $valid;
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/css_js_settings.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/css_js_settings.libraries.yml
new file mode 100644
index 0000000..fdb479f
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/css_js_settings.libraries.yml
@@ -0,0 +1,8 @@
+example:
+  css:
+    base:
+      css/base.css: {}
+  js:
+    js/example.js: {}
+  settings:
+    key: value
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/css_weights.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/css_weights.libraries.yml
new file mode 100644
index 0000000..fd0d6ec
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/css_weights.libraries.yml
@@ -0,0 +1,22 @@
+example:
+  css:
+    theme:
+      css/theme__no_weight.css: {}
+      css/theme__weight.css:
+        weight: 29
+    base:
+      css/base__no_weight.css: {}
+      css/base__weight.css:
+        weight: 97
+    layout:
+      css/layout__no_weight.css: {}
+      css/layout__weight.css:
+        weight: 92
+    component:
+      css/component__no_weight.css: {}
+      css/component__weight.css:
+        weight: 45
+    state:
+      css/state__no_weight.css: {}
+      css/state__weight.css:
+        weight: 8
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/dependencies.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/dependencies.libraries.yml
new file mode 100644
index 0000000..537e0c3
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/dependencies.libraries.yml
@@ -0,0 +1,9 @@
+example:
+  css:
+    css/example.js: {}
+  dependencies:
+    - external/example_external
+    - example_module/example
+    -
+      - example_theme
+      - example
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module.libraries.yml
new file mode 100644
index 0000000..653438c
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module.libraries.yml
@@ -0,0 +1,5 @@
+example:
+  version: VERSION
+  css:
+    theme:
+      css/example.css: {}
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module_missing_information.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module_missing_information.libraries.yml
new file mode 100644
index 0000000..0495fb6
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module_missing_information.libraries.yml
@@ -0,0 +1,2 @@
+example:
+  version: VERSION
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_theme.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_theme.libraries.yml
new file mode 100644
index 0000000..62c1346
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/example_theme.libraries.yml
@@ -0,0 +1,4 @@
+example:
+  css:
+    theme:
+      css/example.css: {}
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/external.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/external.libraries.yml
new file mode 100644
index 0000000..0486a72
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/external.libraries.yml
@@ -0,0 +1,5 @@
+example_external:
+  version: v3.14
+  css:
+    theme:
+      css/example_external.css: {}
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml
new file mode 100644
index 0000000..cb20993
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml
@@ -0,0 +1,2 @@
+example:
+  -=--##;'~ test
diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/js_positive_weight.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/js_positive_weight.libraries.yml
new file mode 100644
index 0000000..9fc4814
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/js_positive_weight.libraries.yml
@@ -0,0 +1,4 @@
+example:
+  js:
+    js/positive_weight.js:
+      weight: 10
