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/activity/public/activity_manager.h"
7 #include "athena/activity/public/activity_view_model.h"
8 #include "athena/resource_manager/memory_pressure_notifier.h"
9 #include "athena/resource_manager/public/resource_manager.h"
10 #include "athena/test/athena_test_base.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/gfx/image/image_skia.h"
13 #include "ui/views/view.h"
14 #include "ui/views/widget/widget.h"
21 // A dummy test app activity which works without content / ShellAppWindow.
22 class TestActivity
: public Activity
,
23 public ActivityViewModel
{
25 TestActivity(std::string title
) : title_(base::UTF8ToUTF16(title
)),
26 view_(new views::View()),
27 current_state_(ACTIVITY_UNLOADED
),
28 media_state_(ACTIVITY_MEDIA_STATE_NONE
),
29 is_visible_(false) {};
30 virtual ~TestActivity() {}
32 void set_media_state(ActivityMediaState media_state
) {
33 media_state_
= media_state
;
35 void set_visible(bool visible
) { is_visible_
= visible
; }
37 // Activity overrides:
38 virtual ActivityViewModel
* GetActivityViewModel() OVERRIDE
{ return this; }
39 virtual void SetCurrentState(ActivityState state
) OVERRIDE
{
40 current_state_
= state
;
42 virtual ActivityState
GetCurrentState() OVERRIDE
{ return current_state_
; }
43 virtual bool IsVisible() OVERRIDE
{ return is_visible_
; }
44 virtual ActivityMediaState
GetMediaState() OVERRIDE
{ return media_state_
; }
45 virtual aura::Window
* GetWindow() OVERRIDE
{
46 return view_
->GetWidget()->GetNativeWindow();
49 // ActivityViewModel overrides:
50 virtual void Init() OVERRIDE
{}
51 virtual SkColor
GetRepresentativeColor() const OVERRIDE
{ return 0; }
52 virtual base::string16
GetTitle() const OVERRIDE
{ return title_
; }
53 virtual bool UsesFrame() const OVERRIDE
{ return true; }
54 virtual views::View
* GetContentsView() OVERRIDE
{ return view_
; }
55 virtual void CreateOverviewModeImage() OVERRIDE
{}
56 virtual gfx::ImageSkia
GetOverviewModeImage() OVERRIDE
{ return image_
; }
59 // The presentation values.
60 const base::string16 title_
;
61 gfx::ImageSkia image_
;
63 // The associated view.
67 ActivityState current_state_
;
69 // The current media state.
70 ActivityMediaState media_state_
;
72 // Returns if it is visible or not.
75 DISALLOW_COPY_AND_ASSIGN(TestActivity
);
81 class ResourceManagerTest
: public AthenaTestBase
{
83 ResourceManagerTest() {}
84 virtual ~ResourceManagerTest() {}
86 virtual void TearDown() OVERRIDE
{
87 while (!activity_list_
.empty())
88 CloseActivity(activity_list_
[0]);
89 AthenaTestBase::TearDown();
92 TestActivity
* CreateActivity(const std::string
& title
) {
93 TestActivity
* activity
= new TestActivity(title
);
94 ActivityManager::Get()->AddActivity(activity
);
95 activity
->SetCurrentState(Activity::ACTIVITY_INVISIBLE
);
96 activity_list_
.push_back(activity
);
100 void CloseActivity(Activity
* activity
) {
102 RunAllPendingInMessageLoop();
103 std::vector
<TestActivity
*>::iterator it
= std::find(activity_list_
.begin(),
104 activity_list_
.end(),
106 DCHECK(it
!= activity_list_
.end());
107 activity_list_
.erase(it
);
111 std::vector
<TestActivity
*> activity_list_
;
113 DISALLOW_COPY_AND_ASSIGN(ResourceManagerTest
);
116 // Only creates and destroys it to see that the system gets properly shut down.
117 TEST_F(ResourceManagerTest
, SimpleTest
) {
120 // Test that we release an activity when the memory pressure goes critical.
121 TEST_F(ResourceManagerTest
, OnCriticalWillUnloadOneActivity
) {
122 // create a few dummy activities.
123 TestActivity
* app_visible
= CreateActivity("visible");
124 TestActivity
* app_unloadable1
= CreateActivity("unloadable1");
125 TestActivity
* app_unloadable2
= CreateActivity("unloadable2");
126 app_visible
->set_visible(true);
127 app_unloadable1
->set_visible(false);
128 app_unloadable2
->set_visible(false);
130 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_visible
->GetCurrentState());
131 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_unloadable1
->GetCurrentState());
132 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_unloadable2
->GetCurrentState());
134 // Call the resource manager and say we are in a critical memory condition.
135 ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
136 MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL
);
137 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_visible
->GetCurrentState());
138 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_unloadable1
->GetCurrentState());
139 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_unloadable2
->GetCurrentState());
141 // Calling it a second time will release the second app.
142 ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
143 MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL
);
144 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_visible
->GetCurrentState());
145 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_unloadable1
->GetCurrentState());
146 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_unloadable2
->GetCurrentState());
148 // Calling it once more will change nothing.
149 ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
150 MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL
);
151 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_visible
->GetCurrentState());
152 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_unloadable1
->GetCurrentState());
153 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_unloadable2
->GetCurrentState());
156 // Test that media playing activities only get unloaded if there is no other
158 TEST_F(ResourceManagerTest
, OnCriticalMediaHandling
) {
159 // create a few dummy activities.
160 TestActivity
* app_visible
= CreateActivity("visible");
161 TestActivity
* app_media_locked1
= CreateActivity("medialocked1");
162 TestActivity
* app_unloadable
= CreateActivity("unloadable2");
163 TestActivity
* app_media_locked2
= CreateActivity("medialocked2");
164 app_visible
->set_visible(true);
165 app_unloadable
->set_visible(false);
166 app_media_locked1
->set_visible(false);
167 app_media_locked2
->set_visible(false);
169 app_media_locked1
->set_media_state(
170 Activity::ACTIVITY_MEDIA_STATE_AUDIO_PLAYING
);
171 app_media_locked2
->set_media_state(Activity::ACTIVITY_MEDIA_STATE_RECORDING
);
173 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_visible
->GetCurrentState());
174 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_media_locked1
->GetCurrentState());
175 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_unloadable
->GetCurrentState());
176 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_media_locked2
->GetCurrentState());
178 // Calling it with a critical situation first, it will release the non media
180 ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
181 MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL
);
182 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_visible
->GetCurrentState());
183 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_media_locked1
->GetCurrentState());
184 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_unloadable
->GetCurrentState());
185 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_media_locked2
->GetCurrentState());
187 // Calling it the second time, the oldest media playing activity will get
189 ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
190 MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL
);
191 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_visible
->GetCurrentState());
192 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_media_locked1
->GetCurrentState());
193 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_unloadable
->GetCurrentState());
194 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_media_locked2
->GetCurrentState());
196 // Calling it the third time, the oldest media playing activity will get
198 ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
199 MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL
);
200 DCHECK_NE(Activity::ACTIVITY_UNLOADED
, app_visible
->GetCurrentState());
201 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_media_locked1
->GetCurrentState());
202 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_unloadable
->GetCurrentState());
203 DCHECK_EQ(Activity::ACTIVITY_UNLOADED
, app_media_locked2
->GetCurrentState());
207 } // namespace athena