github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/app/internal/testapp/testapp.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin linux
     6  
     7  // Small test app used by app/app_test.go.
     8  package main
     9  
    10  import (
    11  	"log"
    12  	"net"
    13  
    14  	"github.com/c-darwin/mobile/app"
    15  	"github.com/c-darwin/mobile/app/internal/apptest"
    16  	"github.com/c-darwin/mobile/event/lifecycle"
    17  	"github.com/c-darwin/mobile/event/paint"
    18  	"github.com/c-darwin/mobile/event/size"
    19  	"github.com/c-darwin/mobile/event/touch"
    20  	"github.com/c-darwin/mobile/gl"
    21  )
    22  
    23  func main() {
    24  	app.Main(func(a app.App) {
    25  		addr := "127.0.0.1:" + apptest.Port
    26  		log.Printf("addr: %s", addr)
    27  
    28  		conn, err := net.Dial("tcp", addr)
    29  		if err != nil {
    30  			log.Fatal(err)
    31  		}
    32  		defer conn.Close()
    33  		log.Printf("dialled")
    34  		comm := &apptest.Comm{
    35  			Conn:   conn,
    36  			Fatalf: log.Panicf,
    37  			Printf: log.Printf,
    38  		}
    39  
    40  		comm.Send("hello_from_testapp")
    41  		comm.Recv("hello_from_host")
    42  
    43  		color := "red"
    44  		sendPainting := false
    45  		for e := range a.Events() {
    46  			switch e := app.Filter(e).(type) {
    47  			case lifecycle.Event:
    48  				switch e.Crosses(lifecycle.StageVisible) {
    49  				case lifecycle.CrossOn:
    50  					comm.Send("lifecycle_visible")
    51  					sendPainting = true
    52  				case lifecycle.CrossOff:
    53  					comm.Send("lifecycle_not_visible")
    54  				}
    55  			case size.Event:
    56  				comm.Send("size", e.PixelsPerPt, e.Orientation)
    57  			case paint.Event:
    58  				if color == "red" {
    59  					gl.ClearColor(1, 0, 0, 1)
    60  				} else {
    61  					gl.ClearColor(0, 1, 0, 1)
    62  				}
    63  				gl.Clear(gl.COLOR_BUFFER_BIT)
    64  				a.EndPaint(e)
    65  				if sendPainting {
    66  					comm.Send("paint", color)
    67  					sendPainting = false
    68  				}
    69  			case touch.Event:
    70  				comm.Send("touch", e.Type, e.X, e.Y)
    71  				if e.Type == touch.TypeEnd {
    72  					if color == "red" {
    73  						color = "green"
    74  					} else {
    75  						color = "red"
    76  					}
    77  					sendPainting = true
    78  				}
    79  			}
    80  		}
    81  	})
    82  }