Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / sync_notifier / chrome_notifier_delegate_unittest.cc
1 // Copyright 2013 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 "base/memory/scoped_vector.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_delegate.h"
8 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
9 #include "chrome/browser/notifications/sync_notifier/sync_notifier_test_utils.h"
10 #include "chrome/browser/notifications/sync_notifier/synced_notification.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/browser_with_test_window_test.h"
14 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/test_utils.h"
19 #include "sync/api/sync_change.h"
20
21 const char kTestNotificationId[] = "SomeRandomNotificationId";
22
23 class StubChromeNotifierService : public notifier::ChromeNotifierService {
24  public:
25   explicit StubChromeNotifierService(Profile* profile)
26       : ChromeNotifierService(profile, NULL) {}
27
28   virtual ~StubChromeNotifierService() {}
29
30   virtual void MarkNotificationAsRead(const std::string& id) OVERRIDE {
31     read_id_ = id;
32   }
33
34   notifier::SyncedNotification* CreateNotification(
35       const std::string& title,
36       const std::string& text,
37       const std::string& app_icon_url,
38       const std::string& image_url,
39       const std::string& app_id,
40       const std::string& key,
41       sync_pb::CoalescedSyncedNotification_ReadState read_state) {
42     syncer::SyncData sync_data = CreateSyncData(title, text, app_icon_url,
43                                                 image_url,app_id, key,
44                                                 read_state);
45     // Set enough fields in sync_data, including specifics, for our tests
46     // to pass.
47     notifier::SyncedNotification* notification =
48         new notifier::SyncedNotification(sync_data);
49     // Retain ownership to avoid memory leaks in tests.
50     owned_notifications_.push_back(notification);
51     return notification;
52   }
53
54   // For testing, just return our test notification no matter what key the
55   // caller sends.
56   virtual notifier::SyncedNotification* FindNotificationById(
57       const std::string& id) OVERRIDE {
58     return CreateNotification(
59         kTitle1, kText1, kIconUrl1, kImageUrl1, kAppId1, kKey1, kUnread);
60   }
61
62   const std::string& read_id() { return read_id_; }
63
64  private:
65   std::string read_id_;
66   ScopedVector<notifier::SyncedNotification> owned_notifications_;
67 };
68
69 class ChromeNotifierDelegateTest : public BrowserWithTestWindowTest {
70  public:
71   ChromeNotifierDelegateTest() {}
72   virtual ~ChromeNotifierDelegateTest() {}
73
74   virtual void SetUp() OVERRIDE {
75     BrowserWithTestWindowTest::SetUp();
76     notifier_.reset(new StubChromeNotifierService(profile()));
77   }
78
79   virtual void TearDown() OVERRIDE {
80     notifier_.reset();
81     BrowserWithTestWindowTest::TearDown();
82   }
83
84  protected:
85   StubChromeNotifierService* notifier() { return notifier_.get(); }
86
87  private:
88   scoped_ptr<StubChromeNotifierService> notifier_;
89
90   DISALLOW_COPY_AND_ASSIGN(ChromeNotifierDelegateTest);
91 };
92
93 TEST_F(ChromeNotifierDelegateTest, ClickTest) {
94   std::string id(kTestNotificationId);
95   scoped_refptr<notifier::ChromeNotifierDelegate> delegate(
96       new notifier::ChromeNotifierDelegate(id, notifier()));
97
98   // Set up an observer to wait for the navigation
99   content::WindowedNotificationObserver observer(
100         chrome::NOTIFICATION_TAB_ADDED,
101         content::NotificationService::AllSources());
102
103   delegate->Click();
104
105   // Wait for navigation to finish.
106   observer.Wait();
107
108   // Verify the navigation happened as expected - we should be on chrome://flags
109   GURL url(kDefaultDestinationUrl);
110   content::WebContents* tab =
111       browser()->tab_strip_model()->GetActiveWebContents();
112   ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL());
113 }
114
115 TEST_F(ChromeNotifierDelegateTest, ButtonClickTest) {
116   std::string id(kTestNotificationId);
117   scoped_refptr<notifier::ChromeNotifierDelegate> delegate(
118       new notifier::ChromeNotifierDelegate(id, notifier()));
119
120   // Set up an observer to wait for the navigation
121   content::WindowedNotificationObserver observer(
122         chrome::NOTIFICATION_TAB_ADDED,
123         content::NotificationService::AllSources());
124
125   delegate->ButtonClick(0);
126
127   // Wait for navigation to finish.
128   observer.Wait();
129
130   // Verify the navigation happened as expected - we should be on chrome://sync
131   content::WebContents* tab;
132   GURL url1(kButtonOneUrl);
133   tab = browser()->tab_strip_model()->GetActiveWebContents();
134   ASSERT_EQ(url1, tab->GetController().GetActiveEntry()->GetVirtualURL());
135
136
137   delegate->ButtonClick(1);
138
139   // Wait for navigation to finish.
140   observer.Wait();
141
142   // Verify the navigation happened as expected - we should be on chrome://sync
143   GURL url2(kButtonTwoUrl);
144   tab = browser()->tab_strip_model()->GetActiveWebContents();
145   ASSERT_EQ(url2, tab->GetController().GetActiveEntry()->GetVirtualURL());
146
147   // Also verify that the click dismissed the notification.
148   ASSERT_EQ(kTestNotificationId, notifier()->read_id());
149 }
150
151 TEST_F(ChromeNotifierDelegateTest, CloseTest) {
152   std::string id(kTestNotificationId);
153   scoped_refptr<notifier::ChromeNotifierDelegate> delegate(
154       new notifier::ChromeNotifierDelegate(id, notifier()));
155
156   delegate->Close(false);
157   ASSERT_EQ("", notifier()->read_id());
158
159   delegate->Close(true);
160   ASSERT_EQ(kTestNotificationId, notifier()->read_id());
161 }