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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/input_method/input_method_persistence.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
10 #include "chrome/browser/chromeos/language_preferences.h"
11 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
12 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
13 #include "chrome/browser/chromeos/profiles/profile_helper.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chrome/test/base/testing_profile_manager.h"
21 #include "chromeos/chromeos_switches.h"
22 #include "components/syncable_prefs/testing_pref_service_syncable.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "testing/gtest/include/gtest/gtest.h"
28 namespace input_method
{
31 const char kInputId1
[] = "xkb:us:dvorak:eng";
32 const char kInputId2
[] = "xkb:us:colemak:eng";
35 class InputMethodPersistenceTest
: public testing::Test
{
37 InputMethodPersistenceTest()
38 : mock_profile_manager_(TestingBrowserProcess::GetGlobal()),
39 fake_user_manager_(new chromeos::FakeChromeUserManager()),
40 user_manager_enabler_(fake_user_manager_
) {}
42 void SetUp() override
{
43 ASSERT_TRUE(mock_profile_manager_
.SetUp());
46 const char kTestUserName
[] = "test-user@example.com";
47 fake_user_manager_
->AddUser(kTestUserName
);
48 fake_user_manager_
->LoginUser(kTestUserName
);
50 // Create a valid profile for the user.
51 TestingProfile
* mock_profile
=
52 mock_profile_manager_
.CreateTestingProfile(kTestUserName
);
53 mock_profile_manager_
.SetLoggedIn(true);
54 EXPECT_TRUE(ProfileManager::GetActiveUserProfile() == mock_profile
);
56 mock_user_prefs_
= mock_profile
->GetTestingPrefService();
59 // Verifies that the user and system prefs contain the expected values.
60 void VerifyPrefs(const std::string
& current_input_method
,
61 const std::string
& previous_input_method
,
62 const std::string
& preferred_keyboard_layout
) {
63 EXPECT_EQ(current_input_method
,
64 mock_user_prefs_
->GetString(prefs::kLanguageCurrentInputMethod
));
65 EXPECT_EQ(previous_input_method
,
66 mock_user_prefs_
->GetString(prefs::kLanguagePreviousInputMethod
));
67 EXPECT_EQ(preferred_keyboard_layout
,
68 g_browser_process
->local_state()->GetString(
69 language_prefs::kPreferredKeyboardLayout
));
72 content::TestBrowserThreadBundle thread_bundle_
;
73 syncable_prefs::TestingPrefServiceSyncable
* mock_user_prefs_
;
74 MockInputMethodManager mock_manager_
;
75 TestingProfileManager mock_profile_manager_
;
76 chromeos::FakeChromeUserManager
* fake_user_manager_
;
77 chromeos::ScopedUserManagerEnabler user_manager_enabler_
;
80 TEST_F(InputMethodPersistenceTest
, TestLifetime
) {
82 InputMethodPersistence
persistence(&mock_manager_
);
83 EXPECT_EQ(1, mock_manager_
.add_observer_count_
);
85 EXPECT_EQ(1, mock_manager_
.remove_observer_count_
);
88 TEST_F(InputMethodPersistenceTest
, TestPrefPersistenceByState
) {
89 InputMethodPersistence
persistence(&mock_manager_
);
91 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOGIN_SCREEN
);
92 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
93 persistence
.InputMethodChanged(&mock_manager_
,
94 ProfileManager::GetActiveUserProfile(), false);
95 VerifyPrefs("", "", kInputId1
);
97 persistence
.OnSessionStateChange(InputMethodManager::STATE_BROWSER_SCREEN
);
98 mock_manager_
.SetCurrentInputMethodId(kInputId2
);
99 persistence
.InputMethodChanged(&mock_manager_
,
100 ProfileManager::GetActiveUserProfile(), false);
101 VerifyPrefs(kInputId2
, "", kInputId1
);
103 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOCK_SCREEN
);
104 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
105 persistence
.InputMethodChanged(&mock_manager_
,
106 ProfileManager::GetActiveUserProfile(), false);
107 VerifyPrefs(kInputId2
, "", kInputId1
);
109 persistence
.OnSessionStateChange(InputMethodManager::STATE_TERMINATING
);
110 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
111 persistence
.InputMethodChanged(&mock_manager_
,
112 ProfileManager::GetActiveUserProfile(), false);
113 VerifyPrefs(kInputId2
, "", kInputId1
);
115 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOGIN_SCREEN
);
116 mock_manager_
.SetCurrentInputMethodId(kInputId2
);
117 persistence
.InputMethodChanged(&mock_manager_
,
118 ProfileManager::GetActiveUserProfile(), false);
119 VerifyPrefs(kInputId2
, "", kInputId2
);
121 persistence
.OnSessionStateChange(InputMethodManager::STATE_BROWSER_SCREEN
);
122 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
123 persistence
.InputMethodChanged(&mock_manager_
,
124 ProfileManager::GetActiveUserProfile(), false);
125 VerifyPrefs(kInputId1
, kInputId2
, kInputId2
);
128 } // namespace input_method
129 } // namespace chromeos