|
1 | | -import { describe, it, expect } from 'vitest'; |
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
2 | 2 |
|
3 | | -import { parseAndroidHierarchy, parseNodeAttributes } from './adb-backend.js'; |
| 3 | +import { |
| 4 | + parseAndroidHierarchy, |
| 5 | + parseNodeAttributes, |
| 6 | + AdbBackend, |
| 7 | +} from './adb-backend.js'; |
4 | 8 | import { findElement } from '../utils/element.js'; |
| 9 | +import * as execModule from '../utils/exec.js'; |
| 10 | + |
| 11 | +vi.mock('../utils/exec.js', () => ({ |
| 12 | + exec: vi.fn(), |
| 13 | + execStrict: vi.fn(), |
| 14 | + isCommandAvailable: vi.fn().mockResolvedValue(true), |
| 15 | +})); |
| 16 | + |
| 17 | +const mockExecStrict = vi.mocked(execModule.execStrict); |
5 | 18 |
|
6 | 19 | const SAMPLE_UIAUTOMATOR_XML = `<?xml version="1.0" encoding="UTF-8"?> |
7 | 20 | <hierarchy rotation="0"> |
@@ -96,3 +109,64 @@ describe('parseNodeAttributes', () => { |
96 | 109 | expect(result!.identifier).toBeUndefined(); |
97 | 110 | }); |
98 | 111 | }); |
| 112 | + |
| 113 | +describe('AdbBackend.getElementText', () => { |
| 114 | + let backend: AdbBackend; |
| 115 | + |
| 116 | + beforeEach(() => { |
| 117 | + vi.clearAllMocks(); |
| 118 | + mockExecStrict.mockResolvedValue('device'); |
| 119 | + backend = new AdbBackend('emulator-5554'); |
| 120 | + }); |
| 121 | + |
| 122 | + const xmlWithElement = `<?xml version="1.0" encoding="UTF-8"?> |
| 123 | +<hierarchy> |
| 124 | + <node text="$42.00" resource-id="io.metamask:id/balance" class="android.widget.TextView" content-desc="Account balance" enabled="true" bounds="[100,200][500,260]" /> |
| 125 | +</hierarchy>`; |
| 126 | + |
| 127 | + it('returns label (content-desc) from matching element', async () => { |
| 128 | + mockExecStrict.mockImplementation(async (_cmd, args) => { |
| 129 | + if (args?.includes('get-state')) { |
| 130 | + return 'device'; |
| 131 | + } |
| 132 | + return xmlWithElement; |
| 133 | + }); |
| 134 | + |
| 135 | + const text = await backend.getElementText({ |
| 136 | + identifier: 'io.metamask:id/balance', |
| 137 | + }); |
| 138 | + |
| 139 | + expect(text).toBe('Account balance'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns value (text attr) when no label', async () => { |
| 143 | + const xmlNoDesc = `<?xml version="1.0" encoding="UTF-8"?> |
| 144 | +<hierarchy> |
| 145 | + <node text="hello" resource-id="field" class="EditText" content-desc="" enabled="true" bounds="[0,0][100,50]" /> |
| 146 | +</hierarchy>`; |
| 147 | + mockExecStrict.mockImplementation(async (_cmd, args) => { |
| 148 | + if (args?.includes('get-state')) { |
| 149 | + return 'device'; |
| 150 | + } |
| 151 | + return xmlNoDesc; |
| 152 | + }); |
| 153 | + |
| 154 | + const text = await backend.getElementText({ identifier: 'field' }); |
| 155 | + |
| 156 | + expect(text).toBe('hello'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('throws when element not found', async () => { |
| 160 | + const emptyXml = `<?xml version="1.0" encoding="UTF-8"?><hierarchy></hierarchy>`; |
| 161 | + mockExecStrict.mockImplementation(async (_cmd, args) => { |
| 162 | + if (args?.includes('get-state')) { |
| 163 | + return 'device'; |
| 164 | + } |
| 165 | + return emptyXml; |
| 166 | + }); |
| 167 | + |
| 168 | + await expect( |
| 169 | + backend.getElementText({ identifier: 'missing' }), |
| 170 | + ).rejects.toThrow('Element not found'); |
| 171 | + }); |
| 172 | +}); |
0 commit comments