Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / notifications / sync_notifier / chrome_notifier_delegate_unittest.cc
blobe952a82c53a2926b307f1c50602b7f391150f00a
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/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"
21 const char kTestNotificationId[] = "SomeRandomNotificationId";
23 class StubChromeNotifierService : public notifier::ChromeNotifierService {
24 public:
25 explicit StubChromeNotifierService(Profile* profile)
26 : ChromeNotifierService(profile, NULL) {}
28 virtual ~StubChromeNotifierService() {}
30 virtual void MarkNotificationAsRead(const std::string& id) OVERRIDE {
31 read_id_ = id;
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;
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);
62 const std::string& read_id() { return read_id_; }
64 private:
65 std::string read_id_;
66 ScopedVector<notifier::SyncedNotification> owned_notifications_;
69 class ChromeNotifierDelegateTest : public BrowserWithTestWindowTest {
70 public:
71 ChromeNotifierDelegateTest() {}
72 virtual ~ChromeNotifierDelegateTest() {}
74 virtual void SetUp() OVERRIDE {
75 BrowserWithTestWindowTest::SetUp();
76 notifier_.reset(new StubChromeNotifierService(profile()));
79 virtual void TearDown() OVERRIDE {
80 notifier_.reset();
81 BrowserWithTestWindowTest::TearDown();
84 protected:
85 StubChromeNotifierService* notifier() { return notifier_.get(); }
87 private:
88 scoped_ptr<StubChromeNotifierService> notifier_;
90 DISALLOW_COPY_AND_ASSIGN(ChromeNotifierDelegateTest);
93 TEST_F(ChromeNotifierDelegateTest, ClickTest) {
94 std::string id(kTestNotificationId);
95 scoped_refptr<notifier::ChromeNotifierDelegate> delegate(
96 new notifier::ChromeNotifierDelegate(id, notifier()));
98 // Set up an observer to wait for the navigation
99 content::WindowedNotificationObserver observer(
100 chrome::NOTIFICATION_TAB_ADDED,
101 content::NotificationService::AllSources());
103 delegate->Click();
105 // Wait for navigation to finish.
106 observer.Wait();
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());
115 TEST_F(ChromeNotifierDelegateTest, ButtonClickTest) {
116 std::string id(kTestNotificationId);
117 scoped_refptr<notifier::ChromeNotifierDelegate> delegate(
118 new notifier::ChromeNotifierDelegate(id, notifier()));
120 // Set up an observer to wait for the navigation
121 content::WindowedNotificationObserver observer(
122 chrome::NOTIFICATION_TAB_ADDED,
123 content::NotificationService::AllSources());
125 delegate->ButtonClick(0);
127 // Wait for navigation to finish.
128 observer.Wait();
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());
137 delegate->ButtonClick(1);
139 // Wait for navigation to finish.
140 observer.Wait();
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());
147 // Also verify that the click dismissed the notification.
148 ASSERT_EQ(kTestNotificationId, notifier()->read_id());
151 TEST_F(ChromeNotifierDelegateTest, CloseTest) {
152 std::string id(kTestNotificationId);
153 scoped_refptr<notifier::ChromeNotifierDelegate> delegate(
154 new notifier::ChromeNotifierDelegate(id, notifier()));
156 delegate->Close(false);
157 ASSERT_EQ("", notifier()->read_id());
159 delegate->Close(true);
160 ASSERT_EQ(kTestNotificationId, notifier()->read_id());