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_prompt_test_utils.h"
6
7#include "chrome/browser/ui/login/login_prompt.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10LoginPromptBrowserTestObserver::LoginPromptBrowserTestObserver()
11    : auth_needed_count_(0), auth_supplied_count_(0), auth_cancelled_count_(0) {
12}
13
14LoginPromptBrowserTestObserver::~LoginPromptBrowserTestObserver() {
15}
16
17void 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
35void LoginPromptBrowserTestObserver::AddHandler(LoginHandler* handler) {
36  std::list<LoginHandler*>::iterator i =
37      std::find(handlers_.begin(), handlers_.end(), handler);
38  // Cannot use ASSERT_EQ, because gTest on Android confuses iterators with
39  // containers.
40  ASSERT_TRUE(i == handlers_.end());
41  handlers_.push_back(handler);
42}
43
44void LoginPromptBrowserTestObserver::RemoveHandler(LoginHandler* handler) {
45  std::list<LoginHandler*>::iterator i =
46      std::find(handlers_.begin(), handlers_.end(), handler);
47  // Cannot use ASSERT_NE, because gTest on Android confuses iterators with
48  // containers.
49  ASSERT_TRUE(i != handlers_.end());
50  handlers_.erase(i);
51}
52
53void 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
60WindowedLoadStopObserver::WindowedLoadStopObserver(
61    content::NavigationController* controller,
62    int notification_count)
63    : WindowedNavigationObserver<content::NOTIFICATION_LOAD_STOP>(controller),
64      remaining_notification_count_(notification_count) {
65  // This should really be an ASSERT, if those were allowed in a method which
66  // does not return void.
67  EXPECT_LE(0, remaining_notification_count_);
68}
69
70void WindowedLoadStopObserver::Observe(
71    int type,
72    const content::NotificationSource& source,
73    const content::NotificationDetails& details) {
74  ASSERT_LT(0, remaining_notification_count_);
75  if (--remaining_notification_count_ == 0)
76    WindowedNotificationObserver::Observe(type, source, details);
77}
78