Revision bbc9fae4
Added by koszko over 1 year ago
| test/haketilo_test/unit/test_popup.py | ||
|---|---|---|
| 81 | 81 |
tab_mock_js = ''' |
| 82 | 82 |
; |
| 83 | 83 |
const mocked_page_info = (%s)[/#mock_page_info-(.*)$/.exec(document.URL)[1]]; |
| 84 |
const old_sendMessage = browser.tabs.sendMessage; |
|
| 84 | 85 |
browser.tabs.sendMessage = async function(tab_id, msg) {
|
| 85 | 86 |
const this_tab_id = (await browser.tabs.getCurrent()).id; |
| 86 | 87 |
if (tab_id !== this_tab_id) |
| 87 | 88 |
throw `not current tab id (${tab_id} instead of ${this_tab_id})`;
|
| 88 | 89 |
|
| 89 |
if (msg[0] === "page_info") {
|
|
| 90 |
if (msg[0] === "page_info") |
|
| 90 | 91 |
return mocked_page_info; |
| 91 |
} else if (msg[0] === "repo_query") {
|
|
| 92 |
const response = await fetch(msg[1]); |
|
| 93 |
if (!response) |
|
| 94 |
return {error: "Something happened :o"};
|
|
| 95 |
|
|
| 96 |
const result = {ok: response.ok, status: response.status};
|
|
| 97 |
try {
|
|
| 98 |
result.json = await response.json(); |
|
| 99 |
} catch(e) {
|
|
| 100 |
result.error_json = "" + e; |
|
| 101 |
} |
|
| 102 |
return result; |
|
| 103 |
} else {
|
|
| 92 |
else if (msg[0] === "repo_query") |
|
| 93 |
return old_sendMessage(tab_id, msg); |
|
| 94 |
else |
|
| 104 | 95 |
throw `bad sendMessage message type: '${msg[0]}'`;
|
| 105 |
} |
|
| 106 | 96 |
} |
| 107 | 97 |
|
| 108 | 98 |
const old_tabs_query = browser.tabs.query; |
| ... | ... | |
| 113 | 103 |
} |
| 114 | 104 |
''' % json.dumps(mocked_page_infos) |
| 115 | 105 |
|
| 106 |
tab_mock_js = mock_cacher_code + tab_mock_js |
|
| 107 |
|
|
| 116 | 108 |
popup_ext_data = {
|
| 117 | 109 |
'background_script': broker_js, |
| 118 | 110 |
'extra_html': ExtraHTML( |
Also available in: Unified diff
serialize and deserialize entire Response object when relaying fetch() calls to other contexts using sendMessage