2014-09-06 17:06:06 -07:00
|
|
|
// Copyright 2014 Team 254. All Rights Reserved.
|
|
|
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
|
|
|
|
2017-08-31 23:26:22 -07:00
|
|
|
package web
|
2014-09-06 17:06:06 -07:00
|
|
|
|
|
|
|
|
import (
|
2018-04-11 23:26:36 -07:00
|
|
|
"github.com/Team254/cheesy-arena/game"
|
2014-09-06 17:06:06 -07:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-08-27 23:42:15 -07:00
|
|
|
"sync"
|
2014-09-06 17:06:06 -07:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAllianceStationDisplay(t *testing.T) {
|
2017-08-28 20:14:32 -07:00
|
|
|
web := setupTestWeb(t)
|
2014-09-06 17:06:06 -07:00
|
|
|
|
2017-08-28 20:14:32 -07:00
|
|
|
recorder := web.getHttpResponse("/displays/alliance_station")
|
2014-09-06 17:06:06 -07:00
|
|
|
assert.Equal(t, 200, recorder.Code)
|
|
|
|
|
assert.Contains(t, recorder.Body.String(), "Alliance Station Display - Untitled Event - Cheesy Arena")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAllianceStationDisplayWebsocket(t *testing.T) {
|
2017-08-28 20:14:32 -07:00
|
|
|
web := setupTestWeb(t)
|
2014-09-06 17:06:06 -07:00
|
|
|
|
2017-08-28 20:14:32 -07:00
|
|
|
server, wsUrl := web.startTestServer()
|
2014-09-06 17:06:06 -07:00
|
|
|
defer server.Close()
|
|
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(wsUrl+"/displays/alliance_station/websocket?displayId=1", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
defer conn.Close()
|
2016-08-27 23:42:15 -07:00
|
|
|
ws := &Websocket{conn, new(sync.Mutex)}
|
2014-09-06 17:06:06 -07:00
|
|
|
|
|
|
|
|
// Should get a few status updates right after connection.
|
|
|
|
|
readWebsocketType(t, ws, "setAllianceStationDisplay")
|
|
|
|
|
readWebsocketType(t, ws, "matchTiming")
|
|
|
|
|
readWebsocketType(t, ws, "matchTime")
|
|
|
|
|
readWebsocketType(t, ws, "setMatch")
|
|
|
|
|
readWebsocketType(t, ws, "realtimeScore")
|
|
|
|
|
|
|
|
|
|
// Change to a different screen.
|
2017-08-28 20:14:32 -07:00
|
|
|
web.arena.AllianceStationDisplayScreen = "logo"
|
|
|
|
|
web.arena.AllianceStationDisplayNotifier.Notify(nil)
|
2014-09-06 17:06:06 -07:00
|
|
|
readWebsocketType(t, ws, "matchTime")
|
|
|
|
|
readWebsocketType(t, ws, "setAllianceStationDisplay")
|
|
|
|
|
|
|
|
|
|
// Inform the server what display ID this is.
|
2017-08-28 20:14:32 -07:00
|
|
|
assert.Equal(t, "", web.arena.AllianceStationDisplays["1"])
|
2014-09-06 17:06:06 -07:00
|
|
|
ws.Write("setAllianceStation", "R3")
|
|
|
|
|
time.Sleep(time.Millisecond * 10) // Allow some time for the command to be processed.
|
2017-08-28 20:14:32 -07:00
|
|
|
assert.Equal(t, "R3", web.arena.AllianceStationDisplays["1"])
|
2014-09-06 17:06:06 -07:00
|
|
|
|
|
|
|
|
// Run through a match cycle.
|
2017-08-28 20:14:32 -07:00
|
|
|
web.arena.MatchLoadTeamsNotifier.Notify(nil)
|
2014-09-06 17:06:06 -07:00
|
|
|
readWebsocketType(t, ws, "setMatch")
|
2017-08-28 20:14:32 -07:00
|
|
|
web.arena.AllianceStations["R1"].Bypass = true
|
|
|
|
|
web.arena.AllianceStations["R2"].Bypass = true
|
|
|
|
|
web.arena.AllianceStations["R3"].Bypass = true
|
|
|
|
|
web.arena.AllianceStations["B1"].Bypass = true
|
|
|
|
|
web.arena.AllianceStations["B2"].Bypass = true
|
|
|
|
|
web.arena.AllianceStations["B3"].Bypass = true
|
|
|
|
|
web.arena.StartMatch()
|
|
|
|
|
web.arena.Update()
|
2018-04-11 23:26:36 -07:00
|
|
|
readWebsocketType(t, ws, "matchTime")
|
|
|
|
|
web.arena.MatchStartTime = time.Now().Add(-time.Duration(game.MatchTiming.WarmupDurationSec) * time.Second)
|
|
|
|
|
web.arena.Update()
|
2015-01-11 15:32:31 -08:00
|
|
|
messages := readWebsocketMultiple(t, ws, 2)
|
2014-09-06 17:06:06 -07:00
|
|
|
_, ok := messages["status"]
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
_, ok = messages["matchTime"]
|
|
|
|
|
assert.True(t, ok)
|
2017-08-28 20:14:32 -07:00
|
|
|
web.arena.RealtimeScoreNotifier.Notify(nil)
|
2014-09-06 17:06:06 -07:00
|
|
|
readWebsocketType(t, ws, "realtimeScore")
|
|
|
|
|
}
|