Commit 710b8488 authored by Oleh Lamzin's avatar Oleh Lamzin Committed by Commit Bot

[Telemetry SWX] add non removable block device browser tests

Bug: b:158658869
Change-Id: I6685b09c455156ad21a62d18546c61bd6a966cea
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2362595
Commit-Queue: Oleh Lamzin <lamzin@google.com>
Reviewed-by: default avatarMahmoud Gawad <mgawad@google.com>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#800096}
parent 72642e63
...@@ -301,9 +301,10 @@ class TelemetryProxy { ...@@ -301,9 +301,10 @@ class TelemetryProxy {
/** /**
* This method converts Mojo types to WebIDL types applying next rules: * This method converts Mojo types to WebIDL types applying next rules:
* 1. convert objects like { value: X } to X, where X is a number; * 1. remove null objects from arrays;
* 2. omit null/undefined properties; * 2. convert objects like { value: X } to X, where X is a number;
* 3. convert objects without properties to null. * 3. omit null/undefined properties;
* 4. convert objects without properties to null.
* @param {?Object|string|number|null|undefined} input * @param {?Object|string|number|null|undefined} input
* @return {?Object|string|number|null} * @return {?Object|string|number|null}
*/ */
...@@ -313,13 +314,25 @@ class TelemetryProxy { ...@@ -313,13 +314,25 @@ class TelemetryProxy {
return null; return null;
} }
// Rule #1: remove null objects from arrays.
if (Array.isArray(input)) {
return input
.map(
(/** @type {?Object|string|number|null} */ item) =>
this.convert(item))
.filter(
(/** @type {!Object|string|number|null} */ item) =>
item !== null);
}
// After this closure compiler knows that input is {!Object}.
if (typeof input !== 'object') { if (typeof input !== 'object') {
return input; return input;
} }
// At this point, closure compiler knows that the input is {!Object}. // At this point, closure compiler knows that the input is {!Object}.
// 1 rule: convert objects like { value: X } to X, where X is a number. // Rule #2: convert objects like { value: X } to X, where X is a number.
if (Object.entries(input).length === 1 && if (Object.entries(input).length === 1 &&
typeof input['value'] === 'number') { typeof input['value'] === 'number') {
return input['value']; return input['value'];
...@@ -331,13 +344,13 @@ class TelemetryProxy { ...@@ -331,13 +344,13 @@ class TelemetryProxy {
const value = /** @type {?Object|string|number|null|undefined} */ (kv[1]); const value = /** @type {?Object|string|number|null|undefined} */ (kv[1]);
const convertedValue = this.convert(value); const convertedValue = this.convert(value);
// 2 rule: omit null/undefined properties. // Rule #3: omit null/undefined properties.
if (convertedValue !== null && typeof convertedValue !== 'undefined') { if (convertedValue !== null && typeof convertedValue !== 'undefined') {
output[key] = convertedValue; output[key] = convertedValue;
} }
}); });
// 3 rule. convert objects without properties to null. // Rule #4. convert objects without properties to null.
if (Object.entries(output).length === 0) { if (Object.entries(output).length === 0) {
return null; return null;
} }
......
...@@ -132,6 +132,7 @@ void TelemetryExtensionUiBrowserTest::SetUpOnMainThread() { ...@@ -132,6 +132,7 @@ void TelemetryExtensionUiBrowserTest::SetUpOnMainThread() {
->SetAvailableRoutinesForTesting(input); ->SetAvailableRoutinesForTesting(input);
} }
auto telemetry_info = chromeos::cros_healthd::mojom::TelemetryInfo::New();
{ {
auto battery_info = chromeos::cros_healthd::mojom::BatteryInfo::New(); auto battery_info = chromeos::cros_healthd::mojom::BatteryInfo::New();
battery_info->cycle_count = 100000000000000; battery_info->cycle_count = 100000000000000;
...@@ -150,16 +151,50 @@ void TelemetryExtensionUiBrowserTest::SetUpOnMainThread() { ...@@ -150,16 +151,50 @@ void TelemetryExtensionUiBrowserTest::SetUpOnMainThread() {
battery_info->temperature = battery_info->temperature =
chromeos::cros_healthd::mojom::UInt64Value::New(7777777777777777); chromeos::cros_healthd::mojom::UInt64Value::New(7777777777777777);
auto info = chromeos::cros_healthd::mojom::TelemetryInfo::New(); telemetry_info->battery_result =
info->battery_result =
chromeos::cros_healthd::mojom::BatteryResult::NewBatteryInfo( chromeos::cros_healthd::mojom::BatteryResult::NewBatteryInfo(
std::move(battery_info)); std::move(battery_info));
}
{
auto block_device_info =
chromeos::cros_healthd::mojom::NonRemovableBlockDeviceInfo::New();
block_device_info->path = "/dev/device1";
block_device_info->size = 5555555555555555;
block_device_info->type = "NVMe";
block_device_info->manufacturer_id = 200;
block_device_info->name = "goog";
block_device_info->serial = 4287654321;
block_device_info->bytes_read_since_last_boot = 9000000000000000;
block_device_info->bytes_written_since_last_boot = 8000000000000000;
block_device_info->read_time_seconds_since_last_boot = 7000000000000000;
block_device_info->write_time_seconds_since_last_boot = 6666666666666666;
block_device_info->io_time_seconds_since_last_boot = 1111111111111;
block_device_info->discard_time_seconds_since_last_boot =
chromeos::cros_healthd::mojom::UInt64Value::New(77777777777777);
// Need to put some placeholder values, otherwise Mojo will crash, because
// mandatory union fields cannot be nullptr.
block_device_info->vendor_id =
chromeos::cros_healthd::mojom::BlockDeviceVendor::NewOther(0);
block_device_info->product_id =
chromeos::cros_healthd::mojom::BlockDeviceProduct::NewOther(0);
block_device_info->revision =
chromeos::cros_healthd::mojom::BlockDeviceRevision::NewOther(0);
block_device_info->firmware_version =
chromeos::cros_healthd::mojom::BlockDeviceFirmware::NewOther(0);
std::vector<chromeos::cros_healthd::mojom::NonRemovableBlockDeviceInfoPtr>
infos;
infos.push_back(std::move(block_device_info));
telemetry_info->block_device_result = chromeos::cros_healthd::mojom::
NonRemovableBlockDeviceResult::NewBlockDeviceInfo(std::move(infos));
}
DCHECK(chromeos::cros_healthd::FakeCrosHealthdClient::Get()); DCHECK(chromeos::cros_healthd::FakeCrosHealthdClient::Get());
chromeos::cros_healthd::FakeCrosHealthdClient::Get() chromeos::cros_healthd::FakeCrosHealthdClient::Get()
->SetProbeTelemetryInfoResponseForTesting(info); ->SetProbeTelemetryInfoResponseForTesting(telemetry_info);
}
SandboxedWebUiAppTestBase::SetUpOnMainThread(); SandboxedWebUiAppTestBase::SetUpOnMainThread();
} }
...@@ -145,6 +145,17 @@ TEST_F( ...@@ -145,6 +145,17 @@ TEST_F(
telemetryProxy.convert({a: {x: null, y: undefined, z: 'zZz'}}), telemetryProxy.convert({a: {x: null, y: undefined, z: 'zZz'}}),
{a: {z: 'zZz'}}); {a: {z: 'zZz'}});
// covnert arrays.
assertDeepEquals(telemetryProxy.convert([]), []);
assertDeepEquals(telemetryProxy.convert([null, undefined]), []);
assertDeepEquals(telemetryProxy.convert([{value: 1}]), [1]);
assertDeepEquals(
telemetryProxy.convert([null, undefined, {a: 1}, []]), [{a: 1}, []]);
assertDeepEquals(
telemetryProxy.convert(
[{a: null, b: undefined}, {x: 'xxx', y: null}]),
[{x: 'xxx'}]);
// convert objects without properties to null. // convert objects without properties to null.
assertEquals(telemetryProxy.convert({}), null); assertEquals(telemetryProxy.convert({}), null);
assertEquals(telemetryProxy.convert({a: null, b: undefined}), null); assertEquals(telemetryProxy.convert({a: null, b: undefined}), null);
......
...@@ -157,6 +157,22 @@ UNTRUSTED_TEST('UntrustedRequestTelemetryInfo', async () => { ...@@ -157,6 +157,22 @@ UNTRUSTED_TEST('UntrustedRequestTelemetryInfo', async () => {
temperature: 7777777777777777, temperature: 7777777777777777,
} }
}, },
blockDeviceResult: {
blockDeviceInfo: [{
path: '/dev/device1',
size: 5555555555555555,
type: 'NVMe',
manufacturerId: 200,
name: 'goog',
serial: '4287654321',
bytesReadSinceLastBoot: 9000000000000000,
bytesWrittenSinceLastBoot: 8000000000000000,
readTimeSecondsSinceLastBoot: 7000000000000000,
writeTimeSecondsSinceLastBoot: 6666666666666666,
ioTimeSecondsSinceLastBoot: 1111111111111,
discardTimeSecondsSinceLastBoot: 77777777777777
}]
},
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment