Ticket #14413: test-bmsg-previous-delete.2.cpp

File test-bmsg-previous-delete.2.cpp, 3.7 KB (added by ahwayakchih, 6 years ago)

same test app, fixed to post message after ready to run, as advised by PulkoMandy

Line 
1/*
2 * Copyright 2018, Marcin Konicki <shard@neoni.net>
3 * All rights reserved. Distributed under the terms of the MIT license.
4 *
5 * Build it with: g++ -o testApp -lbe test-bmsg-previous-delete.cpp
6 *
7 * Run either:
8 * - testApp -i (safe)
9 * - testApp -d (crash)
10 * - testApp (usage info)
11 */
12#include <Application.h>
13#include <stdio.h>
14
15class TestApp : public BApplication
16{
17public:
18 TestApp(bool deletePrevious = true);
19 ~TestApp();
20
21 void ReadyToRun();
22 void MessageReceived(BMessage* msg);
23
24 bool mDeletePrevious;
25};
26
27TestApp::TestApp(bool deletePrevious)
28 :
29 BApplication("application/x-vnd.Test.DeletePreiousMessage"),
30 mDeletePrevious(deletePrevious)
31{
32}
33
34TestApp::~TestApp()
35{
36}
37
38void
39TestApp::ReadyToRun()
40{
41 BMessage request('test');
42 request.AddInt32("step", 1);
43 printf("\tTesting BMessage::Previous %s deleting returned message\n",
44 mDeletePrevious ? "WITH" : "WITHOUT");
45 request.AddBool("deletePrevious", mDeletePrevious);
46 printf("\tSending test message with need for a reply... ");
47 PostMessage(&request, this, this);
48 printf("done\n");
49}
50
51void
52TestApp::MessageReceived(BMessage* msg)
53{
54 printf("\n\tReceived message\n");
55 msg->PrintToStream();
56 if (msg->what != 'test') {
57 printf("\tUnknown message, pass over to BApplication and return\n");
58 BApplication::MessageReceived(msg);
59 return;
60 }
61
62 if (msg->GetInt32("step", 0) == 1) {
63 printf("\tGot request for a reply\n");
64 BMessage reply('test');
65 reply.AddInt32("step", 2);
66 printf("\tSend reply... ");
67 msg->SendReply(&reply);
68 printf("done\n");
69 printf("\tReturn\n");
70 return;
71 }
72
73 if (msg->IsReply() && msg->GetInt32("step", 0) == 2) {
74 printf("\tGot reply\n");
75 const BMessage* previous = msg->Previous();
76 if (previous && previous->GetInt32("step", 0) == 1) {
77 printf("\tFound previous BMessage* with correct step number\n");
78 if (previous->GetBool("deletePrevious", true)) {
79 printf("\tDeleting previous as advised by documentation\n");
80 delete previous;
81 } else
82 printf(
83 "\tLeaving previous, hoping it will be deleted by API\n");
84
85 printf("\tTest complete, posting B_QUIT_REQUESTED to quit... ");
86 PostMessage(B_QUIT_REQUESTED);
87 printf("done\n");
88 printf("\tReturn\n");
89 return;
90 }
91
92 printf("\tPrevious not found or did not contain correct step number\n");
93 printf("\tReturn\n");
94 return;
95 }
96
97 printf("\tHow did we get here? Anyway, pass message over to BApplication "
98 "and return\n");
99 BApplication::MessageReceived(msg);
100}
101
102void
103PrintUsage()
104{
105 printf(
106 ""
107 "Usage: testApp [option]\n"
108 "Options:\n"
109 "\t-h print this usage info\n"
110 "\t-d delete BMessage returned from BMessage::Previous\n"
111 "\t-i ignore documentation and leave returned object to the API\n"
112 "\n"
113 "It runs test application which sends message to itself and then "
114 "replies to it.\n"
115 "After receiving reply, it gets result of BMessage->Previous() and, "
116 "depending \n"
117 "on option passed to it, deletes result or not.\n"
118 "Documentation mentions that result SHOULD be deleted by the caller:\n"
119 "https://www.haiku-os.org/docs/api/"
120 "classBMessage.html#a57c84d02e54ef19fd1516d9bf7e3fe45\n"
121 "But when we do that on Haiku, application crashes. That suggests "
122 "that, unlike\n"
123 "described in documentation, Haiku does not return a COPY of previous "
124 "message.\n"
125 "Instead it seems to return a pointer to ORIGINAL BMessage.\n");
126}
127
128int
129main(int argc, char** argv)
130{
131 if (argc <= 1 || argv[1][0] != '-' || argv[1][1] == 'h') {
132 PrintUsage();
133 return B_NO_ERROR;
134 }
135
136 bool deletePrevious = argv[1][1] == 'd';
137
138 printf("Creating TestApp... ");
139 TestApp* app = new TestApp(deletePrevious);
140 printf("done\n");
141 printf("Calling TestApp->Run()\n");
142 app->Run();
143 printf("... done.\nDeleting TestApp... ");
144 delete app;
145 printf("done.\nBye bye!\n");
146
147 return B_NO_ERROR;
148}