Skip to content

Commit 42b8175

Browse files
author
Akos Kitta
committed
fix: unset board history when discovered board is selected on detected port
1 parent 75a5849 commit 42b8175

File tree

4 files changed

+279
-187
lines changed

4 files changed

+279
-187
lines changed

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export class BoardsServiceProvider
393393
}
394394

395395
if (selectedPort && selectedBoard) {
396-
this._boardListHistory[Port.keyOf(selectedPort)] = selectedBoard;
396+
this.updateBoardListHistory(selectedPort, selectedBoard);
397397
}
398398
if (
399399
previousSelectedPort !== undefined &&
@@ -438,7 +438,7 @@ export class BoardsServiceProvider
438438
const selectedBoard = this._boardsConfig.selectedBoard;
439439
const previousSelectedPort = this._boardsConfig.selectedPort;
440440
if (selectedPort && selectedBoard) {
441-
this._boardListHistory[Port.keyOf(selectedPort)] = selectedBoard;
441+
this.updateBoardListHistory(selectedPort, selectedBoard);
442442
}
443443
this._boardsConfig.selectedPort = selectedPort;
444444
if (portIdentifierEquals(previousSelectedPort, selectedPort)) {
@@ -454,6 +454,26 @@ export class BoardsServiceProvider
454454
return true;
455455
}
456456

457+
private updateBoardListHistory(
458+
selectedPort: PortIdentifier,
459+
selectedBoard: BoardIdentifier
460+
): { [portKey: string]: BoardIdentifier | undefined } {
461+
const match = this.boardList.items.find(
462+
(item) =>
463+
portIdentifierEquals(item.port, selectedPort) &&
464+
item.board &&
465+
boardIdentifierEquals(item.board, selectedBoard)
466+
);
467+
const portKey = Port.keyOf(selectedPort);
468+
// When board `B` is detected on port `P` and saving `B` on `P`, remove the entry instead!
469+
if (match) {
470+
delete this._boardListHistory[portKey];
471+
} else {
472+
this._boardListHistory[portKey] = selectedBoard;
473+
}
474+
return { [portKey]: match ? undefined : selectedBoard };
475+
}
476+
457477
get ready(): Promise<void> {
458478
return this._ready.promise;
459479
}

arduino-ide-extension/src/test/browser/board-service-provider.test.ts

+61-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ import { StorageWrapper } from '../../browser/storage-wrapper';
2525
import { BoardsService, Port } from '../../common/protocol/boards-service';
2626
import { NotificationServiceServer } from '../../common/protocol/notification-service';
2727
import { bindCommon } from '../common/common-test-bindings';
28+
import {
29+
detectedPort,
30+
esp32S3DevModule,
31+
mkr1000,
32+
mkr1000SerialPort,
33+
undiscoveredSerialPort,
34+
uno,
35+
unoSerialPort,
36+
} from '../common/fixtures';
2837

2938
disableJSDOM();
3039

@@ -54,33 +63,74 @@ describe('board-service-provider', () => {
5463
});
5564

5665
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-
};
66+
let boardList = boardsServiceProvider.boardList;
6467
const hasUpdated = boardsServiceProvider.updateConfig({
65-
selectedBoard: board,
66-
selectedPort: port,
68+
selectedBoard: uno,
69+
selectedPort: unoSerialPort,
6770
});
6871
expect(hasUpdated).to.be.true;
69-
expect(boardsServiceProvider.boardList.selectedIndex).to.be.equal(-1);
72+
expect(boardList.selectedIndex).to.be.equal(-1);
73+
let selectedItem = boardList.items[boardList.selectedIndex];
74+
expect(selectedItem).to.be.undefined;
7075

7176
// attach board
7277
notificationCenter.notifyDetectedPortsDidChange({
7378
detectedPorts: {
74-
[Port.keyOf(port)]: { port, boards: [board] },
79+
...detectedPort(unoSerialPort, uno),
7580
},
7681
});
82+
boardList = boardsServiceProvider.boardList;
7783
expect(boardsServiceProvider.boardList.selectedIndex).to.be.equal(0);
84+
selectedItem = boardList.items[boardList.selectedIndex];
85+
expect(selectedItem.board).to.be.deep.equal(uno);
86+
expect(selectedItem.port).to.be.deep.equal(unoSerialPort);
7887

