1 // Copyright (c) 2014 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 "base/observer_list.h"
6 #include "base/prefs/writeable_pref_store.h"
7 #include "base/values.h"
8 #include "chrome/browser/devtools/devtools_window.h"
9 #include "chrome/browser/prefs/browser_ui_prefs_migrator.h"
10 #include "chrome/common/pref_names.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class DictionaryPrefStore : public WriteablePrefStore {
17 DictionaryPrefStore() : WriteablePrefStore() {}
19 // Overrides from PrefStore.
20 void AddObserver(Observer* observer) override {
21 observers_.AddObserver(observer);
24 void RemoveObserver(Observer* observer) override {
25 observers_.RemoveObserver(observer);
28 bool GetValue(const std::string& key,
29 const base::Value** result) const override {
30 return prefs_.Get(key, result);
33 // Overrides from WriteablePrefStore.
34 void SetValue(const std::string& key, base::Value* value) override {
36 prefs_.Set(key, value);
37 ReportValueChanged(key);
40 void RemoveValue(const std::string& key) override {
41 if (prefs_.RemovePath(key, NULL))
42 ReportValueChanged(key);
45 bool GetMutableValue(const std::string& key, base::Value** result) override {
46 return prefs_.Get(key, result);
49 void ReportValueChanged(const std::string& key) override {}
51 void SetValueSilently(const std::string& key, base::Value* value) override {
55 void SignalObservers() {
57 PrefStore::Observer, observers_, OnInitializationCompleted(true));
61 ~DictionaryPrefStore() override {}
63 base::DictionaryValue prefs_;
64 ObserverList<PrefStore::Observer, true> observers_;
66 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore);
71 TEST(UIPrefsMigratorTest, MigrateTest) {
72 scoped_refptr<DictionaryPrefStore> pref_store(new DictionaryPrefStore);
73 scoped_ptr<base::DictionaryValue> browser_window_placement(
74 new base::DictionaryValue);
75 browser_window_placement->SetInteger("bottom", 1000);
76 pref_store->SetValue(prefs::kBrowserWindowPlacement,
77 browser_window_placement.release());
79 scoped_ptr<base::DictionaryValue> browser_window_placement_popup(
80 new base::DictionaryValue);
81 browser_window_placement_popup->SetInteger("top", 50);
82 pref_store->SetValue(prefs::kBrowserWindowPlacementPopup,
83 browser_window_placement_popup.release());
85 scoped_ptr<base::DictionaryValue> single_app_placement_dict(
86 new base::DictionaryValue);
87 single_app_placement_dict->SetInteger("right", 986);
88 const char* kAppName("localhost_/some_app.html");
89 const std::string kOldPathToOneAppDictionary =
90 prefs::kBrowserWindowPlacement + std::string("_") + kAppName;
91 pref_store->SetValue(kOldPathToOneAppDictionary,
92 single_app_placement_dict.release());
93 EXPECT_TRUE(pref_store->GetValue(kOldPathToOneAppDictionary, NULL));
95 scoped_ptr<base::DictionaryValue> devtools_placement_dict(
96 new base::DictionaryValue);
97 devtools_placement_dict->SetInteger("left", 700);
98 const std::string kOldPathToDevToolsDictionary =
99 prefs::kBrowserWindowPlacement + std::string("_") +
100 DevToolsWindow::kDevToolsApp;
101 pref_store->SetValue(kOldPathToDevToolsDictionary,
102 devtools_placement_dict.release());
103 EXPECT_TRUE(pref_store->GetValue(kOldPathToDevToolsDictionary, NULL));
105 pref_store->AddObserver(new BrowserUIPrefsMigrator(pref_store.get()));
106 pref_store->SignalObservers();
108 EXPECT_FALSE(pref_store->GetValue(kOldPathToOneAppDictionary, NULL));
109 EXPECT_FALSE(pref_store->GetValue(kOldPathToDevToolsDictionary, NULL));
111 const base::Value* value = NULL;
112 const base::DictionaryValue* dictionary = NULL;
115 ASSERT_TRUE(pref_store->GetValue(prefs::kBrowserWindowPlacement, &value));
116 ASSERT_TRUE(value->GetAsDictionary(&dictionary));
117 EXPECT_TRUE(dictionary->GetInteger("bottom", &out_value));
118 EXPECT_EQ(1000, out_value);
121 pref_store->GetValue(prefs::kBrowserWindowPlacementPopup, &value));
122 ASSERT_TRUE(value->GetAsDictionary(&dictionary));
123 EXPECT_TRUE(dictionary->GetInteger("top", &out_value));
124 EXPECT_EQ(50, out_value);
126 ASSERT_TRUE(pref_store->GetValue(
127 prefs::kAppWindowPlacement + std::string(".") + kAppName, &value));
128 ASSERT_TRUE(value->GetAsDictionary(&dictionary));
129 EXPECT_TRUE(dictionary->GetInteger("right", &out_value));
130 EXPECT_EQ(986, out_value);
132 ASSERT_TRUE(pref_store->GetValue(prefs::kAppWindowPlacement +
134 DevToolsWindow::kDevToolsApp,
136 ASSERT_TRUE(value->GetAsDictionary(&dictionary));
137 EXPECT_TRUE(dictionary->GetInteger("left", &out_value));
138 EXPECT_EQ(700, out_value);