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.
4
5#include "ash/shell.h"
6#include "ash/system/system_notifier.h"
7#include "ash/test/ash_test_base.h"
8#include "base/command_line.h"
9#include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
10#include "chromeos/login/login_state.h"
11#include "ui/message_center/message_center.h"
12#include "ui/message_center/notification.h"
13
14class LoginStateNotificationBlockerChromeOSTest
15    : public ash::test::AshTestBase,
16      public message_center::NotificationBlocker::Observer {
17 public:
18  LoginStateNotificationBlockerChromeOSTest()
19      : state_changed_count_(0) {}
20  virtual ~LoginStateNotificationBlockerChromeOSTest() {}
21
22  // ash::tests::AshTestBase overrides:
23  virtual void SetUp() OVERRIDE {
24    chromeos::LoginState::Initialize();
25    chromeos::LoginState::Get()->set_always_logged_in(false);
26    ash::test::AshTestBase::SetUp();
27    blocker_.reset(new LoginStateNotificationBlockerChromeOS(
28        message_center::MessageCenter::Get()));
29    blocker_->AddObserver(this);
30  }
31
32  virtual void TearDown() OVERRIDE {
33    blocker_->RemoveObserver(this);
34    blocker_.reset();
35    ash::test::AshTestBase::TearDown();
36    chromeos::LoginState::Shutdown();
37  }
38
39  // message_center::NotificationBlocker::Observer overrides:
40  virtual void OnBlockingStateChanged(
41      message_center::NotificationBlocker* blocker) OVERRIDE {
42    state_changed_count_++;
43  }
44
45  int GetStateChangedCountAndReset() {
46    int result = state_changed_count_;
47    state_changed_count_ = 0;
48    return result;
49  }
50
51  bool ShouldShowNotificationAsPopup(
52      const message_center::NotifierId& notifier_id) {
53    return blocker_->ShouldShowNotificationAsPopup(notifier_id);
54  }
55
56 private:
57  int state_changed_count_;
58  scoped_ptr<message_center::NotificationBlocker> blocker_;
59
60  DISALLOW_COPY_AND_ASSIGN(LoginStateNotificationBlockerChromeOSTest);
61};
62
63TEST_F(LoginStateNotificationBlockerChromeOSTest, BaseTest) {
64  // Default status: OOBE.
65  message_center::NotifierId notifier_id(
66      message_center::NotifierId::APPLICATION, "test-notifier");
67  EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
68
69  // Login screen.
70  chromeos::LoginState::Get()->SetLoggedInState(
71      chromeos::LoginState::LOGGED_IN_NONE,
72      chromeos::LoginState::LOGGED_IN_USER_NONE);
73  EXPECT_EQ(0, GetStateChangedCountAndReset());
74  EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
75
76  // Logged in as a normal user.
77  chromeos::LoginState::Get()->SetLoggedInState(
78      chromeos::LoginState::LOGGED_IN_ACTIVE,
79      chromeos::LoginState::LOGGED_IN_USER_REGULAR);
80  EXPECT_EQ(1, GetStateChangedCountAndReset());
81  EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
82
83  // Lock.
84  ash::Shell::GetInstance()->OnLockStateChanged(true);
85  EXPECT_EQ(1, GetStateChangedCountAndReset());
86  EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
87
88  // Unlock.
89  ash::Shell::GetInstance()->OnLockStateChanged(false);
90  EXPECT_EQ(1, GetStateChangedCountAndReset());
91  EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
92}
93
94TEST_F(LoginStateNotificationBlockerChromeOSTest, AlwaysAllowedNotifier) {
95  // NOTIFIER_DISPLAY is allowed to shown in the login screen.
96  message_center::NotifierId notifier_id(
97      message_center::NotifierId::SYSTEM_COMPONENT,
98      ash::system_notifier::kNotifierDisplay);
99
100  // Default status: OOBE.
101  EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
102
103  // Login screen.
104  chromeos::LoginState::Get()->SetLoggedInState(
105      chromeos::LoginState::LOGGED_IN_NONE,
106      chromeos::LoginState::LOGGED_IN_USER_NONE);
107  EXPECT_EQ(0, GetStateChangedCountAndReset());
108  EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
109
110  // Logged in as a normal user.
111  chromeos::LoginState::Get()->SetLoggedInState(
112      chromeos::LoginState::LOGGED_IN_ACTIVE,
113      chromeos::LoginState::LOGGED_IN_USER_REGULAR);
114  EXPECT_EQ(1, GetStateChangedCountAndReset());
115  EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
116
117  // Lock.
118  ash::Shell::GetInstance()->OnLockStateChanged(true);
119  EXPECT_EQ(1, GetStateChangedCountAndReset());
120  EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
121
122  // Unlock.
123  ash::Shell::GetInstance()->OnLockStateChanged(false);
124  EXPECT_EQ(1, GetStateChangedCountAndReset());
125  EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
126}
127