[tor-commits] [snowflake/master] Make badge and debug extend ui

arlo at torproject.org arlo at torproject.org
Wed May 8 15:41:17 UTC 2019


commit 2b1852c1a5bffd0c939662ccd2ca2233d7e03c84
Author: Arlo Breault <arlolra at gmail.com>
Date:   Tue May 7 14:36:23 2019 -0400

    Make badge and debug extend ui
    
    Permits further ui extension
---
 proxy/init.coffee                | 18 ++++++++++++------
 proxy/shims.coffee               |  5 ++++-
 proxy/snowflake.coffee           |  6 +++---
 proxy/spec/snowflake.spec.coffee |  6 +-----
 proxy/spec/ui.spec.coffee        | 37 +++++++++++--------------------------
 proxy/ui.coffee                  | 29 ++++++++++++++++++-----------
 6 files changed, 49 insertions(+), 52 deletions(-)

diff --git a/proxy/init.coffee b/proxy/init.coffee
index a75f67c..031f7c5 100644
--- a/proxy/init.coffee
+++ b/proxy/init.coffee
@@ -44,17 +44,23 @@ snowflake = null
 # log to console.
 log = (msg) ->
   console.log 'Snowflake: ' + msg
-  snowflake?.ui?.log msg
+  snowflake?.ui.log msg
 
-dbg = (msg) -> log msg if DEBUG or snowflake.ui?.debug
+dbg = (msg) -> log msg if DEBUG or (snowflake?.ui instanceof DebugUI)
 
 
 ###
 Entry point.
 ###
-init = (isNode) ->
-  # Hook up to the debug UI if available.
-  ui = if isNode then null else new UI()
+init = () ->
+  ui = null
+  if (document.getElementById('badge') != null)
+    ui = new BadgeUI()
+  else if (document.getElementById('status') != null)
+    ui = new DebugUI()
+  else
+    ui = new UI()
+
   silenceNotifications = Params.getBool(query, 'silent', false)
   broker = new Broker BROKER
   snowflake = new Snowflake broker, ui
@@ -80,4 +86,4 @@ window.onunload = ->
   pair.close() for pair in snowflake.proxyPairs
   null
 
-window.onload = init.bind null, false
+window.onload = init
diff --git a/proxy/shims.coffee b/proxy/shims.coffee
index a0f53ea..1aeaf3d 100644
--- a/proxy/shims.coffee
+++ b/proxy/shims.coffee
@@ -4,6 +4,8 @@ WebRTC shims for multiple browsers.
 
 if module?.exports
   window = {}
+  document =
+    getElementById: () -> null,
   location = ''
 
   if not TESTING? or not TESTING
@@ -16,10 +18,11 @@ if module?.exports
     WebSocket = require 'ws'
     { XMLHttpRequest } = require 'xmlhttprequest'
 
-    process.nextTick () -> init true
+    process.nextTick () -> init
 
 else
   window = this
+  document = window.document
   location = window.location.search.substr(1)
 
   PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection ||
diff --git a/proxy/snowflake.coffee b/proxy/snowflake.coffee
index cf71edc..1cf6582 100644
--- a/proxy/snowflake.coffee
+++ b/proxy/snowflake.coffee
@@ -55,11 +55,11 @@ class Snowflake
     countdown = (msg, sec, skip) =>
       if not skip then dbg msg
       if sec > 0
-        @ui?.setStatus msg + ' (Polling in ' + sec + ' seconds...)'
+        @ui.setStatus msg + ' (Polling in ' + sec + ' seconds...)'
         sec--
         setTimeout((-> countdown(msg, sec, true)), 1000)
       else
-        @ui?.setStatus msg
+        @ui.setStatus msg
         findClients()
     # Poll broker for clients.
     findClients = =>
@@ -70,7 +70,7 @@ class Snowflake
         return
       msg = 'Polling for client ... '
       msg += '[retries: ' + @retries + ']' if @retries > 0
-      @ui?.setStatus msg
+      @ui.setStatus msg
       recv = @broker.getClientOffer pair.id
       recv.then (desc) =>
         @receiveOffer pair, desc
diff --git a/proxy/spec/snowflake.spec.coffee b/proxy/spec/snowflake.spec.coffee
index 05dd843..8c11a22 100644
--- a/proxy/spec/snowflake.spec.coffee
+++ b/proxy/spec/snowflake.spec.coffee
@@ -17,11 +17,7 @@ class WebSocket
     @bufferedAmount = 0
   send: (data) ->
 log = ->
-class FakeUI
-  log: ->
-  setActive: ->
-  setStatus: ->
-fakeUI = new FakeUI()
+fakeUI = new UI()
 class FakeBroker
   getClientOffer: -> new Promise((F,R) -> {})
 # Fake snowflake to interact with
