Cleaned up deviceType enum

Summary:
The device type enum was mixing two different concepts (emulator vs physical) and (archived vs not), and we already have a separate `isArchived` field. So cleaned this up to not leak it into sandy.

If anybody can think of any unforeseen consequences of this, lemme know :)

Reviewed By: jknoxville

Differential Revision: D22763506

fbshipit-source-id: bd2f7dbd1d2d2e6942ba7c6ddd8dc91ee34d591d
This commit is contained in:
Michel Weststrate
2020-08-04 07:44:56 -07:00
committed by Facebook GitHub Bot
parent fbb1c78184
commit d538b66088
10 changed files with 36 additions and 45 deletions

View File

@@ -7,12 +7,12 @@
* @format * @format
*/ */
import BaseDevice, {DeviceType} from './BaseDevice'; import BaseDevice from './BaseDevice';
import adb, {Client as ADBClient} from 'adbkit'; import adb, {Client as ADBClient} from 'adbkit';
import {Priority} from 'adbkit-logcat'; import {Priority} from 'adbkit-logcat';
import ArchivedDevice from './ArchivedDevice'; import ArchivedDevice from './ArchivedDevice';
import {createWriteStream} from 'fs'; import {createWriteStream} from 'fs';
import {LogLevel} from 'flipper-plugin'; import {LogLevel, DeviceType} from 'flipper-plugin';
const DEVICE_RECORDING_DIR = '/sdcard/flipper_recorder'; const DEVICE_RECORDING_DIR = '/sdcard/flipper_recorder';

View File

