Revision control
Copy as Markdown
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
use crate::sync::engine::TabsEngine;
use crate::TabsStore;
use std::sync::Arc;
impl TabsStore {
// Returns a bridged sync engine for Desktop for this store.
pub fn bridged_engine(self: Arc<Self>) -> Arc<TabsBridgedEngine> {
let engine = TabsEngine::new(self);
Arc::new(TabsBridgedEngine::new(Box::new(engine)))
}
}
// The UniFFI-exposed `TabsBridgedEngine` (a thin newtype around
// `sync15::engine::BridgedEngineWrapper`) is generated by this macro, which
// removes the facade + BSO marshalling boilerplate. The wrapper drives our
// `TabsEngine`'s `SyncEngine` impl directly.
sync15::uniffi_bridged_engine!(TabsBridgedEngine, crate::TabsApiError);
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::{RemoteTab, TABS_CLIENT_TTL};
use crate::sync::record::TabsRecordTab;
use serde_json::json;
use std::collections::HashMap;
use sync15::{ClientData, DeviceType, RemoteClient};
// A copy of the normal "engine" tests but which go via the bridge
#[test]
fn test_sync_via_bridge() {
error_support::init_for_tests();
let store = Arc::new(TabsStore::new_with_mem_path("test-bridge_incoming"));
// Set some local tabs for our device.
let my_tabs = vec![
RemoteTab {
title: "my first tab".to_string(),
last_used: 2,
..Default::default()
},
RemoteTab {
title: "my second tab".to_string(),
last_used: 1,
..Default::default()
},
];
store.set_local_tabs(my_tabs.clone());
let bridge = store.bridged_engine();
let client_data = ClientData {
local_client_id: "my-device".to_string(),
recent_clients: HashMap::from([
(
"my-device".to_string(),
RemoteClient {
fxa_device_id: None,
device_name: "my device".to_string(),
device_type: sync15::DeviceType::Unknown,
},
),
(
"device-no-tabs".to_string(),
RemoteClient {
fxa_device_id: None,
device_name: "device with no tabs".to_string(),
device_type: DeviceType::Unknown,
},
),
(
"device-with-a-tab".to_string(),
RemoteClient {
fxa_device_id: None,
device_name: "device with a tab".to_string(),
device_type: DeviceType::Unknown,
},
),
]),
};
bridge
.set_clients(&serde_json::to_string(&client_data).unwrap())
.expect("should work");
let records = vec![
// my-device should be ignored by sync - here it is acting as what our engine last
// wrote, but the actual tabs in our store we set above are what should be used.
json!({
"id": "my-device",
"clientName": "my device",
"tabs": [{
"title": "the title",
"urlHistory": [
],
"lastUsed": 1643764207
}]
}),
json!({
"id": "device-no-tabs",
"clientName": "device with no tabs",
"tabs": [],
}),
json!({
"id": "device-with-a-tab",
"clientName": "device with a tab",
"tabs": [{
"title": "the title",
"urlHistory": [
],
"lastUsed": 1643764207
}]
}),
// This has the main payload as OK but the tabs part invalid.
json!({
"id": "device-with-invalid-tab",
"clientName": "device with a tab",
"tabs": [{
"foo": "bar",
}]
}),
// We want this to be a valid payload but an invalid tab - so it needs an ID.
json!({
"id": "invalid-tab",
"foo": "bar"
}),
];
let mut incoming = Vec::new();
for record in records {
// Annoyingly we can't use `IncomingEnvelope` directly as it intentionally doesn't
// support Serialize - so need to use explicit json.
let envelope = json!({
"id": record.get("id"),
"modified": 0,
"payload": serde_json::to_string(&record).unwrap(),
});
incoming.push(serde_json::to_string(&envelope).unwrap());
}
bridge.store_incoming(incoming).expect("should store");
// Incoming records above are `modified: 0`
let out = bridge.apply(0).expect("should apply");
assert_eq!(out.len(), 1);
let ours = serde_json::from_str::<serde_json::Value>(&out[0]).unwrap();
// As above, can't use `OutgoingEnvelope` as it doesn't Deserialize.
// First, convert my_tabs from the local `RemoteTab` to the Sync specific `TabsRecord`
let expected_tabs: Vec<TabsRecordTab> = my_tabs.into_iter().map(Into::into).collect();
let expected = json!({
"id": "my-device".to_string(),
"payload": json!({
"id": "my-device".to_string(),
"clientName": "my device",
"tabs": serde_json::to_value(expected_tabs).unwrap(),
}).to_string(),
"ttl": TABS_CLIENT_TTL,
});
assert_eq!(ours, expected);
bridge.set_uploaded(1234, vec![]).unwrap();
assert_eq!(bridge.last_sync().unwrap(), 1234);
}
#[test]
fn test_sync_meta() {
error_support::init_for_tests();
let store = Arc::new(TabsStore::new_with_mem_path("test-meta"));
let bridge = store.bridged_engine();
// Should not error or panic
assert_eq!(bridge.last_sync().unwrap(), 0);
bridge.set_uploaded(3, vec![]).unwrap();
assert_eq!(bridge.last_sync().unwrap(), 3);
assert!(bridge.sync_id().unwrap().is_none());
bridge.ensure_current_sync_id("some_guid").unwrap();
assert_eq!(bridge.sync_id().unwrap(), Some("some_guid".to_string()));
// changing the sync ID should reset the timestamp
assert_eq!(bridge.last_sync().unwrap(), 0);
// set_uploaded advances the engine-owned last_sync (there's no external setter).
bridge.set_uploaded(3, vec![]).unwrap();
bridge.reset_sync_id().unwrap();
// should now be a random guid.
assert_ne!(bridge.sync_id().unwrap(), Some("some_guid".to_string()));
// should have reset the last sync timestamp.
assert_eq!(bridge.last_sync().unwrap(), 0);
// set_uploaded advances the engine-owned last_sync (there's no external setter).
bridge.set_uploaded(3, vec![]).unwrap();
// `reset` clears the guid and the timestamp
bridge.reset().unwrap();
assert_eq!(bridge.last_sync().unwrap(), 0);
assert!(bridge.sync_id().unwrap().is_none());
}
}