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