1 // Copyright 2014 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/notifications/sync_notifier/welcome_delegate.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/notifications/desktop_notification_service.h"
10 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
11 #include "chrome/browser/notifications/message_center_notification_manager.h"
12 #include "chrome/browser/notifications/notification.h"
13 #include "chrome/browser/notifications/notification_test_util.h"
14 #include "chrome/browser/notifications/sync_notifier/sync_notifier_test_utils.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/browser_with_test_window_test.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/test/test_utils.h"
22 #include "ui/message_center/fake_message_center_tray_delegate.h"
23 #include "ui/message_center/message_center.h"
24 #include "ui/message_center/notifier_settings.h"
26 class WelcomeDelegateTest : public BrowserWithTestWindowTest {
28 WelcomeDelegateTest() {}
29 virtual ~WelcomeDelegateTest() {}
31 virtual void SetUp() OVERRIDE {
32 BrowserWithTestWindowTest::SetUp();
33 #if !defined(OS_CHROMEOS)
34 message_center::MessageCenter::Initialize();
36 profile_manager_.reset(
37 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
38 ASSERT_TRUE(profile_manager_->SetUp());
40 MessageCenterNotificationManager* manager =
41 static_cast<MessageCenterNotificationManager*>(
42 g_browser_process->notification_ui_manager());
43 manager->SetMessageCenterTrayDelegateForTest(
44 new message_center::FakeMessageCenterTrayDelegate(
45 message_center::MessageCenter::Get(), base::Closure()));
48 virtual void TearDown() OVERRIDE {
49 g_browser_process->notification_ui_manager()->CancelAll();
50 profile_manager_.reset();
51 #if !defined(OS_CHROMEOS)
52 message_center::MessageCenter::Shutdown();
54 BrowserWithTestWindowTest::TearDown();
58 scoped_ptr<TestingProfileManager> profile_manager_;
59 DISALLOW_COPY_AND_ASSIGN(WelcomeDelegateTest);
62 #if defined(OS_LINUX) && !defined(USE_AURA)
63 #define MAYBE_ClickTest DISABLED_ClickTest
65 #define MAYBE_ClickTest ClickTest
68 // Test that ensures a click removes the welcome notification. Also ensures
69 // navigation to the specified destination URL.
70 TEST_F(WelcomeDelegateTest, MAYBE_ClickTest) {
71 std::string id("foo");
73 message_center::NotifierId notifier_id(
74 message_center::NotifierId::SYNCED_NOTIFICATION_SERVICE,
76 scoped_refptr<notifier::WelcomeDelegate> delegate(
77 new notifier::WelcomeDelegate(
78 id, profile(), notifier_id, GURL(kDefaultDestinationUrl)));
79 Notification notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
80 GURL("http://www.chromium.org"),
81 base::UTF8ToUTF16("title"),
82 base::UTF8ToUTF16("body"),
84 blink::WebTextDirectionDefault,
86 base::UTF8ToUTF16("display source"),
87 base::UTF8ToUTF16("Replace ID"),
88 message_center::RichNotificationData(),
90 g_browser_process->notification_ui_manager()->Add(notification, profile());
92 g_browser_process->notification_ui_manager()->FindById(id));
93 EXPECT_TRUE(delegate->HasClickedListener());
95 // Set up an observer to wait for the navigation
96 content::WindowedNotificationObserver observer(
97 chrome::NOTIFICATION_TAB_ADDED,
98 content::NotificationService::AllSources());
102 // Wait for navigation to finish.
105 // Verify the navigation happened as expected - we should be on chrome://flags
106 GURL url(kDefaultDestinationUrl);
107 content::WebContents* tab =
108 browser()->tab_strip_model()->GetActiveWebContents();
109 ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL());
112 g_browser_process->notification_ui_manager()->FindById(id));
115 #if defined(OS_LINUX) && !defined(USE_AURA)
116 #define MAYBE_ButtonClickTest DISABLED_ButtonClickTest
118 #define MAYBE_ButtonClickTest ButtonClickTest
121 // Test that ensures the notifier is disabled when button is clicked.
122 TEST_F(WelcomeDelegateTest, MAYBE_ButtonClickTest) {
123 std::string id("foo");
125 message_center::NotifierId notifier_id(
126 message_center::NotifierId::SYNCED_NOTIFICATION_SERVICE,
127 std::string("test"));
128 scoped_refptr<notifier::WelcomeDelegate> delegate(
129 new notifier::WelcomeDelegate(id, profile(), notifier_id, GURL()));
130 Notification notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
131 GURL("http://www.chromium.org"),
132 base::UTF8ToUTF16("title"),
133 base::UTF8ToUTF16("body"),
135 blink::WebTextDirectionDefault,
137 base::UTF8ToUTF16("display source"),
138 base::UTF8ToUTF16("Replace ID"),
139 message_center::RichNotificationData(),
142 // Add a notification with a WelcmeDelegate.
143 g_browser_process->notification_ui_manager()->Add(notification, profile());
144 // Expect it to be there.
146 g_browser_process->notification_ui_manager()->FindById(id));
148 // Set up the notification service.
149 DesktopNotificationService* notification_service =
150 DesktopNotificationServiceFactory::GetForProfile(profile());
151 ASSERT_TRUE(NULL != notification_service);
152 notification_service->SetNotifierEnabled(notifier_id, true);
153 ASSERT_TRUE(notification_service->IsNotifierEnabled(notifier_id));
156 delegate->ButtonClick(0);
158 // No more welcome toast.
160 g_browser_process->notification_ui_manager()->FindById(id));
161 // Expect the notifier to be disabled.
162 EXPECT_FALSE(notification_service->IsNotifierEnabled(notifier_id));