提交 aa70d314 编写于 作者: J Joao Moreno

gridview: addView tests

上级 62446d3b
......@@ -17,15 +17,15 @@ function orthogonal(orientation: Orientation): Orientation {
return orientation === Orientation.VERTICAL ? Orientation.HORIZONTAL : Orientation.VERTICAL;
}
export interface ILeafNode<T extends IView> {
view: T;
export class GridLeafNode<T extends IView> {
constructor(readonly view: T) { }
}
export interface IBranchNode<T extends IView> {
children: IGridNode<T>[];
export class GridBranchNode<T extends IView> {
constructor(readonly children: GridNode<T>[]) { }
}
export type IGridNode<T extends IView> = ILeafNode<T> | IBranchNode<T>;
export type GridNode<T extends IView> = GridLeafNode<T> | GridBranchNode<T>;
export interface IGrid<T extends IView> {
layout(width: number, height: number): void;
......@@ -34,7 +34,7 @@ export interface IGrid<T extends IView> {
moveView(from: number[], to: number[]): void;
resizeView(location: number[], size: number): void;
getViewSize(location: number[]): number;
getViews(): IBranchNode<T>;
getViews(): GridBranchNode<T>;
}
function tail<T>(arr: T[]): [T[], T] {
......@@ -134,7 +134,7 @@ class BranchNode<T extends IView> extends AbstractNode {
}
addChild(node: Node<T>, size: number, index: number): void {
if (index < 0 || index >= this.children.length) {
if (index < 0 || index > this.children.length) {
throw new Error('Invalid index');
}
......@@ -294,15 +294,15 @@ export class GridView<T extends IView> implements IGrid<T>, IDisposable {
return parent.getChildSize(index);
}
getViews(): IBranchNode<T> {
return this._getViews(this.root) as IBranchNode<T>;
getViews(): GridBranchNode<T> {
return this._getViews(this.root) as GridBranchNode<T>;
}
private _getViews(node: Node<T>): IGridNode<T> {
private _getViews(node: Node<T>): GridNode<T> {
if (node instanceof BranchNode) {
return { children: node.children.map(c => this._getViews(c)) };
return new GridBranchNode(node.children.map(c => this._getViews(c)));
} else {
return { view: node.view };
return new GridLeafNode(node.view);
}
}
......
......@@ -5,7 +5,7 @@
import * as assert from 'assert';
import { Emitter } from 'vs/base/common/event';
import { GridView, IView, Orientation } from 'vs/base/browser/ui/grid/gridview';
import { GridView, IView, Orientation, GridNode, GridBranchNode } from 'vs/base/browser/ui/grid/gridview';
class TestView implements IView {
......@@ -33,7 +33,7 @@ class TestView implements IView {
private _minimumSize: number,
private _maximumSize: number
) {
assert(_minimumSize <= _maximumSize, 'splitview view minimum size must be <= maximum size');
assert(_minimumSize <= _maximumSize, 'gridview view minimum size must be <= maximum size');
}
render(container: HTMLElement, orientation: Orientation): void {
......@@ -57,10 +57,13 @@ class TestView implements IView {
}
}
// function getSashes(splitview: SplitView): Sash[] {
// return (splitview as any).sashItems.map(i => i.sash) as Sash[];
// }
function nodesToArrays(node: GridNode<any>): any {
if (node instanceof GridBranchNode) {
return node.children.map(nodesToArrays);
} else {
return node.view;
}
}
suite('GridView', function () {
let container: HTMLElement;
......@@ -86,11 +89,22 @@ suite('GridView', 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');
const views = [
new TestView(20, 20),
new TestView(20, 20),
new TestView(20, 20)
];
gridview.addView(views[0], 200, [0]);
gridview.addView(views[1], 200, [1]);
gridview.addView(views[2], 200, [2]);
assert.deepEqual(nodesToArrays(gridview.getViews()), views);
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.
先完成此消息的编辑!
想要评论请 注册