Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / sync_notifier / welcome_delegate_unittest.cc
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.
4
5 #include "chrome/browser/notifications/sync_notifier/welcome_delegate.h"
6
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"
25
26 class WelcomeDelegateTest : public BrowserWithTestWindowTest {
27  public:
28   WelcomeDelegateTest() {}
29   virtual ~WelcomeDelegateTest() {}
30
31   virtual void SetUp() OVERRIDE {
32     BrowserWithTestWindowTest::SetUp();
33 #if !defined(OS_CHROMEOS)
34     message_center::MessageCenter::Initialize();
35 #endif
36     profile_manager_.reset(
37         new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
38     ASSERT_TRUE(profile_manager_->SetUp());
39
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()));
46   }
47
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();
53 #endif
54     BrowserWithTestWindowTest::TearDown();
55   }
56
57  private:
58   scoped_ptr<TestingProfileManager> profile_manager_;
59   DISALLOW_COPY_AND_ASSIGN(WelcomeDelegateTest);
60 };
61
62 #if defined(OS_LINUX) && !defined(USE_AURA)
63 #define MAYBE_ClickTest DISABLED_ClickTest
64 #else
65 #define MAYBE_ClickTest ClickTest
66 #endif
67
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");
72
73   message_center::NotifierId notifier_id(
74       message_center::NotifierId::SYNCED_NOTIFICATION_SERVICE,
75       std::string("test"));
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"),
83                             gfx::Image(),
84                             blink::WebTextDirectionDefault,
85                             notifier_id,
86                             base::UTF8ToUTF16("display source"),
87                             base::UTF8ToUTF16("Replace ID"),
88                             message_center::RichNotificationData(),
89                             delegate.get());
90   g_browser_process->notification_ui_manager()->Add(notification, profile());
91   EXPECT_TRUE(NULL !=
92               g_browser_process->notification_ui_manager()->FindById(id));
93   EXPECT_TRUE(delegate->HasClickedListener());
94
95   // Set up an observer to wait for the navigation
96   content::WindowedNotificationObserver observer(
97       chrome::NOTIFICATION_TAB_ADDED,
98       content::NotificationService::AllSources());
99
100   delegate->Click();
101
102   // Wait for navigation to finish.
103   observer.Wait();
104
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());
110
111   EXPECT_TRUE(NULL ==
112               g_browser_process->notification_ui_manager()->FindById(id));
113 }
114
115 #if defined(OS_LINUX) && !defined(USE_AURA)
116 #define MAYBE_ButtonClickTest DISABLED_ButtonClickTest
117 #else
118 #define MAYBE_ButtonClickTest ButtonClickTest
119 #endif
120
121 // Test that ensures the notifier is disabled when button is clicked.
122 TEST_F(WelcomeDelegateTest, MAYBE_ButtonClickTest) {
123   std::string id("foo");
124
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"),
134                             gfx::Image(),
135                             blink::WebTextDirectionDefault,
136                             notifier_id,
137                             base::UTF8ToUTF16("display source"),
138                             base::UTF8ToUTF16("Replace ID"),
139                             message_center::RichNotificationData(),
140                             delegate.get());
141
142   // Add a notification with a WelcmeDelegate.
143   g_browser_process->notification_ui_manager()->Add(notification, profile());
144   // Expect it to be there.
145   EXPECT_TRUE(NULL !=
146               g_browser_process->notification_ui_manager()->FindById(id));
147
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));
154
155   // Click the button.
156   delegate->ButtonClick(0);
157
158   // No more welcome toast.
159   EXPECT_TRUE(NULL ==
160               g_browser_process->notification_ui_manager()->FindById(id));
161   // Expect the notifier to be disabled.
162   EXPECT_FALSE(notification_service->IsNotifierEnabled(notifier_id));
163 }