Skip to content

Commit 012f837

Browse files
authored
Merge pull request #1356 from MetaMask/mascaraTuneUps
Mascara tune ups
2 parents 6f17c99 + d4cad22 commit 012f837

15 files changed

+281
-84
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ development/bundle.js
1616
builds.zip
1717
test/integration/bundle.js
1818
development/states.js
19+
test/background.js
20+
test/bundle.js
21+
test/test-bundle.js

mascara/README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
start the dual servers (dapp + mascara)
22
```
3-
node server.js
3+
npm run mascara
44
```
55

66
## First time use:
77

8-
- navigate to: http://localhost:9001/popup/popup.html
8+
- navigate to: http://localhost:9001
99
- Create an Account
10-
- go back to http://localhost:9002/
10+
- go back to http://localhost:9002
1111
- open devTools
1212
- click Sync Tx
1313

14-
### Todos
14+
## Tests:
1515

16-
- [ ] Figure out user flows and UI redesign
17-
- [ ] Figure out FireFox
18-
Standing problems:
19-
- [ ] IndexDb
16+
```
17+
npm run testMascara
18+
```
19+
20+
Test will run in browser, you will have to have these browsers installed:
2021

22+
- Chrome
23+
- Firefox
24+
- Opera

mascara/src/background.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
global.window = global
2+
const self = global
23
const pipe = require('pump')
34

45
const SwGlobalListener = require('sw-stream/lib/sw-global-listener.js')
56
const connectionListener = new SwGlobalListener(self)
67
const setupMultiplex = require('../../app/scripts/lib/stream-utils.js').setupMultiplex
78
const PortStream = require('../../app/scripts/lib/port-stream.js')
89

9-
const DbController = require('./lib/index-db-controller')
10+
const DbController = require('idb-global')
1011

1112
const SwPlatform = require('../../app/scripts/platforms/sw')
1213
const MetamaskController = require('../../app/scripts/metamask-controller')
@@ -21,6 +22,7 @@ const STORAGE_KEY = 'metamask-config'
2122
// const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG'
2223
const METAMASK_DEBUG = true
2324
let popupIsOpen = false
25+
let connectedClientCount = 0
2426

2527
const log = require('loglevel')
2628
global.log = log
@@ -31,6 +33,11 @@ self.addEventListener('install', function(event) {
3133
})
3234
self.addEventListener('activate', function(event) {
3335
event.waitUntil(self.clients.claim())
36+
self.clients.matchAll()
37+
.then((clients) => {
38+
if (connectedClientCount < clients.length) sendMessageToAllClients('reconnect')
39+
})
40+
3441
})
3542

