mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-24 21:40:48 +02:00
tests(e2e): improve coverage
This commit is contained in:
@@ -33,6 +33,12 @@
|
||||
import Geolocation from './views/Geolocation.svelte'
|
||||
import Haptics from './views/Haptics.svelte'
|
||||
import Nfc from './views/Nfc.svelte'
|
||||
import Autostart from './views/Autostart.svelte'
|
||||
import DeepLink from './views/DeepLink.svelte'
|
||||
import Positioner from './views/Positioner.svelte'
|
||||
import Sql from './views/Sql.svelte'
|
||||
import Stronghold from './views/Stronghold.svelte'
|
||||
import WebSocket from './views/WebSocket.svelte'
|
||||
|
||||
import TitleBar from './lib/TitleBar.svelte'
|
||||
|
||||
@@ -131,6 +137,36 @@
|
||||
component: WebRTC,
|
||||
icon: 'i-ph-broadcast'
|
||||
},
|
||||
{
|
||||
label: 'WebSocket',
|
||||
component: WebSocket,
|
||||
icon: 'i-ph-plugs-connected'
|
||||
},
|
||||
{
|
||||
label: 'SQL',
|
||||
component: Sql,
|
||||
icon: 'i-ph-database'
|
||||
},
|
||||
{
|
||||
label: 'Stronghold',
|
||||
component: Stronghold,
|
||||
icon: 'i-ph-lock-key'
|
||||
},
|
||||
{
|
||||
label: 'Deep link',
|
||||
component: DeepLink,
|
||||
icon: 'i-ph-link'
|
||||
},
|
||||
!isMobile && {
|
||||
label: 'Autostart',
|
||||
component: Autostart,
|
||||
icon: 'i-ph-power'
|
||||
},
|
||||
!isMobile && {
|
||||
label: 'Positioner',
|
||||
component: Positioner,
|
||||
icon: 'i-ph-arrows-out-cardinal'
|
||||
},
|
||||
isMobile && {
|
||||
label: 'Scanner',
|
||||
component: Scanner,
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<script>
|
||||
import { enable, disable, isEnabled } from '@tauri-apps/plugin-autostart'
|
||||
import { onMount } from 'svelte'
|
||||
|
||||
export let onMessage
|
||||
|
||||
let enabled = false
|
||||
|
||||
async function refresh() {
|
||||
enabled = await isEnabled()
|
||||
}
|
||||
|
||||
function toggle() {
|
||||
;(enabled ? disable() : enable())
|
||||
.then(refresh)
|
||||
.then(() => onMessage(`Autostart ${enabled ? 'enabled' : 'disabled'}`))
|
||||
.catch(onMessage)
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
refresh().catch(onMessage)
|
||||
})
|
||||
</script>
|
||||
|
||||
<div class="flex flex-row gap-2 items-center">
|
||||
<button class="btn" on:click={toggle}>
|
||||
{enabled ? 'Disable' : 'Enable'} autostart
|
||||
</button>
|
||||
<span>Launching at login is {enabled ? 'enabled' : 'disabled'}</span>
|
||||
</div>
|
||||
@@ -0,0 +1,67 @@
|
||||
<script>
|
||||
import {
|
||||
getCurrent,
|
||||
onOpenUrl,
|
||||
register,
|
||||
unregister,
|
||||
isRegistered
|
||||
} from '@tauri-apps/plugin-deep-link'
|
||||
import { onMount, onDestroy } from 'svelte'
|
||||
|
||||
export let onMessage
|
||||
|
||||
let current = null
|
||||
let protocol = 'tauri-api'
|
||||
let unlisten
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
current = await getCurrent()
|
||||
unlisten = await onOpenUrl((urls) => {
|
||||
current = urls
|
||||
onMessage(`Opened with ${urls.join(', ')}`)
|
||||
})
|
||||
} catch (error) {
|
||||
onMessage(error)
|
||||
}
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
unlisten?.()
|
||||
})
|
||||
|
||||
function registerProtocol() {
|
||||
register(protocol)
|
||||
.then(() => onMessage(`Registered ${protocol}://`))
|
||||
.catch(onMessage)
|
||||
}
|
||||
|
||||
function unregisterProtocol() {
|
||||
unregister(protocol)
|
||||
.then(() => onMessage(`Unregistered ${protocol}://`))
|
||||
.catch(onMessage)
|
||||
}
|
||||
|
||||
function checkProtocol() {
|
||||
isRegistered(protocol)
|
||||
.then((registered) =>
|
||||
onMessage(
|
||||
`${protocol}:// is ${registered ? '' : 'not '}handled by this app`
|
||||
)
|
||||
)
|
||||
.catch(onMessage)
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
<div>
|
||||
Current deep link: <code>{current ? current.join(', ') : 'none'}</code>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-row gap-2 items-center">
|
||||
<input class="input grow" placeholder="Scheme" bind:value={protocol} />
|
||||
<button class="btn" on:click={registerProtocol}>Register</button>
|
||||
<button class="btn" on:click={unregisterProtocol}>Unregister</button>
|
||||
<button class="btn" on:click={checkProtocol}>Is registered?</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,40 @@
|
||||
<script>
|
||||
import {
|
||||
moveWindow,
|
||||
moveWindowConstrained,
|
||||
Position
|
||||
} from '@tauri-apps/plugin-positioner'
|
||||
|
||||
export let onMessage
|
||||
|
||||
// `Position` is a numeric enum, so it also maps the values back to their names
|
||||
const positions = Object.keys(Position).filter((key) => isNaN(Number(key)))
|
||||
|
||||
let position = 'Center'
|
||||
let constrained = false
|
||||
|
||||
function move() {
|
||||
;(constrained ? moveWindowConstrained : moveWindow)(Position[position])
|
||||
.then(() => onMessage(`Moved the window to ${position}`))
|
||||
.catch(onMessage)
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
<p>
|
||||
The tray positions only resolve once the tray icon has been clicked, which
|
||||
reports its location to the plugin.
|
||||
</p>
|
||||
<div class="flex flex-row gap-2 items-center">
|
||||
<select class="input" bind:value={position}>
|
||||
{#each positions as name}
|
||||
<option value={name}>{name}</option>
|
||||
{/each}
|
||||
</select>
|
||||
<label>
|
||||
<input type="checkbox" bind:checked={constrained} />
|
||||
Constrain to the tray icon's monitor
|
||||
</label>
|
||||
<button class="btn" on:click={move}>Move window</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,68 @@
|
||||
<script>
|
||||
import Database from '@tauri-apps/plugin-sql'
|
||||
import { onMount } from 'svelte'
|
||||
|
||||
export let onMessage
|
||||
|
||||
// the `todos` table is created by the migration the app registers for this database
|
||||
let db
|
||||
let todos = []
|
||||
let title = ''
|
||||
|
||||
async function refresh() {
|
||||
todos = await db.select('SELECT * FROM todos ORDER BY id')
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
db = await Database.load('sqlite:api.db')
|
||||
await refresh()
|
||||
} catch (error) {
|
||||
onMessage(error)
|
||||
}
|
||||
})
|
||||
|
||||
function add() {
|
||||
db.execute('INSERT INTO todos (title) VALUES ($1)', [title])
|
||||
.then((result) => {
|
||||
onMessage(result)
|
||||
title = ''
|
||||
return refresh()
|
||||
})
|
||||
.catch(onMessage)
|
||||
}
|
||||
|
||||
function toggle(todo) {
|
||||
db.execute('UPDATE todos SET done = $1 WHERE id = $2', [
|
||||
todo.done ? 0 : 1,
|
||||
todo.id
|
||||
])
|
||||
.then(refresh)
|
||||
.catch(onMessage)
|
||||
}
|
||||
|
||||
function remove(todo) {
|
||||
db.execute('DELETE FROM todos WHERE id = $1', [todo.id])
|
||||
.then(refresh)
|
||||
.catch(onMessage)
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
<form class="flex flex-row gap-2" on:submit|preventDefault={add}>
|
||||
<input class="input grow" placeholder="New todo" bind:value={title} />
|
||||
<button class="btn" type="submit" disabled={!db || !title}>Add</button>
|
||||
</form>
|
||||
|
||||
{#each todos as todo (todo.id)}
|
||||
<div class="flex flex-row gap-2 items-center">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={todo.done === 1}
|
||||
on:change={() => toggle(todo)}
|
||||
/>
|
||||
<span class="grow" class:line-through={todo.done === 1}>{todo.title}</span>
|
||||
<button class="btn" on:click={() => remove(todo)}>Delete</button>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
@@ -0,0 +1,71 @@
|
||||
<script>
|
||||
import { Stronghold } from '@tauri-apps/plugin-stronghold'
|
||||
import { appLocalDataDir, join } from '@tauri-apps/api/path'
|
||||
|
||||
export let onMessage
|
||||
|
||||
const clientName = 'api-example'
|
||||
const storeKey = 'secret'
|
||||
|
||||
let password = 'password'
|
||||
let stronghold
|
||||
let store
|
||||
let record = ''
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const path = await join(await appLocalDataDir(), 'api.stronghold')
|
||||
stronghold = await Stronghold.load(path, password)
|
||||
let client
|
||||
try {
|
||||
client = await stronghold.loadClient(clientName)
|
||||
} catch {
|
||||
client = await stronghold.createClient(clientName)
|
||||
}
|
||||
store = client.getStore()
|
||||
const value = await store.get(storeKey)
|
||||
record = value ? new TextDecoder().decode(value) : ''
|
||||
onMessage('Stronghold loaded')
|
||||
} catch (error) {
|
||||
stronghold = null
|
||||
onMessage(error)
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
try {
|
||||
await store.insert(storeKey, Array.from(new TextEncoder().encode(record)))
|
||||
await stronghold.save()
|
||||
onMessage('Record saved')
|
||||
} catch (error) {
|
||||
onMessage(error)
|
||||
}
|
||||
}
|
||||
|
||||
async function unload() {
|
||||
await stronghold.unload().catch(onMessage)
|
||||
stronghold = null
|
||||
store = null
|
||||
record = ''
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
{#if stronghold}
|
||||
<div class="flex flex-row gap-2">
|
||||
<input class="input grow" placeholder="Secret" bind:value={record} />
|
||||
<button class="btn" on:click={save}>Save</button>
|
||||
<button class="btn" on:click={unload}>Lock</button>
|
||||
</div>
|
||||
{:else}
|
||||
<form class="flex flex-row gap-2" on:submit|preventDefault={load}>
|
||||
<input
|
||||
class="input grow"
|
||||
type="password"
|
||||
placeholder="Password"
|
||||
bind:value={password}
|
||||
/>
|
||||
<button class="btn" type="submit">Unlock</button>
|
||||
</form>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -0,0 +1,55 @@
|
||||
<script>
|
||||
import WebSocket from '@tauri-apps/plugin-websocket'
|
||||
import { onDestroy } from 'svelte'
|
||||
|
||||
export let onMessage
|
||||
|
||||
let url = 'wss://echo.websocket.org'
|
||||
let message = ''
|
||||
let ws
|
||||
|
||||
async function connect() {
|
||||
try {
|
||||
ws = await WebSocket.connect(url)
|
||||
ws.addListener((received) => {
|
||||
onMessage(received)
|
||||
if (received.type === 'Close') {
|
||||
ws = null
|
||||
}
|
||||
})
|
||||
onMessage(`Connected to ${url}`)
|
||||
} catch (error) {
|
||||
onMessage(error)
|
||||
}
|
||||
}
|
||||
|
||||
function send() {
|
||||
ws.send(message)
|
||||
.then(() => (message = ''))
|
||||
.catch(onMessage)
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
ws.disconnect().catch(onMessage)
|
||||
ws = null
|
||||
}
|
||||
|
||||
onDestroy(() => {
|
||||
ws?.disconnect().catch(() => {})
|
||||
})
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
{#if ws}
|
||||
<form class="flex flex-row gap-2" on:submit|preventDefault={send}>
|
||||
<input class="input grow" placeholder="Message" bind:value={message} />
|
||||
<button class="btn" type="submit">Send</button>
|
||||
<button class="btn" type="button" on:click={disconnect}>Disconnect</button>
|
||||
</form>
|
||||
{:else}
|
||||
<form class="flex flex-row gap-2" on:submit|preventDefault={connect}>
|
||||
<input class="input grow" placeholder="ws:// or wss:// URL" bind:value={url} />
|
||||
<button class="btn" type="submit">Connect</button>
|
||||
</form>
|
||||
{/if}
|
||||
</div>
|
||||
Reference in New Issue
Block a user