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_pref_service_syncable.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "chrome/test/base/testing_profile_manager.h"
22 #include "chromeos/chromeos_switches.h"
24 #include "testing/gtest/include/gtest/gtest.h"
27 namespace input_method
{
30 const char kInputId1
[] = "xkb:us:dvorak:eng";
31 const char kInputId2
[] = "xkb:us:colemak:eng";
34 class InputMethodPersistenceTest
: public testing::Test
{
36 InputMethodPersistenceTest()
37 : mock_profile_manager_(TestingBrowserProcess::GetGlobal()),
38 fake_user_manager_(new chromeos::FakeChromeUserManager()),
39 user_manager_enabler_(fake_user_manager_
) {}
41 void SetUp() override
{
42 ASSERT_TRUE(mock_profile_manager_
.SetUp());
45 const char kTestUserName
[] = "test-user@example.com";
46 fake_user_manager_
->AddUser(kTestUserName
);
47 fake_user_manager_
->LoginUser(kTestUserName
);
49 // Create a valid profile for the user.
50 TestingProfile
* mock_profile
=
51 mock_profile_manager_
.CreateTestingProfile(kTestUserName
);
52 mock_profile_manager_
.SetLoggedIn(true);
53 EXPECT_TRUE(ProfileManager::GetActiveUserProfile() == mock_profile
);
55 mock_user_prefs_
= mock_profile
->GetTestingPrefService();
58 // Verifies that the user and system prefs contain the expected values.
59 void VerifyPrefs(const std::string
& current_input_method
,
60 const std::string
& previous_input_method
,
61 const std::string
& preferred_keyboard_layout
) {
62 EXPECT_EQ(current_input_method
,
63 mock_user_prefs_
->GetString(prefs::kLanguageCurrentInputMethod
));
64 EXPECT_EQ(previous_input_method
,
65 mock_user_prefs_
->GetString(prefs::kLanguagePreviousInputMethod
));
66 EXPECT_EQ(preferred_keyboard_layout
,
67 g_browser_process
->local_state()->GetString(
68 language_prefs::kPreferredKeyboardLayout
));
71 TestingPrefServiceSyncable
* mock_user_prefs_
;
72 MockInputMethodManager mock_manager_
;
73 TestingProfileManager mock_profile_manager_
;
74 chromeos::FakeChromeUserManager
* fake_user_manager_
;
75 chromeos::ScopedUserManagerEnabler user_manager_enabler_
;
78 TEST_F(InputMethodPersistenceTest
, TestLifetime
) {
80 InputMethodPersistence
persistence(&mock_manager_
);
81 EXPECT_EQ(1, mock_manager_
.add_observer_count_
);
83 EXPECT_EQ(1, mock_manager_
.remove_observer_count_
);
86 TEST_F(InputMethodPersistenceTest
, TestPrefPersistenceByState
) {
87 InputMethodPersistence
persistence(&mock_manager_
);
89 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOGIN_SCREEN
);
90 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
91 persistence
.InputMethodChanged(&mock_manager_
, false);
92 VerifyPrefs("", "", kInputId1
);
94 persistence
.OnSessionStateChange(InputMethodManager::STATE_BROWSER_SCREEN
);
95 mock_manager_
.SetCurrentInputMethodId(kInputId2
);
96 persistence
.InputMethodChanged(&mock_manager_
, false);
97 VerifyPrefs(kInputId2
, "", kInputId1
);
99 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOCK_SCREEN
);
100 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
101 persistence
.InputMethodChanged(&mock_manager_
, false);
102 VerifyPrefs(kInputId2
, "", kInputId1
);
104 persistence
.OnSessionStateChange(InputMethodManager::STATE_TERMINATING
);
105 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
106 persistence
.InputMethodChanged(&mock_manager_
, false);
107 VerifyPrefs(kInputId2
, "", kInputId1
);
109 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOGIN_SCREEN
);
110 mock_manager_
.SetCurrentInputMethodId(kInputId2
);
111 persistence
.InputMethodChanged(&mock_manager_
, false);
112 VerifyPrefs(kInputId2
, "", kInputId2
);
114 persistence
.OnSessionStateChange(InputMethodManager::STATE_BROWSER_SCREEN
);
115 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
116 persistence
.InputMethodChanged(&mock_manager_
, false);
117 VerifyPrefs(kInputId1
, kInputId2
, kInputId2
);
120 } // namespace input_method
121 } // namespace chromeos