go-toast

Send toast notifications in Windows
Log | Files | Refs | README | LICENSE

bind_test.go (3505B)


      1 //go:build windows
      2 
      3 package wintoast
      4 
      5 import (
      6 	"fmt"
      7 	"path/filepath"
      8 	"reflect"
      9 	"testing"
     10 )
     11 
     12 // TestSetAppData ensures correct control flow.
     13 func TestSetAppData(t *testing.T) {
     14 
     15 	t.Run("set data", func(t *testing.T) {
     16 		var didEarlyOut = true
     17 
     18 		setAppDataFunc = func(data AppData) error {
     19 			didEarlyOut = false
     20 			return nil
     21 		}
     22 
     23 		appData = AppData{}
     24 
     25 		input := AppData{AppID: "test-id"}
     26 
     27 		if err := SetAppData(input); err != nil {
     28 			t.Fatalf("error: %v", err)
     29 		}
     30 
     31 		if appData != input {
     32 			t.Fatalf("want=%v, got %v", input, appData)
     33 		}
     34 
     35 		if didEarlyOut {
     36 			t.Fatalf("expected to manipulate registry, instead early out")
     37 		}
     38 	})
     39 
     40 	t.Run("avoid setting empty data", func(t *testing.T) {
     41 		var didEarlyOut = true
     42 
     43 		setAppDataFunc = func(data AppData) error {
     44 			didEarlyOut = false
     45 			return nil
     46 		}
     47 
     48 		appData = AppData{AppID: "test-id"}
     49 
     50 		input := AppData{}
     51 
     52 		if err := SetAppData(input); err != nil {
     53 			t.Fatalf("error: %v", err)
     54 		}
     55 
     56 		if appData == input {
     57 			t.Fatalf("want=%v, got %v", appData, input)
     58 		}
     59 
     60 		if !didEarlyOut {
     61 			t.Fatal("expected early out, instead registry was manipulated")
     62 		}
     63 	})
     64 
     65 	t.Run("cancel on error", func(t *testing.T) {
     66 		setAppDataFunc = func(data AppData) error {
     67 			return fmt.Errorf("fake error")
     68 		}
     69 
     70 		appData = AppData{}
     71 
     72 		input := AppData{AppID: "test-id"}
     73 
     74 		if err := SetAppData(input); err == nil {
     75 			t.Fatalf("expected error, got nil")
     76 		}
     77 
     78 		if appData == input {
     79 			t.Fatalf("want=%v, got %v", appData, input)
     80 		}
     81 	})
     82 
     83 	t.Run("expect registry keys", func(t *testing.T) {
     84 
     85 		t.Run("minimal keys", func(t *testing.T) {
     86 			setAppDataFunc = setAppDataImpl
     87 
     88 			record := map[string]string{}
     89 
     90 			// Capture what would be written out to the registry.
     91 			writeStringValue = func(path, name, value string) error {
     92 				record[filepath.Join(path, name)] = value
     93 				return nil
     94 			}
     95 
     96 			input := AppData{
     97 				AppID: "test-id",
     98 			}
     99 
    100 			if err := SetAppData(input); err != nil {
    101 				t.Fatalf("unexpected error: %v", err)
    102 			}
    103 
    104 			expect := map[string]string{
    105 				filepath.Join(appKeyRoot, input.AppID, "CustomActivator"): GUID_ImplNotificationActivationCallback.String(),
    106 				filepath.Join(appKeyRoot, input.AppID, "DisplayName"):     input.AppID,
    107 			}
    108 
    109 			if !reflect.DeepEqual(expect, record) {
    110 				t.Fatalf("\nwant=%v \ngot =%v\n", expect, record)
    111 			}
    112 		})
    113 
    114 		t.Run("all keys", func(t *testing.T) {
    115 			setAppDataFunc = setAppDataImpl
    116 
    117 			record := map[string]string{}
    118 
    119 			// Capture what would be written out to the registry.
    120 			writeStringValue = func(path, name, value string) error {
    121 				record[filepath.Join(path, name)] = value
    122 				return nil
    123 			}
    124 
    125 			input := AppData{
    126 				AppID:               "test-id",
    127 				IconPath:            "path/to/icon.ico",
    128 				IconBackgroundColor: "#FFFFFF",
    129 				ActivationExe:       "path/to/exe",
    130 			}
    131 
    132 			if err := SetAppData(input); err != nil {
    133 				t.Fatalf("unexpected error: %v", err)
    134 			}
    135 
    136 			expect := map[string]string{
    137 				filepath.Join(appKeyRoot, input.AppID, "CustomActivator"):     GUID_ImplNotificationActivationCallback.String(),
    138 				filepath.Join(appKeyRoot, input.AppID, "DisplayName"):         input.AppID,
    139 				filepath.Join(appKeyRoot, input.AppID, "IconUri"):             input.IconPath,
    140 				filepath.Join(appKeyRoot, input.AppID, "IconBackgroundColor"): input.IconBackgroundColor,
    141 				filepath.Join(activationKey):                                  input.ActivationExe,
    142 			}
    143 
    144 			if !reflect.DeepEqual(expect, record) {
    145 				t.Fatalf("\nwant=%v \ngot =%v\n", expect, record)
    146 			}
    147 		})
    148 	})
    149 }