Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / prefs / browser_ui_prefs_migrator_unittest.cc
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.
4
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"
12
13 namespace {
14
15 class DictionaryPrefStore : public WriteablePrefStore {
16  public:
17   DictionaryPrefStore() : WriteablePrefStore() {}
18
19   // Overrides from PrefStore.
20   void AddObserver(Observer* observer) override {
21     observers_.AddObserver(observer);
22   }
23
24   void RemoveObserver(Observer* observer) override {
25     observers_.RemoveObserver(observer);
26   }
27
28   bool GetValue(const std::string& key,
29                 const base::Value** result) const override {
30     return prefs_.Get(key, result);
31   }
32
33   // Overrides from WriteablePrefStore.
34   void SetValue(const std::string& key, base::Value* value) override {
35     DCHECK(value);
36     prefs_.Set(key, value);
37     ReportValueChanged(key);
38   }
39
40   void RemoveValue(const std::string& key) override {
41     if (prefs_.RemovePath(key, NULL))
42       ReportValueChanged(key);
43   }
44
45   bool GetMutableValue(const std::string& key, base::Value** result) override {
46     return prefs_.Get(key, result);
47   }
48
49   void ReportValueChanged(const std::string& key) override {}
50
51   void SetValueSilently(const std::string& key, base::Value* value) override {
52     NOTIMPLEMENTED();
53   }
54
55   void SignalObservers() {
56     FOR_EACH_OBSERVER(
57         PrefStore::Observer, observers_, OnInitializationCompleted(true));
58   }
59
60  private:
61   ~DictionaryPrefStore() override {}
62
63   base::DictionaryValue prefs_;
64   ObserverList<PrefStore::Observer, true> observers_;
65
66   DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore);
67 };
68
69 }  // namespace
70
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());
78
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());
84
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));
94
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));
104
105   pref_store->AddObserver(new BrowserUIPrefsMigrator(pref_store.get()));
106   pref_store->SignalObservers();
107
108   EXPECT_FALSE(pref_store->GetValue(kOldPathToOneAppDictionary, NULL));
109   EXPECT_FALSE(pref_store->GetValue(kOldPathToDevToolsDictionary, NULL));
110
111   const base::Value* value = NULL;
112   const base::DictionaryValue* dictionary = NULL;
113   int out_value;
114
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);
119
120   ASSERT_TRUE(
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);
125
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);
131
132   ASSERT_TRUE(pref_store->GetValue(prefs::kAppWindowPlacement +
133                                        std::string(".") +
134                                        DevToolsWindow::kDevToolsApp,
135                                    &value));
136   ASSERT_TRUE(value->GetAsDictionary(&dictionary));
137   EXPECT_TRUE(dictionary->GetInteger("left", &out_value));
138   EXPECT_EQ(700, out_value);
139 }