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.
4
5#include "chrome/browser/chromeos/preferences.h"
6
7#include "base/prefs/pref_member.h"
8#include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
9#include "chrome/browser/download/download_prefs.h"
10#include "chrome/common/pref_names.h"
11#include "chrome/test/base/testing_pref_service_syncable.h"
12#include "components/user_prefs/pref_registry_syncable.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace chromeos {
16namespace {
17
18class MyMockInputMethodManager : public input_method::MockInputMethodManager {
19 public:
20  MyMockInputMethodManager(StringPrefMember* previous,
21                           StringPrefMember* current)
22      : previous_(previous),
23        current_(current) {
24  }
25  virtual ~MyMockInputMethodManager() {
26  }
27
28  virtual void ChangeInputMethod(const std::string& input_method_id) OVERRIDE {
29    last_input_method_id_ = input_method_id;
30    // Do the same thing as BrowserStateMonitor::UpdateUserPreferences.
31    const std::string current_input_method_on_pref = current_->GetValue();
32    if (current_input_method_on_pref == input_method_id)
33      return;
34    previous_->SetValue(current_input_method_on_pref);
35    current_->SetValue(input_method_id);
36  }
37
38  std::string last_input_method_id_;
39
40 private:
41  StringPrefMember* previous_;
42  StringPrefMember* current_;
43};
44
45}  // anonymous namespace
46
47TEST(PreferencesTest, TestUpdatePrefOnBrowserScreenDetails) {
48  TestingPrefServiceSyncable prefs;
49  Preferences::RegisterProfilePrefs(prefs.registry());
50  DownloadPrefs::RegisterProfilePrefs(prefs.registry());
51  // kSelectFileLastDirectory is registered for Profile. Here we register it for
52  // testing.
53  prefs.registry()->RegisterStringPref(
54      prefs::kSelectFileLastDirectory,
55      std::string(),
56      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
57
58  StringPrefMember previous;
59  previous.Init(prefs::kLanguagePreviousInputMethod, &prefs);
60  previous.SetValue("KeyboardA");
61  StringPrefMember current;
62  current.Init(prefs::kLanguageCurrentInputMethod, &prefs);
63  current.SetValue("KeyboardB");
64
65  MyMockInputMethodManager mock_manager(&previous, &current);
66  Preferences testee(&mock_manager);
67  testee.InitUserPrefsForTesting(&prefs);
68  testee.SetInputMethodListForTesting();
69
70  // Confirm they're unchanged.
71  EXPECT_EQ("KeyboardA", previous.GetValue());
72  EXPECT_EQ("KeyboardB", current.GetValue());
73  EXPECT_EQ("KeyboardB", mock_manager.last_input_method_id_);
74}
75
76}  // namespace chromeos
77