1"""
2Tests basic Main Thread Checker support (detecting a main-thread-only violation).
3"""
4
5import lldb
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test.decorators import *
8import lldbsuite.test.lldbutil as lldbutil
9from lldbsuite.test.lldbplatformutil import *
10import json
11
12
13class MTCSimpleTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    @skipUnlessDarwin
18    @skipIf(compiler="clang", compiler_version=['<', '9.0'])
19    def test(self):
20        self.mtc_dylib_path = findMainThreadCheckerDylib()
21        if self.mtc_dylib_path == "":
22            self.skipTest("This test requires libMainThreadChecker.dylib")
23
24        self.build()
25        self.mtc_tests()
26
27    @skipIf(archs=['i386'])
28    @skipIf(compiler="clang", compiler_version=['<', '9.0'])
29    def mtc_tests(self):
30        self.assertNotEqual(self.mtc_dylib_path, "")
31
32        # Load the test
33        exe = self.getBuildArtifact("a.out")
34        self.expect("file " + exe, patterns=["Current executable set to .*a.out"])
35
36        self.runCmd("env DYLD_INSERT_LIBRARIES=%s" % self.mtc_dylib_path)
37        self.runCmd("run")
38
39        process = self.dbg.GetSelectedTarget().process
40        thread = process.GetSelectedThread()
41        frame = thread.GetSelectedFrame()
42
43        view = "NSView" if lldbplatformutil.getPlatform() == "macosx" else "UIView"
44
45        self.expect("thread info",
46                    substrs=['stop reason = -[' + view +
47                             ' superview] must be used from main thread only'])
48
49        self.expect(
50            "thread info -s",
51            substrs=[
52                "api_name",
53                "class_name",
54                "description",
55                "instrumentation_class",
56                "selector"
57            ])
58        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonInstrumentation)
59        output_lines = self.res.GetOutput().split('\n')
60        json_line = '\n'.join(output_lines[2:])
61        data = json.loads(json_line)
62        self.assertEqual(data["instrumentation_class"], "MainThreadChecker")
63        self.assertEqual(data["api_name"], "-[" + view + " superview]")
64        self.assertEqual(data["class_name"], view)
65        self.assertEqual(data["selector"], "superview")
66        self.assertEqual(data["description"], "-[" + view + " superview] must be used from main thread only")
67