diff --git a/proxy/spec/ui.spec.coffee b/proxy/spec/ui.spec.coffee
index 8229169..f1cf4b2 100644
--- a/proxy/spec/ui.spec.coffee
+++ b/proxy/spec/ui.spec.coffee
@@ -3,7 +3,7 @@ jasmine tests for Snowflake UI
 ###
 
 document =
-  getElementById: (id) ->
+  getElementById: (id) -> {}
 
 describe 'UI', ->
 
@@ -11,9 +11,8 @@ describe 'UI', ->
     spyOn(document, 'getElementById').and.callFake (id) ->
       return null if 'badge' == id
       return {}
-    u = new UI()
-    expect(u.debug).toBe true
-    expect(document.getElementById.calls.count()).toEqual 3
+    u = new DebugUI()
+    expect(document.getElementById.calls.count()).toEqual 2
     expect(u.$status).not.toBeNull()
     expect(u.$msglog).not.toBeNull()
 
@@ -21,49 +20,35 @@ describe 'UI', ->
     spyOn(document, 'getElementById').and.callFake (id) ->
       return {} if 'badge' == id
       return null
-    u = new UI()
-    expect(u.debug).toBe false
+    u = new BadgeUI()
     expect(document.getElementById).toHaveBeenCalled()
     expect(document.getElementById.calls.count()).toEqual 1
-    expect(u.$status).toBeNull()
-    expect(u.$msglog).toBeNull()
+    expect(u.$badge).not.toBeNull()
 
-  it 'sets status message only when in debug mode', ->
-    u = new UI()
+  it 'sets status message when in debug mode', ->
+    u = new DebugUI()
     u.$status = { innerHTML: '' }
-    u.debug = false
-    u.setStatus('test')
-    expect(u.$status.innerHTML).toEqual ''
-    u.debug = true
     u.setStatus('test')
     expect(u.$status.innerHTML).toEqual 'Status: test'
 
   it 'sets message log css correctly for debug mode', ->
-    u = new UI()
-    u.debug = true
-    u.$msglog = {}
+    u = new DebugUI()
     u.setActive true
     expect(u.$msglog.className).toEqual 'active'
     u.setActive false
     expect(u.$msglog.className).toEqual ''
 
   it 'sets badge css correctly for non-debug mode', ->
-    u = new UI()
-    u.debug = false
+    u = new BadgeUI()
     u.$badge = {}
     u.setActive true
     expect(u.$badge.className).toEqual 'active'
     u.setActive false
     expect(u.$badge.className).toEqual ''
 
-  it 'logs to the textarea correctly, only when debug mode', ->
-    u = new UI()
+  it 'logs to the textarea correctly when debug mode', ->
+    u = new DebugUI()
     u.$msglog = { value: '', scrollTop: 0, scrollHeight: 1337 }
-    u.debug = false
-    u.log 'test'
-    expect(u.$msglog.value).toEqual ''
-    expect(u.$msglog.scrollTop).toEqual 0
-    u.debug = true
     u.log 'test'
     expect(u.$msglog.value).toEqual 'test\n'
     expect(u.$msglog.scrollTop).toEqual 1337
diff --git a/proxy/ui.coffee b/proxy/ui.coffee
index fb13aa4..d887dbd 100644
--- a/proxy/ui.coffee
+++ b/proxy/ui.coffee
@@ -3,17 +3,29 @@ All of Snowflake's DOM manipulation and inputs.
 ###
 
 class UI
-  debug: false  # True when there's no badge
+  setStatus: (msg) =>
+
+  setActive: (connected) =>
+
+  log: (msg) =>
+
 
+class BadgeUI extends UI
+  $badge: null
+
+  constructor: ->
+    @$badge = document.getElementById('badge')
+
+  setActive: (connected) =>
+    @$badge.className = if connected then 'active' else ''
+
+
+class DebugUI extends UI
   # DOM elements references.
   $msglog: null
   $status: null
 
   constructor: ->
-    @$badge = document.getElementById('badge')
-    @debug = null == @$badge
-    return if !@debug
-
     # Setup other DOM handlers if it's debug mode.
     @$status = document.getElementById('status')
     @$msglog = document.getElementById('msglog')
@@ -21,17 +33,12 @@ class UI
 
   # Status bar
   setStatus: (msg) =>
-    return if !@debug
     @$status.innerHTML = 'Status: ' + msg
 
   setActive: (connected) =>
-    if @debug
-      @$msglog.className = if connected then 'active' else ''
-    else
-      @$badge.className = if connected then 'active' else ''
+    @$msglog.className = if connected then 'active' else ''
 
   log: (msg) =>
-    return if !@debug
     # Scroll to latest
     @$msglog.value += msg + '\n'
     @$msglog.scrollTop = @$msglog.scrollHeight





More information about the tor-commits mailing list