text: // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //      http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS-IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  goog.module('goog.testing.fs.FileReaderTest');
  goog.setTestOnly();
  
  const EventObserver = goog.require('goog.testing.events.EventObserver');
  const FsError = goog.require('goog.fs.Error');
  const FsFileReader = goog.require('goog.fs.FileReader');
  const FsFileSystem = goog.require('goog.testing.fs.FileSystem');
  const GoogPromise = goog.require('goog.Promise');
  const TestingFsFileReader = goog.require('goog.testing.fs.FileReader');
  const events = goog.require('goog.events');
  const googArray = goog.require('goog.array');
  const googObject = goog.require('goog.object');
  const testSuite = goog.require('goog.testing.testSuite');
  
  const EventType = FsFileReader.EventType;
  const ReadyState = FsFileReader.ReadyState;
  
  /** @type {!goog.testing.fs.File} */
  let file;
  
  /** @type {!TestingFsFileReader} */
  let reader;
  
  /** @type {!EventObserver} */
  let observer;
  
  /** @const */
  const hasArrayBuffer = goog.isDef(goog.global.ArrayBuffer);
  
  testSuite({
    setUp() {
      const observedEvents = [];
      const fs = new FsFileSystem();
      const fileEntry =
          fs.getRoot().createDirectorySync('foo').createFileSync('bar');
  
      file = fileEntry.fileSync();
      file.setDataInternal('test content');
  
      reader = new TestingFsFileReader();
  
      // Observe all file events fired by the FileReader.
      observer = new EventObserver();
      events.listen(reader, googObject.getValues(EventType), observer);
    },
  
    tearDown() {
      goog.dispose(reader);
    },
  
    testRead() {
      assertEquals(ReadyState.INIT, reader.getReadyState());
      assertUndefined(reader.getResult());
  
      return new GoogPromise((resolve, reject) => {
               events.listen(reader, EventType.LOAD_END, resolve);
               reader.readAsText(file);
               assertEquals(ReadyState.LOADING, reader.getReadyState());
             })
          .then((result) => {
            assertEquals(file.toString(), reader.getResult());
  
            assertEquals(ReadyState.DONE, reader.getReadyState());
            assertArrayEquals(
                [
                  EventType.LOAD_START,
                  EventType.LOAD,
                  EventType.LOAD,
                  EventType.LOAD,
                  EventType.LOAD_END,
                ],
                googArray.map(observer.getEvents(), (e) => e.type));
          });
    },
  
    testReadAsArrayBuffer() {
      if (!hasArrayBuffer) {
        // Skip if array buffer is not supported
        return;
      }
  
      return new GoogPromise((resolve, reject) => {
               events.listen(reader, EventType.LOAD_END, resolve);
               reader.readAsArrayBuffer(file);
               assertEquals(ReadyState.LOADING, reader.getReadyState());
             })
          .then((result) => {
            assertElementsEquals(file.toArrayBuffer(), reader.getResult());
  
            assertEquals(ReadyState.DONE, reader.getReadyState());
            assertArrayEquals(
                [
                  EventType.LOAD_START,
                  EventType.LOAD,
                  EventType.LOAD,
                  EventType.LOAD,
                  EventType.LOAD_END,
                ],
                googArray.map(observer.getEvents(), (e) => e.type));
          });
    },
  
    testReadAsDataUrl() {
      return new GoogPromise((resolve, reject) => {
               events.listen(reader, EventType.LOAD_END, resolve);
               reader.readAsDataUrl(file);
               assertEquals(ReadyState.LOADING, reader.getReadyState());
             })
          .then((result) => {
            assertEquals(file.toDataUrl(), reader.getResult());
  
            assertEquals(ReadyState.DONE, reader.getReadyState());
            assertArrayEquals(
                [
                  EventType.LOAD_START,
                  EventType.LOAD,
                  EventType.LOAD,
                  EventType.LOAD,
                  EventType.LOAD_END,
                ],
                googArray.map(observer.getEvents(), (e) => e.type));
          });
    },
  
    testAbort() {
      return new GoogPromise((resolve, reject) => {
               events.listen(reader, EventType.LOAD_END, resolve);
               reader.readAsText(file);
               assertEquals(ReadyState.LOADING, reader.getReadyState());
               reader.abort();
             })
          .then((result) => {
            assertUndefined(reader.getResult());
  
            assertEquals(ReadyState.DONE, reader.getReadyState());
            assertArrayEquals(
                [EventType.ERROR, EventType.ABORT, EventType.LOAD_END],
                googArray.map(observer.getEvents(), (e) => e.type));
          });
    },
  
    testAbortBeforeRead() {
      const err = assertThrows(() => {
        reader.abort();
      });
      assertEquals(FsError.ErrorCode.INVALID_STATE, err.code);
    },
  
    testReadDuringRead() {
      const err = assertThrows(() => {
        reader.readAsText(file);
        reader.readAsText(file);
      });
      assertEquals(FsError.ErrorCode.INVALID_STATE, err.code);
    },
  });
  