Skip to content

Commit 75a5849

Browse files
author
Akos Kitta
committed
test: added service test
1 parent a496ac8 commit 75a5849

12 files changed

+244
-110
lines changed

.vscode/launch.json

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
"--colors",
9595
"**/${fileBasenameNoExtension}.js"
9696
],
97+
"outFiles": [
98+
"${workspaceRoot}/electron-app/src-gen/backend/*.js",
99+
"${workspaceRoot}/electron-app/src-gen/frontend/*.js",
100+
"${workspaceRoot}/electron-app/lib/**/*.js",
101+
"${workspaceRoot}/arduino-ide-extension/lib/**/*.js",
102+
"${workspaceRoot}/node_modules/@theia/**/*.js"
103+
],
97104
"env": {
98105
"TS_NODE_PROJECT": "${workspaceRoot}/tsconfig.json"
99106
},

arduino-ide-extension/src/browser/boards/boards-service-provider.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application';
22
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
3+
import { StorageService } from '@theia/core/lib/browser/storage-service';
34
import {
45
Command,
56
CommandContribution,
@@ -50,7 +51,6 @@ import type {
5051
StartupTaskProvider,
5152
} from '../../electron-common/startup-task';
5253
import { NotificationCenter } from '../notification-center';
53-
import { StorageWrapper } from '../storage-wrapper';
5454
import { BoardsDataStore } from './boards-data-store';
5555

5656
const boardListHistoryStorageKey = 'arduino-ide:boardListHistory';
@@ -140,6 +140,8 @@ export class BoardsServiceProvider
140140
private readonly boardsService: BoardsService;
141141
@inject(CommandService)
142142
private readonly commandService: CommandService;
143+
@inject(StorageService)
144+
private readonly storageService: StorageService;
143145
@inject(NotificationCenter)
144146
private readonly notificationCenter: NotificationCenter;
145147
@inject(FrontendApplicationStateService)
@@ -547,18 +549,11 @@ export class BoardsServiceProvider
547549
}
548550

549551
private setData<T>(key: string, value: T): Promise<void> {
550-
return this.commandService.executeCommand(
551-
StorageWrapper.Commands.SET_DATA.id,
552-
key,
553-
value
554-
);
552+
return this.storageService.setData(key, value);
555553
}
556554

