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    def test(self):
19        self.mtc_dylib_path = findMainThreadCheckerDylib()
20        if self.mtc_dylib_path == "":
21            self.skipTest("This test requires libMainThreadChecker.dylib")
22
23        self.build()
24        self.mtc_tests()
25
26    @skipIf(archs=['i386'])
27    def mtc_tests(self):
28        self.assertNotEqual(self.mtc_dylib_path, "")
29
30        # Load the test
31        exe = self.getBuildArtifact("a.out")
32        self.expect("file " + exe, patterns=["Current executable set to .*a.out"])
33
34        self.runCmd("env DYLD_INSERT_LIBRARIES=%s" % self.mtc_dylib_path)
35        self.runCmd("run")
36
37        process = self.dbg.GetSelectedTarget().process
38        thread = process.GetSelectedThread()
39        frame = thread.GetSelectedFrame()
40
41        view = "NSView" if lldbplatformutil.getPlatform() == "macosx" else "UIView"
42
43        self.expect("thread info",
44                    substrs=['stop reason = -[' + view +
45                             ' superview] must be used from main thread only'])
46
47        self.expect(
48            "thread info -s",
49            substrs=[
50                "api_name",
51                "class_name",
52                "description",
53                "instrumentation_class",
54                "selector"
55            ])
56        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonInstrumentation)
57        output_lines = self.res.GetOutput().split('\n')
58        json_line = '\n'.join(output_lines[2:])
59        data = json.loads(json_line)
60        self.assertEqual(data["instrumentation_class"], "MainThreadChecker")
61        self.assertEqual(data["api_name"], "-[" + view + " superview]")
62        self.assertEqual(data["class_name"], view)
63        self.assertEqual(data["selector"], "superview")
64        self.assertEqual(data["description"], "-[" + view + " superview] must be used from main thread only")
65