7988
// detach board
8089
notificationCenter.notifyDetectedPortsDidChange({
8190
detectedPorts: {},
8291
});
92+
boardList = boardsServiceProvider.boardList;
8393
expect(boardsServiceProvider.boardList.selectedIndex).to.be.equal(-1);
94+
selectedItem = boardList.items[boardList.selectedIndex];
95+
expect(selectedItem).to.be.undefined;
96+
});
97+
98+
it('should update the board selection history for the port', () => {
99+
notificationCenter.notifyDetectedPortsDidChange({
100+
detectedPorts: {
101+
...detectedPort(undiscoveredSerialPort),
102+
...detectedPort(unoSerialPort, uno),
103+
...detectedPort(mkr1000SerialPort, mkr1000),
104+
},
105+
});
106+
107+
boardsServiceProvider.updateConfig({
108+
selectedBoard: esp32S3DevModule,
109+
selectedPort: undiscoveredSerialPort,
110+
});
111+
112+
expect(boardsServiceProvider['_boardListHistory']).to.be.deep.equal({
113+
[Port.keyOf(undiscoveredSerialPort)]: esp32S3DevModule,
114+
});
115+
116+
boardsServiceProvider.updateConfig({
117+
selectedBoard: esp32S3DevModule,
118+
selectedPort: unoSerialPort,
119+
});
120+
121+
expect(boardsServiceProvider['_boardListHistory']).to.be.deep.equal({
122+
[Port.keyOf(undiscoveredSerialPort)]: esp32S3DevModule,
123+
[Port.keyOf(unoSerialPort)]: esp32S3DevModule,
124+
});
125+
126+
boardsServiceProvider.updateConfig({
127+
selectedBoard: uno,
128+
selectedPort: unoSerialPort,
129+
});
130+
131+
expect(boardsServiceProvider['_boardListHistory']).to.be.deep.equal({
132+
[Port.keyOf(undiscoveredSerialPort)]: esp32S3DevModule,
133+
});
84134
});
85135

