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