557555
private getData<T>(key: string): Promise<T | undefined> {
558-
return this.commandService.executeCommand<T>(
559-
StorageWrapper.Commands.GET_DATA.id,
560-
key
561-
);
556+
return this.storageService.getData(key);
562557
}
563558
}
564559

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';
2+
const disableJSDOM = enableJSDOM();
3+
4+
import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
5+
FrontendApplicationConfigProvider.set({});
6+
7+
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
8+
import {
9+
LocalStorageService,
10+
StorageService,
11+
} from '@theia/core/lib/browser/storage-service';
12+
import { WindowService } from '@theia/core/lib/browser/window/window-service';
13+
import { CommandContribution } from '@theia/core/lib/common/command';
14+
import {
15+
Disposable,
16+
DisposableCollection,
17+
} from '@theia/core/lib/common/disposable';
18+
import { MessageService } from '@theia/core/lib/common/message-service';
19+
import { Container, ContainerModule } from '@theia/core/shared/inversify';
20+
import { expect } from 'chai';
21+
import { BoardsDataStore } from '../../browser/boards/boards-data-store';
22+
import { BoardsServiceProvider } from '../../browser/boards/boards-service-provider';
23+
import { NotificationCenter } from '../../browser/notification-center';
24+
import { StorageWrapper } from '../../browser/storage-wrapper';
25+
import { BoardsService, Port } from '../../common/protocol/boards-service';
26+
import { NotificationServiceServer } from '../../common/protocol/notification-service';
27+
import { bindCommon } from '../common/common-test-bindings';
28+
29+
disableJSDOM();
30+
31+
describe('board-service-provider', () => {
32+
let toDisposeAfterEach: DisposableCollection;
33+
let boardsServiceProvider: BoardsServiceProvider;
34+
let notificationCenter: NotificationCenter;
35+
36+
beforeEach(async () => {
37+
const container = createContainer();
38+
container.get<FrontendApplicationStateService>(
39+
FrontendApplicationStateService
40+
).state = 'ready';
41+
boardsServiceProvider = container.get<BoardsServiceProvider>(
42+
BoardsServiceProvider
43+
);
44+
notificationCenter = container.get<NotificationCenter>(NotificationCenter);
45+
toDisposeAfterEach = new DisposableCollection(
46+
Disposable.create(() => boardsServiceProvider.onStop())
47+
);
48+
boardsServiceProvider.onStart();
49+
await boardsServiceProvider.ready;
50+
});
51+
52+
afterEach(() => {
53+
toDisposeAfterEach.dispose();
54+
});
55+
56+
it('should detect a port change and find selection index', () => {
57+
const board = { name: 'board', fqbn: 'a:b:c' };
58+
const port: Port = {
59+
protocol: 'serial',
60+
protocolLabel: '',
61+
address: 'COM1',
62+
addressLabel: '',
63+
};
64+
const hasUpdated = boardsServiceProvider.updateConfig({
65+
selectedBoard: board,
66+
selectedPort: port,
67+
});
68+
expect(hasUpdated).to.be.true;
69+
expect(boardsServiceProvider.boardList.selectedIndex).to.be.equal(-1);
70+
71+
// attach board
72+
notificationCenter.notifyDetectedPortsDidChange({
73+
detectedPorts: {
74+
[Port.keyOf(port)]: { port, boards: [board] },
75+
},
76+
});
77+
expect(boardsServiceProvider.boardList.selectedIndex).to.be.equal(0);
78+
79+
// detach board
80+
notificationCenter.notifyDetectedPortsDidChange({
81+
detectedPorts: {},
82+
});
83+
expect(boardsServiceProvider.boardList.selectedIndex).to.be.equal(-1);
84+
});
85+
86+
function createContainer(): Container {
87+
const container = new Container({ defaultScope: 'Singleton' });
88+
container.load(
89+
new ContainerModule((bind) => {
90+
bindCommon(bind);
91+
bind(MessageService).toConstantValue(<MessageService>{});
92+
bind(BoardsService).toConstantValue(<BoardsService>{
93+
getDetectedPorts() {
94+
return {};
95+
},
96+
});
97+
bind(NotificationCenter).toSelf().inSingletonScope();
98+
bind(NotificationServiceServer).toConstantValue(<
99+
NotificationServiceServer
100+
>{
101+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
102+
setClient(_) {
103+
// nothing
104+
},
105+
});
106+
bind(FrontendApplicationStateService).toSelf().inSingletonScope();
107+
bind(BoardsDataStore).toConstantValue(<BoardsDataStore>{});
108+
bind(LocalStorageService).toSelf().inSingletonScope();
109+
bind(WindowService).toConstantValue(<WindowService>{});
110+
bind(StorageService).toService(LocalStorageService);
111+
bind(CommandContribution).toService(StorageWrapper);
112+
bind(BoardsServiceProvider).toSelf().inSingletonScope();
113+
})
114+
);
115+
return container;
116+
}
117+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Container, ContainerModule } from '@theia/core/shared/inversify';
2+
import { bindCommon } from '../common/common-test-bindings';
3+
4+
export function createBaseContainer(): Container {
5+
const container = new Container({ defaultScope: 'Singleton' });
6+
container.load(new ContainerModule((bind) => bindCommon(bind)));
7+
return container;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {
2+
CommandContribution,
3+
CommandRegistry,
4+
CommandService,
5+
} from '@theia/core/lib/common/command';
6+
import { bindContributionProvider } from '@theia/core/lib/common/contribution-provider';
7+
import { ILogger, Loggable } from '@theia/core/lib/common/logger';
8+
import { LogLevel } from '@theia/core/lib/common/logger-protocol';
9+
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
10+
import { injectable, interfaces } from '@theia/core/shared/inversify';
11+
12+
export function bindCommon(bind: interfaces.Bind): interfaces.Bind {
13+
bind(ConsoleLogger).toSelf().inSingletonScope();
14+
bind(ILogger).toService(ConsoleLogger);
15+
bind(CommandRegistry).toSelf().inSingletonScope();
16+
bind(CommandService).toService(CommandRegistry);
17+
bindContributionProvider(bind, CommandContribution);
18+
return bind;
19+
}
20+
21+
@injectable()
22+
export class ConsoleLogger extends MockLogger {
23+
override log(
24+
logLevel: number,
25+
arg2: string | Loggable | Error,
26+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27+
...params: any[]
28+
): Promise<void> {
29+
if (arg2 instanceof Error) {
30+
return this.error(String(arg2), params);
31+
}
32+
switch (logLevel) {
33+
case LogLevel.INFO:
34+
return this.info(arg2, params);
35+
case LogLevel.WARN:
36+
return this.warn(arg2, params);
37+
case LogLevel.TRACE:
38+
return this.trace(arg2, params);
39+
case LogLevel.ERROR:
40+
return this.error(arg2, params);
41+
case LogLevel.FATAL:
42+
return this.fatal(arg2, params);
43+
default:
44+
return this.info(arg2, params);
45+
}
46+
}
47+
48+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
49+
override async info(arg: string | Loggable, ...params: any[]): Promise<void> {
50+
if (params.length) {
51+
console.info(arg, ...params);
52+
} else {
53+
console.info(arg);
54+
}
55+
}
56+
57+
override async trace(
58+
arg: string | Loggable,
59+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
60+
...params: any[]
61+
): Promise<void> {
62+
if (params.length) {
63+
console.trace(arg, ...params);
64+
} else {
65+
console.trace(arg);
66+
}
67+
}
68+
69+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
70+
override async warn(arg: string | Loggable, ...params: any[]): Promise<void> {
71+
if (params.length) {
72+
console.warn(arg, ...params);
73+
} else {
74+
console.warn(arg);
75+
}
76+
}
77+
78+
override async error(
79+
arg: string | Loggable,
80+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
81+
...params: any[]
82+
): Promise<void> {
83+
if (params.length) {
84+
console.error(arg, ...params);
85+
} else {
86+
console.error(arg);
87+
}
88+
}
89+
90+
override async fatal(
91+
arg: string | Loggable,
92+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
93+
...params: any[]
94+
): Promise<void> {
95+
return this.error(arg, params);
96+
}
97+
}

arduino-ide-extension/src/test/node/boards-service-impl.slow-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DisposableCollection } from '@theia/core/lib/common/disposable';
22
import { Container } from '@theia/core/shared/inversify';
33
import { expect } from 'chai';
44
import { BoardSearch, BoardsService } from '../../common/protocol';
5-
import { createBaseContainer, startDaemon } from './test-bindings';
5+
import { createBaseContainer, startDaemon } from './node-test-bindings';
66

77
describe('boards-service-impl', () => {
88
let boardService: BoardsService;

arduino-ide-extension/src/test/node/clang-formatter.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
ClangFormatter,
1313
} from '../../node/clang-formatter';
1414
import { spawnCommand } from '../../node/exec-util';
15-
import { createBaseContainer, startDaemon } from './test-bindings';
15+
import { createBaseContainer, startDaemon } from './node-test-bindings';
1616

1717
const unformattedContent = `void setup ( ) { pinMode(LED_BUILTIN, OUTPUT);
1818
}

arduino-ide-extension/src/test/node/core-client-provider.slow-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
createCliConfig,
2525
newTempConfigDirPath,
2626
startDaemon,
27-
} from './test-bindings';
27+
} from './node-test-bindings';
2828

2929
const timeout = 5 * 60 * 1_000; // five minutes
3030

arduino-ide-extension/src/test/node/core-service-impl.slow-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
SketchesService,
1212
isCompileSummary,
1313
} from '../../common/protocol';
14-
import { createBaseContainer, startDaemon } from './test-bindings';
14+
import { createBaseContainer, startDaemon } from './node-test-bindings';
1515

1616
const testTimeout = 30_000;
1717
const setupTimeout = 5 * 60 * 1_000; // five minutes

arduino-ide-extension/src/test/node/library-service-impl.slow-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DisposableCollection } from '@theia/core/lib/common/disposable';
22
import { Container } from '@theia/core/shared/inversify';
33
import { expect } from 'chai';
44
import { LibrarySearch, LibraryService } from '../../common/protocol';
5-
import { createBaseContainer, startDaemon } from './test-bindings';
5+
import { createBaseContainer, startDaemon } from './node-test-bindings';
66

77
describe('library-service-impl', () => {
88
let libraryService: LibraryService;

0 commit comments

Comments
 (0)