Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677package primary
import ( "bytes" "context" "os" "testing" "time")
func TestEncode(t *testing.T) { // wl_display.get_registry(new_id 2): object 1, size 12, opcode 1. got := encode(1, 1, u32(2)) want := []byte{1, 0, 0, 0, 1, 0, 12, 0, 2, 0, 0, 0} if !bytes.Equal(got, want) { t.Errorf("encode = %v, want %v", got, want) } // Strings carry their length with the NUL, and pad to 4 bytes. if got, want := str("wl_seat"), []byte{8, 0, 0, 0, 'w', 'l', '_', 's', 'e', 'a', 't', 0}; !bytes.Equal(got, want) { t.Errorf("str = %v, want %v", got, want) } if got := str("abcd"); len(got) != 4+8 { t.Errorf("str(abcd) is %d bytes, want 12", len(got)) }}
func TestReader(t *testing.T) { body := append(append(u32(7), str("ext_data_control_manager_v1")...), u32(1)...) r := &reader{b: body} if name, iface, version := r.u32(), r.str(), r.u32(); r.err != nil || name != 7 || iface != "ext_data_control_manager_v1" || version != 1 { t.Errorf("read %d %q %d, %v", name, iface, version, r.err) } r = &reader{b: []byte{9, 0, 0, 0, 'a'}} if r.str(); r.err == nil { t.Error("a short string read without error") }}
// TestLive sets and reads the primary selection of the running Wayland// session. It replaces whatever is selected, so it runs only with// PRIMARY_LIVE set.func TestLive(t *testing.T) { if os.Getenv("PRIMARY_LIVE") == "" { t.Skip("set PRIMARY_LIVE to use the session's primary selection") } s, err := Open() if err != nil { t.Fatal(err) } defer s.Close() const text = "100gex primary selection test ✓" if err := s.Set(text); err != nil { t.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(20 * time.Millisecond) { got, err := s.Get(ctx) if err != nil { t.Fatal(err) } if got == text { break } if time.Now().After(deadline) { t.Fatalf("primary selection = %q, want %q", got, text) } } // Another program sees it too. if out := os.Getenv("PRIMARY_LIVE_CHECK"); out != "" { if err := os.WriteFile(out, []byte("ok"), 0o600); err != nil { t.Fatal(err) } time.Sleep(3 * time.Second) // for wl-paste to read it while we own it }}