1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "athena/activity/public/activity.h"
#include "athena/activity/public/activity_manager.h"
#include "athena/activity/public/activity_view_model.h"
#include "athena/resource_manager/memory_pressure_notifier.h"
#include "athena/resource_manager/public/resource_manager.h"
#include "athena/test/base/athena_test_base.h"
#include "athena/test/base/sample_activity.h"
#include "athena/wm/public/window_manager.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace athena {
namespace test {
namespace {
// A dummy test app activity which works without content / ShellAppWindow.
class TestActivity : public SampleActivity {
public:
explicit TestActivity(const std::string& title)
: SampleActivity(0, 0, base::UTF8ToUTF16(title)),
media_state_(ACTIVITY_MEDIA_STATE_NONE),
is_visible_(false) {}
~TestActivity() override {}
void set_media_state(ActivityMediaState media_state) {
media_state_ = media_state;
}
void set_visible(bool visible) { is_visible_ = visible; }
// Activity overrides:
virtual bool IsVisible() override { return is_visible_; }
virtual ActivityMediaState GetMediaState() override { return media_state_; }
private:
// The current media state.
ActivityMediaState media_state_;
// Returns if it is visible or not.
bool is_visible_;
DISALLOW_COPY_AND_ASSIGN(TestActivity);
};
} // namespace
// Our testing base.
class ResourceManagerTest : public AthenaTestBase {
public:
ResourceManagerTest() {}
~ResourceManagerTest() override {}
virtual void SetUp() override {
AthenaTestBase::SetUp();
// Override the delay to be instantaneous.
ResourceManager::Get()->SetWaitTimeBetweenResourceManageCalls(0);
}
virtual void TearDown() override {
while (!activity_list_.empty())
DeleteActivity(activity_list_[0]);
AthenaTestBase::TearDown();
}
TestActivity* CreateActivity(const std::string& title) {
TestActivity* activity = new TestActivity(title);
ActivityManager::Get()->AddActivity(activity);
activity->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
activity_list_.push_back(activity);
return activity;
}
void DeleteActivity(Activity* activity) {
Activity::Delete(activity);
RunAllPendingInMessageLoop();
std::vector<TestActivity*>::iterator it = std::find(activity_list_.begin(),
activity_list_.end(),
activity);
DCHECK(it != activity_list_.end());
activity_list_.erase(it);
}
private:
std::vector<TestActivity*> activity_list_;
DISALLOW_COPY_AND_ASSIGN(ResourceManagerTest);
};
// Only creates and destroys it to see that the system gets properly shut down.
TEST_F(ResourceManagerTest, SimpleTest) {
}
// Test that we release an activity when the memory pressure goes critical.
TEST_F(ResourceManagerTest, OnCriticalWillUnloadOneActivity) {
// Create a few dummy activities in the reverse order as we need them.
TestActivity* app_unloadable2 = CreateActivity("unloadable2");
TestActivity* app_unloadable1 = CreateActivity("unloadable1");
TestActivity* app_visible = CreateActivity("visible");
app_visible->set_visible(true);
app_unloadable1->set_visible(false);
app_unloadable2->set_visible(false);
// Set the initial visibility states.
app_visible->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app_unloadable1->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
app_unloadable2->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
// Call the resource manager and say we are in a critical memory condition.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_visible->GetCurrentState());
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_unloadable1->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_unloadable2->GetCurrentState());
// Calling it a second time will release the second app.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_visible->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_unloadable1->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_unloadable2->GetCurrentState());
// Calling it once more will change nothing.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_visible->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_unloadable1->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_unloadable2->GetCurrentState());
}
// Test that media playing activities only get unloaded if there is no other
// way.
TEST_F(ResourceManagerTest, OnCriticalMediaHandling) {
// Create a few dummy activities in the reverse order as we need them.
TestActivity* app_media_locked2 = CreateActivity("medialocked2");
TestActivity* app_unloadable = CreateActivity("unloadable2");
TestActivity* app_media_locked1 = CreateActivity("medialocked1");
TestActivity* app_visible = CreateActivity("visible");
app_visible->set_visible(true);
app_unloadable->set_visible(false);
app_media_locked1->set_visible(false);
app_media_locked2->set_visible(false);
app_media_locked1->set_media_state(
Activity::ACTIVITY_MEDIA_STATE_AUDIO_PLAYING);
app_media_locked2->set_media_state(Activity::ACTIVITY_MEDIA_STATE_RECORDING);
// Set the initial visibility states.
app_visible->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app_media_locked1->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
app_unloadable->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
app_media_locked2->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_visible->GetCurrentState());
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_media_locked1->GetCurrentState());
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_unloadable->GetCurrentState());
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_media_locked2->GetCurrentState());
// Calling it with a critical situation first, it will release the non media
// locked app.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_visible->GetCurrentState());
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_media_locked1->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_unloadable->GetCurrentState());
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_media_locked2->GetCurrentState());
// Calling it the second time, the oldest media playing activity will get
// unloaded.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_visible->GetCurrentState());
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_media_locked1->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_unloadable->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_media_locked2->GetCurrentState());
// Calling it the third time, the oldest media playing activity will get
// unloaded.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
DCHECK_NE(Activity::ACTIVITY_UNLOADED, app_visible->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_media_locked1->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_unloadable->GetCurrentState());
DCHECK_EQ(Activity::ACTIVITY_UNLOADED, app_media_locked2->GetCurrentState());
}
// Test the visibility of items.
TEST_F(ResourceManagerTest, VisibilityChanges) {
// Create a few dummy activities in the reverse order as we need them.
TestActivity* app4 = CreateActivity("app4");
TestActivity* app3 = CreateActivity("app3");
TestActivity* app2 = CreateActivity("app2");
TestActivity* app1 = CreateActivity("app1");
app1->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app2->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app3->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app4->SetCurrentState(Activity::ACTIVITY_VISIBLE);
// Applying low resource pressure should keep everything as is.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_LOW);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app3->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app4->GetCurrentState());
// Applying moderate resource pressure we should see 3 visible activities.
// This is testing an internal algorithm constant, but for the time being
// this should suffice.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_MODERATE);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app3->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app4->GetCurrentState());
// Applying higher pressure should get rid of everything unneeded.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app3->GetCurrentState());
// Note: This might very well be unloaded with this memory pressure.
EXPECT_NE(Activity::ACTIVITY_VISIBLE, app4->GetCurrentState());
// Once the split view mode gets turned on, more windows should become
// visible.
WindowManager::Get()->ToggleSplitViewForTest();
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app2->GetCurrentState());
EXPECT_NE(Activity::ACTIVITY_VISIBLE, app3->GetCurrentState());
EXPECT_NE(Activity::ACTIVITY_VISIBLE, app4->GetCurrentState());
// Going back to a relaxed memory pressure should reload the old activities.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_LOW);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app3->GetCurrentState());
EXPECT_NE(Activity::ACTIVITY_INVISIBLE, app4->GetCurrentState());
}
// Make sure that an activity which got just reduced from visible to invisible,
// does not get thrown out of memory in the same step.
TEST_F(ResourceManagerTest, NoUnloadFromVisible) {
// The timeout override in milliseconds.
const int kTimeoutOverrideInMs = 20;
// Tell the resource manager to wait for 20ms between calls.
ResourceManager::Get()->SetWaitTimeBetweenResourceManageCalls(
kTimeoutOverrideInMs);
// Create a few dummy activities in the reverse order as we need them.
TestActivity* app2 = CreateActivity("app2");
TestActivity* app1 = CreateActivity("app1");
app1->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app2->SetCurrentState(Activity::ACTIVITY_VISIBLE);
// Applying low resource pressure should turn one item invisible.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app2->GetCurrentState());
// Trying to apply the memory pressure again does not do anything.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app2->GetCurrentState());
// Waiting and applying the pressure again should unload it.
usleep(kTimeoutOverrideInMs * 1000);
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_UNLOADED, app2->GetCurrentState());
}
// Make sure that ActivityVisibility changes will be updated instantaneously
// when the ResourceManager is called for operation.
TEST_F(ResourceManagerTest, VisibilityChangeIsInstantaneous) {
// Create a few dummy activities in the reverse order as we need them.
TestActivity* app3 = CreateActivity("app3");
TestActivity* app2 = CreateActivity("app2");
TestActivity* app1 = CreateActivity("app1");
app1->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app2->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app3->SetCurrentState(Activity::ACTIVITY_VISIBLE);
// Tell the resource manager to wait for a long time between calls.
ResourceManager::Get()->SetWaitTimeBetweenResourceManageCalls(1000);
// Applying higher pressure should get rid of everything unneeded.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app3->GetCurrentState());
// Setting now one window visible again and call a second time should
// immediately change the state again.
app2->SetCurrentState(Activity::ACTIVITY_VISIBLE);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app3->GetCurrentState());
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app3->GetCurrentState());
}
// Make sure that a timeout has to be reached before another resource managing
// operations can be performed.
TEST_F(ResourceManagerTest, ResourceChangeDelayed) {
// Create a few dummy activities in the reverse order as we need them.
TestActivity* app4 = CreateActivity("app4");
TestActivity* app3 = CreateActivity("app3");
TestActivity* app2 = CreateActivity("app2");
TestActivity* app1 = CreateActivity("app1");
app1->SetCurrentState(Activity::ACTIVITY_VISIBLE);
app2->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
app3->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
app4->SetCurrentState(Activity::ACTIVITY_INVISIBLE);
// The timeout override in milliseconds.
const int kTimeoutOverrideInMs = 20;
// Tell the resource manager to wait for 20ms between calls.
ResourceManager::Get()->SetWaitTimeBetweenResourceManageCalls(
kTimeoutOverrideInMs);
// Applying higher pressure should get unload the oldest activity.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app3->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_UNLOADED, app4->GetCurrentState());
// Trying to apply the resource pressure again within the timeout time should
// not trigger any operation.
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app3->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_UNLOADED, app4->GetCurrentState());
// Passing the timeout however should allow for another call.
usleep(kTimeoutOverrideInMs * 1000);
ResourceManager::Get()->SetMemoryPressureAndStopMonitoring(
ResourceManager::MEMORY_PRESSURE_CRITICAL);
EXPECT_EQ(Activity::ACTIVITY_VISIBLE, app1->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_INVISIBLE, app2->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_UNLOADED, app3->GetCurrentState());
EXPECT_EQ(Activity::ACTIVITY_UNLOADED, app4->GetCurrentState());
}
} // namespace test
} // namespace athena
|