diff --git a/api/scanner/periodic_scanner/periodic_scanner_test.go b/api/scanner/periodic_scanner/periodic_scanner_test.go index 2dd48a42..04e6af1e 100644 --- a/api/scanner/periodic_scanner/periodic_scanner_test.go +++ b/api/scanner/periodic_scanner/periodic_scanner_test.go @@ -2,6 +2,7 @@ package periodic_scanner import ( "errors" + "os" "sync" "testing" "time" @@ -23,7 +24,7 @@ func (m *MockScannerQueue) AddAllToQueue() error { } func TestMain(m *testing.M) { - test_utils.UnitTestRun(m) + os.Exit(test_utils.UnitTestRun(m)) } func resetPeriodicScanner() { diff --git a/api/scanner/scanner_test.go b/api/scanner/scanner_test.go index 45534b70..adb06ef4 100644 --- a/api/scanner/scanner_test.go +++ b/api/scanner/scanner_test.go @@ -54,6 +54,9 @@ func TestFullScan(t *testing.T) { "girl_blond3.jpg", "lilac_lilac_bush_lilac.jpg", "mount_merapi_volcano_indonesia.jpg", + + "left_arrow_normal_web.jpg", + "up_arrow_90cw_web.jpg", } wantNonWebPhotos := []string{ "heif.heif", @@ -62,6 +65,9 @@ func TestFullScan(t *testing.T) { "raw_with_jpg.tiff", "standalone_raw.tiff", "tiff.tiff", + + "left_arrow_normal_nonweb.tiff", + "up_arrow_90cw_nonweb.tiff", } wantWebVideos := []string{ "mp4.mp4", @@ -204,6 +210,37 @@ func TestFullScan(t *testing.T) { t.Errorf("all media diff (-got, +want):\n%s", diff) } }) + + t.Run("CheckPhotosOrientation", func(t *testing.T) { + photoFiles := []string{ + "left_arrow_normal_web.jpg", + "up_arrow_90cw_web.jpg", + "left_arrow_normal_nonweb.tiff", + "up_arrow_90cw_nonweb.tiff", + } + for _, filename := range photoFiles { + var media models.Media + if err := db.Preload("MediaURL").Where("title = ?", filename).Find(&media).Error; err != nil { + t.Fatalf("can't find media with name %q: %v", filename, err) + } + + thumbnail, err := media.GetThumbnail() + if err != nil { + t.Fatalf("can't get thumbnail of media %q: %v", filename, err) + } + + switch { + case strings.HasPrefix(filename, "up"): + if thumbnail.Width >= thumbnail.Height { + t.Errorf("media %q dimension: %dx%d, which should be a vertial photo", filename, thumbnail.Width, thumbnail.Height) + } + case strings.HasPrefix(filename, "left"): + if thumbnail.Width <= thumbnail.Height { + t.Errorf("media %q dimension: %dx%d, which should be a horizontal photo", filename, thumbnail.Width, thumbnail.Height) + } + } + } + }) } func equalNameWithoutSuffix(a, b string) bool { diff --git a/api/scanner/test_media/orient/ensure_test.go b/api/scanner/test_media/orient/ensure_test.go new file mode 100644 index 00000000..4e34d488 --- /dev/null +++ b/api/scanner/test_media/orient/ensure_test.go @@ -0,0 +1,61 @@ +package orient + +import ( + "os" + "strings" + "testing" + + "github.com/barasher/go-exiftool" + "github.com/photoview/photoview/api/test_utils" +) + +func TestMain(m *testing.M) { + os.Exit(test_utils.UnitTestRun(m)) +} + +func TestEnsureExifOrient(t *testing.T) { + buf := make([]byte, 64*1024) + + et, err := exiftool.NewExiftool(exiftool.NoPrintConversion(), exiftool.Buffer(buf, 64*1024)) + if err != nil { + t.Fatalf("create exiftool error: %v", err) + } + defer et.Close() + + t.Log("Orientation explaination:") + t.Log("1 = Horizontal (normal)") + t.Log("2 = Mirror horizontal") + t.Log("3 = Rotate 180") + t.Log("4 = Mirror vertical") + t.Log("5 = Mirror horizontal and rotate 270 CW") + t.Log("6 = Rotate 90 CW") + t.Log("7 = Mirror horizontal and rotate 90 CW") + t.Log("8 = Rotate 270 CW") + + // Test files should be present in the same directory as this test + for _, file := range []string{ + "left_arrow_normal_web.jpg", + "up_arrow_90cw_web.jpg", + "left_arrow_normal_nonweb.tiff", + "up_arrow_90cw_nonweb.tiff", + } { + meta := et.ExtractMetadata(file) + if got, want := len(meta), 1; got != want { + t.Fatalf("len(file(%s) meta) = %d, want: %d", file, got, want) + } + + got, err := meta[0].GetInt("Orientation") + if err != nil { + t.Fatalf("get orientation with file %s error: %v", file, err) + } + + want := int64(1) + if strings.Contains(file, "_90cw_") { + want = 6 + } + + if got != want { + t.Errorf("file %q orientation is %d, want: %d", file, got, want) + } + } +} diff --git a/api/scanner/test_media/orient/left_arrow_normal_nonweb.tiff b/api/scanner/test_media/orient/left_arrow_normal_nonweb.tiff new file mode 100644 index 00000000..9ecfb016 Binary files /dev/null and b/api/scanner/test_media/orient/left_arrow_normal_nonweb.tiff differ diff --git a/api/scanner/test_media/orient/left_arrow_normal_web.jpg b/api/scanner/test_media/orient/left_arrow_normal_web.jpg new file mode 100644 index 00000000..dd7b188b Binary files /dev/null and b/api/scanner/test_media/orient/left_arrow_normal_web.jpg differ diff --git a/api/scanner/test_media/orient/up_arrow_90cw_nonweb.tiff b/api/scanner/test_media/orient/up_arrow_90cw_nonweb.tiff new file mode 100644 index 00000000..525aa3cb Binary files /dev/null and b/api/scanner/test_media/orient/up_arrow_90cw_nonweb.tiff differ diff --git a/api/scanner/test_media/orient/up_arrow_90cw_web.jpg b/api/scanner/test_media/orient/up_arrow_90cw_web.jpg new file mode 100644 index 00000000..49451629 Binary files /dev/null and b/api/scanner/test_media/orient/up_arrow_90cw_web.jpg differ