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.
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"
25 const char kTestNotificationId[] = "SomeRandomNotificationId";
27 class StubChromeNotifierService : public notifier::ChromeNotifierService {
29 StubChromeNotifierService()
30 : ChromeNotifierService(ProfileManager::GetDefaultProfile(), NULL) {}
32 virtual ~StubChromeNotifierService() {}
34 virtual void MarkNotificationAsRead(const std::string& id) OVERRIDE {
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,
49 // Set enough fields in sync_data, including specifics, for our tests
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);
58 // For testing, just return our test notification no matter what key the
60 virtual notifier::SyncedNotification* FindNotificationById(
61 const std::string& id) OVERRIDE {
62 return CreateNotification(
63 kTitle1, kText1, kIconUrl1, kImageUrl1, kAppId1, kKey1, kUnread);
66 const std::string& read_id() { return read_id_; }
70 ScopedVector<notifier::SyncedNotification> owned_notifications_;
73 class ChromeNotifierDelegateBrowserTest : public InProcessBrowserTest {};
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 \
80 #define MAYBE_ClickTest \
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, ¬ifier));
90 // Set up an observer to wait for the navigation
91 content::WindowedNotificationObserver observer(
92 chrome::NOTIFICATION_TAB_ADDED,
93 content::NotificationService::AllSources());
97 // Wait for navigation to finish.
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());
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
112 #define MAYBE_ButtonClickTest \
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, ¬ifier));
123 // Set up an observer to wait for the navigation
124 content::WindowedNotificationObserver observer(
125 chrome::NOTIFICATION_TAB_ADDED,
126 content::NotificationService::AllSources());
128 delegate->ButtonClick(0);
130 // Wait for navigation to finish.
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());
140 delegate->ButtonClick(1);
142 // Wait for navigation to finish.
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());
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, ¬ifier));
157 delegate->Close(false);
158 ASSERT_EQ("", notifier.read_id());
160 delegate->Close(true);
161 ASSERT_EQ(kTestNotificationId, notifier.read_id());