Roll src/third_party/WebKit f007c95:0171005 (svn 185074:185088)
[chromium-blink-merge.git] / athena / content / app_activity_browsertest.cc
blob2c954efba31d20996b5edd285c10522590b193eb
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/base/test_util.h"
9 #include "athena/test/chrome/athena_app_browser_test.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"
16 namespace athena {
18 namespace {
19 // The test URL to navigate to.
20 const char kTestUrl[] = "chrome:about";
23 class AppActivityBrowserTest : public AthenaAppBrowserTest {
24 public:
25 AppActivityBrowserTest() {}
26 ~AppActivityBrowserTest() override {}
28 // AthenaAppBrowserTest:
29 void SetUpOnMainThread() override {
30 tracker_.reset(new ActivityLifetimeTracker);
31 AthenaAppBrowserTest::SetUpOnMainThread();
34 void TearDownOnMainThread() override {
35 tracker_.reset();
36 AthenaAppBrowserTest::TearDownOnMainThread();
39 protected:
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();
50 usleep(5000);
52 EXPECT_EQ(deleted_activity, proxy_activity);
53 EXPECT_TRUE(app_activity);
54 return 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 GetBrowserContext(), base::UTF8ToUTF16("App1"), GURL(kTestUrl));
76 ASSERT_TRUE(*web_activity);
77 EXPECT_EQ(*web_activity, tracker_->GetNewActivityAndReset());
78 EXPECT_EQ(nullptr, tracker_->GetDeletedActivityAndReset());
80 const aura::Window::Windows& windows =
81 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
83 // The order of windows should now be: Web activity, app activity.
84 EXPECT_EQ(app_activity->GetWindow(), windows[0]);
85 EXPECT_EQ((*web_activity)->GetWindow(), windows[1]);
87 // We let the ResourceManager unload now the app. To accomplish this, we
88 // first set the app to INIVSIBLE and then let the ResourceManager unload it
89 // by turning on the critical memory pressure.
90 app_activity->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
91 EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app_activity->GetCurrentState());
92 test_util::SendTestMemoryPressureEvent(
93 ResourceManager::MEMORY_PRESSURE_CRITICAL);
94 test_util::WaitUntilIdle();
96 *proxy_activity = tracker_->GetNewActivityAndReset();
97 ASSERT_TRUE(*proxy_activity);
98 EXPECT_NE(app_activity, *proxy_activity);
99 EXPECT_EQ(app_activity, tracker_->GetDeletedActivityAndReset());
101 // Check that the order of windows is correct (the proxy is at the second
102 // location).
103 EXPECT_EQ((*proxy_activity)->GetWindow(), windows[0]);
104 EXPECT_EQ((*web_activity)->GetWindow(), windows[1]);
107 private:
108 // The activity tracker which is used for asynchronous operations.
109 scoped_ptr<ActivityLifetimeTracker> tracker_;
110 DISALLOW_COPY_AND_ASSIGN(AppActivityBrowserTest);
113 // Tests that an application can be loaded.
114 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest, StartApplication) {
115 // There should be an application we can start.
116 ASSERT_TRUE(!GetTestAppID().empty());
118 // We should be able to start the application.
119 ASSERT_TRUE(CreateTestAppActivity(GetTestAppID()));
122 // Test that creating an application (without a particular activity order
123 // location) should activate it initially.
124 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest, CreatedAppGetsFocus) {
125 Activity* web_activity = test_util::CreateTestWebActivity(
126 GetBrowserContext(), base::UTF8ToUTF16("App1"), GURL(kTestUrl));
127 EXPECT_TRUE(IsActivityActive(web_activity));
129 Activity* app_activity = CreateTestAppActivity(GetTestAppID());
130 EXPECT_TRUE(IsActivityActive(app_activity));
133 // Test that setting an application state to UNLOADED a proxy gets created and
134 // upon changing it to invisible it gets reloaded it its current list location.
135 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest, UnloadReloadApplicationInPlace) {
136 // Set up the experiment.
137 Activity* proxy_activity = nullptr;
138 Activity* web_activity = nullptr;
139 SetUpWebAndProxyActivity(&web_activity, &proxy_activity);
140 // By returning to a low memory pressure the application should start again.
141 test_util::SendTestMemoryPressureEvent(ResourceManager::MEMORY_PRESSURE_LOW);
142 Activity* app_activity = WaitForProxyDestruction(proxy_activity);
143 proxy_activity = nullptr; // The proxy is gone now.
145 // After this, the application should remain at its current location in the
146 // stack and the current window should stay active.
147 const aura::Window::Windows& windows =
148 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
149 EXPECT_EQ(app_activity->GetWindow(), windows[0]);
150 EXPECT_EQ(web_activity->GetWindow(), windows[1]);
151 EXPECT_TRUE(IsActivityActive(web_activity));
154 // Check that activating an unloaded application will bring it properly to the
155 // front of the stack (and activate it).
156 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest, ReloadActivatedApplication) {
157 // Set up the experiment.
158 Activity* proxy_activity = nullptr;
159 Activity* web_activity = nullptr;
160 SetUpWebAndProxyActivity(&web_activity, &proxy_activity);
162 // Activating the proxy should push back the web app, lauch the application,
163 // kill the proxy and turn it active.
164 proxy_activity->GetWindow()->Show();
165 wm::ActivateWindow(proxy_activity->GetWindow());
166 const aura::Window::Windows& windows =
167 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
168 EXPECT_EQ(web_activity->GetWindow(), windows[0]);
170 Activity* app_activity = WaitForProxyDestruction(proxy_activity);
171 proxy_activity = nullptr; // The proxy is gone now.
173 // After this, the application should remain at its current location in the
174 // stack and the activation focus should remain on the current window as well.
175 EXPECT_EQ(app_activity->GetWindow(), windows[1]);
176 EXPECT_TRUE(IsActivityActive(app_activity));
177 EXPECT_EQ(web_activity->GetWindow(), windows[0]);
180 // Check that moving a proxy window to the front will properly restart the app
181 // and activate it.
182 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest, ReloadMovedApplication) {
183 // Set up the experiment.
184 Activity* proxy_activity = nullptr;
185 Activity* web_activity = nullptr;
186 SetUpWebAndProxyActivity(&web_activity, &proxy_activity);
187 // Moving the window to the front will restart the app.
188 WindowManager::Get()->GetWindowListProvider()->StackWindowFrontOf(
189 proxy_activity->GetWindow(),
190 web_activity->GetWindow());
191 const aura::Window::Windows& windows =
192 WindowManager::Get()->GetWindowListProvider()->GetWindowList();
193 EXPECT_EQ(web_activity->GetWindow(), windows[0]);
195 Activity* app_activity = WaitForProxyDestruction(proxy_activity);
196 proxy_activity = nullptr; // The proxy is gone now.
198 // After this, the application should remain at its current location in the
199 // stack and the activation focus should remain on the current window as well.
200 EXPECT_EQ(app_activity->GetWindow(), windows[1]);
201 EXPECT_TRUE(IsActivityActive(app_activity));
202 EXPECT_EQ(web_activity->GetWindow(), windows[0]);
205 } // namespace athena