1 // Copyright 2014 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 "chrome/browser/ui/login/login_handler_test_utils.h"
6
7 #include "chrome/browser/ui/login/login_handler.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
LoginPromptBrowserTestObserver()10 LoginPromptBrowserTestObserver::LoginPromptBrowserTestObserver()
11 : auth_needed_count_(0), auth_supplied_count_(0), auth_cancelled_count_(0) {
12 }
13
~LoginPromptBrowserTestObserver()14 LoginPromptBrowserTestObserver::~LoginPromptBrowserTestObserver() {
15 }
16
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)17 void LoginPromptBrowserTestObserver::Observe(
18 int type,
19 const content::NotificationSource& source,
20 const content::NotificationDetails& details) {
21 if (type == chrome::NOTIFICATION_AUTH_NEEDED) {
22 AddHandler(content::Details<LoginNotificationDetails>(details)->handler());
23 auth_needed_count_++;
24 } else if (type == chrome::NOTIFICATION_AUTH_SUPPLIED) {
25 RemoveHandler(content::Details<AuthSuppliedLoginNotificationDetails>(
26 details)->handler());
27 auth_supplied_count_++;
28 } else if (type == chrome::NOTIFICATION_AUTH_CANCELLED) {
29 RemoveHandler(
30 content::Details<LoginNotificationDetails>(details)->handler());
31 auth_cancelled_count_++;
32 }
33 }
34
AddHandler(LoginHandler * handler)35 void LoginPromptBrowserTestObserver::AddHandler(LoginHandler* handler) {
36 ASSERT_FALSE(base::Contains(handlers_, handler));
37 handlers_.push_back(handler);
38 }
39
RemoveHandler(LoginHandler * handler)40 void LoginPromptBrowserTestObserver::RemoveHandler(LoginHandler* handler) {
41 auto i = std::find(handlers_.begin(), handlers_.end(), handler);
42 // Cannot use ASSERT_NE, because gTest on Android confuses iterators with
43 // containers.
44 //
45 // TODO(davidben): NOTIFICATION_AUTH_SUPPLIED and NOTIFICATION_AUTH_CANCELLED
46 // are not quite guaranteed to come after NOTIFICATION_AUTH_NEEDED. Either
47 // remove this assumption from the test class or fix things so this assumption
48 // holds.
49 ASSERT_TRUE(i != handlers_.end());
50 handlers_.erase(i);
51 }
52
Register(const content::NotificationSource & source)53 void LoginPromptBrowserTestObserver::Register(
54 const content::NotificationSource& source) {
55 registrar_.Add(this, chrome::NOTIFICATION_AUTH_NEEDED, source);
56 registrar_.Add(this, chrome::NOTIFICATION_AUTH_SUPPLIED, source);
57 registrar_.Add(this, chrome::NOTIFICATION_AUTH_CANCELLED, source);
58 }
59
UnregisterAll()60 void LoginPromptBrowserTestObserver::UnregisterAll() {
61 registrar_.RemoveAll();
62 }
63
WindowedLoadStopObserver(content::NavigationController * controller,int notification_count)64 WindowedLoadStopObserver::WindowedLoadStopObserver(
65 content::NavigationController* controller,
66 int notification_count)
67 : WindowedNavigationObserver<content::NOTIFICATION_LOAD_STOP>(controller),
68 remaining_notification_count_(notification_count) {
69 // This should really be an ASSERT, if those were allowed in a method which
70 // does not return void.
71 EXPECT_LE(0, remaining_notification_count_);
72 }
73
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)74 void WindowedLoadStopObserver::Observe(
75 int type,
76 const content::NotificationSource& source,
77 const content::NotificationDetails& details) {
78 ASSERT_LT(0, remaining_notification_count_);
79 if (--remaining_notification_count_ == 0)
80 WindowedNotificationObserver::Observe(type, source, details);
81 }
82