Exemple #1
0
        public void Engine_Console_Exists()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                Assert.NotNull(engine.Resolve("console"));
            }
        }
        public void AudioSwitcher_AudioDevice_setMute_false()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                const string js = @"AudioSwitcher.getAudioDevices()[0].mute(false)";
                Assert.Equal(false, engine.Evaluate <bool>(js).Value);
            }
        }
        public void Core_sleep_Exists()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddCoreLibrary();
                engine.Execute("Core = require('Core');");
                engine.Execute("Core.sleep(100)");
            }
        }
        public void Core_Create_Guid()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddCoreLibrary();
                engine.Execute("Core = require('Core');");
                Assert.NotNull(engine.Evaluate <Guid>("Core.createGuid()"));
            }
        }
        public void Engine_AddLibrary_AudioSwitcher()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");
                Assert.NotNull(engine.Resolve("AudioSwitcher"));
            }
        }
        public void Engine_AddLibrary_Core()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddCoreLibrary();
                engine.Execute("Core = require('Core');");
                Assert.NotNull(engine.Resolve("Core"));
            }
        }
Exemple #7
0
        public void Engine_Throws_Something_Useful_2()
        {
            var engine = new JsEngine();

            using (var context = engine.CreateExecutionContext())
            {
                var result = context.Execute("dsafsadf.sasadfaf()");
                Assert.NotNull(result.ExecutionException);
                Assert.NotEmpty(result.ExecutionException.Message);
            }
        }
Exemple #8
0
        public void Engine_AddLibrary_Dynamic()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddLibrary("Dynamic", new TestLibrary());

                engine.Execute("Dynamic = require('Dynamic');");
                Assert.NotNull(engine.Resolve("Dynamic"));
            }
        }
Exemple #9
0
        public void Engine_Console_Logs_Objects()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.SetOutput(new ConsoleScriptOutput());
                var result = engine.Execute("console.log('hi', { hi: 'hi' });");

                Assert.Null(result.ExecutionException);
            }
        }
        public void AudioSwitcher_getAudioDevices_Playback()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");
                engine.Execute("AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.PLAYBACK)");
                Assert.Equal(2, engine.Evaluate <int>("AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.PLAYBACK).length").Value);
            }
        }
        public void AudioSwitcher_getAudioDevices_Capture_IsCapture()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");
                var device  = engine.Evaluate <JavaScriptAudioDevice>("AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.CAPTURE)[0]").Value;
                var devices = engine.Evaluate <JavaScriptAudioDevice[]>("AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.CAPTURE)").Value;
                Assert.True(devices.All(x => x.IsCapture));
                Assert.True(device.IsCapture);
            }
        }
Exemple #12
0
        public void Engine_Dynamic_Value_Exists()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                var testLib = new TestLibrary();
                engine.AddLibrary("Dynamic", testLib);

                engine.Execute("Dynamic = require('Dynamic');");
                Assert.NotNull(engine.Resolve("Dynamic"));
                Assert.Equal(testLib.Property, engine.Evaluate <int>("Dyanmic.Property").Value);
            }
        }
        public void AudioSwitcher_getAudioDevices_Playback_IsPlayback_Enum()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");
                var device  = engine.Evaluate <JavaScriptAudioDevice>("AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.PLAYBACK)[0]").Value;
                var devices = engine.Evaluate <IEnumerable <JavaScriptAudioDevice> >("AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.PLAYBACK)").Value;
                Assert.True(devices.All(x => x.IsPlayback));
                Assert.True(device.IsPlayback);
            }
        }
        public void AudioSwitcher_AudioDevice_setMute_true()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");

                const string js = @"AudioSwitcher.getAudioDevices()[0].mute(true)";

                //Sets to muted
                Assert.Equal(true, engine.Evaluate <bool>(js).Value);
            }
        }