@@ -7,21 +7,11 @@
* @format * @format
*/ */
import {DeviceLogEntry} from 'flipper-plugin'; import {DeviceLogEntry, DeviceType} from 'flipper-plugin';
import BaseDevice from './BaseDevice'; import BaseDevice from './BaseDevice';
import {DeviceType, OS, DeviceShell} from './BaseDevice'; import {OS, DeviceShell} from './BaseDevice';
import {SupportFormRequestDetailsState} from '../reducers/supportForm'; import {SupportFormRequestDetailsState} from '../reducers/supportForm';
function normalizeArchivedDeviceType(deviceType: DeviceType): DeviceType {
let archivedDeviceType = deviceType;
if (archivedDeviceType === 'emulator') {
archivedDeviceType = 'archivedEmulator';
} else if (archivedDeviceType === 'physical') {
archivedDeviceType = 'archivedPhysical';
}
return archivedDeviceType;
}
export default class ArchivedDevice extends BaseDevice { export default class ArchivedDevice extends BaseDevice {
constructor(options: { constructor(options: {
serial: string; serial: string;
@@ -33,12 +23,7 @@ export default class ArchivedDevice extends BaseDevice {
source?: string; source?: string;
supportRequestDetails?: SupportFormRequestDetailsState; supportRequestDetails?: SupportFormRequestDetailsState;
}) { }) {
super( super(options.serial, options.deviceType, options.title, options.os);
options.serial,
normalizeArchivedDeviceType(options.deviceType),
options.title,
options.os,
);
this.logs = options.logEntries; this.logs = options.logEntries;
this.source = options.source || ''; this.source = options.source || '';
this.supportRequestDetails = options.supportRequestDetails; this.supportRequestDetails = options.supportRequestDetails;

View File

@@ -14,6 +14,7 @@ import {
DeviceLogEntry, DeviceLogEntry,
SandyDevicePluginInstance, SandyDevicePluginInstance,
SandyPluginDefinition, SandyPluginDefinition,
DeviceType,
} from 'flipper-plugin'; } from 'flipper-plugin';
import {DevicePluginMap, FlipperDevicePlugin} from '../plugin'; import {DevicePluginMap, FlipperDevicePlugin} from '../plugin';
@@ -23,12 +24,6 @@ export type DeviceShell = {
stdin: stream.Writable; stdin: stream.Writable;
}; };
export type DeviceType =
| 'emulator'
| 'physical'
| 'archivedEmulator'
| 'archivedPhysical';
export type DeviceExport = { export type DeviceExport = {
os: OS; os: OS;
title: string; title: string;

View File

@@ -7,7 +7,8 @@
* @format * @format
*/ */
import BaseDevice, {OS, DeviceType} from './BaseDevice'; import BaseDevice, {OS} from './BaseDevice';
import {DeviceType} from 'flipper-plugin';
export default class FlipperSelfInspectionDevice extends BaseDevice { export default class FlipperSelfInspectionDevice extends BaseDevice {
constructor(serial: string, deviceType: DeviceType, title: string, os: OS) { constructor(serial: string, deviceType: DeviceType, title: string, os: OS) {

View File

@@ -7,8 +7,7 @@
* @format * @format
*/ */
import {LogLevel, DeviceLogEntry} from 'flipper-plugin'; import {LogLevel, DeviceLogEntry, DeviceType} from 'flipper-plugin';
import {DeviceType} from './BaseDevice';
import child_process, {ChildProcess} from 'child_process'; import child_process, {ChildProcess} from 'child_process';
import BaseDevice from './BaseDevice'; import BaseDevice from './BaseDevice';
import JSONStream from 'JSONStream'; import JSONStream from 'JSONStream';

View File

@@ -11,7 +11,7 @@ import {ChildProcess} from 'child_process';
import {Store} from '../reducers/index'; import {Store} from '../reducers/index';
import {setXcodeDetected} from '../reducers/application'; import {setXcodeDetected} from '../reducers/application';
import {Logger} from '../fb-interfaces/Logger'; import {Logger} from '../fb-interfaces/Logger';
import {DeviceType} from '../devices/BaseDevice'; import {DeviceType} from 'flipper-plugin';
import {promisify} from 'util'; import {promisify} from 'util';
import path from 'path'; import path from 'path';
import child_process from 'child_process'; import child_process from 'child_process';

View File

@@ -104,8 +104,8 @@ test('test generateClientIndentifierWithSalt helper function', () => {
}); });
const identifier = generateClientIdentifier(device, 'app'); const identifier = generateClientIdentifier(device, 'app');
const saltIdentifier = generateClientIdentifierWithSalt(identifier, 'salt'); const saltIdentifier = generateClientIdentifierWithSalt(identifier, 'salt');
expect(saltIdentifier).toEqual('app#iOS#archivedEmulator#salt-serial'); expect(saltIdentifier).toEqual('app#iOS#emulator#salt-serial');
expect(identifier).toEqual('app#iOS#archivedEmulator#serial'); expect(identifier).toEqual('app#iOS#emulator#serial');
}); });
test('test generateClientFromClientWithSalt helper function', () => { test('test generateClientFromClientWithSalt helper function', () => {
@@ -120,20 +120,20 @@ test('test generateClientFromClientWithSalt helper function', () => {
const client = generateClientFromDevice(device, 'app'); const client = generateClientFromDevice(device, 'app');
const saltedClient = generateClientFromClientWithSalt(client, 'salt'); const saltedClient = generateClientFromClientWithSalt(client, 'salt');
expect(saltedClient).toEqual({ expect(saltedClient).toEqual({
id: 'app#iOS#archivedEmulator#salt-serial', id: 'app#iOS#emulator#salt-serial',
query: { query: {
app: 'app', app: 'app',
os: 'iOS', os: 'iOS',
device: 'archivedEmulator', device: 'emulator',
device_id: 'salt-serial', device_id: 'salt-serial',
}, },
}); });
expect(client).toEqual({ expect(client).toEqual({
id: 'app#iOS#archivedEmulator#serial', id: 'app#iOS#emulator#serial',
query: { query: {
app: 'app', app: 'app',
os: 'iOS', os: 'iOS',
device: 'archivedEmulator', device: 'emulator',
device_id: 'serial', device_id: 'serial',
}, },
}); });
@@ -150,11 +150,11 @@ test('test generateClientFromDevice helper function', () => {
}); });
const client = generateClientFromDevice(device, 'app'); const client = generateClientFromDevice(device, 'app');
expect(client).toEqual({ expect(client).toEqual({
id: 'app#iOS#archivedEmulator#serial', id: 'app#iOS#emulator#serial',
query: { query: {
app: 'app', app: 'app',
os: 'iOS', os: 'iOS',
device: 'archivedEmulator', device: 'emulator',
device_id: 'serial', device_id: 'serial',
}, },
}); });
@@ -170,7 +170,7 @@ test('test generateClientIdentifier helper function', () => {
screenshotHandle: null, screenshotHandle: null,
}); });
const identifier = generateClientIdentifier(device, 'app'); const identifier = generateClientIdentifier(device, 'app');
expect(identifier).toEqual('app#iOS#archivedEmulator#serial'); expect(identifier).toEqual('app#iOS#emulator#serial');
}); });
test('test generateNotifications helper function', () => { test('test generateNotifications helper function', () => {
@@ -231,7 +231,7 @@ test('test processStore function for an iOS device connected', async () => {
} }
const {serial, deviceType, title, os} = device; const {serial, deviceType, title, os} = device;
expect(serial).toEqual('salt-serial'); expect(serial).toEqual('salt-serial');
expect(deviceType).toEqual('archivedEmulator'); expect(deviceType).toEqual('emulator');
expect(title).toEqual('TestiPhone'); expect(title).toEqual('TestiPhone');
expect(os).toEqual('iOS'); expect(os).toEqual('iOS');
const {pluginStates, activeNotifications} = json.store; const {pluginStates, activeNotifications} = json.store;
@@ -1045,7 +1045,7 @@ test('Sandy plugins are imported properly', async () => {
}, },
], ],
device: { device: {
deviceType: 'archivedPhysical', deviceType: 'physical',
logs: [], logs: [],
os: 'Android', os: 'Android',
serial: '2e52cea6-94b0-4ea1-b9a8-c9135ede14ca-serial', serial: '2e52cea6-94b0-4ea1-b9a8-c9135ede14ca-serial',

View File

@@ -20,6 +20,7 @@ export {
DevicePluginClient, DevicePluginClient,
LogLevel, LogLevel,
SandyDevicePluginInstance, SandyDevicePluginInstance,
DeviceType,
} from './plugin/DevicePlugin'; } from './plugin/DevicePlugin';
export {SandyPluginDefinition} from './plugin/SandyPluginDefinition'; export {SandyPluginDefinition} from './plugin/SandyPluginDefinition';
export {SandyPluginRenderer} from './plugin/PluginRenderer'; export {SandyPluginRenderer} from './plugin/PluginRenderer';

View File

@@ -32,10 +32,14 @@ export type LogLevel =
| 'fatal'; | 'fatal';
export interface Device { export interface Device {
isArchived: boolean; readonly isArchived: boolean;
readonly os: string;
readonly deviceType: DeviceType;
onLogEntry(cb: DeviceLogListener): () => void; onLogEntry(cb: DeviceLogListener): () => void;
} }
export type DeviceType = 'emulator' | 'physical';
export type DevicePluginPredicate = (device: Device) => boolean; export type DevicePluginPredicate = (device: Device) => boolean;
export type DevicePluginFactory = (client: DevicePluginClient) => object; export type DevicePluginFactory = (client: DevicePluginClient) => object;
@@ -48,7 +52,9 @@ export interface DevicePluginClient extends BasePluginClient {
* Wrapper interface around BaseDevice in Flipper * Wrapper interface around BaseDevice in Flipper
*/ */
export interface RealFlipperDevice { export interface RealFlipperDevice {
os: string;
isArchived: boolean; isArchived: boolean;
deviceType: DeviceType;
addLogListener(callback: DeviceLogListener): Symbol; addLogListener(callback: DeviceLogListener): Symbol;
removeLogListener(id: Symbol): void; removeLogListener(id: Symbol): void;
addLogEntry(entry: DeviceLogEntry): void; addLogEntry(entry: DeviceLogEntry): void;
@@ -69,9 +75,11 @@ export class SandyDevicePluginInstance extends BasePluginInstance {
) { ) {
super(definition, initialStates); super(definition, initialStates);
const device: Device = { const device: Device = {
get isArchived() { // N.B. we model OS as string, not as enum, to make custom device types possible in the future
return realDevice.isArchived; os: realDevice.os,
}, isArchived: realDevice.isArchived,
deviceType: realDevice.deviceType,
onLogEntry(cb) { onLogEntry(cb) {
const handle = realDevice.addLogListener(cb); const handle = realDevice.addLogListener(cb);
return () => { return () => {

View File

@@ -341,6 +341,8 @@ export function createMockPluginDetails(
function createMockDevice(options?: StartPluginOptions): RealFlipperDevice { function createMockDevice(options?: StartPluginOptions): RealFlipperDevice {
const logListeners: (undefined | DeviceLogListener)[] = []; const logListeners: (undefined | DeviceLogListener)[] = [];
return { return {
os: 'Android',
deviceType: 'emulator',
isArchived: !!options?.isArchived, isArchived: !!options?.isArchived,
addLogListener(cb) { addLogListener(cb) {
logListeners.push(cb); logListeners.push(cb);