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 virtual void AddObserver(Observer
* observer
) OVERRIDE
{
21 observers_
.AddObserver(observer
);
24 virtual void RemoveObserver(Observer
* observer
) OVERRIDE
{
25 observers_
.RemoveObserver(observer
);
28 virtual bool GetValue(const std::string
& key
,
29 const base::Value
** result
) const OVERRIDE
{
30 return prefs_
.Get(key
, result
);
33 // Overrides from WriteablePrefStore.
34 virtual void SetValue(const std::string
& key
, base::Value
* value
) OVERRIDE
{
36 prefs_
.Set(key
, value
);
37 ReportValueChanged(key
);
40 virtual void RemoveValue(const std::string
& key
) OVERRIDE
{
41 if (prefs_
.RemovePath(key
, NULL
))
42 ReportValueChanged(key
);
45 virtual bool GetMutableValue(const std::string
& key
,
46 base::Value
** result
) OVERRIDE
{
47 return prefs_
.Get(key
, result
);
50 virtual void ReportValueChanged(const std::string
& key
) OVERRIDE
{}
52 virtual void SetValueSilently(const std::string
& key
,
53 base::Value
* value
) OVERRIDE
{
57 void SignalObservers() {
59 PrefStore::Observer
, observers_
, OnInitializationCompleted(true));
63 virtual ~DictionaryPrefStore() {}
65 base::DictionaryValue prefs_
;
66 ObserverList
<PrefStore::Observer
, true> observers_
;
68 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore
);
73 TEST(UIPrefsMigratorTest
, MigrateTest
) {
74 scoped_refptr
<DictionaryPrefStore
> pref_store(new DictionaryPrefStore
);
75 scoped_ptr
<base::DictionaryValue
> browser_window_placement(
76 new base::DictionaryValue
);
77 browser_window_placement
->SetInteger("bottom", 1000);
78 pref_store
->SetValue(prefs::kBrowserWindowPlacement
,
79 browser_window_placement
.release());
81 scoped_ptr
<base::DictionaryValue
> browser_window_placement_popup(
82 new base::DictionaryValue
);
83 browser_window_placement_popup
->SetInteger("top", 50);
84 pref_store
->SetValue(prefs::kBrowserWindowPlacementPopup
,
85 browser_window_placement_popup
.release());
87 scoped_ptr
<base::DictionaryValue
> single_app_placement_dict(
88 new base::DictionaryValue
);
89 single_app_placement_dict
->SetInteger("right", 986);
90 const char* kAppName("localhost_/some_app.html");
91 const std::string kOldPathToOneAppDictionary
=
92 prefs::kBrowserWindowPlacement
+ std::string("_") + kAppName
;
93 pref_store
->SetValue(kOldPathToOneAppDictionary
,
94 single_app_placement_dict
.release());
95 EXPECT_TRUE(pref_store
->GetValue(kOldPathToOneAppDictionary
, NULL
));
97 scoped_ptr
<base::DictionaryValue
> devtools_placement_dict(
98 new base::DictionaryValue
);
99 devtools_placement_dict
->SetInteger("left", 700);
100 const std::string kOldPathToDevToolsDictionary
=
101 prefs::kBrowserWindowPlacement
+ std::string("_") +
102 DevToolsWindow::kDevToolsApp
;
103 pref_store
->SetValue(kOldPathToDevToolsDictionary
,
104 devtools_placement_dict
.release());
105 EXPECT_TRUE(pref_store
->GetValue(kOldPathToDevToolsDictionary
, NULL
));
107 pref_store
->AddObserver(new BrowserUIPrefsMigrator(pref_store
.get()));
108 pref_store
->SignalObservers();
110 EXPECT_FALSE(pref_store
->GetValue(kOldPathToOneAppDictionary
, NULL
));
111 EXPECT_FALSE(pref_store
->GetValue(kOldPathToDevToolsDictionary
, NULL
));
113 const base::Value
* value
= NULL
;
114 const base::DictionaryValue
* dictionary
= NULL
;
117 ASSERT_TRUE(pref_store
->GetValue(prefs::kBrowserWindowPlacement
, &value
));
118 ASSERT_TRUE(value
->GetAsDictionary(&dictionary
));
119 EXPECT_TRUE(dictionary
->GetInteger("bottom", &out_value
));
120 EXPECT_EQ(1000, out_value
);
123 pref_store
->GetValue(prefs::kBrowserWindowPlacementPopup
, &value
));
124 ASSERT_TRUE(value
->GetAsDictionary(&dictionary
));
125 EXPECT_TRUE(dictionary
->GetInteger("top", &out_value
));
126 EXPECT_EQ(50, out_value
);
128 ASSERT_TRUE(pref_store
->GetValue(
129 prefs::kAppWindowPlacement
+ std::string(".") + kAppName
, &value
));
130 ASSERT_TRUE(value
->GetAsDictionary(&dictionary
));
131 EXPECT_TRUE(dictionary
->GetInteger("right", &out_value
));
132 EXPECT_EQ(986, out_value
);
134 ASSERT_TRUE(pref_store
->GetValue(prefs::kAppWindowPlacement
+
136 DevToolsWindow::kDevToolsApp
,
138 ASSERT_TRUE(value
->GetAsDictionary(&dictionary
));
139 EXPECT_TRUE(dictionary
->GetInteger("left", &out_value
));
140 EXPECT_EQ(700, out_value
);