2021-02-04 19:15:23 +01:00
|
|
|
import { strict as assert } from 'assert';
|
2020-10-06 19:57:02 +02:00
|
|
|
import {
|
|
|
|
getEnvironmentType,
|
|
|
|
sufficientBalance,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../app/scripts/lib/util';
|
2021-02-12 16:25:58 +01:00
|
|
|
import { isPrefixedFormattedHexString } from '../../../shared/modules/network.utils';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
|
|
|
import {
|
Close window after opening fullscreen (#6966)
* Add background environment type
The `getEnvironmentType` method now checks for the background
environment as well, instead of returning 'notification' for that case.
Instead of adding another regex for the background path, the regexes
for each environment have been replaced with the URL constructor[0].
This is the standard method of parsing URLs, and is available in all
supported browsers.
[0]: https://developer.mozilla.org/en-US/docs/Web/API/URL
* Add note regarding a missing manifest permission
The `url` parameter to `tabs.query(...)` requires the `tabs` permission,
and will be ignored otherwise. We are missing this permission, so that
call does not work.
* Close window after opening full screen
The browser behaviour when opening a new tab differs between Chrome and
Firefox. In the case of a popup, Chrome will close the popup whereas
Firefox will leave it open. In the case of the notification window,
Chrome will move the new tab to the foreground, whereas Firefox will
leave the notification window in the foreground when opening a new tab.
We always want to close the current UI (popup or notification) when
switching to a full-screen view. The only exception to this is when the
switch is triggered from the background, which has no UI.
Closes #6513, #6685
2019-08-08 16:50:32 +02:00
|
|
|
ENVIRONMENT_TYPE_POPUP,
|
|
|
|
ENVIRONMENT_TYPE_NOTIFICATION,
|
|
|
|
ENVIRONMENT_TYPE_FULLSCREEN,
|
|
|
|
ENVIRONMENT_TYPE_BACKGROUND,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../shared/constants/app';
|
2017-08-04 20:36:27 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('app utils', function () {
|
|
|
|
describe('getEnvironmentType', function () {
|
|
|
|
it('should return popup type', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const environmentType = getEnvironmentType(
|
|
|
|
'http://extension-id/popup.html',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(environmentType, ENVIRONMENT_TYPE_POPUP);
|
|
|
|
});
|
Close window after opening fullscreen (#6966)
* Add background environment type
The `getEnvironmentType` method now checks for the background
environment as well, instead of returning 'notification' for that case.
Instead of adding another regex for the background path, the regexes
for each environment have been replaced with the URL constructor[0].
This is the standard method of parsing URLs, and is available in all
supported browsers.
[0]: https://developer.mozilla.org/en-US/docs/Web/API/URL
* Add note regarding a missing manifest permission
The `url` parameter to `tabs.query(...)` requires the `tabs` permission,
and will be ignored otherwise. We are missing this permission, so that
call does not work.
* Close window after opening full screen
The browser behaviour when opening a new tab differs between Chrome and
Firefox. In the case of a popup, Chrome will close the popup whereas
Firefox will leave it open. In the case of the notification window,
Chrome will move the new tab to the foreground, whereas Firefox will
leave the notification window in the foreground when opening a new tab.
We always want to close the current UI (popup or notification) when
switching to a full-screen view. The only exception to this is when the
switch is triggered from the background, which has no UI.
Closes #6513, #6685
2019-08-08 16:50:32 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return notification type', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const environmentType = getEnvironmentType(
|
|
|
|
'http://extension-id/notification.html',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(environmentType, ENVIRONMENT_TYPE_NOTIFICATION);
|
|
|
|
});
|
Close window after opening fullscreen (#6966)
* Add background environment type
The `getEnvironmentType` method now checks for the background
environment as well, instead of returning 'notification' for that case.
Instead of adding another regex for the background path, the regexes
for each environment have been replaced with the URL constructor[0].
This is the standard method of parsing URLs, and is available in all
supported browsers.
[0]: https://developer.mozilla.org/en-US/docs/Web/API/URL
* Add note regarding a missing manifest permission
The `url` parameter to `tabs.query(...)` requires the `tabs` permission,
and will be ignored otherwise. We are missing this permission, so that
call does not work.
* Close window after opening full screen
The browser behaviour when opening a new tab differs between Chrome and
Firefox. In the case of a popup, Chrome will close the popup whereas
Firefox will leave it open. In the case of the notification window,
Chrome will move the new tab to the foreground, whereas Firefox will
leave the notification window in the foreground when opening a new tab.
We always want to close the current UI (popup or notification) when
switching to a full-screen view. The only exception to this is when the
switch is triggered from the background, which has no UI.
Closes #6513, #6685
2019-08-08 16:50:32 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return fullscreen type for home.html', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const environmentType = getEnvironmentType(
|
|
|
|
'http://extension-id/home.html',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(environmentType, ENVIRONMENT_TYPE_FULLSCREEN);
|
|
|
|
});
|
Close window after opening fullscreen (#6966)
* Add background environment type
The `getEnvironmentType` method now checks for the background
environment as well, instead of returning 'notification' for that case.
Instead of adding another regex for the background path, the regexes
for each environment have been replaced with the URL constructor[0].
This is the standard method of parsing URLs, and is available in all
supported browsers.
[0]: https://developer.mozilla.org/en-US/docs/Web/API/URL
* Add note regarding a missing manifest permission
The `url` parameter to `tabs.query(...)` requires the `tabs` permission,
and will be ignored otherwise. We are missing this permission, so that
call does not work.
* Close window after opening full screen
The browser behaviour when opening a new tab differs between Chrome and
Firefox. In the case of a popup, Chrome will close the popup whereas
Firefox will leave it open. In the case of the notification window,
Chrome will move the new tab to the foreground, whereas Firefox will
leave the notification window in the foreground when opening a new tab.
We always want to close the current UI (popup or notification) when
switching to a full-screen view. The only exception to this is when the
switch is triggered from the background, which has no UI.
Closes #6513, #6685
2019-08-08 16:50:32 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return fullscreen type for phishing.html', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const environmentType = getEnvironmentType(
|
|
|
|
'http://extension-id/phishing.html',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(environmentType, ENVIRONMENT_TYPE_FULLSCREEN);
|
|
|
|
});
|
2019-10-18 18:05:32 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return background type', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const environmentType = getEnvironmentType(
|
|
|
|
'http://extension-id/_generated_background_page.html',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(environmentType, ENVIRONMENT_TYPE_BACKGROUND);
|
|
|
|
});
|
Close window after opening fullscreen (#6966)
* Add background environment type
The `getEnvironmentType` method now checks for the background
environment as well, instead of returning 'notification' for that case.
Instead of adding another regex for the background path, the regexes
for each environment have been replaced with the URL constructor[0].
This is the standard method of parsing URLs, and is available in all
supported browsers.
[0]: https://developer.mozilla.org/en-US/docs/Web/API/URL
* Add note regarding a missing manifest permission
The `url` parameter to `tabs.query(...)` requires the `tabs` permission,
and will be ignored otherwise. We are missing this permission, so that
call does not work.
* Close window after opening full screen
The browser behaviour when opening a new tab differs between Chrome and
Firefox. In the case of a popup, Chrome will close the popup whereas
Firefox will leave it open. In the case of the notification window,
Chrome will move the new tab to the foreground, whereas Firefox will
leave the notification window in the foreground when opening a new tab.
We always want to close the current UI (popup or notification) when
switching to a full-screen view. The only exception to this is when the
switch is triggered from the background, which has no UI.
Closes #6513, #6685
2019-08-08 16:50:32 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return the correct type for a URL with a hash fragment', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const environmentType = getEnvironmentType(
|
|
|
|
'http://extension-id/popup.html#hash',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(environmentType, ENVIRONMENT_TYPE_POPUP);
|
|
|
|
});
|
Close window after opening fullscreen (#6966)
* Add background environment type
The `getEnvironmentType` method now checks for the background
environment as well, instead of returning 'notification' for that case.
Instead of adding another regex for the background path, the regexes
for each environment have been replaced with the URL constructor[0].
This is the standard method of parsing URLs, and is available in all
supported browsers.
[0]: https://developer.mozilla.org/en-US/docs/Web/API/URL
* Add note regarding a missing manifest permission
The `url` parameter to `tabs.query(...)` requires the `tabs` permission,
and will be ignored otherwise. We are missing this permission, so that
call does not work.
* Close window after opening full screen
The browser behaviour when opening a new tab differs between Chrome and
Firefox. In the case of a popup, Chrome will close the popup whereas
Firefox will leave it open. In the case of the notification window,
Chrome will move the new tab to the foreground, whereas Firefox will
leave the notification window in the foreground when opening a new tab.
We always want to close the current UI (popup or notification) when
switching to a full-screen view. The only exception to this is when the
switch is triggered from the background, which has no UI.
Closes #6513, #6685
2019-08-08 16:50:32 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return the correct type for a URL with query parameters', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const environmentType = getEnvironmentType(
|
|
|
|
'http://extension-id/popup.html?param=foo',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(environmentType, ENVIRONMENT_TYPE_POPUP);
|
|
|
|
});
|
Close window after opening fullscreen (#6966)
* Add background environment type
The `getEnvironmentType` method now checks for the background
environment as well, instead of returning 'notification' for that case.
Instead of adding another regex for the background path, the regexes
for each environment have been replaced with the URL constructor[0].
This is the standard method of parsing URLs, and is available in all
supported browsers.
[0]: https://developer.mozilla.org/en-US/docs/Web/API/URL
* Add note regarding a missing manifest permission
The `url` parameter to `tabs.query(...)` requires the `tabs` permission,
and will be ignored otherwise. We are missing this permission, so that
call does not work.
* Close window after opening full screen
The browser behaviour when opening a new tab differs between Chrome and
Firefox. In the case of a popup, Chrome will close the popup whereas
Firefox will leave it open. In the case of the notification window,
Chrome will move the new tab to the foreground, whereas Firefox will
leave the notification window in the foreground when opening a new tab.
We always want to close the current UI (popup or notification) when
switching to a full-screen view. The only exception to this is when the
switch is triggered from the background, which has no UI.
Closes #6513, #6685
2019-08-08 16:50:32 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return the correct type for a URL with query parameters and a hash fragment', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const environmentType = getEnvironmentType(
|
|
|
|
'http://extension-id/popup.html?param=foo#hash',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(environmentType, ENVIRONMENT_TYPE_POPUP);
|
|
|
|
});
|
|
|
|
});
|
2017-08-04 20:36:27 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('SufficientBalance', function () {
|
|
|
|
it('returns true if max tx cost is equal to balance.', function () {
|
|
|
|
const tx = {
|
2020-11-03 00:41:28 +01:00
|
|
|
value: '0x1',
|
|
|
|
gas: '0x2',
|
|
|
|
gasPrice: '0x3',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const balance = '0x8';
|
2017-08-04 20:36:27 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const result = sufficientBalance(tx, balance);
|
|
|
|
assert.ok(result, 'sufficient balance found.');
|
|
|
|
});
|
2017-08-04 20:36:27 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('returns true if max tx cost is less than balance.', function () {
|
|
|
|
const tx = {
|
2020-11-03 00:41:28 +01:00
|
|
|
value: '0x1',
|
|
|
|
gas: '0x2',
|
|
|
|
gasPrice: '0x3',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const balance = '0x9';
|
2017-08-04 20:36:27 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const result = sufficientBalance(tx, balance);
|
|
|
|
assert.ok(result, 'sufficient balance found.');
|
|
|
|
});
|
2017-08-04 20:36:27 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('returns false if max tx cost is more than balance.', function () {
|
|
|
|
const tx = {
|
2020-11-03 00:41:28 +01:00
|
|
|
value: '0x1',
|
|
|
|
gas: '0x2',
|
|
|
|
gasPrice: '0x3',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const balance = '0x6';
|
2017-08-04 20:36:27 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const result = sufficientBalance(tx, balance);
|
|
|
|
assert.ok(!result, 'insufficient balance found.');
|
|
|
|
});
|
|
|
|
});
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
describe('isPrefixedFormattedHexString', function () {
|
|
|
|
it('should return true for valid hex strings', function () {
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('0x1'),
|
|
|
|
true,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return true',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('0xa'),
|
|
|
|
true,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return true',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('0xabcd1123fae909aad87452'),
|
|
|
|
true,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return true',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
it('should return false for invalid hex strings', function () {
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('0x'),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('0x0'),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('0x01'),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString(' 0x1'),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('0x1 '),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('0x1afz'),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString('z'),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString(2),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
isPrefixedFormattedHexString(['0x1']),
|
|
|
|
false,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should return false',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(
|
|
|
|
isPrefixedFormattedHexString(),
|
|
|
|
false,
|
|
|
|
'should return false',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|