Skip to content

Commit

Permalink
extract some condition check and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuCocoa committed Jun 21, 2024
1 parent 060a432 commit 7c30588
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 10 deletions.
36 changes: 29 additions & 7 deletions lib/uiautomator2.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ class UiAutomator2Server {
return resultInfo;
}

/**
* @typedef {Object} PackageInfo
* @property {string} installState
* @property {string} appPath
* @property {string} appId
*/
/**
* @param {PackageInfo[]} packagesInfo
* @returns {boolean}
*/
shouldUninstallServerPackages(packagesInfo = []) {
return (packagesInfo.some(({installState}) => installState === this.adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED)
&& !packagesInfo.every(({installState}) => installState === this.adb.APP_INSTALL_STATE.NOT_INSTALLED));
}

/**
* @param {PackageInfo[]} packagesInfo
* @returns {boolean}
*/
canInstallServerPackages (packagesInfo = []) {
return packagesInfo.some(({installState}) => [
/** @type {string} */ (this.adb.APP_INSTALL_STATE.NOT_INSTALLED),
/** @type {string} */ (this.adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED),
].includes(installState));
}

/**
* Installs the apks on to the device or emulator.
*
Expand All @@ -117,15 +143,11 @@ class UiAutomator2Server {

this.log.debug(`Server packages status: ${JSON.stringify(packagesInfo)}`);
// Enforce server packages reinstall if any of the packages is not installed,
// or newer one than uia2 driver is going to use.
const shouldUninstallServerPackages = (packagesInfo.some(({installState}) => installState === this.adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED)
&& !packagesInfo.every(({installState}) => installState === this.adb.APP_INSTALL_STATE.NOT_INSTALLED));
// or new server packages are newer than servers on the deivce.
const shouldUninstallServerPackages = this.shouldUninstallServerPackages(packagesInfo);
// Install must always follow uninstall. Also, perform the install if
// any of server packages is not installed or is outdated
const shouldInstallServerPackages = shouldUninstallServerPackages || packagesInfo.some(({installState}) => [
this.adb.APP_INSTALL_STATE.NOT_INSTALLED,
this.adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
].includes(installState));
const shouldInstallServerPackages = shouldUninstallServerPackages || this.canInstallServerPackages(packagesInfo);
this.log.info(`Server packages are ${shouldInstallServerPackages ? '' : 'not '}going to be (re)installed`);
if (shouldInstallServerPackages && shouldUninstallServerPackages) {
this.log.info('Full packages reinstall is going to be performed');
Expand Down
181 changes: 178 additions & 3 deletions test/unit/uiautomator2-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,182 @@ chai.use(chaiAsPromised);

describe('UiAutomator2', function () {
const adb = new ADB();

describe('shouldUninstallServerPackages', function () {
let uiautomator2;
const serverApk = {
'appPath': 'path/to/appium-uiautomator2-server.apk',
'appId': 'io.appium.uiautomator2.server'
};
const serverTestApk = {
'appPath': 'path/to/appium-uiautomator2-server-test.apk',
'appId': 'io.appium.uiautomator2.server.test'
};
beforeEach(function () {
uiautomator2 = new UiAutomator2Server(log, {
adb,
tmpDir: 'tmp',
systemPort: 4724,
host: 'localhost',
devicePort: 6790,
disableWindowAnimation: false,
disableSuppressAccessibilityService: false
});
});
it('with newer servers are installed', function () {
uiautomator2.shouldUninstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED,
...serverApk
},
{
'installState': adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED,
...serverTestApk
}
]).should.eql(true);
}),
it('with newer server is installed but the other could be old one', function () {
// Then, enforce to uninstall all apks
uiautomator2.shouldUninstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED,
...serverApk
},
{
'installState': adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
...serverTestApk
}
]).should.eql(true);
}),
it('with newer server is installed', function () {
uiautomator2.shouldUninstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.SAME_VERSION_INSTALLED,
...serverApk
},
{
'installState': adb.APP_INSTALL_STATE.SAME_VERSION_INSTALLED,
...serverTestApk
}
]).should.eql(false);
}),
it('with older servers are installed', function () {
// then, installing newer serves are sufficient.
uiautomator2.shouldUninstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
...serverApk
},
{
'installState': adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
...serverTestApk
}
]).should.eql(false);
}),
it('with no server are installed', function () {
uiautomator2.shouldUninstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.NOT_INSTALLED,
'appPath': 'path/to/appium-uiautomator2-server.apk',
'appId': 'io.appium.uiautomator2.server'
},
{
'installState': adb.APP_INSTALL_STATE.NOT_INSTALLED,
'appPath': 'path/to/appium-uiautomator2-server-test.apk',
'appId': 'io.appium.uiautomator2.server.test'
}
]).should.eql(false);
});
});

describe('canInstallServerPackages', function () {
let uiautomator2;
const serverApk = {
'appPath': 'path/to/appium-uiautomator2-server.apk',
'appId': 'io.appium.uiautomator2.server'
};
const serverTestApk = {
'appPath': 'path/to/appium-uiautomator2-server-test.apk',
'appId': 'io.appium.uiautomator2.server.test'
};
beforeEach(function () {
uiautomator2 = new UiAutomator2Server(log, {
adb,
tmpDir: 'tmp',
systemPort: 4724,
host: 'localhost',
devicePort: 6790,
disableWindowAnimation: false,
disableSuppressAccessibilityService: false
});
});
it('with newer servers are installed', function () {
uiautomator2.canInstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED,
...serverApk
},
{
'installState': adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED,
...serverTestApk
}
// since installation may fail
]).should.eql(false);
}),
it('with newer server is installed but the other could be old one', function () {
// Then, enforce to uninstall all apks
uiautomator2.canInstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED,
...serverApk
},
{
'installState': adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
...serverTestApk
}
]).should.eql(true);
}),
it('with newer server is installed', function () {
uiautomator2.canInstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.SAME_VERSION_INSTALLED,
...serverApk
},
{
'installState': adb.APP_INSTALL_STATE.SAME_VERSION_INSTALLED,
...serverTestApk
}
]).should.eql(false);
}),
it('with older servers are installed', function () {
// then, installing newer serves are sufficient.
uiautomator2.canInstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
...serverApk
},
{
'installState': adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
...serverTestApk
}
]).should.eql(true);
}),
it('with no server are installed', function () {
uiautomator2.canInstallServerPackages([
{
'installState': adb.APP_INSTALL_STATE.NOT_INSTALLED,
'appPath': 'path/to/appium-uiautomator2-server.apk',
'appId': 'io.appium.uiautomator2.server'
},
{
'installState': adb.APP_INSTALL_STATE.NOT_INSTALLED,
'appPath': 'path/to/appium-uiautomator2-server-test.apk',
'appId': 'io.appium.uiautomator2.server.test'
}
]).should.eql(true);
});
});

describe('installServerApk', withMocks({adb, helpers}, (mocks) => {
let uiautomator2;
beforeEach(function () {
Expand All @@ -35,7 +211,7 @@ describe('UiAutomator2', function () {

// SERVER_PACKAGE_ID
mocks.adb.expects('getApplicationInstallState').once()
.returns(adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED);
.returns(adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED);

// SERVER_PACKAGE_ID and SERVER_TEST_PACKAGE_ID
mocks.adb.expects('checkApkCert').twice().returns(true);
Expand All @@ -58,7 +234,7 @@ describe('UiAutomator2', function () {

// SERVER_PACKAGE_ID
mocks.adb.expects('getApplicationInstallState').once()
.returns(adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED);
.returns(adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED);

// SERVER_PACKAGE_ID and SERVER_TEST_PACKAGE_ID
mocks.adb.expects('checkApkCert').twice().returns(true);
Expand Down Expand Up @@ -147,7 +323,6 @@ describe('UiAutomator2', function () {
await uiautomator2.installServerApk();
});


it('a server is installed but server.test is not', async function () {
mocks.helpers.expects('isWriteable').never();

Expand Down

0 comments on commit 7c30588

Please sign in to comment.