|
| 1 | +//------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 4 | +//------------------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +#include "stdafx.h" |
| 7 | +#include "catch.hpp" |
| 8 | +#include <process.h> |
| 9 | + |
| 10 | +#pragma warning(disable:4100) // unreferenced formal parameter |
| 11 | +#pragma warning(disable:6387) // suppressing preFAST which raises warning for passing null to the JsRT APIs |
| 12 | +#pragma warning(disable:6262) // CATCH is using stack variables to report errors, suppressing the preFAST warning. |
| 13 | + |
| 14 | +namespace JsDiagApiTest |
| 15 | +{ |
| 16 | + static void CALLBACK DebugEventCallback(JsDiagDebugEvent debugEvent, JsValueRef eventData, void* callbackState) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + bool TestSetup(JsRuntimeHandle *runtime) |
| 21 | + { |
| 22 | + JsValueRef context = JS_INVALID_REFERENCE; |
| 23 | + JsValueRef setContext = JS_INVALID_REFERENCE; |
| 24 | + |
| 25 | + // Create runtime, context and set current context |
| 26 | + REQUIRE(JsCreateRuntime(JsRuntimeAttributeNone, nullptr, runtime) == JsNoError); |
| 27 | + REQUIRE(JsCreateContext(*runtime, &context) == JsNoError); |
| 28 | + REQUIRE(JsSetCurrentContext(context) == JsNoError); |
| 29 | + REQUIRE(((JsGetCurrentContext(&setContext) == JsNoError) || setContext == context)); |
| 30 | + |
| 31 | + // Enable debugging |
| 32 | + REQUIRE(JsDiagStartDebugging(*runtime, JsDiagApiTest::DebugEventCallback, nullptr) == JsNoError); |
| 33 | + |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + bool TestCleanup(JsRuntimeHandle runtime) |
| 38 | + { |
| 39 | + if (runtime != nullptr) |
| 40 | + { |
| 41 | + // Disable debugging |
| 42 | + JsDiagStopDebugging(runtime, nullptr); |
| 43 | + |
| 44 | + JsSetCurrentContext(nullptr); |
| 45 | + JsDisposeRuntime(runtime); |
| 46 | + } |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + template <class Handler> |
| 51 | + void WithSetup(Handler handler) |
| 52 | + { |
| 53 | + JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE; |
| 54 | + if (!TestSetup(&runtime)) |
| 55 | + { |
| 56 | + REQUIRE(false); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + handler(runtime); |
| 61 | + |
| 62 | + TestCleanup(runtime); |
| 63 | + } |
| 64 | + |
| 65 | +#ifndef BUILD_WITHOUT_SCRIPT_DEBUG |
| 66 | + void BreakpointsContextTest(JsRuntimeHandle runtime) |
| 67 | + { |
| 68 | + JsContextRef context = JS_INVALID_REFERENCE; |
| 69 | + REQUIRE(JsGetCurrentContext(&context) == JsNoError); |
| 70 | + |
| 71 | + // Verify no errors with a context set |
| 72 | + JsValueRef scriptsArray = JS_INVALID_REFERENCE; |
| 73 | + REQUIRE(JsDiagGetScripts(&scriptsArray) == JsNoError); |
| 74 | + |
| 75 | + // Verify the APIs return an error when no current context set |
| 76 | + JsSetCurrentContext(nullptr); |
| 77 | + CHECK(JsDiagGetScripts(&scriptsArray) == JsErrorNoCurrentContext); |
| 78 | + |
| 79 | + JsValueRef breakpoint = JS_INVALID_REFERENCE; |
| 80 | + CHECK(JsDiagSetBreakpoint(0, 0, 0, &breakpoint) == JsErrorNoCurrentContext); |
| 81 | + |
| 82 | + CHECK(JsDiagRemoveBreakpoint(0) == JsErrorNoCurrentContext); |
| 83 | + } |
| 84 | + |
| 85 | + TEST_CASE("JsDiagApiTest_BreakpointsContextTest", "[JsDiagApiTest]") |
| 86 | + { |
| 87 | + JsDiagApiTest::WithSetup(JsDiagApiTest::BreakpointsContextTest); |
| 88 | + } |
| 89 | +#endif // BUILD_WITHOUT_SCRIPT_DEBUG |
| 90 | +} |
0 commit comments