Skip to content

Commit

Permalink
Update some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jan 19, 2024
1 parent 59ea0dd commit ae7a111
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 178 deletions.
2 changes: 1 addition & 1 deletion lib/commands/context/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const NATIVE_WIN = 'NATIVE_APP';
export const WEBVIEW_WIN = 'WEBVIEW';
export const CHROMIUM_WIN = 'CHROMIUM';
export const WEBVIEW_BASE = `${WEBVIEW_WIN}_`;
const DEVTOOLS_SOCKET_PATTERN = /@[\w.]+_devtools_remote_?([\w.]+_)?(\d+)?\b/;
export const DEVTOOLS_SOCKET_PATTERN = /@[\w.]+_devtools_remote_?([\w.]+_)?(\d+)?\b/;
const WEBVIEW_PID_PATTERN = new RegExp(`^${WEBVIEW_BASE}(\\d+)`);
const WEBVIEW_PKG_PATTERN = new RegExp(`^${WEBVIEW_BASE}([^\\d\\s][\\w.]*)`);
const WEBVIEW_WAIT_INTERVAL_MS = 200;
Expand Down
20 changes: 10 additions & 10 deletions lib/commands/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {retryInterval} from 'asyncbox';
import _ from 'lodash';
import {requireArgs} from '../utils';