Exemple #15
0
        public void Engine_WithArgs()
        {
            var test = new[]
            {
                "One",
                "Two"
            };

            var engine = new JsEngine();

            using (var context = engine.CreateExecutionContext(test))
            {
                Assert.NotNull(context.Evaluate <string[]>("args", test));
            }
        }
        public void AudioSwitcher_AudioDevice_toggleMute()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");

                const string js = @"AudioSwitcher.getAudioDevices()[0].toggleMute()";

                //Toggles the mute and tests non equality of state
                var isMuted = engine.Evaluate <bool>(js).Value;
                Assert.NotEqual(isMuted, engine.Evaluate <bool>(js).Value);
            }
        }
        public void AudioSwitcher_AudioDevice_getFlags()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");

                const string js     = @"AudioSwitcher.getAudioDevices()[0].flags";
                var          result = engine.Execute(js);
                Assert.NotNull(result);
                Assert.Null(result.ExecutionException);
                Assert.True(result.Success);
            }
        }
Exemple #18
0
        public void Engine_Dynamic_Method_Returns()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                var testLib = new TestLibrary();
                engine.AddLibrary("Dynamic", testLib);

                engine.Execute("Dynamic = require('Dynamic');");
                Assert.NotNull(engine.Resolve("Dynamic"));

                var result = engine.Evaluate <string>("Dynamic.Method()");

                Assert.Equal(testLib.Method(), result.Value);
            }
        }
        public void AudioSwitcher_AudioDevice_setDefault_Capture_Comm()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");

                const string setDefault   = @"AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.CAPTURE)[0].setAsDefaultComm()";
                const string checkDefault = @"AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.CAPTURE)[0].isDefaultComm";

                engine.Execute(setDefault);

                Assert.Equal(true, engine.Evaluate <bool>(checkDefault).Value);
            }
        }
Exemple #20
0
        public void Engine_Dynamic_Method_Returns_Clr()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                var testLib = new TestLibrary();
                engine.AddLibrary("Dynamic", testLib);

                engine.Execute("Dynamic = require('Dynamic');");
                Assert.NotNull(engine.Resolve("Dynamic"));

                var result = engine.Evaluate <int>("Dynamic.MethodReturnClr().Field");

                Assert.Equal(new ClrObject().Field, result.Value);
            }
        }
        public void AudioSwitcher_AudioDevice_getVolume()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");

                const string js = @"AudioSwitcher.getAudioDevices()[0].volume()";

                //don't care what it returns, just that it exists
                var result = engine.Execute(js);
                Assert.NotNull(result);
                Assert.Null(result.ExecutionException);
                Assert.True(result.Success);
            }
        }
Exemple #22
0
        public void Engine_Dynamic_Delegate_Func_Fail()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                var testLib = new TestLibrary();
                engine.AddLibrary("Dynamic", testLib);

                engine.Execute("Dynamic = require('Dynamic');");
                Assert.NotNull(engine.Resolve("Dynamic"));

                var result = engine.Evaluate <string>("Dynamic.DelegateWithArguments('Hello')");

                //Assert.NotNull(result.ExecutionException);
                Assert.Equal(testLib.DelegateWithArguments("Hello"), result.Value);
            }
        }
        public void AudioSwitcher_AudioDevice_setVolume()
        {
            var eng = new JsEngine();

            using (var context = eng.CreateExecutionContext())
            {
                context.AddAudioSwitcherLibrary(GetAudioController());
                context.Execute("AudioSwitcher = require('AudioSwitcher');");

                const string setTo10       = @"AudioSwitcher.getAudioDevices()[0].volume(10)";
                const string getVolume     = @"AudioSwitcher.getAudioDevices()[0].volume()";
                var          orignalVol    = context.Evaluate <int>(getVolume).Value;
                var          setToOriginal = @"AudioSwitcher.getAudioDevices()[0].volume(" + orignalVol + ")";

                Assert.Equal(10, context.Evaluate <int>(setTo10).Value);
                Assert.Equal(10, context.Evaluate <int>(getVolume).Value);
                Assert.Equal(orignalVol, context.Evaluate <int>(setToOriginal).Value);
            }
        }
