~mterry/ubuntu-app-launch/fix-ftbfs

« back to all changes in this revision

Viewing changes to tests/libual-test.cc

Merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include <gtest/gtest.h>
21
21
#include <gio/gio.h>
 
22
#include <zeitgeist.h>
22
23
 
23
24
extern "C" {
24
25
#include "ubuntu-app-launch.h"
69
70
                }
70
71
 
71
72
                virtual void SetUp() {
 
73
                        /* Click DB test mode */
 
74
                        g_setenv("TEST_CLICK_DB", "click-db-dir", TRUE);
 
75
                        g_setenv("TEST_CLICK_USER", "test-user", TRUE);
 
76
 
72
77
                        gchar * linkfarmpath = g_build_filename(CMAKE_SOURCE_DIR, "link-farm", NULL);
73
78
                        g_setenv("UBUNTU_APP_LAUNCH_LINK_FARM", linkfarmpath, TRUE);
74
79
                        g_free(linkfarmpath);
110
115
                                "Start",
111
116
                                G_VARIANT_TYPE("(asb)"),
112
117
                                NULL,
113
 
                                "if args[0][0] == 'APP_ID=foo':"
 
118
                                "if args[0][0] == 'APP_ID=com.test.good_application_1.2.3':"
114
119
                                "    raise dbus.exceptions.DBusException('Foo running', name='com.ubuntu.Upstart0_6.Error.AlreadyStarted')",
115
120
                                NULL);
116
121
 
132
137
                        dbus_test_dbus_mock_object_add_property(mock, instobj,
133
138
                                "name",
134
139
                                G_VARIANT_TYPE_STRING,
135
 
                                g_variant_new_string("foo"),
 
140
                                g_variant_new_string("com.test.good_application_1.2.3"),
136
141
                                NULL);
137
142
                        gchar * process_var = g_strdup_printf("[('main', %d)]", getpid());
138
143
                        dbus_test_dbus_mock_object_add_property(mock, instobj,
259
264
                                pause(100);
260
265
                                cleartry++;
261
266
                        }
262
 
                        ASSERT_EQ(bus, nullptr);
 
267
                        ASSERT_EQ(nullptr, bus);
263
268
                }
264
269
 
265
270
                bool check_env (GVariant * env_array, const gchar * var, const gchar * value) {
322
327
        DbusTestDbusMockObject * obj = dbus_test_dbus_mock_get_object(mock, "/com/test/application_click", "com.ubuntu.Upstart0_6.Job", NULL);
323
328
 
324
329
        /* Basic make sure we can send the event */
325
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foolike", NULL));
 
330
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.multiple_first_1.2.3", NULL));
326
331
        EXPECT_EQ(1, dbus_test_dbus_mock_object_check_method_call(mock, obj, "Start", NULL, NULL));
327
332
 
328
333
        ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(mock, obj, NULL));
329
334
 
330
335
        /* Now look at the details of the call */
331
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foolike", NULL));
 
336
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.multiple_first_1.2.3", NULL));
332
337
 
333
338
        guint len = 0;
334
339
        const DbusTestDbusMockCall * calls = dbus_test_dbus_mock_object_get_method_calls(mock, obj, "Start", &len, NULL);
343
348
        g_variant_unref(block);
344
349
 
345
350
        GVariant * env = g_variant_get_child_value(calls->params, 0);
346
 
        EXPECT_TRUE(check_env(env, "APP_ID", "foolike"));
 
351
        EXPECT_TRUE(check_env(env, "APP_ID", "com.test.multiple_first_1.2.3"));
347
352
        g_variant_unref(env);
348
353
 
349
354
        ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(mock, obj, NULL));
355
360
                "file:///home/phablet/test.txt",
356
361
                NULL
357
362
        };
358
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foolike", urls));
 
363
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.multiple_first_1.2.3", urls));
359
364
 
360
365
        len = 0;
361
366
        calls = dbus_test_dbus_mock_object_get_method_calls(mock, obj, "Start", &len, NULL);
363
368
        EXPECT_EQ(1, len);
364
369
 
365
370
        env = g_variant_get_child_value(calls->params, 0);
366
 
        EXPECT_TRUE(check_env(env, "APP_ID", "foolike"));
 
371
        EXPECT_TRUE(check_env(env, "APP_ID", "com.test.multiple_first_1.2.3"));
367
372
        EXPECT_TRUE(check_env(env, "APP_URIS", "'http://ubuntu.com/' 'https://ubuntu.com/' 'file:///home/phablet/test.txt'"));
368
373
        g_variant_unref(env);
369
374
 
374
379
{
375
380
        DbusTestDbusMockObject * obj = dbus_test_dbus_mock_get_object(mock, "/com/test/application_click", "com.ubuntu.Upstart0_6.Job", NULL);
376
381
 
377
 
        ASSERT_TRUE(ubuntu_app_launch_start_application_test("foolike", NULL));
 
382
        ASSERT_TRUE(ubuntu_app_launch_start_application_test("com.test.multiple_first_1.2.3", NULL));
378
383
 
379
384
        guint len = 0;
380
385
        const DbusTestDbusMockCall * calls = dbus_test_dbus_mock_object_get_method_calls(mock, obj, "Start", &len, NULL);
389
394
        g_variant_unref(block);
390
395
 
391
396
        GVariant * env = g_variant_get_child_value(calls->params, 0);
392
 
        EXPECT_TRUE(check_env(env, "APP_ID", "foolike"));
 
397
        EXPECT_TRUE(check_env(env, "APP_ID", "com.test.multiple_first_1.2.3"));
393
398
        EXPECT_TRUE(check_env(env, "QT_LOAD_TESTABILITY", "1"));
394
399
        g_variant_unref(env);
395
400
}
398
403
{
399
404
        DbusTestDbusMockObject * obj = dbus_test_dbus_mock_get_object(mock, "/com/test/application_click", "com.ubuntu.Upstart0_6.Job", NULL);
400
405
 
401
 
        ASSERT_TRUE(ubuntu_app_launch_stop_application("foo"));
 
406
        ASSERT_TRUE(ubuntu_app_launch_stop_application("com.test.good_application_1.2.3"));
402
407
 
403
408
        ASSERT_EQ(dbus_test_dbus_mock_object_check_method_call(mock, obj, "Stop", NULL, NULL), 1);
404
409
 
406
411
 
407
412
TEST_F(LibUAL, ApplicationLog)
408
413
{
409
 
        gchar * click_log = ubuntu_app_launch_application_log_path("foo");
410
 
        EXPECT_STREQ(CMAKE_SOURCE_DIR "/upstart/application-click-foo.log", click_log);
 
414
        gchar * click_log = ubuntu_app_launch_application_log_path("com.test.good_application_1.2.3");
 
415
        EXPECT_STREQ(CMAKE_SOURCE_DIR "/upstart/application-click-com.test.good_application_1.2.3.log", click_log);
411
416
        g_free(click_log);
412
417
 
413
418
        gchar * legacy_single = ubuntu_app_launch_application_log_path("single");
423
428
{
424
429
        /* Check bad params */
425
430
        EXPECT_EQ(0, ubuntu_app_launch_get_primary_pid(NULL));
426
 
        EXPECT_FALSE(ubuntu_app_launch_pid_in_app_id(0, "foo"));
 
431
        EXPECT_FALSE(ubuntu_app_launch_pid_in_app_id(0, "com.test.good_application_1.2.3"));
427
432
        EXPECT_FALSE(ubuntu_app_launch_pid_in_app_id(100, NULL));
428
433
 
429
434
        /* Check primary pid, which comes from Upstart */
430
 
        EXPECT_EQ(ubuntu_app_launch_get_primary_pid("foo"), getpid());
 
435
        EXPECT_EQ(ubuntu_app_launch_get_primary_pid("com.test.good_application_1.2.3"), getpid());
431
436
        EXPECT_EQ(ubuntu_app_launch_get_primary_pid("bar"), 5678);
432
437
 
433
438
        /* Look at the full PID list from CG Manager */
436
441
        guint len = 0;
437
442
 
438
443
        /* Click in the set */
439
 
        EXPECT_TRUE(ubuntu_app_launch_pid_in_app_id(100, "foo"));
 
444
        EXPECT_TRUE(ubuntu_app_launch_pid_in_app_id(100, "com.test.good_application_1.2.3"));
440
445
        calls = dbus_test_dbus_mock_object_get_method_calls(cgmock, cgobject, "GetTasks", &len, NULL);
441
446
        EXPECT_EQ(1, len);
442
447
        EXPECT_STREQ("GetTasks", calls->name);
443
 
        EXPECT_TRUE(g_variant_equal(calls->params, g_variant_new("(ss)", "freezer", "upstart/application-click-foo")));
 
448
        EXPECT_TRUE(g_variant_equal(calls->params, g_variant_new("(ss)", "freezer", "upstart/application-click-com.test.good_application_1.2.3")));
444
449
        ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(cgmock, cgobject, NULL));
445
450
 
446
451
        /* Click out of the set */
447
 
        EXPECT_FALSE(ubuntu_app_launch_pid_in_app_id(101, "foo"));
 
452
        EXPECT_FALSE(ubuntu_app_launch_pid_in_app_id(101, "com.test.good_application_1.2.3"));
448
453
        calls = dbus_test_dbus_mock_object_get_method_calls(cgmock, cgobject, "GetTasks", &len, NULL);
449
454
        EXPECT_EQ(1, len);
450
455
        EXPECT_STREQ("GetTasks", calls->name);
451
 
        EXPECT_TRUE(g_variant_equal(calls->params, g_variant_new("(ss)", "freezer", "upstart/application-click-foo")));
 
456
        EXPECT_TRUE(g_variant_equal(calls->params, g_variant_new("(ss)", "freezer", "upstart/application-click-com.test.good_application_1.2.3")));
452
457
        ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(cgmock, cgobject, NULL));
453
458
 
454
459
        /* Legacy Single Instance */
531
536
 
532
537
        /* Not enforcing order, but wanting to use the GTest functions
533
538
           for "actually testing" so the errors look right. */
534
 
        if (g_strcmp0(apps[0], "foo") == 0) {
535
 
                ASSERT_STREQ(apps[0], "foo");
536
 
                ASSERT_STREQ(apps[1], "bar");
 
539
        if (g_strcmp0(apps[0], "com.test.good_application_1.2.3") == 0) {
 
540
                ASSERT_STREQ("com.test.good_application_1.2.3", apps[0]);
 
541
                ASSERT_STREQ("bar", apps[1]);
537
542
        } else {
538
 
                ASSERT_STREQ(apps[0], "bar");
539
 
                ASSERT_STREQ(apps[1], "foo");
 
543
                ASSERT_STREQ("bar", apps[0]);
 
544
                ASSERT_STREQ("com.test.good_application_1.2.3", apps[1]);
540
545
        }
541
546
 
542
547
        g_strfreev(apps);
579
584
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
580
585
                "EventEmitted",
581
586
                G_VARIANT_TYPE("(sas)"),
582
 
                g_variant_new_parsed("('started', ['JOB=application-click', 'INSTANCE=foo'])"),
 
587
                g_variant_new_parsed("('started', ['JOB=application-click', 'INSTANCE=com.test.good_application_1.2.3'])"),
583
588
                NULL
584
589
        );
585
590
 
593
598
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
594
599
                "EventEmitted",
595
600
                G_VARIANT_TYPE("(sas)"),
596
 
                g_variant_new_parsed("('stopped', ['JOB=application-click', 'INSTANCE=foo'])"),
 
601
                g_variant_new_parsed("('stopped', ['JOB=application-click', 'INSTANCE=com.test.good_application_1.2.3'])"),
597
602
                NULL
598
603
        );
599
604
 
639
644
 
640
645
        /* Test Noise Start */
641
646
        start_data.count = 0;
642
 
        start_data.name = "foo";
 
647
        start_data.name = "com.test.good_application_1.2.3";
643
648
        stop_data.count = 0;
644
 
        stop_data.name = "foo";
 
649
        stop_data.name = "com.test.good_application_1.2.3";
645
650
 
646
651
        /* A full lifecycle */
647
652
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
648
653
                "EventEmitted",
649
654
                G_VARIANT_TYPE("(sas)"),
650
 
                g_variant_new_parsed("('starting', ['JOB=application-click', 'INSTANCE=foo'])"),
651
 
                NULL
652
 
        );
653
 
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
654
 
                "EventEmitted",
655
 
                G_VARIANT_TYPE("(sas)"),
656
 
                g_variant_new_parsed("('started', ['JOB=application-click', 'INSTANCE=foo'])"),
657
 
                NULL
658
 
        );
659
 
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
660
 
                "EventEmitted",
661
 
                G_VARIANT_TYPE("(sas)"),
662
 
                g_variant_new_parsed("('stopping', ['JOB=application-click', 'INSTANCE=foo'])"),
663
 
                NULL
664
 
        );
665
 
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
666
 
                "EventEmitted",
667
 
                G_VARIANT_TYPE("(sas)"),
668
 
                g_variant_new_parsed("('stopped', ['JOB=application-click', 'INSTANCE=foo'])"),
 
655
                g_variant_new_parsed("('starting', ['JOB=application-click', 'INSTANCE=com.test.good_application_1.2.3'])"),
 
656
                NULL
 
657
        );
 
658
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
 
659
                "EventEmitted",
 
660
                G_VARIANT_TYPE("(sas)"),
 
661
                g_variant_new_parsed("('started', ['JOB=application-click', 'INSTANCE=com.test.good_application_1.2.3'])"),
 
662
                NULL
 
663
        );
 
664
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
 
665
                "EventEmitted",
 
666
                G_VARIANT_TYPE("(sas)"),
 