3643
console.log('inside:open')
@@ -40,7 +47,6 @@ console.log('inside:open')
4047
let diskStore
4148
const dbController = new DbController({
4249
key: STORAGE_KEY,
43-
version: 2,
4450
})
4551
loadStateFromPersistence()
4652
.then((initState) => setupController(initState))
@@ -107,6 +113,7 @@ function setupController (initState, client) {
107113

108114
connectionListener.on('remote', (portStream, messageEvent) => {
109115
console.log('REMOTE CONECTION FOUND***********')
116+
connectedClientCount += 1
110117
connectRemote(portStream, messageEvent.data.context)
111118
})
112119

@@ -142,4 +149,12 @@ function setupController (initState, client) {
142149
return Promise.resolve()
143150

144151
}
152+
153+
function sendMessageToAllClients (message) {
154+
self.clients.matchAll().then(function(clients) {
155+
clients.forEach(function(client) {
156+
client.postMessage(message)
157+
})
158+
})
159+
}
145160
function noop () {}

mascara/src/lib/index-db-controller.js

Lines changed: 0 additions & 71 deletions
This file was deleted.

mascara/src/proxy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ background.on('ready', (_) => {
2020
pageStream.pipe(swStream).pipe(pageStream)
2121

2222
})
23+
background.on('updatefound', () => window.location.reload())
2324

2425
background.on('error', console.error)
2526
background.startWorker()

mascara/src/ui.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const background = new SWcontroller({
2727
wakeUpInterval: 20000
2828
})
2929
// Setup listener for when the service worker is read
30-
background.on('ready', (readSw) => {
30+
const connectApp = function (readSw) {
3131
let connectionStream = SwStream({
3232
serviceWorker: background.controller,
3333
context: name,
@@ -39,6 +39,14 @@ background.on('ready', (readSw) => {
3939
if (state.appState.shouldClose) window.close()
4040
})
4141
})
42+
}
43+
44+
background.on('ready', (sw) => {
45+
background.removeListener('updatefound', connectApp)
46+
connectApp(sw)
4247
})
48+
background.on('updatefound', () => window.location.reload())
49+
4350
background.startWorker()
51+
// background.startWorker()
4452
console.log('hello from MetaMascara ui!')

mascara/test/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function wait(time) {
2+
return new Promise(function(resolve, reject) {
3+
setTimeout(function() {
4+
resolve()
5+
}, time * 3 || 1500)
6+
})
7+
}

mascara/test/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width">
6+
<title>QUnit Example</title>
7+
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.0.css">
8+
</head>
9+
<body>
10+
<div id="qunit"></div>
11+
<div id="qunit-fixture"></div>
12+
<script src="https://code.jquery.com/qunit/qunit-2.0.0.js"></script>
13+
<script src="./jquery-3.1.0.min.js"></script>
14+
<script src="./helpers.js"></script>
15+
<script src="./test-bundle.js"></script>
16+
<script src="/testem.js"></script>
17+
18+
<div id="app-content"></div>
19+
<script src="./bundle.js"></script>
20+
</body>
21+
</html>

mascara/test/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
var browserify = require('browserify');
4+
var tests = fs.readdirSync(path.join(__dirname, 'lib'))
5+
var bundlePath = path.join(__dirname, 'test-bundle.js')
6+
var b = browserify();
7+
8+
// Remove old bundle
9+
try {
10+
fs.unlinkSync(bundlePath)
11+
} catch (e) {
12+
console.error(e)
13+
}
14+
15+
var writeStream = fs.createWriteStream(bundlePath)
16+
17+
tests.forEach(function(fileName) {
18+
b.add(path.join(__dirname, 'lib', fileName))
19+
})
20+
21+
b.bundle().pipe(writeStream);
22+

mascara/test/jquery-3.1.0.min.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mascara/test/lib/first-time.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
const PASSWORD = 'password123'
2+
3+
QUnit.module('first time usage')
4+
5+
QUnit.test('render init screen', function (assert) {
6+
var done = assert.async()
7+
let app
8+
9+
wait(1000).then(function() {
10+
app = $('#app-content').contents()
11+
const recurseNotices = function () {
12+
let button = app.find('button')
13+
if (button.html() === 'Continue') {
14+
let termsPage = app.find('.markdown')[0]
15+
termsPage.scrollTop = termsPage.scrollHeight
16+
return wait().then(() => {
17+
button.click()
18+
return wait()
19+
}).then(() => {
20+
return recurseNotices()
21+
})
22+
} else {
23+
return wait()
24+
}
25+
}
26+
return recurseNotices()
27+
}).then(function() {
28+
// Scroll through terms
29+
var title = app.find('h1').text()
30+
assert.equal(title, 'MetaMask', 'title screen')
31+
32+
// enter password
33+
var pwBox = app.find('#password-box')[0]
34+
var confBox = app.find('#password-box-confirm')[0]
35+
pwBox.value = PASSWORD
36+
confBox.value = PASSWORD
37+
38+
return wait()
39+
}).then(function() {
40+
41+
// create vault
42+
var createButton = app.find('button.primary')[0]
43+
createButton.click()
44+
45+
return wait(1500)
46+
}).then(function() {
47+
48+
var created = app.find('h3')[0]
49+
assert.equal(created.textContent, 'Vault Created', 'Vault created screen')
50+
51+
// Agree button
52+
var button = app.find('button')[0]
53+
assert.ok(button, 'button present')
54+
button.click()
55+
56+
return wait(1000)
57+
}).then(function() {
58+
59+
var detail = app.find('.account-detail-section')[0]
60+
assert.ok(detail, 'Account detail section loaded.')
61+
62+
var sandwich = app.find('.sandwich-expando')[0]
63+
sandwich.click()
64+
65+
return wait()
66+
}).then(function() {
67+
68+
var sandwich = app.find('.menu-droppo')[0]
69+
var children = sandwich.children
70+
var lock = children[children.length - 2]
71+
assert.ok(lock, 'Lock menu item found')
72+
lock.click()
73+
74+
return wait(1000)
75+
}).then(function() {
76+
77+
var pwBox = app.find('#password-box')[0]
78+
pwBox.value = PASSWORD
79+
80+
var createButton = app.find('button.primary')[0]
81+
createButton.click()
82+
83+
return wait(1000)
84+
}).then(function() {
85+
86+
var detail = app.find('.account-detail-section')[0]
87+
assert.ok(detail, 'Account detail section loaded again.')
88+
89+
return wait()
90+
}).then(function (){
91+
92+
var qrButton = app.find('.fa.fa-qrcode')[0]
93+
qrButton.click()
94+
95+
return wait(1000)
96+
}).then(function (){
97+
98+
var qrHeader = app.find('.qr-header')[0]
99+
var qrContainer = app.find('#qr-container')[0]
100+
assert.equal(qrHeader.textContent, 'Account 1', 'Should show account label.')
101+
assert.ok(qrContainer, 'QR Container found')
102+
103+
return wait()
104+
}).then(function (){
105+
106+
var networkMenu = app.find('.network-indicator')[0]
107+
networkMenu.click()
108+
109+
return wait()
110+
}).then(function (){
111+
112+
var networkMenu = app.find('.network-indicator')[0]
113+
var children = networkMenu.children
114+
children.length[3]
115+
assert.ok(children, 'All network options present')
116+
117+
done()
118+
})
119+
})

mascara/test/testem.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
launch_in_dev:
2+
- Chrome
3+
- Firefox
4+
- Opera
5+
launch_in_ci:
6+
- Chrome
7+
- Firefox
8+
- Opera
9+
framework:
10+
- qunit
11+
before_tests: "npm run mascaraCi"
12+
after_tests: "rm ./background.js ./test-bundle.js ./bundle.js"
13+
test_page: "./index.html"

0 commit comments

Comments
 (0)