Browse Source

wip imagewidget refactor and tests

Steven Silvester 8 năm trước cách đây
mục cha
commit
ca0bd649cb
4 tập tin đã thay đổi với 217 bổ sung58 xóa
  1. 4 0
      src/imagewidget/index.ts
  2. 18 9
      src/imagewidget/widget.ts
  3. 144 0
      test/src/imagewidget/widget.spec.ts
  4. 51 49
      test/src/index.ts

+ 4 - 0
src/imagewidget/index.ts

@@ -0,0 +1,4 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+export * from './widget';

+ 18 - 9
src/imagewidget/widget.ts

@@ -33,12 +33,14 @@ class ImageWidget extends Widget {
     this.node.tabIndex = -1;
     this.addClass(IMAGE_CLASS);
 
-    if (context.model.toString()) {
+    this._onTitleChanged();
+    context.pathChanged.connect(this._onTitleChanged, this);
+
+    context.ready.then(() => {
       this.update();
-    }
-    context.pathChanged.connect(() => { this.update(); });
-    context.model.contentChanged.connect(() => { this.update(); });
-    context.fileChanged.connect(() => { this.update(); });
+      context.model.contentChanged.connect(this.update, this);
+      context.fileChanged.connect(this.update, this);
+    });
   }
 
   /**
@@ -81,12 +83,12 @@ class ImageWidget extends Widget {
    * Handle `update-request` messages for the widget.
    */
   protected onUpdateRequest(msg: Message): void {
-    this.title.label = this._context.path.split('/').pop();
-    let cm = this._context.contentsModel;
-    if (cm === null) {
+    let context = this._context;
+    if (this.isDisposed || !context.isReady) {
       return;
     }
-    let content = this._context.model.toString();
+    let cm = this._context.contentsModel;
+    let content = context.model.toString();
     let src = `data:${cm.mimetype};${cm.format},${content}`;
     this.node.querySelector('img').setAttribute('src', src);
   }
@@ -98,6 +100,13 @@ class ImageWidget extends Widget {
     this.node.focus();
   }
 
+  /**
+   * Handle a change to the title.
+   */
+  private _onTitleChanged(): void {
+    this.title.label = this._context.path.split('/').pop();
+  }
+
   private _context: DocumentRegistry.Context;
   private _scale = 1;
 }

+ 144 - 0
test/src/imagewidget/widget.spec.ts

@@ -0,0 +1,144 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import expect = require('expect.js');
+
+import {
+  Contents, ServiceManager, utils
+} from '@jupyterlab/services';
+
+import {
+  Message, sendMessage
+} from 'phosphor/lib/core/messaging';
+
+import {
+  WidgetMessage
+} from 'phosphor/lib/ui/widget';
+
+import {
+  Base64ModelFactory, Context, DocumentRegistry
+} from '../../../lib/docregistry';
+
+import {
+  ImageWidget, ImageWidgetFactory
+} from '../../../lib/imagewidget';
+
+
+class LogImage extends ImageWidget {
+
+  methods: string[] = [];
+
+  protected onUpdateRequest(msg: Message): void {
+    super.onUpdateRequest(msg);
+    this.methods.push('onUpdateRequest');
+  }
+
+  protected onActivateRequest(msg: Message): void {
+    super.onActivateRequest(msg);
+    this.methods.push('onActivateRequest');
+  }
+}
+
+
+const IMAGES: Contents.IModel[] = [
+  {
+    path: utils.uuid() + '.png',
+    type: 'file',
+    mimetype: 'image/png',
+    content:  'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
+    format: 'base64'
+  },
+  {
+    path: utils.uuid() + '.gif',
+    type: 'file',
+    mimetype: 'image/gif',
+    content: 'R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=',
+    format: 'base64'
+  }
+];
+
+
+describe('ImageWidget', () => {
+
+  let factory = new Base64ModelFactory();
+  let context: DocumentRegistry.Context;
+  let manager: ServiceManager.IManager;
+  let widget: ImageWidget;
+
+  before((done) => {
+    manager = new ServiceManager();
+    manager.ready.then(() => {
+      return manager.contents.save(IMAGES[0].path, IMAGES[0]);
+    }).then(() => {
+      return manager.contents.save(IMAGES[1].path, IMAGES[1]);
+    }).then(() => {
+      done();
+    }).catch(done);
+  });
+
+  beforeEach((done) => {
+    context = new Context({ manager, factory, path: IMAGES[0].path });
+    widget = new ImageWidget(context);
+    return context.revert().then(done, done);
+  });
+
+  afterEach(() => {
+    widget.dispose();
+  });
+
+  describe('#constructor()', () => {
+
+    it('should create an ImageWidget', () => {
+      expect(widget).to.be.an(ImageWidget);
+    });
+
+    it('should keep the title in sync with the file name', (done) => {
+      let newPath = utils.uuid() + '.png';
+      expect(widget.title.label).to.be(context.path);
+      context.pathChanged.connect(() => {
+        expect(widget.title.label).to.be(newPath);
+        done();
+      });
+      return manager.contents.rename(context.path, newPath).catch(done);
+    });
+
+    it('should handle a change to the content', () => {
+
+    });
+
+  });
+
+  describe('#context', () => {
+
+    it('should be the context associated with the widget', () => {
+      expect(widget.context).to.be(context);
+    });
+
+  });
+
+  describe('#scale', () => {
+
+  });
+
+  describe('#dispose()', () => {
+
+  });
+
+  describe('#onUpdateRequest()', () => {
+
+  });
+
+  describe('#onActivateRequest()', () => {
+
+  });
+
+});
+
+
+describe('ImageWidgetFactory', () => {
+
+  describe('#createNewWidget', () => {
+
+  });
+
+});

