Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / notifications / notification_test_util.cc
blobe5a56704e08788ff840541e8ad4ce9e9a7f6cc90
1 // Copyright (c) 2012 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 "chrome/browser/notifications/notification_test_util.h"
7 MockNotificationDelegate::MockNotificationDelegate(const std::string& id)
8 : id_(id) {}
10 MockNotificationDelegate::~MockNotificationDelegate() {}
12 std::string MockNotificationDelegate::id() const { return id_; }
14 // -----------------------------------------------------------------------------
16 StubNotificationUIManager::StubNotificationUIManager() {}
18 StubNotificationUIManager::~StubNotificationUIManager() {}
20 unsigned int StubNotificationUIManager::GetNotificationCount() const {
21 return notifications_.size();
24 const Notification& StubNotificationUIManager::GetNotificationAt(
25 unsigned int index) const {
26 DCHECK_GT(GetNotificationCount(), index);
27 return notifications_[index].first;
30 void StubNotificationUIManager::SetNotificationAddedCallback(
31 const base::Closure& callback) {
32 notification_added_callback_ = callback;
35 void StubNotificationUIManager::Add(const Notification& notification,
36 Profile* profile) {
37 notifications_.push_back(std::make_pair(
38 notification, NotificationUIManager::GetProfileID(profile)));
40 if (!notification_added_callback_.is_null()) {
41 notification_added_callback_.Run();
42 notification_added_callback_.Reset();
45 // Fire the Display() event on the delegate.
46 notification.delegate()->Display();
49 bool StubNotificationUIManager::Update(const Notification& notification,
50 Profile* profile) {
51 return false;
54 const Notification* StubNotificationUIManager::FindById(
55 const std::string& delegate_id,
56 ProfileID profile_id) const {
57 auto iter = notifications_.begin();
58 for (; iter != notifications_.end(); ++iter) {
59 if (iter->first.delegate_id() != delegate_id || iter->second != profile_id)
60 continue;
62 return &iter->first;
65 return nullptr;
68 bool StubNotificationUIManager::CancelById(const std::string& delegate_id,
69 ProfileID profile_id) {
70 auto iter = notifications_.begin();
71 for (; iter != notifications_.end(); ++iter) {
72 if (iter->first.delegate_id() != delegate_id ||
73 iter->second != profile_id)
74 continue;
76 iter->first.delegate()->Close(false /* by_user */);
77 notifications_.erase(iter);
78 return true;
81 return false;
84 std::set<std::string>
85 StubNotificationUIManager::GetAllIdsByProfileAndSourceOrigin(
86 ProfileID profile_id,
87 const GURL& source) {
88 std::set<std::string> delegate_ids;
89 for (const auto& pair : notifications_) {
90 if (pair.second == profile_id && pair.first.origin_url() == source)
91 delegate_ids.insert(pair.first.delegate_id());
93 return delegate_ids;
96 std::set<std::string> StubNotificationUIManager::GetAllIdsByProfile(
97 ProfileID profile_id) {
98 std::set<std::string> delegate_ids;
99 for (const auto& pair : notifications_) {
100 if (pair.second == profile_id)
101 delegate_ids.insert(pair.first.delegate_id());
103 return delegate_ids;
106 bool StubNotificationUIManager::CancelAllBySourceOrigin(
107 const GURL& source_origin) {
108 NOTIMPLEMENTED();
109 return false;
112 bool StubNotificationUIManager::CancelAllByProfile(ProfileID profile_id) {
113 NOTIMPLEMENTED();
114 return false;
117 void StubNotificationUIManager::CancelAll() {
118 for (const auto& pair : notifications_)
119 pair.first.delegate()->Close(false /* by_user */);
120 notifications_.clear();