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/profiles/profile_manager.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "chromeos/chromeos_switches.h"
21 #include "testing/gtest/include/gtest/gtest.h"
24 namespace input_method
{
27 const char kInputId1
[] = "xkb:us:dvorak:eng";
28 const char kInputId2
[] = "xkb:us:colemak:eng";
31 class InputMethodPersistenceTest
: public testing::Test
{
33 InputMethodPersistenceTest()
34 : mock_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
36 virtual void SetUp() OVERRIDE
{
37 // Set up a valid profile for our test.
38 ASSERT_TRUE(mock_profile_manager_
.SetUp());
39 TestingProfile
* mock_profile
=
40 mock_profile_manager_
.CreateTestingProfile(chrome::kTestUserProfileDir
);
41 CommandLine
*cl
= CommandLine::ForCurrentProcess();
42 cl
->AppendSwitchASCII(switches::kLoginProfile
, chrome::kTestUserProfileDir
);
43 mock_profile_manager_
.SetLoggedIn(true);
44 EXPECT_TRUE(ProfileManager::GetActiveUserProfile() != NULL
);
45 mock_user_prefs_
= mock_profile
->GetTestingPrefService();
48 // Verifies that the user and system prefs contain the expected values.
49 void VerifyPrefs(const std::string
& current_input_method
,
50 const std::string
& previous_input_method
,
51 const std::string
& preferred_keyboard_layout
) {
52 EXPECT_EQ(current_input_method
,
53 mock_user_prefs_
->GetString(prefs::kLanguageCurrentInputMethod
));
54 EXPECT_EQ(previous_input_method
,
55 mock_user_prefs_
->GetString(prefs::kLanguagePreviousInputMethod
));
56 EXPECT_EQ(preferred_keyboard_layout
,
57 g_browser_process
->local_state()->GetString(
58 language_prefs::kPreferredKeyboardLayout
));
61 TestingPrefServiceSyncable
* mock_user_prefs_
;
62 MockInputMethodManager mock_manager_
;
63 TestingProfileManager mock_profile_manager_
;
66 TEST_F(InputMethodPersistenceTest
, TestLifetime
) {
68 InputMethodPersistence
persistence(&mock_manager_
);
69 EXPECT_EQ(1, mock_manager_
.add_observer_count_
);
71 EXPECT_EQ(1, mock_manager_
.remove_observer_count_
);
74 TEST_F(InputMethodPersistenceTest
, TestPrefPersistenceByState
) {
75 InputMethodPersistence
persistence(&mock_manager_
);
77 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOGIN_SCREEN
);
78 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
79 persistence
.InputMethodChanged(&mock_manager_
, false);
80 VerifyPrefs("", "", kInputId1
);
82 persistence
.OnSessionStateChange(InputMethodManager::STATE_BROWSER_SCREEN
);
83 mock_manager_
.SetCurrentInputMethodId(kInputId2
);
84 persistence
.InputMethodChanged(&mock_manager_
, false);
85 VerifyPrefs(kInputId2
, "", kInputId1
);
87 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOCK_SCREEN
);
88 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
89 persistence
.InputMethodChanged(&mock_manager_
, false);
90 VerifyPrefs(kInputId2
, "", kInputId1
);
92 persistence
.OnSessionStateChange(InputMethodManager::STATE_TERMINATING
);
93 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
94 persistence
.InputMethodChanged(&mock_manager_
, false);
95 VerifyPrefs(kInputId2
, "", kInputId1
);
97 persistence
.OnSessionStateChange(InputMethodManager::STATE_LOGIN_SCREEN
);
98 mock_manager_
.SetCurrentInputMethodId(kInputId2
);
99 persistence
.InputMethodChanged(&mock_manager_
, false);
100 VerifyPrefs(kInputId2
, "", kInputId2
);
102 persistence
.OnSessionStateChange(InputMethodManager::STATE_BROWSER_SCREEN
);
103 mock_manager_
.SetCurrentInputMethodId(kInputId1
);
104 persistence
.InputMethodChanged(&mock_manager_
, false);
105 VerifyPrefs(kInputId1
, kInputId2
, kInputId2
);
108 } // namespace input_method
109 } // namespace chromeos