+ 51 - 49
test/src/index.ts

@@ -1,74 +1,76 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import './application/loader.spec';
-import './application/shell.spec';
+// import './application/loader.spec';
+// import './application/shell.spec';
 
-import './commandlinker/commandlinker.spec';
+// import './commandlinker/commandlinker.spec';
 
-import './common/activitymonitor.spec';
-import './common/instancetracker.spec';
-import './common/observablestring.spec';
-import './common/observablevector.spec';
-import './common/vdom.spec';
+// import './common/activitymonitor.spec';
+// import './common/instancetracker.spec';
+// import './common/observablestring.spec';
+// import './common/observablevector.spec';
+// import './common/vdom.spec';
 
-import './completer/handler.spec';
-import './completer/model.spec';
-import './completer/widget.spec';
+// import './completer/handler.spec';
+// import './completer/model.spec';
+// import './completer/widget.spec';
 
-import './console/content.spec';
-import './console/foreign.spec';
-import './console/history.spec';
-import './console/panel.spec';
+// import './console/content.spec';
+// import './console/foreign.spec';
+// import './console/history.spec';
+// import './console/panel.spec';
 
-import './csvwidget/table.spec';
-import './csvwidget/toolbar.spec';
-import './csvwidget/widget.spec';
+// import './csvwidget/table.spec';
+// import './csvwidget/toolbar.spec';
+// import './csvwidget/widget.spec';
 
-import './dialog/dialog.spec';
+// import './dialog/dialog.spec';
 
-import './docmanager/manager.spec';
-import './docmanager/savehandler.spec';
-import './docmanager/widgetmanager.spec';
+// import './docmanager/manager.spec';
+// import './docmanager/savehandler.spec';
+// import './docmanager/widgetmanager.spec';
 
-import './docregistry/context.spec';
-import './docregistry/default.spec';
-import './docregistry/registry.spec';
+// import './docregistry/context.spec';
+// import './docregistry/default.spec';
+// import './docregistry/registry.spec';
 
-import './filebrowser/crumbs.spec';
-import './filebrowser/model.spec';
+// import './filebrowser/crumbs.spec';
+// import './filebrowser/model.spec';
 
-import './inspector/inspector.spec';
+import './imagewidget/widget.spec';
 
-import './markdownwidget/widget.spec';
+// import './inspector/inspector.spec';
 
-import './renderers/renderers.spec';
-import './renderers/latex.spec';
+// import './markdownwidget/widget.spec';
 
-import './rendermime/rendermime.spec';
+// import './renderers/renderers.spec';
+// import './renderers/latex.spec';
 
-import './notebook/cells/editor.spec';
-import './notebook/cells/model.spec';
-import './notebook/cells/widget.spec';
+// import './rendermime/rendermime.spec';
 
-import './notebook/common/undo.spec';
+// import './notebook/cells/editor.spec';
+// import './notebook/cells/model.spec';
+// import './notebook/cells/widget.spec';
 
-import './notebook/notebook/actions.spec';
-import './notebook/notebook/default-toolbar.spec';
-import './notebook/notebook/model.spec';
-import './notebook/notebook/modelfactory.spec';
-import './notebook/notebook/panel.spec';
-import './notebook/notebook/trust.spec';
-import './notebook/notebook/widget.spec';
-import './notebook/notebook/widgetfactory.spec';
+// import './notebook/common/undo.spec';
 
-import './notebook/output-area/model.spec';
-import './notebook/output-area/widget.spec';
+// import './notebook/notebook/actions.spec';
+// import './notebook/notebook/default-toolbar.spec';
+// import './notebook/notebook/model.spec';
+// import './notebook/notebook/modelfactory.spec';
+// import './notebook/notebook/panel.spec';
+// import './notebook/notebook/trust.spec';
+// import './notebook/notebook/widget.spec';
+// import './notebook/notebook/widgetfactory.spec';
 
-import './notebook/tracker.spec';
+// import './notebook/output-area/model.spec';
+// import './notebook/output-area/widget.spec';
 
-import './terminal/terminal.spec';
+// import './notebook/tracker.spec';
 
-import './toolbar/toolbar.spec';
+// import './terminal/terminal.spec';
+
+// import './toolbar/toolbar.spec';
 
 import 'phosphor/styles/base.css';