1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "athena/activity/public/activity.h"
6 #include "athena/resource_manager/public/resource_manager.h"
7 #include "athena/test/base/activity_lifetime_tracker.h"
8 #include "athena/test/chrome/athena_app_browsertest.h"
9 #include "athena/test/chrome/test_util.h"
10 #include "athena/wm/public/window_list_provider.h"
11 #include "athena/wm/public/window_manager.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "ui/aura/client/focus_client.h"
14 #include "ui/wm/core/window_util.h"
19 // The test URL to navigate to.
20 const char kTestUrl
[] = "chrome:about";
23 class AppActivityBrowserTest
: public AthenaAppBrowserTest
{
25 AppActivityBrowserTest() {}
26 ~AppActivityBrowserTest() override
{}
28 // AthenaAppBrowserTest:
29 void SetUpOnMainThread() override
{
30 tracker_
.reset(new ActivityLifetimeTracker
);
31 AthenaAppBrowserTest::SetUpOnMainThread();
34 void TearDownOnMainThread() override
{
36 AthenaAppBrowserTest::TearDownOnMainThread();
40 // A |proxy_activity| got deleted and this function waits, using the |tracker|
41 // until the application got restarted returning the new application activity.
42 Activity
* WaitForProxyDestruction(Activity
* proxy_activity
) {
43 ActivityLifetimeTracker tracker
;
44 void* deleted_activity
= nullptr;
45 Activity
* app_activity
= nullptr;
46 while (!app_activity
&& !deleted_activity
) {
47 deleted_activity
= tracker_
->GetDeletedActivityAndReset();
48 app_activity
= tracker_
->GetNewActivityAndReset();
49 test_util::WaitUntilIdle();
52 EXPECT_EQ(deleted_activity
, proxy_activity
);
53 EXPECT_TRUE(app_activity
);
57 // Returns true when the window of the |activity| has the focus.
58 bool IsActivityActive(Activity
* activity
) {
59 return wm::IsActiveWindow(activity
->GetWindow());
62 // Create a setup where the frontmost window is a web activity and then
63 // an unloaded app activity (proxy). Note that the resource manager will be
64 // set to CRITICAL to force the application to unload.
65 void SetUpWebAndProxyActivity(Activity
** web_activity
,
66 Activity
** proxy_activity
) {
67 // Create an application activity.
68 Activity
* app_activity
= CreateTestAppActivity(GetTestAppID());
69 ASSERT_TRUE(app_activity
);
70 EXPECT_EQ(app_activity
, tracker_
->GetNewActivityAndReset());
71 EXPECT_EQ(nullptr, tracker_
->GetDeletedActivityAndReset());
73 // Then a web activity (which will then be in front of the app).
74 *web_activity
= test_util::CreateTestWebActivity(
75 test_util::GetBrowserContext(),
76 base::UTF8ToUTF16("App1"),
78 ASSERT_TRUE(*web_activity
);
79 EXPECT_EQ(*web_activity
, tracker_
->GetNewActivityAndReset());
80 EXPECT_EQ(nullptr, tracker_
->GetDeletedActivityAndReset());
82 const aura::Window::Windows
& windows
=
83 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
85 // The order of windows should now be: Web activity, app activity.
86 EXPECT_EQ(app_activity
->GetWindow(), windows
[0]);
87 EXPECT_EQ((*web_activity
)->GetWindow(), windows
[1]);
89 // We let the ResourceManager unload now the app. To accomplish this, we
90 // first set the app to INIVSIBLE and then let the ResourceManager unload it
91 // by turning on the critical memory pressure.
92 app_activity
->SetCurrentState(Activity::ACTIVITY_INVISIBLE
);
93 EXPECT_EQ(Activity::ACTIVITY_INVISIBLE
, app_activity
->GetCurrentState());
94 test_util::SendTestMemoryPressureEvent(
95 ResourceManager::MEMORY_PRESSURE_CRITICAL
);
96 test_util::WaitUntilIdle();
98 *proxy_activity
= tracker_
->GetNewActivityAndReset();
99 ASSERT_TRUE(*proxy_activity
);
100 EXPECT_NE(app_activity
, *proxy_activity
);
101 EXPECT_EQ(app_activity
, tracker_
->GetDeletedActivityAndReset());
103 // Check that the order of windows is correct (the proxy is at the second
105 EXPECT_EQ((*proxy_activity
)->GetWindow(), windows
[0]);
106 EXPECT_EQ((*web_activity
)->GetWindow(), windows
[1]);
110 // The activity tracker which is used for asynchronous operations.
111 scoped_ptr
<ActivityLifetimeTracker
> tracker_
;
112 DISALLOW_COPY_AND_ASSIGN(AppActivityBrowserTest
);
115 // Tests that an application can be loaded.
116 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest
, StartApplication
) {
117 // There should be an application we can start.
118 ASSERT_TRUE(!GetTestAppID().empty());
120 // We should be able to start the application.
121 ASSERT_TRUE(CreateTestAppActivity(GetTestAppID()));
124 // Test that creating an application (without a particular activity order
125 // location) should activate it initially.
126 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest
, CreatedAppGetsFocus
) {
127 Activity
* web_activity
=
128 test_util::CreateTestWebActivity(test_util::GetBrowserContext(),
129 base::UTF8ToUTF16("App1"),
131 EXPECT_TRUE(IsActivityActive(web_activity
));
133 Activity
* app_activity
= CreateTestAppActivity(GetTestAppID());
134 EXPECT_TRUE(IsActivityActive(app_activity
));
137 // Test that setting an application state to UNLOADED a proxy gets created and
138 // upon changing it to invisible it gets reloaded it its current list location.
139 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest
, UnloadReloadApplicationInPlace
) {
140 // Set up the experiment.
141 Activity
* proxy_activity
= nullptr;
142 Activity
* web_activity
= nullptr;
143 SetUpWebAndProxyActivity(&web_activity
, &proxy_activity
);
144 // By returning to a low memory pressure the application should start again.
145 test_util::SendTestMemoryPressureEvent(ResourceManager::MEMORY_PRESSURE_LOW
);
146 Activity
* app_activity
= WaitForProxyDestruction(proxy_activity
);
147 proxy_activity
= nullptr; // The proxy is gone now.
149 // After this, the application should remain at its current location in the
150 // stack and the current window should stay active.
151 const aura::Window::Windows
& windows
=
152 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
153 EXPECT_EQ(app_activity
->GetWindow(), windows
[0]);
154 EXPECT_EQ(web_activity
->GetWindow(), windows
[1]);
155 EXPECT_TRUE(IsActivityActive(web_activity
));
158 // Check that activating an unloaded application will bring it properly to the
159 // front of the stack (and activate it).
160 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest
, ReloadActivatedApplication
) {
161 // Set up the experiment.
162 Activity
* proxy_activity
= nullptr;
163 Activity
* web_activity
= nullptr;
164 SetUpWebAndProxyActivity(&web_activity
, &proxy_activity
);
166 // Activating the proxy should push back the web app, lauch the application,
167 // kill the proxy and turn it active.
168 proxy_activity
->GetWindow()->Show();
169 wm::ActivateWindow(proxy_activity
->GetWindow());
170 const aura::Window::Windows
& windows
=
171 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
172 EXPECT_EQ(web_activity
->GetWindow(), windows
[0]);
174 Activity
* app_activity
= WaitForProxyDestruction(proxy_activity
);
175 proxy_activity
= nullptr; // The proxy is gone now.
177 // After this, the application should remain at its current location in the
178 // stack and the activation focus should remain on the current window as well.
179 EXPECT_EQ(app_activity
->GetWindow(), windows
[1]);
180 EXPECT_TRUE(IsActivityActive(app_activity
));
181 EXPECT_EQ(web_activity
->GetWindow(), windows
[0]);
184 // Check that moving a proxy window to the front will properly restart the app
186 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest
, ReloadMovedApplication
) {
187 // Set up the experiment.
188 Activity
* proxy_activity
= nullptr;
189 Activity
* web_activity
= nullptr;
190 SetUpWebAndProxyActivity(&web_activity
, &proxy_activity
);
191 // Moving the window to the front will restart the app.
192 WindowManager::Get()->GetWindowListProvider()->StackWindowFrontOf(
193 proxy_activity
->GetWindow(),
194 web_activity
->GetWindow());
195 const aura::Window::Windows
& windows
=
196 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
197 EXPECT_EQ(web_activity
->GetWindow(), windows
[0]);
199 Activity
* app_activity
= WaitForProxyDestruction(proxy_activity
);
200 proxy_activity
= nullptr; // The proxy is gone now.
202 // After this, the application should remain at its current location in the
203 // stack and the activation focus should remain on the current window as well.
204 EXPECT_EQ(app_activity
->GetWindow(), windows
[1]);
205 EXPECT_TRUE(IsActivityActive(app_activity
));
206 EXPECT_EQ(web_activity
->GetWindow(), windows
[0]);
209 } // namespace athena