|
| 1 | +package lilliput |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestGIFOperations(t *testing.T) { |
| 11 | + t.Run("GIFDuration", testGIFDuration) |
| 12 | +} |
| 13 | + |
| 14 | +func testGIFDuration(t *testing.T) { |
| 15 | + testCases := []struct { |
| 16 | + name string |
| 17 | + filename string |
| 18 | + wantLoopCount int |
| 19 | + wantFrames int |
| 20 | + wantDuration time.Duration |
| 21 | + description string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "Standard animated GIF", |
| 25 | + filename: "testdata/party-discord.gif", |
| 26 | + wantLoopCount: 0, // infinite loop |
| 27 | + wantFrames: 16, |
| 28 | + wantDuration: time.Millisecond * 480, |
| 29 | + description: "Basic animation with custom delays", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Static GIF image", |
| 33 | + filename: "testdata/ferry_sunset.gif", |
| 34 | + wantLoopCount: 1, // play once |
| 35 | + wantFrames: 1, |
| 36 | + wantDuration: 0, |
| 37 | + description: "Static image, no animation", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Single loop GIF", |
| 41 | + filename: "testdata/no-loop.gif", |
| 42 | + wantLoopCount: 1, // play once |
| 43 | + wantFrames: 44, |
| 44 | + wantDuration: time.Millisecond * 4400, |
| 45 | + description: "Animation that plays only once", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Duplicate loop count GIF", |
| 49 | + filename: "testdata/duplicate_number_of_loops.gif", |
| 50 | + wantLoopCount: 2, // play twice |
| 51 | + wantFrames: 2, |
| 52 | + wantDuration: 0, // unable to determine duration |
| 53 | + description: "Animation with duplicate NETSCAPE2.0 extension blocks", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Background dispose GIF", |
| 57 | + filename: "testdata/dispose_bgnd.gif", |
| 58 | + wantLoopCount: 0, // infinite loop |
| 59 | + wantFrames: 5, |
| 60 | + wantDuration: time.Second * 5, |
| 61 | + description: "Animation with background disposal method", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range testCases { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + testGIFImage, err := os.ReadFile(tc.filename) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("Failed to read gif image: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + decoder, err := newGifDecoder(testGIFImage) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("Failed to create decoder: %v", err) |
| 75 | + } |
| 76 | + defer decoder.Close() |
| 77 | + |
| 78 | + // Test loop count |
| 79 | + if got := decoder.LoopCount(); got != tc.wantLoopCount { |
| 80 | + t.Errorf("LoopCount() = %v, want %v", got, tc.wantLoopCount) |
| 81 | + } |
| 82 | + |
| 83 | + // Test frame count |
| 84 | + if got := decoder.FrameCount(); got != tc.wantFrames { |
| 85 | + t.Errorf("FrameCount() = %v, want %v", got, tc.wantFrames) |
| 86 | + } |
| 87 | + |
| 88 | + // Test total duration |
| 89 | + if got := decoder.Duration(); got != tc.wantDuration { |
| 90 | + t.Errorf("Duration() = %v, want %v (%s)", got, tc.wantDuration, tc.description) |
| 91 | + } |
| 92 | + |
| 93 | + // Test per-frame durations |
| 94 | + header, err := decoder.Header() |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("Failed to get header: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + framebuffer := NewFramebuffer(header.width, header.height) |
| 100 | + defer framebuffer.Close() |
| 101 | + |
| 102 | + var totalDuration time.Duration |
| 103 | + frameCount := 0 |
| 104 | + for { |
| 105 | + err = decoder.DecodeTo(framebuffer) |
| 106 | + if err == io.EOF { |
| 107 | + break |
| 108 | + } |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("DecodeTo failed: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + totalDuration += framebuffer.Duration() |
| 114 | + frameCount++ |
| 115 | + } |
| 116 | + |
| 117 | + // Verify total duration matches sum of frame durations |
| 118 | + if totalDuration != tc.wantDuration { |
| 119 | + t.Errorf("Sum of frame durations (%v) doesn't match total duration (%v)", |
| 120 | + totalDuration, tc.wantDuration) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments