OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | 5 #include "chrome/browser/ui/webui/signin/login_ui_service.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 class TestLoginUI : public LoginUIService::LoginUI { | 12 class TestLoginUI : public LoginUIService::LoginUI { |
13 public: | 13 public: |
14 TestLoginUI() { } | 14 TestLoginUI() { } |
15 ~TestLoginUI() override {} | 15 ~TestLoginUI() override {} |
16 void FocusUI() override {} | 16 void FocusUI() override {} |
17 void CloseUI() override {} | |
18 | 17 |
19 private: | 18 private: |
20 DISALLOW_COPY_AND_ASSIGN(TestLoginUI); | 19 DISALLOW_COPY_AND_ASSIGN(TestLoginUI); |
21 }; | 20 }; |
22 | 21 |
23 class TestObserver : public LoginUIService::Observer { | 22 TEST(LoginUIServiceTest, CanSetMultipleLoginUIs) { |
24 public: | 23 LoginUIService service(nullptr); |
25 TestObserver() : ui_shown_count_(0), | |
26 ui_closed_count_(0) { | |
27 } | |
28 | 24 |
29 int ui_shown_count() const { return ui_shown_count_; } | 25 EXPECT_EQ(nullptr, service.current_login_ui()); |
30 int ui_closed_count() const { return ui_closed_count_; } | |
31 | 26 |
32 private: | 27 TestLoginUI ui; |
33 void OnLoginUIShown(LoginUIService::LoginUI* ui) override { | 28 service.SetLoginUI(&ui); |
34 ++ui_shown_count_; | 29 EXPECT_EQ(&ui, service.current_login_ui()); |
35 } | |
36 | 30 |
37 void OnLoginUIClosed(LoginUIService::LoginUI* ui) override { | 31 // Test that we can replace the active login UI. |
38 ++ui_closed_count_; | 32 TestLoginUI other_ui; |
39 } | 33 service.SetLoginUI(&other_ui); |
| 34 EXPECT_EQ(&other_ui, service.current_login_ui()); |
40 | 35 |
41 int ui_shown_count_; | 36 // Test that closing the non-active login UI has no effect. |
42 int ui_closed_count_; | 37 service.LoginUIClosed(&ui); |
| 38 EXPECT_EQ(&other_ui, service.current_login_ui()); |
43 | 39 |
44 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 40 // Test that closing the foreground UI yields the background UI. |
45 }; | 41 service.SetLoginUI(&ui); |
| 42 EXPECT_EQ(&ui, service.current_login_ui()); |
| 43 service.LoginUIClosed(&ui); |
| 44 EXPECT_EQ(&other_ui, service.current_login_ui()); |
46 | 45 |
47 class LoginUIServiceTest : public testing::Test { | 46 // Test that closing the last login UI makes the current login UI nullptr. |
48 public: | 47 service.LoginUIClosed(&other_ui); |
49 LoginUIServiceTest() : service_(NULL) { } | 48 EXPECT_EQ(nullptr, service.current_login_ui()); |
50 ~LoginUIServiceTest() override {} | |
51 | |
52 protected: | |
53 LoginUIService service_; | |
54 TestObserver observer_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(LoginUIServiceTest); | |
57 }; | |
58 | |
59 // Test that the observer is called back when login UI is shown | |
60 // or closed. | |
61 TEST_F(LoginUIServiceTest, LoginUIServiceObserver) { | |
62 service_.AddObserver(&observer_); | |
63 EXPECT_EQ(NULL, service_.current_login_ui()); | |
64 TestLoginUI ui; | |
65 service_.SetLoginUI(&ui); | |
66 EXPECT_EQ(1, observer_.ui_shown_count()); | |
67 | |
68 // If a different UI is closed than the one that was shown, no | |
69 // notification should be fired. | |
70 TestLoginUI other_ui; | |
71 service_.LoginUIClosed(&other_ui); | |
72 EXPECT_EQ(1, observer_.ui_shown_count()); | |
73 | |
74 // If the right UI is closed, notification should be fired. | |
75 service_.LoginUIClosed(&ui); | |
76 EXPECT_EQ(1, observer_.ui_closed_count()); | |
77 service_.RemoveObserver(&observer_); | |
78 } | 49 } |
OLD | NEW |