Skip to content

Commit 31e6805

Browse files
author
Alberto Iannaccone
committed
add serial unit tests
1 parent ba5eaed commit 31e6805

File tree

9 files changed

+483
-78
lines changed

9 files changed

+483
-78
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
6464
* This even also fires, when the boards package was not available for the currently selected board,
6565
* and the user installs the board package. Note: installing a board package will set the `fqbn` of the
6666
* currently selected board.\
67-
* This even also emitted when the board package for the currently selected board was uninstalled.
67+
* This event is also emitted when the board package for the currently selected board was uninstalled.
6868
*/
6969
readonly onBoardsConfigChanged = this.onBoardsConfigChangedEmitter.event;
7070
readonly onAvailableBoardsChanged =

Diff for: arduino-ide-extension/src/browser/monitor/monitor-connection.ts

+29-35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { injectable, inject, postConstruct } from 'inversify';
1+
import { injectable, inject } from 'inversify';
22
import { deepClone } from '@theia/core/lib/common/objects';
33
import { Emitter, Event } from '@theia/core/lib/common/event';
44
import { MessageService } from '@theia/core/lib/common/message-service';
@@ -24,31 +24,13 @@ import { nls } from '@theia/core/lib/browser/nls';
2424

2525
@injectable()
2626
export class SerialConnectionManager {
27-
@inject(MonitorModel)
28-
protected readonly monitorModel: MonitorModel;
29-
30-
@inject(MonitorService)
31-
protected readonly monitorService: MonitorService;
32-
33-
@inject(MonitorServiceClient)
34-
protected readonly monitorServiceClient: MonitorServiceClient;
35-
36-
@inject(BoardsService)
37-
protected readonly boardsService: BoardsService;
38-
39-
@inject(BoardsServiceProvider)
40-
protected readonly boardsServiceProvider: BoardsServiceProvider;
41-
42-
@inject(NotificationCenter)
43-
protected readonly notificationCenter: NotificationCenter;
44-
45-
@inject(MessageService)
46-
protected messageService: MessageService;
47-
48-
@inject(ThemeService)
49-
protected readonly themeService: ThemeService;
5027
protected _state: Serial.State = [];
51-
protected _connected: boolean;
28+
protected _connected = false;
29+
protected config: Partial<MonitorConfig> = {
30+
board: undefined,
31+
port: undefined,
32+
baudRate: undefined,
33+
};
5234

5335
/**
5436
* Note: The idea is to toggle this property from the UI (`Monitor` view)
@@ -73,17 +55,21 @@ export class SerialConnectionManager {
7355
* When the websocket server is up on the backend, we save the port here, so that the client knows how to connect to it
7456
* */
7557
protected wsPort?: number;
76-
7758
protected webSocket?: WebSocket;
7859

79-
protected config: Partial<MonitorConfig> = {
80-
board: undefined,
81-
port: undefined,
82-
baudRate: undefined,
83-
};
84-
85-
@postConstruct()
86-
protected init(): void {
60+
constructor(
61+
@inject(MonitorModel) protected readonly monitorModel: MonitorModel,
62+
@inject(MonitorService) protected readonly monitorService: MonitorService,
63+
@inject(MonitorServiceClient)
64+
protected readonly monitorServiceClient: MonitorServiceClient,
65+
@inject(BoardsService) protected readonly boardsService: BoardsService,
66+
@inject(BoardsServiceProvider)
67+
protected readonly boardsServiceProvider: BoardsServiceProvider,
68+
@inject(NotificationCenter)
69+
protected readonly notificationCenter: NotificationCenter,
70+
@inject(MessageService) protected messageService: MessageService,
71+
@inject(ThemeService) protected readonly themeService: ThemeService
72+
) {
8773
this.monitorServiceClient.onWebSocketChanged(
8874
this.handleWebSocketChanged.bind(this)
8975
);
@@ -136,7 +122,7 @@ export class SerialConnectionManager {
136122
*
137123
* @param newConfig the porperties of the config that has changed
138124
*/
139-
protected setConfig(newConfig: Partial<MonitorConfig>): void {
125+
setConfig(newConfig: Partial<MonitorConfig>): void {
140126
let configHasChanged = false;
141127
Object.keys(this.config).forEach((key: keyof MonitorConfig) => {
142128
if (newConfig[key] && newConfig[key] !== this.config[key]) {
@@ -149,10 +135,18 @@ export class SerialConnectionManager {
149135
}
150136
}
151137

138+
getConfig(): Partial<MonitorConfig> {
139+
return this.config;
140+
}
141+
152142
getWsPort(): number | undefined {
153143
return this.wsPort;
154144
}
155145

146+
isWebSocketConnected(): boolean {
147+
return !!this.webSocket?.url;
148+
}
149+
156150
protected handleWebSocketChanged(wsPort: number): void {
157151
this.wsPort = wsPort;
158152
}

Diff for: arduino-ide-extension/src/browser/monitor/monitor-service-client-impl.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export class MonitorServiceClientImpl implements MonitorServiceClient {
1212
protected readonly onErrorEmitter = new Emitter<MonitorError>();
1313
readonly onError = this.onErrorEmitter.event;
1414

15-
protected readonly onMessageEmitter = new Emitter<number>();
16-
readonly onWebSocketChanged = this.onMessageEmitter.event;
15+
protected readonly onWebSocketChangedEmitter = new Emitter<number>();
16+
readonly onWebSocketChanged = this.onWebSocketChangedEmitter.event;
1717

1818
protected readonly onBaudEmitter = new Emitter<MonitorConfig.BaudRate>();
1919
readonly onBaudRateChanged = this.onBaudEmitter.event;
@@ -26,7 +26,7 @@ export class MonitorServiceClientImpl implements MonitorServiceClient {
2626
}
2727

2828
notifyWebSocketChanged(message: number): void {
29-
this.onMessageEmitter.fire(message);
29+
this.onWebSocketChangedEmitter.fire(message);
3030
}
3131

3232
notifyBaudRateChanged(message: MonitorConfig.BaudRate): void {

Diff for: arduino-ide-extension/src/common/protocol/monitor-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface ErrorStatus extends Status {
1111
}
1212
export namespace Status {
1313
export function isOK(status: Status & { message?: string }): status is OK {
14-
return typeof status.message !== 'string';
14+
return !!status && typeof status.message !== 'string';
1515
}
1616
export const OK: OK = {};
1717
export const NOT_CONNECTED: ErrorStatus = { message: 'Not connected.' };

Diff for: arduino-ide-extension/src/test/browser/boards-auto-installer.test.ts

+1-34
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,19 @@ import { MessageService } from '@theia/core';
1111
import { BoardsServiceProvider } from '../../browser/boards/boards-service-provider';
1212
import { BoardsListWidgetFrontendContribution } from '../../browser/boards/boards-widget-frontend-contribution';
1313
import {
14-
Board,
1514
BoardsPackage,
1615
BoardsService,
17-
Port,
1816
ResponseServiceArduino,
1917
} from '../../common/protocol';
2018
import { IMock, It, Mock, Times } from 'typemoq';
2119
import { Container, ContainerModule } from 'inversify';
2220
import { BoardsAutoInstaller } from '../../browser/boards/boards-auto-installer';
23-
import { BoardsConfig } from '../../browser/boards/boards-config';
2421
import { tick } from '../utils';
2522
import { ListWidget } from '../../browser/widgets/component-list/list-widget';
23+
import { aBoardConfig, anInstalledPackage, aPackage } from './fixtures/boards';
2624

2725
disableJSDOM();
2826

29-
const aBoard: Board = {
30-
fqbn: 'some:board:fqbn',
31-
name: 'Some Arduino Board',
32-
port: { address: '/lol/port1234', protocol: 'serial' },
33-
};
34-
const aPort: Port = {
35-
address: aBoard.port!.address,
36-
protocol: aBoard.port!.protocol,
37-
};
38-
const aBoardConfig: BoardsConfig.Config = {
39-
selectedBoard: aBoard,
40-
selectedPort: aPort,
41-
};
42-
const aPackage: BoardsPackage = {
43-
author: 'someAuthor',
44-
availableVersions: ['some.ver.sion', 'some.other.version'],
45-
boards: [aBoard],
46-
deprecated: false,
47-
description: 'Some Arduino Board, Some Other Arduino Board',
48-
id: 'some:arduinoCoreId',
49-
installable: true,
50-
moreInfoLink: 'http://www.some-url.lol/',
51-
name: 'Some Arduino Package',
52-
summary: 'Boards included in this package:',
53-
};
54-
55-
const anInstalledPackage: BoardsPackage = {
56-
...aPackage,
57-
installedVersion: 'some.ver.sion',
58-
};
59-
6027
describe('BoardsAutoInstaller', () => {
6128
let subject: BoardsAutoInstaller;
6229
let messageService: IMock<MessageService>;
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { BoardsConfig } from '../../../browser/boards/boards-config';
2+
import { Board, BoardsPackage, Port } from '../../../common/protocol';
3+
4+
export const aBoard: Board = {
5+
fqbn: 'some:board:fqbn',
6+
name: 'Some Arduino Board',
7+
port: { address: '/lol/port1234', protocol: 'serial' },
8+
};
9+
export const aPort: Port = {
10+
address: aBoard.port!.address,
11+
protocol: aBoard.port!.protocol,
12+
};
13+
export const aBoardConfig: BoardsConfig.Config = {
14+
selectedBoard: aBoard,
15+
selectedPort: aPort,
16+
};
17+
export const anotherBoard: Board = {
18+
fqbn: 'another:board:fqbn',
19+
name: 'Another Arduino Board',
20+
port: { address: '/kek/port5678', protocol: 'serial' },
21+
};
22+
export const anotherPort: Port = {
23+
address: anotherBoard.port!.address,
24+
protocol: anotherBoard.port!.protocol,
25+
};
26+
export const anotherBoardConfig: BoardsConfig.Config = {
27+
selectedBoard: anotherBoard,
28+
selectedPort: anotherPort,
29+
};
30+
31+
export const aPackage: BoardsPackage = {
32+
author: 'someAuthor',
33+
availableVersions: ['some.ver.sion', 'some.other.version'],
34+
boards: [aBoard],
35+
deprecated: false,
36+
description: 'Some Arduino Board, Some Other Arduino Board',
37+
id: 'some:arduinoCoreId',
38+
installable: true,
39+
moreInfoLink: 'http://www.some-url.lol/',
40+
name: 'Some Arduino Package',
41+
summary: 'Boards included in this package:',
42+
};
43+
44+
export const anInstalledPackage: BoardsPackage = {
45+
...aPackage,
46+
installedVersion: 'some.ver.sion',
47+
};
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { MonitorConfig } from '../../../common/protocol/monitor-service';
2+
import { aBoard, anotherBoard, anotherPort, aPort } from './boards';
3+
4+
export const aSerialConfig: MonitorConfig = {
5+
board: aBoard,
6+
port: aPort,
7+
baudRate: 9600,
8+
};
9+
10+
export const anotherSerialConfig: MonitorConfig = {
11+
board: anotherBoard,
12+
port: anotherPort,
13+
baudRate: 9600,
14+
};
15+
16+
export class WebSocketMock {
17+
readonly url: string;
18+
constructor(url: string) {
19+
this.url = url;
20+
}
21+
close() {}
22+
}

0 commit comments

Comments
 (0)