Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / login_state_notification_blocker_chromeos_browsertest.cc
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 "base/command_line.h"
8 #include "chrome/browser/chromeos/login/login_manager_test.h"
9 #include "chrome/browser/chromeos/login/startup_utils.h"
10 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
11 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
12 #include "content/public/test/test_utils.h"
13 #include "ui/message_center/message_center.h"
14
15 using namespace testing;
16
17 namespace {
18
19 const char* kTestUsers[] = {"test-user@gmail.com",
20                             "test-user1@gmail.com"};
21
22 }  // anonymous namespace
23
24 class LoginStateNotificationBlockerChromeOSBrowserTest
25     : public chromeos::LoginManagerTest,
26       public message_center::NotificationBlocker::Observer {
27  public:
28   LoginStateNotificationBlockerChromeOSBrowserTest()
29       : chromeos::LoginManagerTest(false),
30         state_changed_count_(0) {}
31   virtual ~LoginStateNotificationBlockerChromeOSBrowserTest() {}
32
33   virtual void SetUpOnMainThread() override {
34     chromeos::LoginState::Get()->set_always_logged_in(false);
35     chromeos::LoginManagerTest::SetUpOnMainThread();
36   }
37
38   virtual void TearDownOnMainThread() override {
39     if (blocker_)
40       blocker_->RemoveObserver(this);
41     blocker_.reset();
42     chromeos::LoginManagerTest::TearDownOnMainThread();
43   }
44
45  protected:
46   void CreateBlocker() {
47     blocker_.reset(new LoginStateNotificationBlockerChromeOS(
48         message_center::MessageCenter::Get()));
49     blocker_->AddObserver(this);
50   }
51
52   // message_center::NotificationBlocker::Observer ovverrides:
53   virtual void OnBlockingStateChanged(
54       message_center::NotificationBlocker* blocker) override {
55     state_changed_count_++;
56   }
57
58   int GetStateChangedCountAndReset() {
59     int result = state_changed_count_;
60     state_changed_count_ = 0;
61     return result;
62   }
63
64   bool ShouldShowNotificationAsPopup(
65       const message_center::NotifierId& notifier_id) {
66     return blocker_->ShouldShowNotificationAsPopup(notifier_id);
67   }
68
69  private:
70   int state_changed_count_;
71   scoped_ptr<message_center::NotificationBlocker> blocker_;
72
73   DISALLOW_COPY_AND_ASSIGN(LoginStateNotificationBlockerChromeOSBrowserTest);
74 };
75
76 IN_PROC_BROWSER_TEST_F(LoginStateNotificationBlockerChromeOSBrowserTest,
77                        PRE_BaseTest) {
78   RegisterUser(kTestUsers[0]);
79   RegisterUser(kTestUsers[1]);
80   chromeos::StartupUtils::MarkOobeCompleted();
81 }
82
83  // Disabled due to flakiness. See: http://crbug.com/421325.
84 IN_PROC_BROWSER_TEST_F(LoginStateNotificationBlockerChromeOSBrowserTest,
85                        DISABLED_BaseTest) {
86   CreateBlocker();
87   message_center::NotifierId notifier_id(
88       message_center::NotifierId::APPLICATION, "test-notifier");
89
90   // Logged in as a normal user.
91   EXPECT_CALL(login_utils(), DoBrowserLaunch(_, _)).Times(1);
92   LoginUser(kTestUsers[0]);
93   EXPECT_EQ(1, GetStateChangedCountAndReset());
94   EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
95
96   // Multi-login user switch.
97   chromeos::UserAddingScreen::Get()->Start();
98   content::RunAllPendingInMessageLoop();
99   EXPECT_EQ(1, GetStateChangedCountAndReset());
100   EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
101
102   // Multi-login user switch off.
103   chromeos::UserAddingScreen::Get()->Cancel();
104   content::RunAllPendingInMessageLoop();
105   EXPECT_EQ(1, GetStateChangedCountAndReset());
106   EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
107 }
108
109 IN_PROC_BROWSER_TEST_F(LoginStateNotificationBlockerChromeOSBrowserTest,
110                        PRE_AlwaysAllowedNotifier) {
111   RegisterUser(kTestUsers[0]);
112   RegisterUser(kTestUsers[1]);
113   chromeos::StartupUtils::MarkOobeCompleted();
114 }
115
116 IN_PROC_BROWSER_TEST_F(LoginStateNotificationBlockerChromeOSBrowserTest,
117                        AlwaysAllowedNotifier) {
118   CreateBlocker();
119
120   // NOTIFIER_DISPLAY is allowed to shown in the login screen.
121   message_center::NotifierId notifier_id(
122       message_center::NotifierId::SYSTEM_COMPONENT,
123       ash::system_notifier::kNotifierDisplay);
124
125   // Logged in as a normal user.
126   EXPECT_CALL(login_utils(), DoBrowserLaunch(_, _)).Times(1);
127   LoginUser(kTestUsers[0]);
128   EXPECT_EQ(1, GetStateChangedCountAndReset());
129   EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
130
131   // Multi-login user switch.
132   chromeos::UserAddingScreen::Get()->Start();
133   content::RunAllPendingInMessageLoop();
134   EXPECT_EQ(1, GetStateChangedCountAndReset());
135   EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
136
137   // Multi-login user switch off.
138   chromeos::UserAddingScreen::Get()->Cancel();
139   content::RunAllPendingInMessageLoop();
140   EXPECT_EQ(1, GetStateChangedCountAndReset());
141   EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
142 }