1 // Copyright (c) 2012 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 "chrome/browser/task_manager/task_manager_browsertest_util.h"
7 #include "base/location.h"
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/pattern.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/test_timeouts.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/timer/timer.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/task_manager/resource_provider.h"
20 #include "chrome/browser/task_manager/task_manager.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "extensions/strings/grit/extensions_strings.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/base/l10n/l10n_util.h"
26 namespace task_manager
{
27 namespace browsertest_util
{
31 class ResourceChangeObserver
: public TaskManagerModelObserver
{
33 ResourceChangeObserver(const TaskManagerModel
* model
,
35 const base::string16
& title_pattern
,
36 ColumnSpecifier column_specifier
,
37 size_t min_column_value
)
39 required_count_(required_count
),
40 title_pattern_(title_pattern
),
41 column_specifier_(column_specifier
),
42 min_column_value_(min_column_value
) {}
44 void OnModelChanged() override
{ OnResourceChange(); }
46 void OnItemsChanged(int start
, int length
) override
{ OnResourceChange(); }
48 void OnItemsAdded(int start
, int length
) override
{ OnResourceChange(); }
50 void OnItemsRemoved(int start
, int length
) override
{ OnResourceChange(); }
52 void RunUntilSatisfied() {
53 // See if the condition is satisfied without having to run the loop. This
54 // check has to be placed after the installation of the
55 // TaskManagerModelObserver, because resources may change before that.
59 timer_
.Start(FROM_HERE
,
60 TestTimeouts::action_timeout(),
62 &ResourceChangeObserver::OnTimeout
);
66 // If we succeeded normally (no timeout), check our post condition again
67 // before returning control to the test. If it is no longer satisfied, the
68 // test is likely flaky: we were waiting for a state that was only achieved
69 // emphemerally), so treat this as a failure.
70 if (!IsSatisfied() && timer_
.IsRunning()) {
71 FAIL() << "Wait condition satisfied only emphemerally. Likely test "
72 << "problem. Maybe wait instead for the state below?\n"
73 << DumpTaskManagerModel();
80 void OnResourceChange() {
84 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
85 run_loop_
.QuitClosure());
88 bool IsSatisfied() { return CountMatches() == required_count_
; }
92 for (int i
= 0; i
< model_
->ResourceCount(); i
++) {
93 if (!base::MatchPattern(model_
->GetResourceTitle(i
), title_pattern_
))
96 if (GetColumnValue(i
) < min_column_value_
)
104 size_t GetColumnValue(int index
) {
106 bool success
= false;
107 switch (column_specifier_
) {
111 success
= model_
->GetV8Memory(index
, &value
);
114 success
= model_
->GetV8MemoryUsed(index
, &value
);
116 case SQLITE_MEMORY_USED
:
117 success
= model_
->GetSqliteMemoryUsedBytes(index
, &value
);
125 const char* GetColumnName() {
126 switch (column_specifier_
) {
132 return "V8 Memory Used";
133 case SQLITE_MEMORY_USED
:
134 return "SQLite Memory Used";
140 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
141 run_loop_
.QuitClosure());
142 FAIL() << "Timed out.\n" << DumpTaskManagerModel();
145 testing::Message
DumpTaskManagerModel() {
146 testing::Message task_manager_state_dump
;
147 task_manager_state_dump
<< "Waiting for exactly " << required_count_
148 << " matches of wildcard pattern \""
149 << base::UTF16ToASCII(title_pattern_
) << "\"";
150 if (min_column_value_
> 0) {
151 task_manager_state_dump
<< " && [" << GetColumnName()
152 << " >= " << min_column_value_
<< "]";
154 task_manager_state_dump
<< "\nCurrently there are " << CountMatches()
156 task_manager_state_dump
<< "\nCurrent Task Manager Model is:";
157 for (int i
= 0; i
< model_
->ResourceCount(); i
++) {
158 task_manager_state_dump
159 << "\n > " << std::setw(40) << std::left
160 << base::UTF16ToASCII(model_
->GetResourceTitle(i
));
161 if (min_column_value_
> 0) {
162 task_manager_state_dump
<< " [" << GetColumnName()
163 << " == " << GetColumnValue(i
) << "]";
166 return task_manager_state_dump
;
169 const TaskManagerModel
* model_
;
170 const int required_count_
;
171 const base::string16 title_pattern_
;
172 const ColumnSpecifier column_specifier_
;
173 const size_t min_column_value_
;
174 base::RunLoop run_loop_
;
175 base::OneShotTimer
<ResourceChangeObserver
> timer_
;
180 void WaitForTaskManagerRows(int required_count
,
181 const base::string16
& title_pattern
) {
182 TaskManagerModel
* model
= TaskManager::GetInstance()->model();
184 const int column_value_dont_care
= 0;
185 ResourceChangeObserver
observer(model
, required_count
, title_pattern
,
186 COLUMN_NONE
, column_value_dont_care
);
187 model
->AddObserver(&observer
);
188 observer
.RunUntilSatisfied();
189 model
->RemoveObserver(&observer
);
192 void WaitForTaskManagerStatToExceed(const base::string16
& title_pattern
,
193 ColumnSpecifier column_getter
,
194 size_t min_column_value
) {
195 TaskManagerModel
* model
= TaskManager::GetInstance()->model();
197 const int wait_for_one_match
= 1;
198 ResourceChangeObserver
observer(model
, wait_for_one_match
, title_pattern
,
199 column_getter
, min_column_value
);
200 model
->AddObserver(&observer
);
201 observer
.RunUntilSatisfied();
202 model
->RemoveObserver(&observer
);
205 base::string16
MatchTab(const char* title
) {
206 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX
,
207 base::ASCIIToUTF16(title
));
210 base::string16
MatchAnyTab() { return MatchTab("*"); }
212 base::string16
MatchAboutBlankTab() { return MatchTab("about:blank"); }
214 base::string16
MatchExtension(const char* title
) {
215 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_EXTENSION_PREFIX
,
216 base::ASCIIToUTF16(title
));
219 base::string16
MatchAnyExtension() { return MatchExtension("*"); }
221 base::string16
MatchApp(const char* title
) {
222 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_APP_PREFIX
,
223 base::ASCIIToUTF16(title
));
226 base::string16
MatchAnyApp() { return MatchApp("*"); }
228 base::string16
MatchWebView(const char* title
) {
229 return l10n_util::GetStringFUTF16(
230 IDS_EXTENSION_TASK_MANAGER_WEBVIEW_TAG_PREFIX
,
231 base::ASCIIToUTF16(title
));
234 base::string16
MatchAnyWebView() { return MatchWebView("*"); }
236 base::string16
MatchBackground(const char* title
) {
237 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_BACKGROUND_PREFIX
,
238 base::ASCIIToUTF16(title
));
241 base::string16
MatchAnyBackground() { return MatchBackground("*"); }
243 base::string16
MatchPrint(const char* title
) {
244 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRINT_PREFIX
,
245 base::ASCIIToUTF16(title
));
248 base::string16
MatchAnyPrint() { return MatchPrint("*"); }
250 base::string16
MatchSubframe(const char* title
) {
251 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_SUBFRAME_PREFIX
,
252 base::ASCIIToUTF16(title
));
255 base::string16
MatchAnySubframe() {
256 return MatchSubframe("*");
259 base::string16
MatchUtility(const base::string16
& title
) {
260 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX
, title
);
263 base::string16
MatchAnyUtility() {
264 return MatchUtility(base::ASCIIToUTF16("*"));
267 } // namespace browsertest_util
268 } // namespace task_manager