1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/file_manager/desktop_notifications.h"
#include <gtest/gtest.h>
#include <string>
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
namespace file_manager {
// Add "FileManager" prefix to the class to avoid name conflicts.
class FileManagerDesktopNotificationsBrowserTest : public InProcessBrowserTest {
public:
FileManagerDesktopNotificationsBrowserTest() {}
virtual void CleanUpOnMainThread() OVERRIDE {
notifications_.reset();
}
protected:
// This must be initialized late in test startup.
void InitNotifications() {
Profile* profile = browser()->profile();
notifications_.reset(new DesktopNotifications(profile));
}
bool FindNotification(const std::string& id) {
return notifications_->HasNotificationForTest(id);
}
scoped_ptr<DesktopNotifications> notifications_;
};
IN_PROC_BROWSER_TEST_F(FileManagerDesktopNotificationsBrowserTest, TestBasic) {
InitNotifications();
// Showing a notification adds a new notification.
notifications_->ShowNotification(DesktopNotifications::DEVICE, "path");
EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("Device_path"));
// Updating the same notification maintains the same count.
notifications_->ShowNotification(DesktopNotifications::DEVICE, "path");
EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("Device_path"));
// A new notification increases the count.
notifications_->ShowNotification(DesktopNotifications::DEVICE_FAIL,
"path");
EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("DeviceFail_path"));
EXPECT_TRUE(FindNotification("Device_path"));
// Hiding a notification removes it from our data.
notifications_->HideNotification(DesktopNotifications::DEVICE_FAIL,
"path");
EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
EXPECT_FALSE(FindNotification("DeviceFail_path"));
EXPECT_TRUE(FindNotification("Device_path"));
};
// Note: Delayed tests use a delay time of 0 so that tasks wille execute
// when RunAllPendingInMessageLoop() is called.
IN_PROC_BROWSER_TEST_F(FileManagerDesktopNotificationsBrowserTest,
ShowDelayedTest) {
InitNotifications();
// Adding a delayed notification does not create a notification.
notifications_->ShowNotificationDelayed(DesktopNotifications::DEVICE,
"path",
base::TimeDelta::FromSeconds(0));
EXPECT_EQ(0u, notifications_->GetNotificationCountForTest());
EXPECT_FALSE(FindNotification("Device_path"));
// Running the message loop should create the notification.
content::RunAllPendingInMessageLoop();
EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("Device_path"));
// Showing a notification both immediately and delayed results in one
// additional notification.
notifications_->ShowNotificationDelayed(DesktopNotifications::DEVICE_FAIL,
"path",
base::TimeDelta::FromSeconds(0));
notifications_->ShowNotification(DesktopNotifications::DEVICE_FAIL,
"path");
EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("DeviceFail_path"));
// When the delayed notification is processed, it's an update, so we still
// only have two notifications.
content::RunAllPendingInMessageLoop();
EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("DeviceFail_path"));
// If we schedule a show for later, then hide before it becomes visible,
// the notification should not be added.
notifications_->ShowNotificationDelayed(DesktopNotifications::FORMAT_FAIL,
"path",
base::TimeDelta::FromSeconds(0));
notifications_->HideNotification(DesktopNotifications::FORMAT_FAIL,
"path");
EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("Device_path"));
EXPECT_TRUE(FindNotification("DeviceFail_path"));
EXPECT_FALSE(FindNotification("Format_path"));
// Even after processing messages, no new notification should be added.
content::RunAllPendingInMessageLoop();
EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("Device_path"));
EXPECT_TRUE(FindNotification("DeviceFail_path"));
EXPECT_FALSE(FindNotification("Format_path"));
}
IN_PROC_BROWSER_TEST_F(FileManagerDesktopNotificationsBrowserTest,
HideDelayedTest) {
InitNotifications();
// Showing now, and scheduling a hide for later, results in one notification.
notifications_->ShowNotification(DesktopNotifications::DEVICE, "path");
notifications_->HideNotificationDelayed(DesktopNotifications::DEVICE,
"path",
base::TimeDelta::FromSeconds(0));
EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
EXPECT_TRUE(FindNotification("Device_path"));
// Running pending messges should remove the notification.
content::RunAllPendingInMessageLoop();
EXPECT_EQ(0u, notifications_->GetNotificationCountForTest());
// Immediate show then hide results in no notification.
notifications_->ShowNotification(DesktopNotifications::DEVICE_FAIL,
"path");
notifications_->HideNotification(DesktopNotifications::DEVICE_FAIL,
"path");
content::RunAllPendingInMessageLoop();
EXPECT_EQ(0u, notifications_->GetNotificationCountForTest());
// Delayed hide for a notification that doesn't exist does nothing.
notifications_->HideNotificationDelayed(DesktopNotifications::DEVICE_FAIL,
"path",
base::TimeDelta::FromSeconds(0));
content::RunAllPendingInMessageLoop();
EXPECT_EQ(0u, notifications_->GetNotificationCountForTest());
}
// Tests that showing two notifications with the same notification ids and
// different messages results in showing the second notification's message.
// This situation can be encountered while showing notifications for
// MountCompletedEvent.
IN_PROC_BROWSER_TEST_F(FileManagerDesktopNotificationsBrowserTest,
IdenticalNotificationIds) {
InitNotifications();
notifications_->RegisterDevice("path");
notifications_->ManageNotificationsOnMountCompleted("path", "", false, false,
false);
content::RunAllPendingInMessageLoop();
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_DEVICE_UNKNOWN_DEFAULT_MESSAGE),
notifications_->GetNotificationMessageForTest("DeviceFail_path"));
notifications_->ManageNotificationsOnMountCompleted("path", "", false, false,
false);
content::RunAllPendingInMessageLoop();
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_MULTIPART_DEVICE_UNSUPPORTED_DEFAULT_MESSAGE),
notifications_->GetNotificationMessageForTest("DeviceFail_path"));
notifications_->UnregisterDevice("path");
}
} // namespace file_manager.
|