const NETWORK_KEYS = [
export const NETWORK_KEYS = [
[
'bucketStart',
'activeTime',
Expand All @@ -16,9 +16,9 @@ const NETWORK_KEYS = [
],
['st', 'activeTime', 'rb', 'rp', 'tb', 'tp', 'op', 'bucketDuration'],
];
const CPU_KEYS = /** @type {const} */ (['user', 'kernel']);
const BATTERY_KEYS = ['power'];
const MEMORY_KEYS = [
export const CPU_KEYS = /** @type {const} */ (['user', 'kernel']);
export const BATTERY_KEYS = ['power'];
export const MEMORY_KEYS = [
'totalPrivateDirty',
'nativePrivateDirty',
'dalvikPrivateDirty',
Expand All @@ -35,7 +35,7 @@ const MEMORY_KEYS = [
'dalvikRss',
'totalRss',
];
const SUPPORTED_PERFORMANCE_DATA_TYPES = Object.freeze({
export const SUPPORTED_PERFORMANCE_DATA_TYPES = Object.freeze({
cpuinfo:
'the amount of cpu by user and kernel process - cpu information for applications on real devices and simulators',
memoryinfo:
Expand All @@ -45,7 +45,7 @@ const SUPPORTED_PERFORMANCE_DATA_TYPES = Object.freeze({
networkinfo:
'the network statistics - network rx/tx information for applications on real devices and simulators',
});
const MEMINFO_TITLES = Object.freeze({
export const MEMINFO_TITLES = Object.freeze({
NATIVE: 'Native',
DALVIK: 'Dalvik',
EGL: 'EGL',
Expand Down Expand Up @@ -225,7 +225,7 @@ function parseMeminfoForApiAbove29(entries, valDict) {
* @param {string} packageName
* @param {number} retries
*/
async function getMemoryInfo(packageName, retries = 2) {
export async function getMemoryInfo(packageName, retries = 2) {
return await retryInterval(retries, RETRY_PAUSE_MS, async () => {
const cmd = [
'dumpsys',
Expand Down Expand Up @@ -267,7 +267,7 @@ async function getMemoryInfo(packageName, retries = 2) {
* @this {AndroidDriver}
* @param {number} retries
*/
async function getNetworkTrafficInfo(retries = 2) {
export async function getNetworkTrafficInfo(retries = 2) {
return await retryInterval(retries, RETRY_PAUSE_MS, async () => {
let returnValue = [];
let bucketDuration, bucketStart, activeTime, rxBytes, rxPackets, txBytes, txPackets, operations;
Expand Down Expand Up @@ -450,7 +450,7 @@ async function getNetworkTrafficInfo(retries = 2) {
* '14.3' is usage by the user (%), '28.2' is usage by the kernel (%)
* @throws {Error} If it failed to parse the result of dumpsys, or no package name exists.
*/
async function getCPUInfo(packageName, retries = 2) {
export async function getCPUInfo(packageName, retries = 2) {
// TODO: figure out why this is
// sometimes, the function of 'adb.shell' fails. when I tested this function on the target of 'Galaxy Note5',
// adb.shell(dumpsys cpuinfo) returns cpu datas for other application packages, but I can't find the data for packageName.
Expand Down Expand Up @@ -494,7 +494,7 @@ async function getCPUInfo(packageName, retries = 2) {
* @this {AndroidDriver}
* @param {number} retries
*/
async function getBatteryInfo(retries = 2) {
export async function getBatteryInfo(retries = 2) {
return await retryInterval(retries, RETRY_PAUSE_MS, async () => {
let cmd = ['dumpsys', 'battery', '|', 'grep', 'level'];
let data = await this.adb.shell(cmd);
Expand Down
22 changes: 11 additions & 11 deletions lib/commands/system-bars.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function getSystemBars() {
`Cannot retrieve system bars details. Original error: ${/** @type {Error} */ (e).message}`,
);
}
return parseWindows(stdout, this.log);
return parseWindows.bind(this)(stdout);
}

/**
Expand Down Expand Up @@ -86,19 +86,19 @@ export async function mobilePerformStatusBarCommand(opts) {
/**
* Parses window properties from adb dumpsys output
*
* @this {import('../driver').AndroidDriver}
* @param {string} name The name of the window whose properties are being parsed
* @param {Array<string>} props The list of particular window property lines.
* Check the corresponding unit tests for more details on the input format.
* @param {import('@appium/types').AppiumLogger} [log] Logger instance
* @returns {WindowProperties} Parsed properties object
* @throws {Error} If there was an issue while parsing the properties string
*/
export function parseWindowProperties(name, props, log) {
export function parseWindowProperties(name, props) {
const result = _.cloneDeep(DEFAULT_WINDOW_PROPERTIES);
const propLines = props.join('\n');
const frameMatch = FRAME_PATTERN.exec(propLines);
if (!frameMatch) {
log?.debug(propLines);
this.log.debug(propLines);
throw new Error(`Cannot parse the frame size from '${name}' window properties`);
}
result.x = parseFloat(frameMatch[1]);
Expand All @@ -107,7 +107,7 @@ export function parseWindowProperties(name, props, log) {
result.height = parseFloat(frameMatch[4]) - result.y;
const visibilityMatch = VIEW_VISIBILITY_PATTERN.exec(propLines);
if (!visibilityMatch) {
log?.debug(propLines);
this.log.debug(propLines);
throw new Error(`Cannot parse the visibility value from '${name}' window properties`);
}
result.visible = parseInt(visibilityMatch[1], 16) === VIEW_VISIBLE;
Expand All @@ -117,14 +117,14 @@ export function parseWindowProperties(name, props, log) {
/**
* Extracts status and navigation bar information from the window manager output.
*
* @this {import('../driver').AndroidDriver}
* @param {string} lines Output from dumpsys command.
* Check the corresponding unit tests for more details on the input format.
* @param {import('@appium/types').AppiumLogger} [log] Logger instance
* @return {StringRecord} An object containing two items where keys are statusBar and navigationBar,
* and values are corresponding WindowProperties objects
* @throws {Error} If no window properties could be parsed
*/
export function parseWindows(lines, log) {
export function parseWindows(lines) {
/**
* @type {StringRecord}
*/
Expand All @@ -147,25 +147,25 @@ export function parseWindows(lines, log) {
}
}
if (_.isEmpty(windows)) {
log?.debug(lines);
this.log.debug(lines);
throw new Error('Cannot parse any window information from the dumpsys output');
}

/** @type {{statusBar?: WindowProperties, navigationBar?: WindowProperties}} */
const result = {};
for (const [name, props] of _.toPairs(windows)) {
if (name.startsWith(STATUS_BAR_WINDOW_NAME_PREFIX)) {
result.statusBar = parseWindowProperties(name, props, log);
result.statusBar = parseWindowProperties.bind(this)(name, props);
} else if (name.startsWith(NAVIGATION_BAR_WINDOW_NAME_PREFIX)) {
result.navigationBar = parseWindowProperties(name, props, log);
result.navigationBar = parseWindowProperties.bind(this)(name, props);
}
}
const unmatchedWindows = /** @type {const} */ ([
['statusBar', STATUS_BAR_WINDOW_NAME_PREFIX],
['navigationBar', NAVIGATION_BAR_WINDOW_NAME_PREFIX],
]).filter(([name]) => _.isNil(result[name]));
for (const [window, namePrefix] of unmatchedWindows) {
log?.info(
this.log.info(
`No windows have been found whose title matches to ` +
`'${namePrefix}'. Assuming it is invisible. ` +
`Only the following windows are available: ${_.keys(windows)}`,
Expand Down
Binary file removed test/assets/chromedriver-2.20/linux-32/chromedriver
Binary file not shown.
Binary file removed test/assets/chromedriver-2.20/linux-64/chromedriver
Binary file not shown.
Binary file removed test/assets/chromedriver-2.20/mac/chromedriver
Binary file not shown.
Binary file not shown.
31 changes: 0 additions & 31 deletions test/functional/capabilities.js

This file was deleted.

41 changes: 0 additions & 41 deletions test/functional/helpers.js

This file was deleted.

32 changes: 17 additions & 15 deletions test/unit/commands/context-specs.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import * as webviewHelpers from '../../../lib/commands/context/helpers';
import {
default as webviewHelpers,
NATIVE_WIN,
WEBVIEW_BASE,
WEBVIEW_WIN,
CHROMIUM_WIN,
} from '../../../lib/helpers/webview';
} from '../../../lib/commands/context/helpers';
import {AndroidDriver} from '../../../lib/driver';
import Chromedriver from 'appium-chromedriver';
import {errors} from 'appium/driver';

/** @type {AndroidDriver} */
let driver;
/** @type {Chromedriver} */
let stubbedChromedriver;
let sandbox = sinon.createSandbox();
let expect = chai.expect;
Expand Down Expand Up @@ -49,7 +51,7 @@ describe('Context', function () {
});
it('should return NATIVE_APP if no context is set', async function () {
driver.curContext = null;
await driver.getCurrentContext().should.become(NATIVE_WIN);
await driver.getCurrentContext().should.become(webviewHelpers.NATIVE_WIN);
});
});
describe('getContexts', function () {
Expand Down Expand Up @@ -138,24 +140,24 @@ describe('Context', function () {
});
});
describe('defaultContextName', function () {
it('should return NATIVE_WIN', async function () {
await driver.defaultContextName().should.be.equal(NATIVE_WIN);
it('should return NATIVE_WIN', function () {
driver.defaultContextName().should.be.equal(NATIVE_WIN);
});
});
describe('defaultWebviewName', function () {
it('should return WEBVIEW with package if "autoWebviewName" option is not set', async function () {
it('should return WEBVIEW with package if "autoWebviewName" option is not set', function () {
driver.opts = {appPackage: 'pkg'};
await driver.defaultWebviewName().should.be.equal(WEBVIEW_BASE + 'pkg');
driver.defaultWebviewName().should.be.equal(WEBVIEW_BASE + 'pkg');
});
it('should return WEBVIEW with value from "autoWebviewName" option', async function () {
it('should return WEBVIEW with value from "autoWebviewName" option', function () {
driver.opts = {appPackage: 'pkg', autoWebviewName: 'foo'};
await driver.defaultWebviewName().should.be.equal(WEBVIEW_BASE + 'foo');
driver.defaultWebviewName().should.be.equal(WEBVIEW_BASE + 'foo');
});
});
describe('isWebContext', function () {
it('should return true if current context is not native', async function () {
it('should return true if current context is not native', function () {
driver.curContext = 'current_context';
await driver.isWebContext().should.be.true;
driver.isWebContext().should.be.true;
});
});
describe('startChromedriverProxy', function () {
Expand Down Expand Up @@ -216,7 +218,7 @@ describe('Context', function () {
});
describe('suspendChromedriverProxy', function () {
it('should suspend chrome driver proxy', async function () {
await driver.suspendChromedriverProxy();
driver.suspendChromedriverProxy();
(driver.chromedriver == null).should.be.true;
(driver.proxyReqRes == null).should.be.true;
driver.jwpProxyActive.should.be.false;
Expand Down Expand Up @@ -255,9 +257,9 @@ describe('Context', function () {
});
});
describe('isChromedriverContext', function () {
it('should return true if context is webview or chromium', async function () {
await driver.isChromedriverContext(WEBVIEW_WIN + '_1').should.be.true;
await driver.isChromedriverContext(CHROMIUM_WIN).should.be.true;
it('should return true if context is webview or chromium', function () {
driver.isChromedriverContext(WEBVIEW_WIN + '_1').should.be.true;
driver.isChromedriverContext(CHROMIUM_WIN).should.be.true;
});
});
describe('setupNewChromedriver', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import {AndroidDriver} from '../../../lib/driver';

/** @type {AndroidDriver} */
let driver;
let sandbox = sinon.createSandbox();
chai.should();
chai.use(chaiAsPromised);

describe('Execute', function () {
describe('Emulator Actions', function () {
beforeEach(function () {
driver = new AndroidDriver();
});
afterEach(function () {
sandbox.restore();
});
describe('execute', function () {
describe('sensorSet', function () {
it('should call sensorSet', async function () {
sandbox.stub(driver, 'sensorSet');
await driver.executeMobile('sensorSet', {sensorType: 'light', value: 0});
Expand Down
1 change: 1 addition & 0 deletions test/unit/commands/file-actions-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {AndroidDriver} from '../../../lib/driver';
import * as support from '@appium/support';
import ADB from 'appium-adb';

/** @type {AndroidDriver} */
let driver;
let sandbox = sinon.createSandbox();
chai.should();
Expand Down
1 change: 1 addition & 0 deletions test/unit/commands/ime-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ chai.should();
chai.use(chaiAsPromised);

describe('IME', function () {
/** @type {AndroidDriver} */
let driver;
let sandbox = sinon.createSandbox();
beforeEach(function () {
Expand Down
1 change: 1 addition & 0 deletions test/unit/commands/log-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ chai.should();
chai.use(chaiAsPromised);

describe('commands - logging', function () {
/** @type {AndroidDriver} */
let driver;
before(function () {
driver = new AndroidDriver();
Expand Down
3 changes: 3 additions & 0 deletions test/unit/commands/network-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import {AndroidDriver} from '../../../lib/driver';
import B from 'bluebird';
import { SettingsApp } from 'io.appium.settings';

/** @type {AndroidDriver} */
let driver;
/** @type {ADB} */
let adb;
/** @type {SettingsApp} */
let settingsApp;
let sandbox = sinon.createSandbox();
chai.should();
Expand Down
Loading

0 comments on commit ae7a111

Please sign in to comment.