667
                g_variant_new_parsed("('stopping', ['JOB=application-click', 'INSTANCE=com.test.good_application_1.2.3'])"),
 
668
                NULL
 
669
        );
 
670
        dbus_test_dbus_mock_object_emit_signal(mock, obj,
 
671
                "EventEmitted",
 
672
                G_VARIANT_TYPE("(sas)"),
 
673
                g_variant_new_parsed("('stopped', ['JOB=application-click', 'INSTANCE=com.test.good_application_1.2.3'])"),
669
674
                NULL
670
675
        );
671
676
 
721
726
                "/", /* path */
722
727
                "com.canonical.UbuntuAppLaunch", /* interface */
723
728
                "UnityStartingBroadcast", /* signal */
724
 
                g_variant_new("(s)", "foo"), /* params, the same */
 
729
                g_variant_new("(s)", "com.test.good_application_1.2.3"), /* params, the same */
725
730
                NULL);
726
731
 
727
732
        pause(100);
728
733
 
729
 
        EXPECT_EQ("foo", last_observer);
 
734
        EXPECT_EQ("com.test.good_application_1.2.3", last_observer);
730
735
        EXPECT_EQ(1, starting_count);
731
736
 
732
737
        EXPECT_TRUE(ubuntu_app_launch_observer_delete_app_starting(starting_observer, &last_observer));
737
742
 
738
743
TEST_F(LibUAL, AppIdTest)
739
744
{
740
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foo", NULL));
 
745
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.good_application_1.2.3", NULL));
741
746
        pause(50); /* Ensure all the events come through */
742
 
        EXPECT_EQ("foo", this->last_focus_appid);
743
 
        EXPECT_EQ("foo", this->last_resume_appid);
 
747
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_focus_appid);
 
748
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_resume_appid);
744
749
}
745
750
 
746
751
GDBusMessage *
765
770
        GDBusConnection * session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
766
771
        guint filter = g_dbus_connection_add_filter(session,
767
772
                filter_func_good,
768
 
                (gpointer)"/foo",
 
773
                (gpointer)"/com_2etest_2egood_5fapplication_5f1_2e2_2e3",
769
774
                NULL);
770
775
 
771
776
        const gchar * uris[] = {
772
777
                "http://www.test.com",
773
778
                NULL
774
779
        };
775
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foo", uris));
 
780
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.good_application_1.2.3", uris));
776
781
        pause(100); /* Ensure all the events come through */
777
782
 
778
 
        EXPECT_EQ("foo", this->last_focus_appid);
779
 
        EXPECT_EQ("foo", this->last_resume_appid);
 
783
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_focus_appid);
 
784
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_resume_appid);
780
785
 
781
786
        g_dbus_connection_remove_filter(session, filter);
782
787
 
790
795
                        "/", /* path */
791
796
                        "com.canonical.UbuntuAppLaunch", /* interface */
792
797
                        "UnityResumeResponse", /* signal */
793
 
                        g_variant_new("(s)", "foo"), /* params, the same */
 
798
                        g_variant_new("(s)", "com.test.good_application_1.2.3"), /* params, the same */
794
799
                        NULL);
795
800
 
796
801
                pause(50); /* Ensure all the events come through */
806
811
                NULL
807
812
        };
808
813
 
809
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foo", uris));
 
814
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.good_application_1.2.3", uris));
810
815
        pause(100); /* Ensure all the events come through */
811
816
 
812
 
        EXPECT_EQ("foo", this->last_focus_appid);
813
 
        EXPECT_EQ("foo", this->last_resume_appid);
 
817
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_focus_appid);
 
818
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_resume_appid);
814
819
}
815
820
 
816
821
TEST_F(LibUAL, UnityTimeoutTest)
817
822
{
818
823
        this->resume_timeout = 100;
819
824
 
820
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foo", NULL));
 
825
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.good_application_1.2.3", NULL));
821
826
        pause(1000); /* Ensure all the events come through */
822
 
        EXPECT_EQ("foo", this->last_focus_appid);
823
 
        EXPECT_EQ("foo", this->last_resume_appid);
 
827
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_focus_appid);
 
828
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_resume_appid);
824
829
}
825
830
 
826
831
TEST_F(LibUAL, UnityTimeoutUriTest)
832
837
                NULL
833
838
        };
834
839
 
835
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foo", uris));
 
840
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.good_application_1.2.3", uris));
836
841
        pause(1000); /* Ensure all the events come through */
837
 
        EXPECT_EQ("foo", this->last_focus_appid);
838
 
        EXPECT_EQ("foo", this->last_resume_appid);
 
842
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_focus_appid);
 
843
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_resume_appid);
839
844
}
840
845
 
