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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" |
10 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
11 #include "base/prefs/scoped_user_pref_update.h" | 12 #include "base/prefs/scoped_user_pref_update.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/background/background_contents_service.h" | 14 #include "chrome/browser/background/background_contents_service.h" |
14 #include "chrome/browser/background/background_contents_service_factory.h" | 15 #include "chrome/browser/background/background_contents_service_factory.h" |
15 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
16 #include "chrome/browser/tab_contents/background_contents.h" | 17 #include "chrome/browser/tab_contents/background_contents.h" |
17 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
| 19 #include "chrome/common/extensions/extension_test_util.h" |
18 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
19 #include "chrome/test/base/testing_browser_process.h" | 21 #include "chrome/test/base/testing_browser_process.h" |
20 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 23 #include "chrome/test/base/testing_profile_manager.h" |
21 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/test/test_browser_thread.h" |
| 26 #include "extensions/common/extension.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "testing/platform_test.h" | 28 #include "testing/platform_test.h" |
24 #include "url/gurl.h" | 29 #include "url/gurl.h" |
25 | 30 |
| 31 #if defined(ENABLE_NOTIFICATIONS) |
| 32 #include "chrome/browser/notifications/notification.h" |
| 33 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 34 #include "ui/message_center/message_center.h" |
| 35 #include "ui/message_center/message_center_observer.h" |
| 36 #endif |
| 37 |
| 38 #if defined(USE_ASH) |
| 39 #include "ash/test/ash_test_helper.h" |
| 40 #endif |
| 41 |
26 class BackgroundContentsServiceTest : public testing::Test { | 42 class BackgroundContentsServiceTest : public testing::Test { |
27 public: | 43 public: |
28 BackgroundContentsServiceTest() {} | 44 BackgroundContentsServiceTest() {} |
29 virtual ~BackgroundContentsServiceTest() {} | 45 virtual ~BackgroundContentsServiceTest() {} |
30 virtual void SetUp() { | 46 virtual void SetUp() { |
31 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); | 47 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); |
32 } | 48 } |
33 | 49 |
34 const base::DictionaryValue* GetPrefs(Profile* profile) { | 50 const base::DictionaryValue* GetPrefs(Profile* profile) { |
35 return profile->GetPrefs()->GetDictionary( | 51 return profile->GetPrefs()->GetDictionary( |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 private: | 113 private: |
98 GURL url_; | 114 GURL url_; |
99 | 115 |
100 // The ID of our parent application | 116 // The ID of our parent application |
101 base::string16 appid_; | 117 base::string16 appid_; |
102 | 118 |
103 // Parent profile | 119 // Parent profile |
104 Profile* profile_; | 120 Profile* profile_; |
105 }; | 121 }; |
106 | 122 |
| 123 #if defined(ENABLE_NOTIFICATIONS) |
| 124 // Wait for the notification created. |
| 125 class NotificationWaiter : public message_center::MessageCenterObserver { |
| 126 public: |
| 127 explicit NotificationWaiter(const std::string& target_id) |
| 128 : target_id_(target_id) {} |
| 129 virtual ~NotificationWaiter() {} |
| 130 |
| 131 void WaitForNotificationAdded() { |
| 132 message_center::MessageCenter* message_center = |
| 133 message_center::MessageCenter::Get(); |
| 134 if (message_center->HasNotification(target_id_)) |
| 135 return; |
| 136 |
| 137 message_center->AddObserver(this); |
| 138 base::MessageLoop::current()->Run(); |
| 139 message_center->RemoveObserver(this); |
| 140 } |
| 141 |
| 142 private: |
| 143 // message_center::MessageCenterObserver overrides: |
| 144 virtual void OnNotificationAdded( |
| 145 const std::string& notification_id) OVERRIDE { |
| 146 if (notification_id == target_id_) |
| 147 base::MessageLoop::current()->Quit(); |
| 148 } |
| 149 |
| 150 std::string target_id_; |
| 151 |
| 152 DISALLOW_COPY_AND_ASSIGN(NotificationWaiter); |
| 153 }; |
| 154 |
| 155 class BackgroundContentsServiceNotificationTest : public testing::Test { |
| 156 public: |
| 157 BackgroundContentsServiceNotificationTest() |
| 158 : ui_thread_(content::BrowserThread::UI, &ui_loop_), |
| 159 #if defined(USE_ASH) |
| 160 ash_helper_(&ui_loop_), |
| 161 #endif |
| 162 profile_(NULL) {} |
| 163 virtual ~BackgroundContentsServiceNotificationTest() {} |
| 164 |
| 165 // Overridden from testing::Test |
| 166 virtual void SetUp() { |
| 167 // In Ash environment, ash_helper_ can initialize MessageCenter. |
| 168 #if defined(USE_ASH) |
| 169 ash_helper_.SetUp(true); |
| 170 #else |
| 171 message_center::MessageCenter::Initialize(); |
| 172 #endif |
| 173 profile_manager_.reset(new TestingProfileManager( |
| 174 TestingBrowserProcess::GetGlobal())); |
| 175 ASSERT_TRUE(profile_manager_->SetUp()); |
| 176 profile_ = profile_manager_->CreateTestingProfile("Default"); |
| 177 } |
| 178 |
| 179 virtual void TearDown() { |
| 180 profile_manager_.reset(); |
| 181 profile_ = NULL; |
| 182 #if defined(USE_ASH) |
| 183 ash_helper_.TearDown(); |
| 184 #else |
| 185 message_center::MessageCenter::Shutdown(); |
| 186 #endif |
| 187 } |
| 188 |
| 189 protected: |
| 190 Profile* profile() { return profile_; } |
| 191 |
| 192 // Creates crash notification for the specified extension and returns |
| 193 // the created one. |
| 194 const Notification* CreateCrashNotification( |
| 195 scoped_refptr<extensions::Extension> extension) { |
| 196 std::string notification_id = |
| 197 BackgroundContentsService::GetNotificationIdForExtension( |
| 198 extension->id()); |
| 199 NotificationWaiter waiter(notification_id); |
| 200 BackgroundContentsService::ShowBalloon(extension.get(), profile_); |
| 201 waiter.WaitForNotificationAdded(); |
| 202 |
| 203 return g_browser_process->notification_ui_manager()->FindById( |
| 204 notification_id); |
| 205 } |
| 206 |
| 207 private: |
| 208 base::MessageLoopForUI ui_loop_; |
| 209 content::TestBrowserThread ui_thread_; |
| 210 #if defined(USE_ASH) |
| 211 ash::test::AshTestHelper ash_helper_; |
| 212 #endif |
| 213 scoped_ptr<TestingProfileManager> profile_manager_; |
| 214 TestingProfile* profile_; |
| 215 |
| 216 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsServiceNotificationTest); |
| 217 }; |
| 218 #endif // ENABLE_NOTIFICATIONS |
| 219 |
107 TEST_F(BackgroundContentsServiceTest, Create) { | 220 TEST_F(BackgroundContentsServiceTest, Create) { |
108 // Check for creation and leaks. | 221 // Check for creation and leaks. |
109 TestingProfile profile; | 222 TestingProfile profile; |
110 BackgroundContentsService service(&profile, command_line_.get()); | 223 BackgroundContentsService service(&profile, command_line_.get()); |
111 } | 224 } |
112 | 225 |
113 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { | 226 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { |
114 TestingProfile profile; | 227 TestingProfile profile; |
115 BackgroundContentsService service(&profile, command_line_.get()); | 228 BackgroundContentsService service(&profile, command_line_.get()); |
116 MockBackgroundContents* contents = new MockBackgroundContents(&profile); | 229 MockBackgroundContents* contents = new MockBackgroundContents(&profile); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 341 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
229 contents2->Navigate(url2); | 342 contents2->Navigate(url2); |
230 EXPECT_EQ(2U, GetPrefs(&profile)->size()); | 343 EXPECT_EQ(2U, GetPrefs(&profile)->size()); |
231 service.ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16("appid")); | 344 service.ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16("appid")); |
232 EXPECT_FALSE(service.IsTracked(contents)); | 345 EXPECT_FALSE(service.IsTracked(contents)); |
233 EXPECT_EQ(NULL, | 346 EXPECT_EQ(NULL, |
234 service.GetAppBackgroundContents(base::ASCIIToUTF16("appid"))); | 347 service.GetAppBackgroundContents(base::ASCIIToUTF16("appid"))); |
235 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 348 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
236 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); | 349 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); |
237 } | 350 } |
| 351 |
| 352 #if defined(ENABLE_NOTIFICATIONS) |
| 353 TEST_F(BackgroundContentsServiceNotificationTest, TestShowBalloon) { |
| 354 scoped_refptr<extensions::Extension> extension = |
| 355 extension_test_util::LoadManifest("image_loading_tracker", "app.json"); |
| 356 ASSERT_TRUE(extension.get()); |
| 357 ASSERT_TRUE(extension->GetManifestData("icons")); |
| 358 |
| 359 const Notification* notification = CreateCrashNotification(extension); |
| 360 EXPECT_FALSE(notification->icon().IsEmpty()); |
| 361 } |
| 362 |
| 363 // Verify if a test notification can show the default extension icon for |
| 364 // a crash notification for an extension without icon. |
| 365 TEST_F(BackgroundContentsServiceNotificationTest, TestShowBalloonNoIcon) { |
| 366 // Extension manifest file with no 'icon' field. |
| 367 scoped_refptr<extensions::Extension> extension = |
| 368 extension_test_util::LoadManifest("app", "manifest.json"); |
| 369 ASSERT_TRUE(extension.get()); |
| 370 ASSERT_FALSE(extension->GetManifestData("icons")); |
| 371 |
| 372 const Notification* notification = CreateCrashNotification(extension); |
| 373 EXPECT_FALSE(notification->icon().IsEmpty()); |
| 374 } |
| 375 #endif |
OLD | NEW |