OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/test/base/in_process_browser_test.h" | 5 #include "chrome/test/base/in_process_browser_test.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/string_number_conversions.h" | |
10 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
12 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/performance_monitor/constants.h" | 13 #include "chrome/browser/performance_monitor/constants.h" |
12 #include "chrome/browser/performance_monitor/database.h" | 14 #include "chrome/browser/performance_monitor/database.h" |
13 #include "chrome/browser/performance_monitor/performance_monitor.h" | 15 #include "chrome/browser/performance_monitor/performance_monitor.h" |
14 #include "chrome/browser/extensions/extension_browsertest.h" | 16 #include "chrome/browser/extensions/extension_browsertest.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/extensions/unpacked_installer.h" | 18 #include "chrome/browser/extensions/unpacked_installer.h" |
17 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/profiles/profile_manager.h" | |
18 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/browser_tabstrip.h" | 22 #include "chrome/browser/ui/browser_tabstrip.h" |
23 #include "chrome/common/chrome_constants.h" | |
20 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
21 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
22 #include "chrome/common/chrome_version_info.h" | 26 #include "chrome/common/chrome_version_info.h" |
23 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
24 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
25 #include "chrome/test/base/ui_test_utils.h" | 29 #include "chrome/test/base/ui_test_utils.h" |
26 #include "content/public/browser/notification_registrar.h" | 30 #include "content/public/browser/notification_registrar.h" |
27 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
28 | 32 |
29 using extensions::Extension; | 33 using extensions::Extension; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 class PerformanceMonitorBrowserTest : public ExtensionBrowserTest { | 127 class PerformanceMonitorBrowserTest : public ExtensionBrowserTest { |
124 public: | 128 public: |
125 virtual void SetUpOnMainThread() OVERRIDE { | 129 virtual void SetUpOnMainThread() OVERRIDE { |
126 CHECK(db_dir_.CreateUniqueTempDir()); | 130 CHECK(db_dir_.CreateUniqueTempDir()); |
127 performance_monitor_ = PerformanceMonitor::GetInstance(); | 131 performance_monitor_ = PerformanceMonitor::GetInstance(); |
128 performance_monitor_->SetDatabasePath(db_dir_.path()); | 132 performance_monitor_->SetDatabasePath(db_dir_.path()); |
129 performance_monitor_->Start(); | 133 performance_monitor_->Start(); |
130 | 134 |
131 // Wait for DB to finish setting up. | 135 // Wait for DB to finish setting up. |
132 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 136 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
137 ui_test_utils::RunAllPendingInMessageLoop(); | |
133 } | 138 } |
134 | 139 |
135 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) { | 140 void GetEventsOnBackgroundThread(std::vector<linked_ptr<Event> >* events) { |
136 *events = performance_monitor_->database()->GetEvents(); | 141 *events = performance_monitor_->database()->GetEvents(); |
137 } | 142 } |
138 | 143 |
139 // A handle for getting the events from the database, which must be done on | 144 // A handle for getting the events from the database, which must be done on |
140 // the background thread. Since we are testing, we can mock synchronicity | 145 // the background thread. Since we are testing, we can mock synchronicity |
141 // with FlushForTesting(). | 146 // with FlushForTesting(). |
142 std::vector<linked_ptr<Event> > GetEvents() { | 147 std::vector<linked_ptr<Event> > GetEvents() { |
143 std::vector<linked_ptr<Event> > events; | 148 std::vector<linked_ptr<Event> > events; |
144 content::BrowserThread::PostBlockingPoolSequencedTask( | 149 content::BrowserThread::PostBlockingPoolSequencedTask( |
145 Database::kDatabaseSequenceToken, | 150 Database::kDatabaseSequenceToken, |
146 FROM_HERE, | 151 FROM_HERE, |
147 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, | 152 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, |
148 base::Unretained(this), | 153 base::Unretained(this), |
149 &events)); | 154 &events)); |
150 | 155 |
151 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 156 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
152 return events; | 157 return events; |
153 } | 158 } |
154 | 159 |
160 void AddStateValue(const std::string& key, const std::string& value) { | |
161 content::BrowserThread::PostBlockingPoolSequencedTask( | |
162 Database::kDatabaseSequenceToken, | |
163 FROM_HERE, | |
164 base::Bind(base::IgnoreResult(&Database::AddStateValue), | |
165 base::Unretained(performance_monitor()->database()), | |
166 key, | |
167 value)); | |
168 | |
169 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
170 } | |
171 | |
155 PerformanceMonitor* performance_monitor() const { | 172 PerformanceMonitor* performance_monitor() const { |
156 return performance_monitor_; | 173 return performance_monitor_; |
157 } | 174 } |
158 | 175 |
159 protected: | 176 protected: |
160 ScopedTempDir db_dir_; | 177 ScopedTempDir db_dir_; |
161 PerformanceMonitor* performance_monitor_; | 178 PerformanceMonitor* performance_monitor_; |
162 }; | 179 }; |
163 | 180 |
181 class PerformanceMonitorUncleanExitBrowserTest | |
182 : public PerformanceMonitorBrowserTest { | |
183 public: | |
184 virtual bool SetUpUserDataDirectory() OVERRIDE { | |
185 FilePath user_data_directory; | |
186 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); | |
187 | |
188 FilePath default_profile = user_data_directory.AppendASCII("Default"); | |
189 CHECK(file_util::CreateDirectory(default_profile)); | |
190 | |
191 FilePath stock_prefs_file; | |
192 PathService::Get(chrome::DIR_TEST_DATA, &stock_prefs_file); | |
193 stock_prefs_file = stock_prefs_file.AppendASCII("performance_monitor") | |
194 .AppendASCII("unclean_exit_prefs"); | |
195 CHECK(file_util::PathExists(stock_prefs_file)); | |
196 | |
197 FilePath default_profile_prefs_file = | |
198 default_profile.AppendASCII(chrome::kPreferencesFilename); | |
199 CHECK(file_util::CopyFile(stock_prefs_file, default_profile_prefs_file)); | |
200 CHECK(file_util::PathExists(default_profile_prefs_file)); | |
201 | |
202 FilePath second_profile = user_data_directory.AppendASCII("Profile 1"); | |
203 CHECK(file_util::CreateDirectory(second_profile)); | |
204 | |
205 FilePath second_profile_prefs_file = | |
206 second_profile.AppendASCII(chrome::kPreferencesFilename); | |
207 CHECK(file_util::CopyFile(stock_prefs_file, second_profile_prefs_file)); | |
208 CHECK(file_util::PathExists(second_profile_prefs_file)); | |
209 | |
210 return true; | |
211 } | |
212 }; | |
213 | |
164 // Test that PerformanceMonitor will correctly record an extension installation | 214 // Test that PerformanceMonitor will correctly record an extension installation |
165 // event. | 215 // event. |
166 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, InstallExtensionEvent) { | 216 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, InstallExtensionEvent) { |
167 FilePath extension_path; | 217 FilePath extension_path; |
168 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); | 218 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); |
169 extension_path = extension_path.AppendASCII("performance_monitor") | 219 extension_path = extension_path.AppendASCII("performance_monitor") |
170 .AppendASCII("extensions") | 220 .AppendASCII("extensions") |
171 .AppendASCII("simple_extension_v1"); | 221 .AppendASCII("simple_extension_v1"); |
172 const Extension* extension = LoadExtension(extension_path); | 222 const Extension* extension = LoadExtension(extension_path); |
173 | 223 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 std::vector<linked_ptr<Event> > events = GetEvents(); | 338 std::vector<linked_ptr<Event> > events = GetEvents(); |
289 | 339 |
290 CheckExtensionEvents(expected_event_types, events, extension_infos); | 340 CheckExtensionEvents(expected_event_types, events, extension_infos); |
291 | 341 |
292 // There will be an additional field: The unload reason. | 342 // There will be an additional field: The unload reason. |
293 int unload_reason = -1; | 343 int unload_reason = -1; |
294 ASSERT_TRUE(events[2]->data()->GetInteger("unloadReason", &unload_reason)); | 344 ASSERT_TRUE(events[2]->data()->GetInteger("unloadReason", &unload_reason)); |
295 ASSERT_EQ(extension_misc::UNLOAD_REASON_UPDATE, unload_reason); | 345 ASSERT_EQ(extension_misc::UNLOAD_REASON_UPDATE, unload_reason); |
296 } | 346 } |
297 | 347 |
348 IN_PROC_BROWSER_TEST_F(PerformanceMonitorUncleanExitBrowserTest, | |
349 OneProfileUncleanExit) { | |
350 const std::string profile_name = "Default"; | |
351 const std::string time = "12985807272597591"; | |
352 const size_t kNumEvents = 1; | |
353 | |
354 // Initialize the database value (if there's no value in the database, it | |
355 // can't determine the last active time of the profile, and doesn't insert | |
356 // the event). | |
357 AddStateValue(kStateProfile + profile_name, time); | |
358 | |
359 performance_monitor()->CheckForUncleanExit(); | |
360 | |
361 ui_test_utils::RunAllPendingInMessageLoop(); | |
362 | |
363 std::vector<linked_ptr<Event> > events = GetEvents(); | |
364 ASSERT_EQ(kNumEvents, events.size()); | |
365 | |
366 CheckEvent(EVENT_UNCLEAN_EXIT, events[0]); | |
367 | |
368 std::string event_profile; | |
369 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); | |
370 ASSERT_EQ(profile_name, event_profile); | |
371 } | |
372 | |
373 IN_PROC_BROWSER_TEST_F(PerformanceMonitorUncleanExitBrowserTest, | |
374 TwoProfileUncleanExit) { | |
Yoyo Zhou
2012/07/10 02:35:06
It looks like these tests are only testing one of
Devlin
2012/07/10 17:18:32
I assume that by the "Two paths" you mean either a
Yoyo Zhou
2012/07/12 18:13:44
Yes, that's what I meant. Apparently I didn't read
| |
375 const std::string profile1_name = "Default"; | |
376 const std::string profile2_name = "Profile 1"; | |
377 const std::string time1 = "12985807272597591"; | |
378 const std::string time2 = "12985807272599918"; | |
379 const size_t kNumEvents = 2; | |
380 | |
381 FilePath profile2_path; | |
382 PathService::Get(chrome::DIR_USER_DATA, &profile2_path); | |
383 profile2_path = profile2_path.AppendASCII(profile2_name); | |
384 | |
385 AddStateValue(kStateProfile + profile1_name, time1); | |
386 AddStateValue(kStateProfile + profile2_name, time2); | |
387 | |
388 performance_monitor()->CheckForUncleanExit(); | |
389 ui_test_utils::RunAllPendingInMessageLoop(); | |
390 | |
391 // Load the second profile, which has also exited uncleanly. | |
392 g_browser_process->profile_manager()->GetProfile(profile2_path); | |
393 ui_test_utils::RunAllPendingInMessageLoop(); | |
394 | |
395 std::vector<linked_ptr<Event> > events = GetEvents(); | |
396 | |
397 ASSERT_EQ(kNumEvents, events.size()); | |
398 CheckEvent(EVENT_UNCLEAN_EXIT, events[0]); | |
399 CheckEvent(EVENT_UNCLEAN_EXIT, events[1]); | |
400 | |
401 std::string event_profile; | |
402 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); | |
403 ASSERT_EQ(profile1_name, event_profile); | |
404 | |
405 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile)); | |
406 ASSERT_EQ(profile2_name, event_profile); | |
407 } | |
408 | |
298 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { | 409 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, KilledByOSEvent) { |
299 ui_test_utils::CrashTab(chrome::GetActiveWebContents(browser())); | 410 ui_test_utils::CrashTab(chrome::GetActiveWebContents(browser())); |
300 | 411 |
301 std::vector<linked_ptr<Event> > events = GetEvents(); | 412 std::vector<linked_ptr<Event> > events = GetEvents(); |
302 | 413 |
303 ASSERT_EQ(1u, events.size()); | 414 ASSERT_EQ(1u, events.size()); |
304 CheckEvent(EVENT_KILLED_BY_OS_CRASH, events[0]); | 415 CheckEvent(EVENT_KILLED_BY_OS_CRASH, events[0]); |
305 } | 416 } |
306 | 417 |
307 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, RendererCrashEvent) { | 418 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, RendererCrashEvent) { |
308 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); | 419 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUICrashURL)); |
309 | 420 |
310 std::vector<linked_ptr<Event> > events = GetEvents(); | 421 std::vector<linked_ptr<Event> > events = GetEvents(); |
311 ASSERT_EQ(1u, events.size()); | 422 ASSERT_EQ(1u, events.size()); |
312 | 423 |
313 CheckEvent(EVENT_RENDERER_CRASH, events[0]); | 424 CheckEvent(EVENT_RENDERER_CRASH, events[0]); |
314 } | 425 } |
315 | 426 |
316 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, NewVersionEvent) { | 427 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, NewVersionEvent) { |
317 const char kOldVersion[] = "0.0"; | 428 const char kOldVersion[] = "0.0"; |
318 | 429 |
319 content::BrowserThread::PostBlockingPoolSequencedTask( | 430 AddStateValue(kStateChromeVersion, kOldVersion); |
320 Database::kDatabaseSequenceToken, | |
321 FROM_HERE, | |
322 base::Bind(base::IgnoreResult(&Database::AddStateValue), | |
323 base::Unretained(performance_monitor()->database()), | |
324 std::string(kStateChromeVersion), | |
325 std::string(kOldVersion))); | |
326 | |
327 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
328 | 431 |
329 performance_monitor()->CheckForVersionUpdate(); | 432 performance_monitor()->CheckForVersionUpdate(); |
330 | 433 |
331 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 434 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
332 | 435 |
333 chrome::VersionInfo version; | 436 chrome::VersionInfo version; |
334 ASSERT_TRUE(version.is_valid()); | 437 ASSERT_TRUE(version.is_valid()); |
335 std::string version_string = version.Version(); | 438 std::string version_string = version.Version(); |
336 | 439 |
337 std::vector<linked_ptr<Event> > events = GetEvents(); | 440 std::vector<linked_ptr<Event> > events = GetEvents(); |
338 ASSERT_EQ(1u, events.size()); | 441 ASSERT_EQ(1u, events.size()); |
339 ASSERT_EQ(EVENT_CHROME_UPDATE, events[0]->type()); | 442 ASSERT_EQ(EVENT_CHROME_UPDATE, events[0]->type()); |
340 | 443 |
341 const base::DictionaryValue* value; | 444 const base::DictionaryValue* value; |
342 ASSERT_TRUE(events[0]->data()->GetAsDictionary(&value)); | 445 ASSERT_TRUE(events[0]->data()->GetAsDictionary(&value)); |
343 | 446 |
344 std::string previous_version; | 447 std::string previous_version; |
345 std::string current_version; | 448 std::string current_version; |
346 | 449 |
347 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); | 450 ASSERT_TRUE(value->GetString("previousVersion", &previous_version)); |
348 ASSERT_EQ(kOldVersion, previous_version); | 451 ASSERT_EQ(kOldVersion, previous_version); |
349 ASSERT_TRUE(value->GetString("currentVersion", ¤t_version)); | 452 ASSERT_TRUE(value->GetString("currentVersion", ¤t_version)); |
350 ASSERT_EQ(version_string, current_version); | 453 ASSERT_EQ(version_string, current_version); |
351 } | 454 } |
352 | 455 |
353 } // namespace performance_monitor | 456 } // namespace performance_monitor |
OLD | NEW |