841
846
GDBusMessage *
864
869
                NULL
865
870
        };
866
871
 
867
 
        ASSERT_TRUE(ubuntu_app_launch_start_application("foo", uris));
 
872
        ASSERT_TRUE(ubuntu_app_launch_start_application("com.test.good_application_1.2.3", uris));
868
873
 
869
874
        guint end = g_get_monotonic_time();
870
875
 
871
 
        EXPECT_LT(end - start, 600 * 1000);
 
876
        g_debug("Start call time: %d ms", (end - start) / 1000);
 
877
        EXPECT_LT(end - start, 2000 * 1000);
872
878
 
873
879
        pause(1000); /* Ensure all the events come through */
874
880
 
875
 
        EXPECT_EQ("foo", this->last_focus_appid);
876
 
        EXPECT_EQ("foo", this->last_resume_appid);
 
881
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_focus_appid);
 
882
        EXPECT_EQ("com.test.good_application_1.2.3", this->last_resume_appid);
877
883
 
878
884
        g_dbus_connection_remove_filter(session, filter);
879
885
        g_object_unref(session);
949
955
                "/", /* path */
950
956
                "com.canonical.UbuntuAppLaunch", /* interface */
951
957
                "ApplicationFailed", /* signal */
952
 
                g_variant_new("(ss)", "foo", "crash"), /* params, the same */
953
 
                NULL);
954
 
 
955
 
        pause(100);
956
 
 
957
 
        EXPECT_EQ("foo", last_observer);
958
 
 
959
 
        last_observer.clear();
960
 
 
961
 
        g_dbus_connection_emit_signal(session,
962
 
                NULL, /* destination */
963
 
                "/", /* path */
964
 
                "com.canonical.UbuntuAppLaunch", /* interface */
965
 
                "ApplicationFailed", /* signal */
966
 
                g_variant_new("(ss)", "foo", "blahblah"), /* params, the same */
967
 
                NULL);
968
 
 
969
 
        pause(100);
970
 
 
971
 
        EXPECT_EQ("foo", last_observer);
972
 
 
973
 
        last_observer.clear();
974
 
 
975
 
        g_dbus_connection_emit_signal(session,
976
 
                NULL, /* destination */
977
 
                "/", /* path */
978
 
                "com.canonical.UbuntuAppLaunch", /* interface */
979
 
                "ApplicationFailed", /* signal */
980
 
                g_variant_new("(ss)", "foo", "start-failure"), /* params, the same */
 
958
                g_variant_new("(ss)", "com.test.good_application_1.2.3", "crash"), /* params, the same */
 
959
                NULL);
 
960
 
 
961
        pause(100);
 
962
 
 
963
        EXPECT_EQ("com.test.good_application_1.2.3", last_observer);
 
964
 
 
965
        last_observer.clear();
 
966
 
 
967
        g_dbus_connection_emit_signal(session,
 
968
                NULL, /* destination */
 
969
                "/", /* path */
 
970
                "com.canonical.UbuntuAppLaunch", /* interface */
 
971
                "ApplicationFailed", /* signal */
 
972
                g_variant_new("(ss)", "com.test.good_application_1.2.3", "blahblah"), /* params, the same */
 
973
                NULL);
 
974
 
 
975
        pause(100);
 
976
 
 
977
        EXPECT_EQ("com.test.good_application_1.2.3", last_observer);
 
978
 
 
979
        last_observer.clear();
 
980
 
 
981
        g_dbus_connection_emit_signal(session,
 
982
                NULL, /* destination */
 
983
                "/", /* path */
 
984
                "com.canonical.UbuntuAppLaunch", /* interface */
 
985
                "ApplicationFailed", /* signal */
 
986
                g_variant_new("(ss)", "com.test.good_application_1.2.3", "start-failure"), /* params, the same */
981
987
                NULL);
982
988
 
983
989
        pause(100);
994
1000
        DbusTestDbusMockObject * obj = dbus_test_dbus_mock_get_object(mock, "/com/test/untrusted/helper", "com.ubuntu.Upstart0_6.Job", NULL);
995
1001
 
996
1002
        /* Basic make sure we can send the event */
997
 
        ASSERT_TRUE(ubuntu_app_launch_start_helper("untrusted-type", "foolike", NULL));
 
1003
        ASSERT_TRUE(ubuntu_app_launch_start_helper("untrusted-type", "com.test.multiple_first_1.2.3", NULL));
998
1004
        EXPECT_EQ(1, dbus_test_dbus_mock_object_check_method_call(mock, obj, "Start", NULL, NULL));
999
1005
 
1000
1006
        ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(mock, obj, NULL));
1001
1007
 
1002
1008
        /* Now look at the details of the call */
