OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/browser/task_manager/task_manager.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/extensions/extension_browsertest.h" |
| 10 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 11 #include "chrome/browser/notifications/notification.h" |
| 12 #include "chrome/browser/notifications/notification_test_util.h" |
| 13 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/test/in_process_browser_test.h" |
| 17 #include "chrome/test/ui_test_utils.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 class ResourceChangeObserver : public TaskManagerModelObserver { |
| 23 public: |
| 24 ResourceChangeObserver(const TaskManagerModel* model, |
| 25 int target_resource_count) |
| 26 : model_(model), |
| 27 target_resource_count_(target_resource_count) { |
| 28 } |
| 29 |
| 30 virtual void OnModelChanged() { |
| 31 OnResourceChange(); |
| 32 } |
| 33 |
| 34 virtual void OnItemsChanged(int start, int length) { |
| 35 OnResourceChange(); |
| 36 } |
| 37 |
| 38 virtual void OnItemsAdded(int start, int length) { |
| 39 OnResourceChange(); |
| 40 } |
| 41 |
| 42 virtual void OnItemsRemoved(int start, int length) { |
| 43 OnResourceChange(); |
| 44 } |
| 45 |
| 46 private: |
| 47 void OnResourceChange() { |
| 48 if (model_->ResourceCount() == target_resource_count_) |
| 49 MessageLoopForUI::current()->Quit(); |
| 50 } |
| 51 |
| 52 const TaskManagerModel* model_; |
| 53 const int target_resource_count_; |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 class TaskManagerNotificationBrowserTest : public ExtensionBrowserTest { |
| 59 public: |
| 60 TaskManagerModel* model() const { |
| 61 return TaskManager::GetInstance()->model(); |
| 62 } |
| 63 |
| 64 void WaitForResourceChange(int target_count) { |
| 65 if (model()->ResourceCount() == target_count) |
| 66 return; |
| 67 ResourceChangeObserver observer(model(), target_count); |
| 68 model()->AddObserver(&observer); |
| 69 ui_test_utils::RunMessageLoop(); |
| 70 model()->RemoveObserver(&observer); |
| 71 } |
| 72 }; |
| 73 |
| 74 IN_PROC_BROWSER_TEST_F(TaskManagerNotificationBrowserTest, |
| 75 NoticeNotificationChanges) { |
| 76 EXPECT_EQ(0, model()->ResourceCount()); |
| 77 |
| 78 // Show the task manager. |
| 79 browser()->window()->ShowTaskManager(); |
| 80 // Expect to see the browser and the New Tab Page renderer. |
| 81 WaitForResourceChange(2); |
| 82 |
| 83 // Show a notification. |
| 84 NotificationUIManager* notifications = |
| 85 g_browser_process->notification_ui_manager(); |
| 86 |
| 87 string16 content = DesktopNotificationService::CreateDataUrl( |
| 88 GURL(), ASCIIToUTF16("Hello World!"), string16(), |
| 89 WebKit::WebTextDirectionDefault); |
| 90 |
| 91 scoped_refptr<NotificationDelegate> del1(new MockNotificationDelegate("n1")); |
| 92 Notification n1( |
| 93 GURL(), GURL(content), ASCIIToUTF16("Test 1"), string16(), del1.get()); |
| 94 scoped_refptr<NotificationDelegate> del2(new MockNotificationDelegate("n2")); |
| 95 Notification n2( |
| 96 GURL(), GURL(content), ASCIIToUTF16("Test 2"), string16(), del2.get()); |
| 97 |
| 98 notifications->Add(n1, browser()->profile()); |
| 99 WaitForResourceChange(3); |
| 100 notifications->Add(n2, browser()->profile()); |
| 101 WaitForResourceChange(4); |
| 102 notifications->CancelById(n1.notification_id()); |
| 103 WaitForResourceChange(3); |
| 104 notifications->CancelById(n2.notification_id()); |
| 105 WaitForResourceChange(2); |
| 106 } |
OLD | NEW |