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 |
| 28 // AthenaAppBrowserTest: |
| 29 virtual void SetUpOnMainThread() override { |
| 30 tracker_.reset(new ActivityLifetimeTracker); |
| 31 AthenaAppBrowserTest::SetUpOnMainThread(); |
| 32 } |
| 33 |
| 34 virtual void TearDownOnMainThread() override { |
| 35 tracker_.reset(); |
| 36 AthenaAppBrowserTest::TearDownOnMainThread(); |
| 37 } |
| 38 |
| 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 = NULL; |
| 45 Activity* app_activity = NULL; |
| 46 while (!app_activity && !deleted_activity) { |
| 47 deleted_activity = tracker_->GetDeletedActivityAndReset(); |
| 48 app_activity = tracker_->GetNewActivityAndReset(); |
| 49 test_util::WaitUntilIdle(); |
| 50 usleep(5000); |
| 51 } |
| 52 EXPECT_EQ(deleted_activity, proxy_activity); |
| 53 EXPECT_TRUE(app_activity); |
| 54 return app_activity; |
| 55 } |
| 56 |
| 57 // Returns true when the window of the |activity| has the focus. |
| 58 bool IsActivityActive(Activity* activity) { |
| 59 return wm::IsActiveWindow(activity->GetWindow()); |
| 60 } |
| 61 |
| 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(NULL, tracker_->GetDeletedActivityAndReset()); |
| 72 |
| 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"), |
| 77 GURL(kTestUrl)); |
| 78 ASSERT_TRUE(*web_activity); |
| 79 EXPECT_EQ(*web_activity, tracker_->GetNewActivityAndReset()); |
| 80 EXPECT_EQ(NULL, tracker_->GetDeletedActivityAndReset()); |
| 81 |
| 82 const aura::Window::Windows& windows = |
| 83 WindowManager::Get()->GetWindowListProvider()->GetWindowList(); |
| 84 |
| 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]); |
| 88 |
| 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(); |
| 97 |
| 98 *proxy_activity = tracker_->GetNewActivityAndReset(); |
| 99 ASSERT_TRUE(*proxy_activity); |
| 100 EXPECT_NE(app_activity, *proxy_activity); |
| 101 EXPECT_EQ(app_activity, tracker_->GetDeletedActivityAndReset()); |
| 102 |
| 103 // Check that the order of windows is correct (the proxy is at the second |
| 104 // location). |
| 105 EXPECT_EQ((*proxy_activity)->GetWindow(), windows[0]); |
| 106 EXPECT_EQ((*web_activity)->GetWindow(), windows[1]); |
| 107 } |
| 108 |
| 109 private: |
| 110 // The activity tracker which is used for asynchronous operations. |
| 111 scoped_ptr<ActivityLifetimeTracker> tracker_; |
| 112 DISALLOW_COPY_AND_ASSIGN(AppActivityBrowserTest); |
| 113 }; |
10 | 114 |
11 // Tests that an application can be loaded. | 115 // Tests that an application can be loaded. |
12 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest, StartApplication) { | 116 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest, StartApplication) { |
13 // There should be an application we can start. | 117 // There should be an application we can start. |
14 ASSERT_TRUE(!GetTestAppID().empty()); | 118 ASSERT_TRUE(!GetTestAppID().empty()); |
15 | 119 |
16 // We should be able to start the application. | 120 // We should be able to start the application. |
17 ASSERT_TRUE(CreateTestAppActivity(GetTestAppID())); | 121 ASSERT_TRUE(CreateTestAppActivity(GetTestAppID())); |
18 } | 122 } |
19 | 123 |
| 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"), |
| 130 GURL(kTestUrl)); |
| 131 EXPECT_TRUE(IsActivityActive(web_activity)); |
| 132 |
| 133 Activity* app_activity = CreateTestAppActivity(GetTestAppID()); |
| 134 EXPECT_TRUE(IsActivityActive(app_activity)); |
| 135 } |
| 136 |
| 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 = NULL; |
| 142 Activity* web_activity = NULL; |
| 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 = NULL; // The proxy is gone now. |
| 148 |
| 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)); |
| 156 } |
| 157 |
| 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 = NULL; |
| 163 Activity* web_activity = NULL; |
| 164 SetUpWebAndProxyActivity(&web_activity, &proxy_activity); |
| 165 |
| 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]); |
| 173 |
| 174 Activity* app_activity = WaitForProxyDestruction(proxy_activity); |
| 175 proxy_activity = NULL; // The proxy is gone now. |
| 176 |
| 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]); |
| 182 } |
| 183 |
| 184 // Check that moving a proxy window to the front will properly restart the app |
| 185 // and activate it. |
| 186 IN_PROC_BROWSER_TEST_F(AppActivityBrowserTest, ReloadMovedApplication) { |
| 187 // Set up the experiment. |
| 188 Activity* proxy_activity = NULL; |
| 189 Activity* web_activity = NULL; |
| 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]); |
| 198 |
| 199 Activity* app_activity = WaitForProxyDestruction(proxy_activity); |
| 200 proxy_activity = NULL; // The proxy is gone now. |
| 201 |
| 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]); |
| 207 } |
| 208 |
20 } // namespace athena | 209 } // namespace athena |
OLD | NEW |