1003
 
        ASSERT_TRUE(ubuntu_app_launch_start_helper("untrusted-type", "foolike", NULL));
 
1009
        ASSERT_TRUE(ubuntu_app_launch_start_helper("untrusted-type", "com.test.multiple_first_1.2.3", NULL));
1004
1010
 
1005
1011
        guint len = 0;
1006
1012
        const DbusTestDbusMockCall * calls = dbus_test_dbus_mock_object_get_method_calls(mock, obj, "Start", &len, NULL);
1015
1021
        g_variant_unref(block);
1016
1022
 
1017
1023
        GVariant * env = g_variant_get_child_value(calls->params, 0);
1018
 
        EXPECT_TRUE(check_env(env, "APP_ID", "foolike"));
 
1024
        EXPECT_TRUE(check_env(env, "APP_ID", "com.test.multiple_first_1.2.3"));
1019
1025
        EXPECT_TRUE(check_env(env, "HELPER_TYPE", "untrusted-type"));
1020
1026
        EXPECT_FALSE(check_env(env, "INSTANCE_ID", NULL));
1021
1027
        g_variant_unref(env);
1023
1029
        ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(mock, obj, NULL));
1024
1030
 
1025
1031
        /* Now check a multi out */ 
1026
 
        gchar * instance_id = ubuntu_app_launch_start_multiple_helper("untrusted-type", "foolike", NULL);
 
1032
        gchar * instance_id = ubuntu_app_launch_start_multiple_helper("untrusted-type", "com.test.multiple_first_1.2.3", NULL);
1027
1033
        ASSERT_NE(nullptr, instance_id);
1028
1034
        g_debug("Multi-instance ID: %s", instance_id);
1029
1035
 
1040
1046
        g_variant_unref(block);
1041
1047
 
1042
1048
        env = g_variant_get_child_value(calls->params, 0);
1043
 
        EXPECT_TRUE(check_env(env, "APP_ID", "foolike"));
 
1049
        EXPECT_TRUE(check_env(env, "APP_ID", "com.test.multiple_first_1.2.3"));
1044
1050
        EXPECT_TRUE(check_env(env, "HELPER_TYPE", "untrusted-type"));
1045
1051
        EXPECT_TRUE(check_env(env, "INSTANCE_ID", instance_id));
1046
1052
        g_variant_unref(env);
1055
1061
                "file:///home/phablet/test.txt",
1056
1062
                NULL
1057
1063
        };
1058
 
        ASSERT_TRUE(ubuntu_app_launch_start_helper("untrusted-type", "foolike", urls));
 
1064
        ASSERT_TRUE(ubuntu_app_launch_start_helper("untrusted-type", "com.test.multiple_first_1.2.3", urls));
1059
1065
 
1060
1066
        len = 0;
1061
1067
        calls = dbus_test_dbus_mock_object_get_method_calls(mock, obj, "Start", &len, NULL);
1063
1069
        EXPECT_EQ(1, len);
1064
1070
 
1065
1071
        env = g_variant_get_child_value(calls->params, 0);
1066
 
        EXPECT_TRUE(check_env(env, "APP_ID", "foolike"));
 
1072
        EXPECT_TRUE(check_env(env, "APP_ID", "com.test.multiple_first_1.2.3"));
1067
1073
        EXPECT_TRUE(check_env(env, "APP_URIS", "'http://ubuntu.com/' 'https://ubuntu.com/' 'file:///home/phablet/test.txt'"));
1068
1074
        EXPECT_TRUE(check_env(env, "HELPER_TYPE", "untrusted-type"));
1069
1075
        EXPECT_FALSE(check_env(env, "INSTANCE_ID", NULL));
1077
1083
        DbusTestDbusMockObject * obj = dbus_test_dbus_mock_get_object(mock, "/com/test/untrusted/helper", "com.ubuntu.Upstart0_6.Job", NULL);
1078
1084
 
1079
1085
        /* Basic helper */
1080
 
        ASSERT_TRUE(ubuntu_app_launch_stop_helper("untrusted-type", "foo"));
 
1086
        ASSERT_TRUE(ubuntu_app_launch_stop_helper("untrusted-type", "com.test.good_application_1.2.3"));
1081
1087
 
1082
1088
        ASSERT_EQ(dbus_test_dbus_mock_object_check_method_call(mock, obj, "Stop", NULL, NULL), 1);
1083
1089
 
1094
1100
        g_variant_unref(block);
1095
1101
 
1096
1102
        GVariant * env = g_variant_get_child_value(calls->params, 0);
1097
 
        EXPECT_TRUE(check_env(env, "APP_ID", "foo"));
 