Exemple #24
0
        public void Engine_Dynamic_Delegate_Returns()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                var testLib = new TestLibrary();
                engine.AddLibrary("Dynamic", testLib);

                engine.Execute("import('Dyanmic');");

                engine.Execute("Dynamic = require('Dynamic');");
                Assert.NotNull(engine.Resolve("Dynamic"));

                var result = engine.Evaluate <int>("Dynamic.Delegate()");

                Assert.Equal(testLib.Property, result.Value);
            }
        }
        public void AudioSwitcher_AudioDevice_Exists()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");

                const string js = @"AudioSwitcher.getAudioDevices()[0];";

                var result = engine.Execute(js);
                Assert.NotNull(result);
                Assert.Null(result.ExecutionException);
                Assert.True(result.Success);
                Assert.NotEqual(null, engine.Evaluate <JavaScriptAudioDevice>(js));
                Assert.IsType <JavaScriptAudioDevice>(engine.Evaluate <JavaScriptAudioDevice>(js).Value);
            }
        }
        public void AudioSwitcher_getAudioDeviceByName_Capture_Flags()
        {
            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddAudioSwitcherLibrary(GetAudioController());
                engine.Execute("AudioSwitcher = require('AudioSwitcher');");

                const string js = @"AudioSwitcher.getAudioDevice(AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.CAPTURE)[0].name, AudioSwitcher.DeviceType.CAPTURE);";

                var result = engine.Execute(js);
                Assert.NotNull(result);
                Assert.Null(result.ExecutionException);
                Assert.True(result.Success);
                var audioDevice         = engine.Evaluate <JavaScriptAudioDevice>("AudioSwitcher.getAudioDevices(AudioSwitcher.DeviceType.CAPTURE)[0]").Value;
                var resolvedAudioDevice = engine.Evaluate <JavaScriptAudioDevice>(js).Value;

                Assert.NotEqual(null, resolvedAudioDevice);
                Assert.Equal(audioDevice.Id, resolvedAudioDevice.Id);
                Assert.IsType <JavaScriptAudioDevice>(resolvedAudioDevice);
                Assert.True(resolvedAudioDevice.IsCapture);
            }
        }
Exemple #27
0
        private static int Main(string[] args)
        {
            if (args.Length > 2 || args.Length < 1)
            {
                return(PrintUsage());
            }

            //Process CLI Arguments
            for (var i = 0; i < args.Length - 1; i++)
            {
                switch (args[i])
                {
                case "--debug":
                    _isDebug = true;
                    break;
                }
            }

            //Process file name
            var fName = args[args.Length - 1];

            if (!fName.EndsWith(".js"))
            {
                Console.WriteLine("Invalid input file");
                Console.WriteLine();
                return(PrintUsage());
            }

            IAudioController controller;

            if (_isDebug)
            {
                controller = new SandboxAudioController(new CoreAudioController());
            }
            else
            {
                controller = new CoreAudioController();
            }

            var eng = new JsEngine();

            using (var engine = eng.CreateExecutionContext())
            {
                engine.AddCoreLibrary();
                engine.AddAudioSwitcherLibrary(controller);

                //Enable to log to CLI
                //engine.SetGlobalValue("console", new ConsoleOutput(engine));
                //engine.InternalEngine.SetGlobalValue("console", new FirebugConsole(engine.InternalEngine));
                engine.SetOutput(new ConsoleScriptOutput());

                try
                {
                    using (var file = File.OpenText(fName))
                    {
                        Console.WriteLine("Executing {0}...", fName);
                        var result = engine.Execute(file.ReadToEnd());
                        if (!result.Success)
                        {
                            throw result.ExecutionException;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

#if DEBUG
            Console.ReadKey();
#endif

            return(0);
        }