OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/basictypes.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/shared_options_browsertest.h" |
6 #include "base/compiler_specific.h" | 6 |
7 // todo | |
7 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/chromeos/login/login_manager_test.h" | |
9 #include "chrome/browser/chromeos/login/startup_utils.h" | 9 #include "chrome/browser/chromeos/login/startup_utils.h" |
10 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" | 10 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" |
11 #include "chrome/browser/chromeos/login/users/user_manager.h" | 11 #include "chrome/browser/chromeos/login/users/user_manager.h" |
12 #include "chrome/browser/chromeos/settings/cros_settings.h" | 12 #include "chrome/browser/chromeos/settings/cros_settings.h" |
13 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 13 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
16 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
19 #include "chromeos/settings/cros_settings_names.h" | 19 #include "chromeos/settings/cros_settings_names.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
22 #include "content/public/test/test_utils.h" | 22 #include "content/public/test/test_utils.h" |
23 | 23 |
24 #include "base/bind.h" | |
25 #include "base/prefs/pref_service.h" | |
26 #include "base/values.h" | |
27 #include "chrome/browser/profiles/profile.h" | |
28 #include "chrome/browser/ui/browser.h" | |
29 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
30 #include "content/public/browser/navigation_controller.h" | |
31 #include "content/public/browser/navigation_entry.h" | |
32 #include "content/public/browser/web_contents.h" | |
33 #include "content/public/browser/web_ui.h" | |
34 #include "url/gurl.h" | |
35 | |
36 #include "content/public/test/browser_test_utils.h" | |
37 #include "content/public/test/test_utils.h" | |
38 | |
39 #include "testing/gmock/include/gmock/gmock.h" | |
40 #include "testing/gtest/include/gtest/gtest.h" | |
41 | |
42 using ::testing::AnyNumber; | |
43 using ::testing::Invoke; | |
44 using ::testing::InvokeWithoutArgs; | |
45 using ::testing::Return; | |
46 using ::testing::ReturnNull; | |
47 using ::testing::Sequence; | |
48 using ::testing::WithArg; | |
49 using ::testing::_; | |
50 | |
24 namespace chromeos { | 51 namespace chromeos { |
25 | 52 |
26 namespace { | 53 namespace { |
27 | 54 |
28 const char* kTestUsers[] = { "test-user1@gmail.com", "test-user2@gmail.com" }; | 55 const char* kTestOwner = "test-user0@example.com"; |
29 | 56 |
30 } // namespace | 57 } // namespace |
31 | 58 |
32 class SharedOptionsTest : public LoginManagerTest { | 59 SharedOptionsTest::SharedOptionsTest() |
33 public: | 60 : device_settings_provider_(NULL), |
34 SharedOptionsTest() | 61 login_manager_test_helper_(new LoginManagerTestHelper) { |
35 : LoginManagerTest(false), | 62 set_exit_when_last_browser_closes(false); |
36 device_settings_provider_(NULL) { | 63 stub_settings_provider_.Set(kDeviceOwner, base::StringValue(kTestOwner)); |
37 stub_settings_provider_.Set(kDeviceOwner, base::StringValue(kTestUsers[0])); | 64 } |
65 | |
66 SharedOptionsTest::~SharedOptionsTest() { | |
67 } | |
68 | |
69 void SharedOptionsTest::RegisterMessages() { | |
70 web_ui()->RegisterMessageCallback( | |
71 "sharedOptionsTestRegisterUsers", | |
72 base::Bind(&SharedOptionsTest::RegisterUsers, | |
73 base::Unretained(this))); | |
74 web_ui()->RegisterMessageCallback( | |
75 "sharedOptionsTestLogInUser", | |
76 base::Bind(&SharedOptionsTest::LogInUser, | |
77 base::Unretained(this))); | |
78 web_ui()->RegisterMessageCallback( | |
79 "sharedOptionsTestAddUser", | |
80 base::Bind(&SharedOptionsTest::AddUser, | |
81 base::Unretained(this))); | |
82 } | |
83 | |
84 void SharedOptionsTest::SetUpLoginDisplay() { | |
85 // for existing_user_controller.cc:login_display_.get() | |
86 /* EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_)) | |
87 .Times(1) | |
88 .WillOnce(Return(mock_login_display_));*/ | |
89 } | |
90 | |
91 void SharedOptionsTest::SetUpOnMainThread() { | |
92 /* | |
93 // Create a mock login display host so we can create an | |
94 // ExistingUserController. | |
michaelpg
2014/05/20 00:41:37
This is similar to what existing_user_controller_b
| |
95 mock_login_display_host_.reset(new MockLoginDisplayHost); | |
96 mock_login_display_ = new MockLoginDisplay(); | |
97 SetUpLoginDisplay(); | |
98 | |
99 // Create an ExistingUserController. Otherwise | |
100 // ExistingUserController::current_controller() won't exist and test fails. | |
101 // Umm, now this isn't true anymore. | |
102 // existing_user_controller_.reset( | |
michaelpg
2014/05/20 00:41:37
I'm confused, because at first the test wouldn't r
| |
103 // new ExistingUserController(mock_login_display_host_.get())); | |
104 */ | |
105 | |
106 // login_manager_test_helper_->WaitForLoginWebUI(); | |
michaelpg
2014/05/20 00:41:37
Normally a LoginManagerTest would wait for the Log
| |
107 CrosSettings* settings = CrosSettings::Get(); | |
108 device_settings_provider_ = settings->GetProvider(kDeviceOwner); | |
109 settings->RemoveSettingsProvider(device_settings_provider_); | |
110 settings->AddSettingsProvider(&stub_settings_provider_); | |
111 WebUIBrowserTest::SetUpOnMainThread(); | |
112 LOG(ERROR) << "Set up"; | |
113 } | |
114 | |
115 void SharedOptionsTest::CleanUpOnMainThread() { | |
116 existing_user_controller_.reset(); | |
117 CrosSettings* settings = CrosSettings::Get(); | |
118 settings->RemoveSettingsProvider(&stub_settings_provider_); | |
119 settings->AddSettingsProvider(device_settings_provider_); | |
120 login_manager_test_helper_->CloseLoginWebUI(); | |
121 WebUIBrowserTest::CleanUpOnMainThread(); | |
122 } | |
123 | |
124 void SharedOptionsTest::SetUpCommandLine(CommandLine* command_line) { | |
125 // Don't add kLoginManager switches, or running PRE_ test will fail with no | |
126 // error message. | |
127 // login_manager_test_helper_->SetUpCommandLine(command_line); | |
michaelpg
2014/05/20 00:41:37
This would add kLoginManager and kForceLoginManage
Nikita (slow)
2014/05/20 12:50:35
As far as I remember the issue with WebUIBrowserTe
| |
128 command_line->AppendSwitch(::switches::kMultiProfiles); | |
129 WebUIBrowserTest::SetUpCommandLine(command_line); | |
130 } | |
131 | |
132 void SharedOptionsTest::SetUpInProcessBrowserTestFixture() { | |
133 LOG(ERROR) << "Setting up in process browser test fixture"; | |
134 login_manager_test_helper_->SetUpLoginUtils(false /* ? */); | |
135 } | |
136 | |
137 void SharedOptionsTest::RegisterUsers(const base::ListValue* list_value) { | |
138 LOG(ERROR) << "Register users called"; | |
139 int num_users; | |
140 ASSERT_TRUE(list_value->GetInteger(0, &num_users)); | |
141 | |
142 LOG(ERROR) << "num users: " << num_users;; | |
143 | |
144 char user_name[28]; | |
145 for (int i = 0; i < num_users; i++) { | |
146 sprintf(user_name, "test-user%d@example.com", i); | |
147 login_manager_test_helper_->RegisterUser(user_name); | |
148 LOG(ERROR) << "Registered " << user_name; | |
38 } | 149 } |
39 | 150 |
40 virtual ~SharedOptionsTest() { | |
41 } | |
42 | |
43 virtual void SetUpOnMainThread() OVERRIDE { | |
44 LoginManagerTest::SetUpOnMainThread(); | |
45 CrosSettings* settings = CrosSettings::Get(); | |
46 device_settings_provider_ = settings->GetProvider(kDeviceOwner); | |
47 settings->RemoveSettingsProvider(device_settings_provider_); | |
48 settings->AddSettingsProvider(&stub_settings_provider_); | |
49 } | |
50 | |
51 virtual void CleanUpOnMainThread() OVERRIDE { | |
52 CrosSettings* settings = CrosSettings::Get(); | |
53 settings->RemoveSettingsProvider(&stub_settings_provider_); | |
54 settings->AddSettingsProvider(device_settings_provider_); | |
55 LoginManagerTest::CleanUpOnMainThread(); | |
56 } | |
57 | |
58 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
59 LoginManagerTest::SetUpCommandLine(command_line); | |
60 command_line->AppendSwitch(::switches::kMultiProfiles); | |
61 } | |
62 | |
63 protected: | |
64 void CheckOptionsUI(const User* user, bool is_primary) { | |
65 Profile* profile = UserManager::Get()->GetProfileByUser(user); | |
66 profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | |
67 user->email()); | |
68 | |
69 ui_test_utils::BrowserAddedObserver observer; | |
70 Browser* browser = CreateBrowser(profile); | |
71 observer.WaitForSingleNewBrowser(); | |
72 | |
73 ui_test_utils::NavigateToURL(browser, | |
74 GURL("chrome://settings-frame")); | |
75 content::WebContents* contents = | |
76 browser->tab_strip_model()->GetActiveWebContents(); | |
77 | |
78 bool banner_visible; | |
79 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
80 contents, | |
81 "var e = document.getElementById('secondary-user-banner');" | |
82 "var visible = e.offsetWidth > 0 && e.offsetHeight > 0;" | |
83 "window.domAutomationController.send(visible);", | |
84 &banner_visible)); | |
85 EXPECT_EQ(is_primary, !banner_visible); | |
86 } | |
87 | |
88 StubCrosSettingsProvider stub_settings_provider_; | |
89 CrosSettingsProvider* device_settings_provider_; | |
90 | |
91 private: | |
92 DISALLOW_COPY_AND_ASSIGN(SharedOptionsTest); | |
93 }; | |
94 | |
95 IN_PROC_BROWSER_TEST_F(SharedOptionsTest, PRE_SharedOptions) { | |
96 RegisterUser(kTestUsers[0]); | |
97 RegisterUser(kTestUsers[1]); | |
98 StartupUtils::MarkOobeCompleted(); | 151 StartupUtils::MarkOobeCompleted(); |
152 | |
153 web_ui()->CallJavascriptFunction( | |
154 "SharedOptionsTest.registerUsersCallback"); | |
99 } | 155 } |
100 | 156 |
101 IN_PROC_BROWSER_TEST_F(SharedOptionsTest, SharedOptions) { | 157 void SharedOptionsTest::LogInUser(const base::ListValue* list_value) { |
102 LoginUser(kTestUsers[0]); | 158 int user_num; |
159 ASSERT_TRUE(list_value->GetInteger(0, &user_num)); | |
160 | |
161 char user_name[28]; | |
162 sprintf(user_name, "test-user%d@example.com", user_num); | |
163 login_manager_test_helper_->LogInUser(user_name); | |
164 | |
103 UserAddingScreen::Get()->Start(); | 165 UserAddingScreen::Get()->Start(); |
104 content::RunAllPendingInMessageLoop(); | 166 content::RunAllPendingInMessageLoop(); |
105 AddUser(kTestUsers[1]); | |
106 | 167 |
107 UserManager* manager = UserManager::Get(); | 168 web_ui()->CallJavascriptFunction( |
108 ASSERT_EQ(2u, manager->GetLoggedInUsers().size()); | 169 "SharedOptionsTest.logInUserCallback"); |
170 } | |
109 | 171 |
110 CheckOptionsUI(manager->FindUser(kTestUsers[0]), true /* is_primary */); | 172 void SharedOptionsTest::AddUser(const base::ListValue* list_value) { |
111 CheckOptionsUI(manager->FindUser(kTestUsers[1]), false /* is_primary */); | 173 int user_num; |
174 LOG(ERROR) << "Adding user"; | |
175 ASSERT_TRUE(list_value->GetInteger(0, &user_num)); | |
176 | |
177 char user_name[28]; | |
178 sprintf(user_name, "test-user%d@example.com", user_num); | |
179 LOG(ERROR) << "Adding user " << user_name; | |
180 | |
181 login_manager_test_helper_->AddUser(user_name); | |
michaelpg
2014/05/20 00:41:37
This is where the test is failing now.
| |
182 | |
183 // There are 3 of these lines output. | |
184 // I don't know if that's causing this test to fail: | |
185 // [22018:22018:0519/172540:INFO:CONSOLE(1)] "Uncaught ReferenceError: | |
186 // cr is not defined", source: (1) | |
187 | |
188 LOG(ERROR) << "Calling addUserCallback"; | |
189 web_ui()->CallJavascriptFunction( | |
190 "SharedOptionsTest.addUserCallback"); | |
191 } | |
192 | |
193 content::WebUIMessageHandler* SharedOptionsTest::GetMockMessageHandler() { | |
194 return this; | |
112 } | 195 } |
113 | 196 |
114 } // namespace chromeos | 197 } // namespace chromeos |
OLD | NEW |