354
358
EXPECT_EQ(first_session, the_app->session()->session());
357
TEST_F(ApplicationManagerTests, focused_app_can_rerequest_focus)
359
using namespace ::testing;
360
const pid_t a_procId = 5921;
361
const char an_app_id[] = "some_app";
362
QByteArray a_cmd("/usr/bin/app1 --desktop_file_hint=some_app");
363
FakeMirSurface *aSurface = new FakeMirSurface;
365
ON_CALL(procInfo, command_line(_)).WillByDefault(Return(a_cmd));
366
ON_CALL(*taskController, appIdHasProcessId(_,_)).WillByDefault(Return(false));
370
std::shared_ptr<mir::scene::Session> a_session = std::make_shared<MockSession>("Oo", a_procId);
372
applicationManager.authorizeSession(a_procId, authed);
373
onSessionStarting(a_session);
374
onSessionCreatedSurface(a_session.get(), aSurface);
375
aSurface->drawFirstFrame();
377
Application * the_app = applicationManager.findApplication(an_app_id);
378
applicationManager.focusApplication(an_app_id);
380
EXPECT_EQ(Application::Running, the_app->state());
381
EXPECT_EQ(true, the_app->focused());
383
applicationManager.focusApplication(an_app_id);
384
EXPECT_EQ(true, the_app->focused());
387
361
TEST_F(ApplicationManagerTests,starting_app_is_suspended_when_it_gets_ready_if_requested)
389
363
using namespace ::testing;
913
876
.WillOnce(Return(true));
915
applicationManager.startApplication(appId);
916
applicationManager.onProcessStarting(appId);
917
std::shared_ptr<mir::scene::Session> session = std::make_shared<MockSession>("", procId);
919
applicationManager.authorizeSession(procId, authed);
920
onSessionStarting(session);
922
FakeMirSurface *surface = new FakeMirSurface;
923
onSessionCreatedSurface(session.get(), surface);
924
surface->drawFirstFrame();
926
QSignalSpy countSpy(&applicationManager, SIGNAL(countChanged()));
927
QSignalSpy removedSpy(&applicationManager, SIGNAL(applicationRemoved(const QString &)));
930
applicationManager.stopApplication(appId);
932
EXPECT_EQ(countSpy.count(), 2); //FIXME(greyback)
933
EXPECT_EQ(applicationManager.count(), 0);
934
EXPECT_EQ(removedSpy.count(), 1);
935
EXPECT_EQ(removedSpy.takeFirst().at(0).toString(), appId);
939
* Test that a suspended application is stopped correctly
941
TEST_F(ApplicationManagerTests,shellStopsSuspendedAppCorrectly)
943
using namespace ::testing;
944
const QString appId("testAppId");
945
const pid_t procId = 5551;
947
// Set up Mocks & signal watcher
948
ON_CALL(*taskController, primaryPidForAppId(appId)).WillByDefault(Return(procId));
949
ON_CALL(desktopFileReaderFactory, createInstance(appId, _)).WillByDefault(Invoke(createMockDesktopFileReader));
951
EXPECT_CALL(*taskController, start(appId, _))
953
.WillOnce(Return(true));
955
Application *application = applicationManager.startApplication(appId);
956
applicationManager.onProcessStarting(appId);
957
std::shared_ptr<mir::scene::Session> session = std::make_shared<MockSession>("", procId);
959
applicationManager.authorizeSession(procId, authed);
960
onSessionStarting(session);
961
applicationManager.onProcessStarting(appId);
963
FakeMirSurface *surface = new FakeMirSurface;
964
onSessionCreatedSurface(session.get(), surface);
965
surface->drawFirstFrame();
967
suspend(application);
969
QSignalSpy countSpy(&applicationManager, SIGNAL(countChanged()));
970
QSignalSpy removedSpy(&applicationManager, SIGNAL(applicationRemoved(const QString &)));
973
applicationManager.stopApplication(appId);
975
EXPECT_EQ(countSpy.count(), 2); //FIXME(greyback)
976
EXPECT_EQ(applicationManager.count(), 0);
977
EXPECT_EQ(removedSpy.count(), 1);
978
EXPECT_EQ(removedSpy.takeFirst().at(0).toString(), appId);
878
Application *app = applicationManager.startApplication(appId);
879
applicationManager.onProcessStarting(appId);
880
std::shared_ptr<mir::scene::Session> session = std::make_shared<NiceMock<MockSession>>("", procId);
882
applicationManager.authorizeSession(procId, authed);
883
onSessionStarting(session);
885
FakeMirSurface *surface = new FakeMirSurface;
886
onSessionCreatedSurface(session.get(), surface);
887
surface->drawFirstFrame();
889
QSignalSpy countSpy(&applicationManager, SIGNAL(countChanged()));
891
QSignalSpy closeRequestedSpy(surface, SIGNAL(closeRequested()));
894
applicationManager.stopApplication(appId);
896
// Asking ApplicationManager to stop the application just makes it request its surfaces to be
898
EXPECT_EQ(1, closeRequestedSpy.count());
904
// now it's the turn of the application process itself to go away, since its last surface has gone
905
EXPECT_EQ(Application::InternalState::Closing, app->internalState());
907
// Simulates that the application complied to the close() request and stopped itself
908
onSessionStopping(session);
909
applicationManager.onProcessStopped(appId);
911
EXPECT_EQ(countSpy.count(), 2); //FIXME(greyback)
912
EXPECT_EQ(applicationManager.count(), 0);
1946
* Test that there is an attempt at polite exiting of the app by requesting closure of the surface.
1948
TEST_F(ApplicationManagerTests,suspendedApplicationResumesClosesAndDeletes)
1950
using namespace ::testing;
1952
const QString appId("testAppId");
1953
quint64 procId = 5551;
1954
Application* app = startApplication(procId, appId);
1955
std::shared_ptr<mir::scene::Session> session = app->session()->session();
1957
FakeMirSurface *surface = new FakeMirSurface;
1958
onSessionCreatedSurface(session.get(), surface);
1959
surface->drawFirstFrame();
1960
EXPECT_EQ(Application::InternalState::Running, app->internalState());
1961
EXPECT_EQ(SessionInterface::Running, app->session()->state());
1963
// Suspend the application.
1965
EXPECT_EQ(Application::InternalState::Suspended, app->internalState());
1968
applicationManager.stopApplication(appId);
1969
EXPECT_EQ(Application::InternalState::Closing, app->internalState());
1970
EXPECT_EQ(SessionInterface::Running, app->session()->state());
1974
* Test that a application which fails to close will eventually be forceable closed.
1976
TEST_F(ApplicationManagerTests,failedApplicationCloseEventualyDeletesApplication)
1878
Change focus between surfaces of different applications and check that
1879
ApplicationManager::focusedApplicationId changes accordingly
1881
TEST_F(ApplicationManagerTests,focusedApplicationId)
1978
1883
using namespace ::testing;
1982
QCoreApplication qtApp(argc, argv); // app for deleteLater event
1887
QCoreApplication qtApp(argc, argv);
1889
const QString appId1("testAppId1");
1890
quint64 procId1 = 5551;
1891
const QString appId2("testAppId2");
1892
quint64 procId2 = 5552;
1894
ON_CALL(*taskController, primaryPidForAppId(appId1)).WillByDefault(Return(procId1));
1895
ON_CALL(desktopFileReaderFactory, createInstance(appId1, _)).WillByDefault(Invoke(createMockDesktopFileReader));
1896
ON_CALL(*taskController, primaryPidForAppId(appId2)).WillByDefault(Return(procId2));
1897
ON_CALL(desktopFileReaderFactory, createInstance(appId2, _)).WillByDefault(Invoke(createMockDesktopFileReader));
1899
EXPECT_CALL(*taskController, start(appId1, _))
1901
.WillOnce(Return(true));
1903
auto app1 = applicationManager.startApplication(appId1);
1904
applicationManager.onProcessStarting(appId1);
1905
std::shared_ptr<mir::scene::Session> session1 = std::make_shared<MockSession>("", procId1);
1907
applicationManager.authorizeSession(procId1, authed);
1908
onSessionStarting(session1);
1910
FakeMirSurface *surface1 = new FakeMirSurface;
1911
surface1->setSession(app1->session());
1912
onSessionCreatedSurface(session1.get(), surface1);
1913
surface1->drawFirstFrame();
1915
EXPECT_EQ(Application::InternalState::Running, app1->internalState());
1917
QSignalSpy focusedApplicationIdChangedSpy(&applicationManager,
1918
&unityapi::ApplicationManagerInterface::focusedApplicationIdChanged);
1920
MirFocusController::instance()->setFocusedSurface(surface1);
1921
qtApp.processEvents(); // process queued signal-slot connections
1923
EXPECT_EQ(1, focusedApplicationIdChangedSpy.count());
1924
EXPECT_EQ(appId1, applicationManager.focusedApplicationId());
1926
EXPECT_CALL(*taskController, start(appId2, _))
1928
.WillOnce(Return(true));
1930
auto app2 = applicationManager.startApplication(appId2);
1931
applicationManager.onProcessStarting(appId2);
1932
std::shared_ptr<mir::scene::Session> session2 = std::make_shared<MockSession>("", procId2);
1934
applicationManager.authorizeSession(procId2, authed);
1935
onSessionStarting(session2);
1937
FakeMirSurface *surface2 = new FakeMirSurface;
1938
surface2->setSession(app2->session());
1939
onSessionCreatedSurface(session2.get(), surface2);
1940
surface2->drawFirstFrame();
1942
EXPECT_EQ(Application::InternalState::Running, app2->internalState());
1944
MirFocusController::instance()->setFocusedSurface(surface2);
1945
qtApp.processEvents(); // process queued signal-slot connections
1947
EXPECT_EQ(2, focusedApplicationIdChangedSpy.count());
1948
EXPECT_EQ(appId2, applicationManager.focusedApplicationId());
1950
MirFocusController::instance()->setFocusedSurface(surface1);
1951
qtApp.processEvents(); // process queued signal-slot connections
1953
EXPECT_EQ(3, focusedApplicationIdChangedSpy.count());
1954
EXPECT_EQ(appId1, applicationManager.focusedApplicationId());
1962
This is the compatibility mode between application-centric window management
1963
and the new (proper) surface-based window management
1965
Whenever a surface request focus, its corresponding application should do likewise,
1966
causing ApplicationManager::focusRequested(appId) to be emitted as well.
1968
TEST_F(ApplicationManagerTests,surfaceFocusRequestGeneratesApplicationFocusRequest)
1970
using namespace ::testing;
1984
1972
const QString appId("testAppId");
1985
1973
quint64 procId = 5551;
1987
ON_CALL(procInfo,command_line(procId)).WillByDefault(Return(QByteArray("/usr/bin/testAppId")));
1988
ON_CALL(*taskController,appIdHasProcessId(appId, procId)).WillByDefault(Return(true));
1975
ON_CALL(*taskController, primaryPidForAppId(appId)).WillByDefault(Return(procId));
1976
ON_CALL(desktopFileReaderFactory, createInstance(appId, _)).WillByDefault(Invoke(createMockDesktopFileReader));
1990
ON_CALL(desktopFileReaderFactory, createInstance(appId, _))
1991
.WillByDefault(Invoke(
1992
[](const QString &appId, const QFileInfo&) { return new FakeDesktopFileReader(appId); }
1978
EXPECT_CALL(*taskController, start(appId, _))
1980
.WillOnce(Return(true));
1995
1982
auto app = applicationManager.startApplication(appId);
1996
1983
applicationManager.onProcessStarting(appId);
2006
1993
EXPECT_EQ(Application::InternalState::Running, app->internalState());
2008
QSharedPointer<FakeTimeSource> fakeTimeSource(new FakeTimeSource);
2009
FakeTimer *fakeCloseTimer = new FakeTimer(fakeTimeSource);
2010
app->setCloseTimer(fakeCloseTimer);
2012
EXPECT_CALL(*taskController, stop(appId))
2015
[this, session](const QString &appIdParam) {
2016
// No point in emitting it as applicationManager is not connected to the taskController
2017
// FIXME: Connect applicationManager to taskController so that we have a better test environment!
2018
// In the meantime call the ApplicationManager method directly, emulating the missing
2019
// signal-slot connection
2020
// Q_EMIT taskController->processStopped(appIdParam);
2021
onSessionStopping(session);
2022
applicationManager.onProcessStopped(appIdParam);
2027
QSignalSpy appDestroyedSpy(app, SIGNAL(destroyed(QObject*)));
2030
applicationManager.stopApplication(appId);
2032
if (fakeCloseTimer->isRunning()) {
2033
// Simulate that closeTimer has timed out.
2034
fakeTimeSource->m_msecsSinceReference = fakeCloseTimer->nextTimeoutTime() + 1;
2035
fakeCloseTimer->update();
2038
// DeferredDelete is special: likes to be called out specifically or it won't come out
2039
qtApp.sendPostedEvents(app, QEvent::DeferredDelete);
2040
qtApp.sendPostedEvents();
2042
EXPECT_EQ(1, appDestroyedSpy.count());
2046
* Test that an application that is suspended after its session is stopped is closed
2048
TEST_F(ApplicationManagerTests,CloseWhenSuspendedAfterSessionStopped)
2050
using namespace ::testing;
2052
const QString appId("testAppId");
2053
quint64 procId = 5551;
2055
auto application = startApplication(procId, "testAppId");
2057
qtmir::Session* session(static_cast<qtmir::Session*>(application->session()));
2059
FakeMirSurface *surface = new FakeMirSurface;
2060
onSessionCreatedSurface(session->session().get(), surface);
2061
surface->drawFirstFrame();
2062
EXPECT_EQ(Application::InternalState::Running, application->internalState());
2064
// session is suspended
2065
application->setRequestedState(Application::RequestedSuspended);
2066
EXPECT_EQ(Application::InternalState::SuspendingWaitSession, application->internalState());
2067
session->doSuspend();
2068
EXPECT_EQ(Application::InternalState::SuspendingWaitProcess, application->internalState());
2069
session->setLive(false);
2070
EXPECT_EQ(Application::InternalState::Closing, application->internalState());
2072
// The process can be suspended after the session has dissapeared.
2073
applicationManager.onProcessSuspended(application->appId());
2074
EXPECT_EQ(Application::InternalState::Closing, application->internalState());
2076
QSignalSpy spy(application, SIGNAL(stopped()));
2077
applicationManager.onProcessStopped(application->appId());
2078
EXPECT_EQ(Application::Stopped, application->state());
2079
EXPECT_EQ(spy.count(), 1);
2083
* Test that an application that fails while suspended will stop on close request
2085
TEST_F(ApplicationManagerTests,CloseWhenSuspendedProcessFailed)
2087
using namespace ::testing;
2089
const QString appId("testAppId");
2090
quint64 procId = 5551;
2092
auto application = startApplication(procId, "testAppId");
2094
qtmir::Session* session(static_cast<qtmir::Session*>(application->session()));
2096
FakeMirSurface *surface = new FakeMirSurface;
2097
onSessionCreatedSurface(session->session().get(), surface);
2098
surface->drawFirstFrame();
2099
EXPECT_EQ(Application::InternalState::Running, application->internalState());
2101
// Session is suspended
2102
suspend(application);
2105
onSessionStopping(session->session());
2106
applicationManager.onProcessFailed(appId, TaskController::Error::APPLICATION_FAILED_TO_START);
2107
applicationManager.onProcessStopped(appId);
2108
EXPECT_EQ(Application::InternalState::StoppedResumable, application->internalState());
2110
QSignalSpy spy(application, SIGNAL(stopped()));
2111
application->close();
2113
EXPECT_EQ(Application::Stopped, application->state());
2114
EXPECT_EQ(spy.count(), 1);
1995
QSignalSpy focusRequestedSpy(&applicationManager,
1996
&unityapi::ApplicationManagerInterface::focusRequested);
1998
surface->requestFocus();
2000
EXPECT_EQ(1, focusRequestedSpy.count());