1103
        EXPECT_TRUE(check_env(env, "APP_ID", "com.test.good_application_1.2.3"));
1098
1104
        EXPECT_TRUE(check_env(env, "HELPER_TYPE", "untrusted-type"));
1099
1105
        EXPECT_FALSE(check_env(env, "INSTANCE_ID", NULL));
1100
1106
        g_variant_unref(env);
1102
1108
        ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(mock, obj, NULL));
1103
1109
 
1104
1110
        /* Multi helper */
1105
 
        ASSERT_TRUE(ubuntu_app_launch_stop_multiple_helper("untrusted-type", "foo", "instance-me"));
 
1111
        ASSERT_TRUE(ubuntu_app_launch_stop_multiple_helper("untrusted-type", "com.test.good_application_1.2.3", "instance-me"));
1106
1112
 
1107
1113
        ASSERT_EQ(dbus_test_dbus_mock_object_check_method_call(mock, obj, "Stop", NULL, NULL), 1);
1108
1114
 
1119
1125
        g_variant_unref(block);
1120
1126
 
1121
1127
        env = g_variant_get_child_value(calls->params, 0);
1122
 
        EXPECT_TRUE(check_env(env, "APP_ID", "foo"));
 
1128
        EXPECT_TRUE(check_env(env, "APP_ID", "com.test.good_application_1.2.3"));
1123
1129
        EXPECT_TRUE(check_env(env, "HELPER_TYPE", "untrusted-type"));
1124
1130
        EXPECT_TRUE(check_env(env, "INSTANCE_ID", "instance-me"));
1125
1131
        g_variant_unref(env);
1244
1250
        ASSERT_TRUE(ubuntu_app_launch_observer_delete_helper_started(helper_observer_cb, "my-type-is-scorpio", &start_data));
1245
1251
        ASSERT_TRUE(ubuntu_app_launch_observer_delete_helper_stop(helper_observer_cb, "my-type-is-libra", &stop_data));
1246
1252
}
 
1253
 
 
1254
gboolean
 
1255
datain (GIOChannel * source, GIOCondition cond, gpointer data)
 
1256
{
 
1257
        gsize * datacnt = static_cast<gsize *>(data);
 
1258
        gchar * str = NULL;
 
1259
        gsize len = 0;
 
1260
        GError * error = NULL;
 
1261
 
 
1262
        g_io_channel_read_line(source, &str, &len, NULL, &error);
 
1263
        g_free(str);
 
1264
 
 
1265
        if (error != NULL) {
 
1266
                g_warning("Unable to read from channel: %s", error->message);
 
1267
                g_error_free(error);
 
1268
        }
 
1269
 
 
1270
        *datacnt += len;
 
1271
 
 
1272
        return TRUE;
 
1273
}
 
1274
 
 
1275
TEST_F(LibUAL, PauseResume)
 
