提交 62446d3b 编写于 作者: J Joao Moreno

gridview: first tests

上级 bb0892f0
......@@ -38,7 +38,11 @@ export interface IGrid<T extends IView> {
}
function tail<T>(arr: T[]): [T[], T] {
return [arr.slice(0, arr.length - 1), arr[length - 1]];
if (arr.length === 0) {
throw new Error('Invalid tail call');
}
return [arr.slice(0, arr.length - 1), arr[arr.length - 1]];
}
abstract class AbstractNode implements IView {
......@@ -130,12 +134,20 @@ class BranchNode<T extends IView> extends AbstractNode {
}
addChild(node: Node<T>, size: number, index: number): void {
if (index < 0 || index >= this.children.length) {
throw new Error('Invalid index');
}
this.splitview.addView(node, size, index);
this.children.splice(index, 0, node);
this.onDidChildrenChange();
}
removeChild(index: number): Node<T> {
if (index < 0 || index >= this.children.length) {
throw new Error('Invalid index');
}
const child = this.children[index];
this.splitview.removeView(index);
this.children.splice(index, 1);
......@@ -144,10 +156,18 @@ class BranchNode<T extends IView> extends AbstractNode {
}
resizeChild(index: number, size: number): void {
if (index < 0 || index >= this.children.length) {
throw new Error('Invalid index');
}
this.splitview.resizeView(index, size);
}
getChildSize(index: number): number {
if (index < 0 || index >= this.children.length) {
throw new Error('Invalid index');
}
return this.splitview.getViewSize(index);
}
......@@ -296,6 +316,11 @@ export class GridView<T extends IView> implements IGrid<T>, IDisposable {
}
const [index, ...rest] = location;
if (index < 0 || index >= node.children.length) {
throw new Error('Invalid location');
}
const child = node.children[index];
path.push(node);
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { Emitter } from 'vs/base/common/event';
import { GridView, IView, Orientation } from 'vs/base/browser/ui/grid/gridview';
class TestView implements IView {
private _onDidChange = new Emitter<number | undefined>();
readonly onDidChange = this._onDidChange.event;
get minimumSize(): number { return this._minimumSize; }
set minimumSize(size: number) { this._minimumSize = size; this._onDidChange.fire(); }
get maximumSize(): number { return this._maximumSize; }
set maximumSize(size: number) { this._maximumSize = size; this._onDidChange.fire(); }
private _onDidRender = new Emitter<{ container: HTMLElement; orientation: Orientation }>();
readonly onDidRender = this._onDidRender.event;
private _size = 0;
get size(): number { return this._size; }
private _onDidLayout = new Emitter<{ size: number; orientation: Orientation }>();
readonly onDidLayout = this._onDidLayout.event;
private _onDidFocus = new Emitter<void>();
readonly onDidFocus = this._onDidFocus.event;
constructor(
private _minimumSize: number,
private _maximumSize: number
) {
assert(_minimumSize <= _maximumSize, 'splitview view minimum size must be <= maximum size');
}
render(container: HTMLElement, orientation: Orientation): void {
this._onDidRender.fire({ container, orientation });
}
layout(size: number, orientation: Orientation): void {
this._size = size;
this._onDidLayout.fire({ size, orientation });
}
focus(): void {
this._onDidFocus.fire();
}
dispose(): void {
this._onDidChange.dispose();
this._onDidRender.dispose();
this._onDidLayout.dispose();
this._onDidFocus.dispose();
}
}
// function getSashes(splitview: SplitView): Sash[] {
// return (splitview as any).sashItems.map(i => i.sash) as Sash[];
// }
suite('GridView', function () {
let container: HTMLElement;
setup(function () {
container = document.createElement('div');
container.style.position = 'absolute';
container.style.width = `${200}px`;
container.style.height = `${200}px`;
});
teardown(function () {
container = null;
});
test('empty gridview is empty', function () {
const gridview = new GridView(container);
assert.deepEqual(gridview.getViews(), { children: [] });
gridview.dispose();
});
test('gridview addView', function () {
const gridview = new GridView(container);
const view = new TestView(20, 20);
assert.throws(() => gridview.addView(view, 200, []), 'empty location');
assert.throws(() => gridview.addView(view, 200, [1]), 'index overflow');
assert.throws(() => gridview.addView(view, 200, [0, 0]), 'hierarchy overflow');
gridview.dispose();
});
});
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册