@@ -43,6 +43,15 @@ static constexpr char kClipboardSetDataUnknownTypeMessage[] =
4343 " {\" method\" :\" Clipboard.setData\" ,\" args\" :{\" madeuptype\" :\" hello\" }}" ;
4444static constexpr char kSystemSoundTypeAlertMessage [] =
4545 " {\" method\" :\" SystemSound.play\" ,\" args\" :\" SystemSoundType.alert\" }" ;
46+ static constexpr char kSystemExitApplicationRequiredMessage [] =
47+ " {\" method\" :\" System.exitApplication\" ,\" args\" :{\" type\" :\" required\" ,"
48+ " \" exitCode\" :1}}" ;
49+ static constexpr char kSystemExitApplicationCancelableMessage [] =
50+ " {\" method\" :\" System.exitApplication\" ,\" args\" :{\" type\" :\" cancelable\" ,"
51+ " \" exitCode\" :2}}" ;
52+ static constexpr char kExitResponseCancelMessage [] =
53+ " [{\" response\" :\" cancel\" }]" ;
54+ static constexpr char kExitResponseExitMessage [] = " [{\" response\" :\" exit\" }]" ;
4655
4756static constexpr int kAccessDeniedErrorCode = 5 ;
4857static constexpr int kErrorSuccess = 0 ;
@@ -73,6 +82,8 @@ class MockPlatformHandler : public PlatformHandler {
7382 void (const std::string&,
7483 std::unique_ptr<MethodResult<rapidjson::Document>>));
7584
85+ MOCK_METHOD1 (QuitApplication, void (int64_t exit_code));
86+
7687 private:
7788 FML_DISALLOW_COPY_AND_ASSIGN (MockPlatformHandler);
7889};
@@ -470,5 +481,70 @@ TEST_F(PlatformHandlerTest, PlaySystemSound) {
470481 EXPECT_EQ (result, " [null]" );
471482}
472483
484+ TEST_F (PlatformHandlerTest, SystemExitApplicationRequired) {
485+ use_headless_engine ();
486+ int exit_code = -1 ;
487+
488+ TestBinaryMessenger messenger ([](const std::string& channel,
489+ const uint8_t * message, size_t size,
490+ BinaryReply reply) {});
491+ MockPlatformHandler platform_handler (&messenger, engine ());
492+
493+ ON_CALL (platform_handler, QuitApplication)
494+ .WillByDefault ([&exit_code](int ec) { exit_code = ec; });
495+ EXPECT_CALL (platform_handler, QuitApplication).Times (1 );
496+
497+ std::string result = SimulatePlatformMessage (
498+ &messenger, kSystemExitApplicationRequiredMessage );
499+ EXPECT_EQ (result, " [{\" response\" :\" exit\" }]" );
500+ EXPECT_EQ (exit_code, 1 );
501+ }
502+
503+ TEST_F (PlatformHandlerTest, SystemExitApplicationCancelableCancel) {
504+ use_headless_engine ();
505+ bool called_cancel = false ;
506+
507+ TestBinaryMessenger messenger (
508+ [&called_cancel](const std::string& channel, const uint8_t * message,
509+ size_t size, BinaryReply reply) {
510+ reply (reinterpret_cast <const uint8_t *>(kExitResponseCancelMessage ),
511+ sizeof (kExitResponseCancelMessage ));
512+ called_cancel = true ;
513+ });
514+ MockPlatformHandler platform_handler (&messenger, engine ());
515+
516+ EXPECT_CALL (platform_handler, QuitApplication).Times (0 );
517+
518+ std::string result = SimulatePlatformMessage (
519+ &messenger, kSystemExitApplicationCancelableMessage );
520+ EXPECT_EQ (result, " [{\" response\" :\" cancel\" }]" );
521+ EXPECT_TRUE (called_cancel);
522+ }
523+
524+ TEST_F (PlatformHandlerTest, SystemExitApplicationCancelableExit) {
525+ use_headless_engine ();
526+ bool called_cancel = false ;
527+ int exit_code = -1 ;
528+
529+ TestBinaryMessenger messenger (
530+ [&called_cancel](const std::string& channel, const uint8_t * message,
531+ size_t size, BinaryReply reply) {
532+ reply (reinterpret_cast <const uint8_t *>(kExitResponseExitMessage ),
533+ sizeof (kExitResponseExitMessage ));
534+ called_cancel = true ;
535+ });
536+ MockPlatformHandler platform_handler (&messenger, engine ());
537+
538+ ON_CALL (platform_handler, QuitApplication)
539+ .WillByDefault ([&exit_code](int ec) { exit_code = ec; });
540+ EXPECT_CALL (platform_handler, QuitApplication).Times (1 );
541+
542+ std::string result = SimulatePlatformMessage (
543+ &messenger, kSystemExitApplicationCancelableMessage );
544+ EXPECT_EQ (result, " [{\" response\" :\" cancel\" }]" );
545+ EXPECT_TRUE (called_cancel);
546+ EXPECT_EQ (exit_code, 2 );
547+ }
548+
473549} // namespace testing
474550} // namespace flutter
0 commit comments