86136
function createContainer(): Container {

arduino-ide-extension/src/test/common/board-list.test.ts

+20-174
Original file line numberDiff line numberDiff line change
@@ -7,183 +7,29 @@ import {
77
SelectBoardsConfigActionParams,
88
} from '../../common/protocol/board-list';
99
import {
10-
BoardIdentifier,
11-
DetectedPort,
12-
DetectedPorts,
1310
emptyBoardsConfig,
14-
Port,
1511
unconfirmedBoard,
1612
} from '../../common/protocol/boards-service';
17-
18-
const mkr1000: BoardIdentifier = {
19-
name: 'Arduino MKR1000',
20-
fqbn: 'arduino:samd:mkr1000',
21-
};
22-
const uno: BoardIdentifier = {
23-
name: 'Arduino Uno',
24-
fqbn: 'arduino:avr:uno',
25-
};
26-
const arduinoNanoEsp32: BoardIdentifier = {
27-
fqbn: 'arduino:esp32:nano_nora',
28-
name: 'Arduino Nano ESP32',
29-
};
30-
const esp32NanoEsp32: BoardIdentifier = {
31-
fqbn: 'esp32:esp32:nano_nora',
32-
name: 'Arduino Nano ESP32',
33-
};
34-
const esp32S3DevModule: BoardIdentifier = {
35-
name: 'ESP32S3 Dev Module',
36-
fqbn: 'esp32:esp32:esp32s3',
37-
};
38-
const esp32S3Box: BoardIdentifier = {
39-
name: 'ESP32-S3-Box',
40-
fqbn: 'esp32:esp32:esp32s3box',
41-
};
42-
43-
const bluetoothSerialPort: Port = {
44-
address: '/dev/cu.Bluetooth-Incoming-Port',
45-
addressLabel: '/dev/cu.Bluetooth-Incoming-Port',
46-
protocol: 'serial',
47-
protocolLabel: 'Serial Port',
48-
properties: {},
49-
hardwareId: '',
50-
};
51-
const builtinSerialPort: Port = {
52-
address: '/dev/cu.BLTH',
53-
addressLabel: '/dev/cu.BLTH',
54-
protocol: 'serial',
55-
protocolLabel: 'Serial Port',
56-
properties: {},
57-
hardwareId: '',
58-
};
59-
const undiscoveredSerialPort: Port = {
60-
address: '/dev/cu.usbserial-0001',
61-
addressLabel: '/dev/cu.usbserial-0001',
62-
protocol: 'serial',
63-
protocolLabel: 'Serial Port (USB)',
64-
properties: {
65-
pid: '0xEA60',
66-
serialNumber: '0001',
67-
vid: '0x10C4',
68-
},
69-
hardwareId: '0001',
70-
};
71-
const mkr1000NetworkPort: Port = {
72-
address: '192.168.0.104',
73-
addressLabel: 'Arduino at 192.168.0.104',
74-
protocol: 'network',
75-
protocolLabel: 'Network Port',
76-
properties: {
77-
'.': 'mkr1000',
78-
auth_upload: 'yes',
79-
board: 'mkr1000',
80-
hostname: 'Arduino.local.',
81-
port: '65280',
82-
ssh_upload: 'no',
83-
tcp_check: 'no',
84-
},
85-
hardwareId: '',
86-
};
87-
const undiscoveredUsbToUARTSerialPort: Port = {
88-
address: '/dev/cu.SLAB_USBtoUART',
89-
addressLabel: '/dev/cu.SLAB_USBtoUART',
90-
protocol: 'serial',
91-
protocolLabel: 'Serial Port (USB)',
92-
properties: {
93-
pid: '0xEA60',
94-
serialNumber: '0001',
95-
vid: '0x10C4',
96-
},
97-
hardwareId: '0001',
98-
};
99-
const mkr1000SerialPort: Port = {
100-
address: '/dev/cu.usbmodem14301',
101-
addressLabel: '/dev/cu.usbmodem14301',
102-
protocol: 'serial',
103-
protocolLabel: 'Serial Port (USB)',
104-
properties: {
105-
pid: '0x804E',
106-
serialNumber: '94A3397C5150435437202020FF150838',
107-
vid: '0x2341',
108-
},
109-
hardwareId: '94A3397C5150435437202020FF150838',
110-
};
111-
const unoSerialPort: Port = {
112-
address: '/dev/cu.usbmodem14201',
113-
addressLabel: '/dev/cu.usbmodem14201',
114-
protocol: 'serial',
115-
protocolLabel: 'Serial Port (USB)',
116-
properties: {
117-
pid: '0x0043',
118-
serialNumber: '75830303934351618212',
119-
vid: '0x2341',
120-
},
121-
hardwareId: '75830303934351618212',
122-
};
123-
const nanoEsp32SerialPort: Port = {
124-
address: '/dev/cu.usbmodem3485187BD9882',
125-
addressLabel: '/dev/cu.usbmodem3485187BD9882',
126-
protocol: 'serial',
127-
protocolLabel: 'Serial Port (USB)',
128-
properties: {
129-
pid: '0x0070',
130-
serialNumber: '3485187BD988',
131-
vid: '0x2341',
132-
},
133-
hardwareId: '3485187BD988',
134-
};
135-
const nanoEsp32DetectsMultipleEsp32BoardsSerialPort: Port = {
136-
address: 'COM41',
137-
addressLabel: 'COM41',
138-
protocol: 'serial',
139-
protocolLabel: 'Serial Port (USB)',
140-
properties: {
141-
pid: '0x1001',
142-
serialNumber: '',
143-
vid: '0x303A',
144-
},
145-
};
146-
147-
function createPort(address: string, protocol = 'serial'): Port {
148-
return {
149-
address,
150-
addressLabel: `Address label: ${address}`,
151-
protocol,
152-
protocolLabel: `Protocol label: ${protocol}`,
153-
};
154-
}
155-
156-
function detectedPort(
157-
port: Port,
158-
...boards: BoardIdentifier[]
159-
): { [portKey: string]: DetectedPort } {
160-
return { [Port.keyOf(port)]: boards.length ? { port, boards } : { port } };
161-
}
162-
163-
function history(
164-
port: Port,
165-
board: BoardIdentifier
166-
): { [portKey: string]: BoardIdentifier } {
167-
return { [Port.keyOf(port)]: board };
168-
}
169-
170-
const detectedPorts: DetectedPorts = {
171-
...detectedPort(builtinSerialPort),
172-
...detectedPort(bluetoothSerialPort),
173-
...detectedPort(unoSerialPort, uno),
174-
...detectedPort(mkr1000SerialPort, mkr1000),
175-
...detectedPort(mkr1000NetworkPort, mkr1000),
176-
...detectedPort(undiscoveredSerialPort),
177-
...detectedPort(undiscoveredUsbToUARTSerialPort),
178-
// multiple discovered on the same port with different board names
179-
...detectedPort(
180-
nanoEsp32DetectsMultipleEsp32BoardsSerialPort,
181-
esp32S3DevModule,
182-
esp32S3Box
183-
),
184-
// multiple discovered on the same port with the same board name
185-
...detectedPort(nanoEsp32SerialPort, arduinoNanoEsp32, esp32NanoEsp32),
186-
};
13+
import {
14+
arduinoNanoEsp32,
15+
bluetoothSerialPort,
16+
builtinSerialPort,
17+
createPort,
18+
detectedPort,
19+
detectedPorts,
20+
esp32NanoEsp32,
21+
esp32S3Box,
22+
esp32S3DevModule,
23+
history,
24+
mkr1000,
25+
mkr1000SerialPort,
26+
nanoEsp32DetectsMultipleEsp32BoardsSerialPort,
27+
nanoEsp32SerialPort,
28+
undiscoveredSerialPort,
29+
undiscoveredUsbToUARTSerialPort,
30+
uno,
31+
unoSerialPort,
32+
} from './fixtures';
18733

18834
describe('board-list', () => {
18935
describe('createBoardList', () => {

0 commit comments

Comments
 (0)