1276
{
 
1277
        g_setenv("UBUNTU_APP_LAUNCH_NO_SET_OOM", "TRUE", 1);
 
1278
 
 
1279
        /* Setup some spew */
 
1280
        GPid spewpid = 0;
 
1281
        gint spewstdout = 0;
 
1282
        const gchar * spewline[] = { SPEW_UTILITY, NULL };
 
1283
        ASSERT_TRUE(g_spawn_async_with_pipes(NULL,
 
1284
                (gchar **)spewline,
 
1285
                NULL, /* environment */
 
1286
                G_SPAWN_DEFAULT,
 
1287
                NULL, NULL, /* child setup */
 
1288
                &spewpid,
 
1289
                NULL, /* stdin */
 
1290
                &spewstdout,
 
1291
                NULL, /* stderr */
 
1292
                NULL)); /* error */
 
1293
 
 
1294
        gsize datacnt = 0;
 
1295
        GIOChannel * spewoutchan = g_io_channel_unix_new(spewstdout);
 
1296
        g_io_channel_set_flags(spewoutchan, G_IO_FLAG_NONBLOCK, NULL);
 
1297
        g_io_add_watch(spewoutchan, G_IO_IN, datain, &datacnt);
 
1298
 
 
1299
        /* Setup the cgroup */
 
1300
        g_setenv("UBUNTU_APP_LAUNCH_CG_MANAGER_NAME", "org.test.cgmock2", TRUE);
 
1301
        DbusTestDbusMock * cgmock2 = dbus_test_dbus_mock_new("org.test.cgmock2");
 
1302
        DbusTestDbusMockObject * cgobject = dbus_test_dbus_mock_get_object(cgmock2, "/org/linuxcontainers/cgmanager", "org.linuxcontainers.cgmanager0_0", NULL);
 
1303
        gchar * pypids = g_strdup_printf("ret = [%d]", spewpid);
 
1304
        dbus_test_dbus_mock_object_add_method(cgmock, cgobject,
 
1305
                "GetTasks",
 
1306
                G_VARIANT_TYPE("(ss)"),
 
1307
                G_VARIANT_TYPE("ai"),
 
1308
                pypids,
 
1309
                NULL);
 
1310
        g_free(pypids);
 
1311
 
 
1312
        dbus_test_service_add_task(service, DBUS_TEST_TASK(cgmock2));
 
1313
        dbus_test_task_run(DBUS_TEST_TASK(cgmock2));
 
1314
        g_object_unref(G_OBJECT(cgmock2));
 
1315
        
 
1316
        /* Setup ZG Mock */
 
1317
        DbusTestDbusMock * zgmock = dbus_test_dbus_mock_new("org.gnome.zeitgeist.Engine");
 
1318
        DbusTestDbusMockObject * zgobj = dbus_test_dbus_mock_get_object(zgmock, "/org/gnome/zeitgeist/log/activity", "org.gnome.zeitgeist.Log", NULL);
 
1319
 
 
1320
        dbus_test_dbus_mock_object_add_method(zgmock, zgobj,
 
1321
                "InsertEvents",
 
1322
                G_VARIANT_TYPE("a(asaasay)"),
 
1323
                G_VARIANT_TYPE("au"),
 
1324
                "ret = [ 0 ]",
 
1325
                NULL);
 
1326
 
 
1327
        dbus_test_service_add_task(service, DBUS_TEST_TASK(zgmock));
 
1328
        dbus_test_task_run(DBUS_TEST_TASK(zgmock));
 
1329
        g_object_unref(G_OBJECT(zgmock));
 
1330
 
 
1331
        /* Give things a chance to start */
 
1332
        do {
 
1333
                g_debug("Giving mocks a chance to start");
 
1334
                pause(200);
 
1335
        } while (dbus_test_task_get_state(DBUS_TEST_TASK(cgmock2)) != DBUS_TEST_TASK_STATE_RUNNING &&
 
1336
                dbus_test_task_get_state(DBUS_TEST_TASK(zgmock)) != DBUS_TEST_TASK_STATE_RUNNING);
 
1337
 
 
1338
        /* Test it */
 
1339
        EXPECT_NE(0, datacnt);
 
1340
 
 
1341
        /* Pause the app */
 
1342
        EXPECT_TRUE(ubuntu_app_launch_pause_application("com.test.good_application_1.2.3"));
 
1343
 
 
1344
        pause(0); /* Flush queued events */
 
1345
        datacnt = 0; /* clear it */
 
1346
 
 
1347
        pause(200);
 
1348
 
 
1349
        EXPECT_EQ(0, datacnt);
 
1350
 
 
1351
        /* Check to make sure we sent the event to ZG */
 
1352
        guint numcalls = 0;
 
1353
        const DbusTestDbusMockCall * calls = dbus_test_dbus_mock_object_get_method_calls(zgmock, zgobj, "InsertEvents", &numcalls, NULL);
 
1354
 
 
1355
        EXPECT_NE(nullptr, calls);
 
1356
        EXPECT_EQ(1, numcalls);
 
1357
 
 
1358
        dbus_test_dbus_mock_object_clear_method_calls(zgmock, zgobj, NULL);
 
1359
 
 
1360
        /* No Resume the App */
 
1361
        EXPECT_TRUE(ubuntu_app_launch_resume_application("com.test.good_application_1.2.3"));
 
1362
 
 
1363
        pause(200);
 
1364
 
 
1365
        EXPECT_NE(0, datacnt);
 
1366
 
 
1367
        /* Check to make sure we sent the event to ZG */
 
1368
        numcalls = 0;
 
1369
        calls = dbus_test_dbus_mock_object_get_method_calls(zgmock, zgobj, "InsertEvents", &numcalls, NULL);
 
1370
 
 
1371
        EXPECT_NE(nullptr, calls);
 
1372
        EXPECT_EQ(1, numcalls);
 
1373
 
 
1374
        /* Clean up */
 
1375
        gchar * killstr = g_strdup_printf("kill -9 %d", spewpid);
 
1376
        ASSERT_TRUE(g_spawn_command_line_sync(killstr, NULL, NULL, NULL, NULL));
 
1377
        g_free(killstr);
 
1378
 
 
1379
        g_io_channel_unref(spewoutchan);
 
1380
 
 
1381
        /* Kill ZG default instance :-( */
 
1382
        ZeitgeistLog * log = zeitgeist_log_get_default();
 
1383
        g_object_unref(log);
 
1384
        g_object_unref(log);
 
1385
}