Skip to content

Commit bb249c2

Browse files
markers: add test for marker-tree-label-provider
Added tests for the `marker-tree-label-provider`: - `getName` - `getLongName` - with a multiple root scenario - `getDescription` - `getIcon` - `canHandle` Signed-off-by: vince-fugnitto <vincent.fugnitto@ericsson.com>
1 parent 02d1956 commit bb249c2

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/********************************************************************************
2+
* Copyright (C) 2020 Ericsson and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the Eclipse
10+
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
11+
* with the GNU Classpath Exception which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
********************************************************************************/
16+
17+
import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';
18+
19+
let disableJSDOM = enableJSDOM();
20+
21+
import URI from '@theia/core/lib/common/uri';
22+
import { expect } from 'chai';
23+
import { Container } from 'inversify';
24+
import { ContributionProvider } from '@theia/core/lib/common';
25+
import { FileStat } from '@theia/filesystem/lib/common';
26+
import { LabelProvider, LabelProviderContribution, DefaultUriLabelProviderContribution } from '@theia/core/lib/browser';
27+
import { MarkerInfoNode } from './marker-tree';
28+
import { MarkerTreeLabelProvider } from './marker-tree-label-provider';
29+
import { TreeLabelProvider } from '@theia/core/lib/browser/tree/tree-label-provider';
30+
import { WorkspaceService } from '@theia/workspace/lib/browser';
31+
32+
disableJSDOM();
33+
34+
let markerTreeLabelProvider: MarkerTreeLabelProvider;
35+
let workspaceService: WorkspaceService;
36+
37+
before(() => {
38+
disableJSDOM = enableJSDOM();
39+
const testContainer = new Container();
40+
41+
workspaceService = new WorkspaceService();
42+
testContainer.bind(WorkspaceService).toConstantValue(workspaceService);
43+
44+
testContainer.bind(DefaultUriLabelProviderContribution).toSelf().inSingletonScope();
45+
testContainer.bind(LabelProvider).toSelf().inSingletonScope();
46+
testContainer.bind(MarkerTreeLabelProvider).toSelf().inSingletonScope();
47+
testContainer.bind(TreeLabelProvider).toSelf().inSingletonScope();
48+
49+
testContainer.bind<ContributionProvider<LabelProviderContribution>>(ContributionProvider).toDynamicValue(ctx => ({
50+
getContributions(): LabelProviderContribution[] {
51+
return [
52+
ctx.container.get<MarkerTreeLabelProvider>(MarkerTreeLabelProvider),
53+
ctx.container.get<TreeLabelProvider>(TreeLabelProvider),
54+
ctx.container.get<DefaultUriLabelProviderContribution>(DefaultUriLabelProviderContribution)
55+
];
56+
}
57+
})).inSingletonScope();
58+
59+
markerTreeLabelProvider = testContainer.get<MarkerTreeLabelProvider>(MarkerTreeLabelProvider);
60+
workspaceService = testContainer.get<WorkspaceService>(WorkspaceService);
61+
});
62+
63+
after(() => {
64+
disableJSDOM();
65+
});
66+
67+
describe('Marker Tree Label Provider', () => {
68+
69+
it('should return the filename and extension for #getName', () => {
70+
const label = markerTreeLabelProvider.getName(
71+
createMarkerInfoNode('a/b/c/foo.ts')
72+
);
73+
expect(label).equals('foo.ts');
74+
});
75+
76+
it('should return the folder name for #getLongName', async () => {
77+
78+
// Verify that the label provider successfully returns the directory name.
79+
let label = markerTreeLabelProvider.getLongName(
80+
createMarkerInfoNode('a/b/c/foo.ts')
81+
);
82+
expect(label).equals('/a/b/c');
83+
84+
// Verify that the label provider successfully returns the directory name (starting with a period).
85+
label = markerTreeLabelProvider.getLongName(
86+
createMarkerInfoNode('a/b/.c/foo.ts')
87+
);
88+
expect(label).equals('/a/b/.c');
89+
90+
// Verify that the label provider successfully returns the directory name (at the root).
91+
label = markerTreeLabelProvider.getLongName(
92+
createMarkerInfoNode('foo.ts')
93+
);
94+
expect(label).equals('/');
95+
96+
// Verify that the label provider successfully returns the directory and root name for a multiple root workspace.
97+
const uri: string = 'file:///file';
98+
const file = <FileStat>{
99+
uri: uri,
100+
lastModification: 0,
101+
isDirectory: false
102+
};
103+
const root1 = <FileStat>{
104+
uri: 'file:///root1',
105+
lastModification: 0,
106+
isDirectory: true
107+
};
108+
const root2 = <FileStat>{
109+
uri: 'file:///root2',
110+
lastModification: 0,
111+
isDirectory: true
112+
};
113+
workspaceService['_workspace'] = file;
114+
workspaceService['_roots'] = [root1, root2];
115+
label = markerTreeLabelProvider.getLongName(
116+
createMarkerInfoNode('file:///root1/foo/foo.ts')
117+
);
118+
expect(label).equals('root1 ● /root1/foo');
119+
120+
label = markerTreeLabelProvider.getLongName(
121+
createMarkerInfoNode('file:///root2/foo/foo.ts')
122+
);
123+
expect(label).equals('root2 ● /root2/foo');
124+
});
125+
126+
it('should return the filename and extension for #getIcon', () => {
127+
128+
// Verify that a typescript icon is returned for a typescript file.
129+
const typescriptIcon = markerTreeLabelProvider.getIcon(
130+
createMarkerInfoNode('a/b/c/foo.ts')
131+
);
132+
expect(typescriptIcon).contain('ts-icon');
133+
134+
// Verify that a json icon is returned for a json file.
135+
const jsonIcon = markerTreeLabelProvider.getIcon(
136+
createMarkerInfoNode('a/b/c/foo.json')
137+
);
138+
expect(jsonIcon).contain('database-icon');
139+
140+
// Verify that a markdown icon is returned for a markdown file.
141+
const markdownIcon = markerTreeLabelProvider.getIcon(
142+
createMarkerInfoNode('a/b/c/foo.md')
143+
);
144+
expect(markdownIcon).contain('markdown-icon');
145+
});
146+
147+
it('should return the parent\'s long name for #getDescription', () => {
148+
149+
let label = markerTreeLabelProvider.getDescription(
150+
createMarkerInfoNode('a/b/c/foo.ts')
151+
);
152+
expect(label).equals('/a/b/c');
153+
154+
label = markerTreeLabelProvider.getDescription(
155+
createMarkerInfoNode('foo.ts')
156+
);
157+
expect(label).equals('/');
158+
});
159+
160+
it('should successfully handle \'MarkerInfoNodes\'', () => {
161+
const node = createMarkerInfoNode('a/b/c/foo.ts');
162+
expect(markerTreeLabelProvider.canHandle(node)).greaterThan(0);
163+
});
164+
});
165+
166+
/**
167+
* Create a marker info node for test purposes.
168+
* @param uri the marker uri.
169+
*
170+
* @returns a mock marker info node.
171+
*/
172+
function createMarkerInfoNode(uri: string): MarkerInfoNode {
173+
return {
174+
id: 'id',
175+
parent: {
176+
id: 'parent-id',
177+
kind: '',
178+
parent: undefined,
179+
children: []
180+
},
181+
numberOfMarkers: 1,
182+
children: [],
183+
expanded: true,
184+
selected: true,
185+
uri: new URI(uri)
186+
};
187+
}

0 commit comments

Comments
 (0)