summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrake.Interop/HandBrakeInterop.Test/TestEncodes.cs
blob: 47498ef7624d3f56cfa2bc9413364396fd5b0f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using HandBrake.Interop;
using HandBrake.Interop.Model;
using HandBrake.Interop.Model.Encoding;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HandBrakeInterop.Test
{
	[TestClass]
	public class TestEncodes
	{
		public const string OutputVideoDirectoryName = "OutputVideos";
		private static readonly string OutputVideoDirectory = Path.Combine(Environment.CurrentDirectory, OutputVideoDirectoryName);

		private ManualResetEvent resetEvent = new ManualResetEvent(false);

		[ClassInitialize]
		public static void Init(TestContext context)
		{
			EnsureOutputVideoDirectoryExists();

			FileInfo[] files = new DirectoryInfo(OutputVideoDirectory).GetFiles();
			foreach (FileInfo file in files)
			{
				file.Delete();
			}
		}

		[TestMethod]
		public void Normal()
		{
			this.RunJob("Normal");
		}

		private void RunJob(string jobName)
		{
			this.resetEvent.Reset();

			EncodeJob job = EncodeJobsPersist.GetJob("Normal");

			if (job.SourceType == SourceType.VideoFolder)
			{
				job.SourcePath = Path.Combine(Environment.CurrentDirectory, Path.GetFileName(job.SourcePath));
			}

			if (job.SourceType == SourceType.File)
			{
				job.SourcePath = Path.Combine(Environment.CurrentDirectory, Path.GetFileName(job.SourcePath));
			}

			string extension;
			if (job.EncodingProfile.OutputFormat == OutputFormat.Mkv)
			{
				extension = ".mkv";
			}
			else
			{
				extension = ".mp4";
			}

			job.OutputPath = Path.Combine(OutputVideoDirectory, jobName + extension);

			var instance = new HandBrakeInstance();
			instance.Initialize(0);
			instance.ScanCompleted += (sender, e) =>
			{
				this.resetEvent.Set();
			};

			instance.StartScan(job.SourcePath, 10);
			this.resetEvent.WaitOne();

			this.resetEvent.Reset();
			instance.EncodeCompleted += (sender, e) =>
			{
				Assert.IsFalse(e.Error);
				this.resetEvent.Set();
			};

			instance.StartEncode(job);
			this.resetEvent.WaitOne();

			Assert.IsTrue(File.Exists(job.OutputPath));

			var fileInfo = new FileInfo(job.OutputPath);
			Assert.IsTrue(fileInfo.Length > 1024);
		}

		private static void EnsureOutputVideoDirectoryExists()
		{
			if (!Directory.Exists(OutputVideoDirectory))
			{
				Directory.CreateDirectory(OutputVideoDirectory);
			}
		}
	}
}