summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prefs/browser_ui_prefs_migrator_unittest.cc
blob: 575fcea31762fbe69251e6fbb8edb2bc3f5714a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/observer_list.h"
#include "base/prefs/writeable_pref_store.h"
#include "base/values.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/prefs/browser_ui_prefs_migrator.h"
#include "chrome/common/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class DictionaryPrefStore : public WriteablePrefStore {
 public:
  DictionaryPrefStore() : WriteablePrefStore() {}

  // Overrides from PrefStore.
  void AddObserver(Observer* observer) override {
    observers_.AddObserver(observer);
  }

  void RemoveObserver(Observer* observer) override {
    observers_.RemoveObserver(observer);
  }

  bool GetValue(const std::string& key,
                const base::Value** result) const override {
    return prefs_.Get(key, result);
  }

  // Overrides from WriteablePrefStore.
  void SetValue(const std::string& key, base::Value* value) override {
    DCHECK(value);
    prefs_.Set(key, value);
    ReportValueChanged(key);
  }

  void RemoveValue(const std::string& key) override {
    if (prefs_.RemovePath(key, NULL))
      ReportValueChanged(key);
  }

  bool GetMutableValue(const std::string& key, base::Value** result) override {
    return prefs_.Get(key, result);
  }

  void ReportValueChanged(const std::string& key) override {}

  void SetValueSilently(const std::string& key, base::Value* value) override {
    NOTIMPLEMENTED();
  }

  void SignalObservers() {
    FOR_EACH_OBSERVER(
        PrefStore::Observer, observers_, OnInitializationCompleted(true));
  }

 private:
  ~DictionaryPrefStore() override {}

  base::DictionaryValue prefs_;
  ObserverList<PrefStore::Observer, true> observers_;

  DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore);
};

}  // namespace

TEST(UIPrefsMigratorTest, MigrateTest) {
  scoped_refptr<DictionaryPrefStore> pref_store(new DictionaryPrefStore);
  scoped_ptr<base::DictionaryValue> browser_window_placement(
      new base::DictionaryValue);
  browser_window_placement->SetInteger("bottom", 1000);
  pref_store->SetValue(prefs::kBrowserWindowPlacement,
                       browser_window_placement.release());

  scoped_ptr<base::DictionaryValue> browser_window_placement_popup(
      new base::DictionaryValue);
  browser_window_placement_popup->SetInteger("top", 50);
  pref_store->SetValue(prefs::kBrowserWindowPlacementPopup,
                       browser_window_placement_popup.release());

  scoped_ptr<base::DictionaryValue> single_app_placement_dict(
      new base::DictionaryValue);
  single_app_placement_dict->SetInteger("right", 986);
  const char* kAppName("localhost_/some_app.html");
  const std::string kOldPathToOneAppDictionary =
      prefs::kBrowserWindowPlacement + std::string("_") + kAppName;
  pref_store->SetValue(kOldPathToOneAppDictionary,
                       single_app_placement_dict.release());
  EXPECT_TRUE(pref_store->GetValue(kOldPathToOneAppDictionary, NULL));

  scoped_ptr<base::DictionaryValue> devtools_placement_dict(
      new base::DictionaryValue);
  devtools_placement_dict->SetInteger("left", 700);
  const std::string kOldPathToDevToolsDictionary =
      prefs::kBrowserWindowPlacement + std::string("_") +
      DevToolsWindow::kDevToolsApp;
  pref_store->SetValue(kOldPathToDevToolsDictionary,
                       devtools_placement_dict.release());
  EXPECT_TRUE(pref_store->GetValue(kOldPathToDevToolsDictionary, NULL));

  pref_store->AddObserver(new BrowserUIPrefsMigrator(pref_store.get()));
  pref_store->SignalObservers();

  EXPECT_FALSE(pref_store->GetValue(kOldPathToOneAppDictionary, NULL));
  EXPECT_FALSE(pref_store->GetValue(kOldPathToDevToolsDictionary, NULL));

  const base::Value* value = NULL;
  const base::DictionaryValue* dictionary = NULL;
  int out_value;

  ASSERT_TRUE(pref_store->GetValue(prefs::kBrowserWindowPlacement, &value));
  ASSERT_TRUE(value->GetAsDictionary(&dictionary));
  EXPECT_TRUE(dictionary->GetInteger("bottom", &out_value));
  EXPECT_EQ(1000, out_value);

  ASSERT_TRUE(
      pref_store->GetValue(prefs::kBrowserWindowPlacementPopup, &value));
  ASSERT_TRUE(value->GetAsDictionary(&dictionary));
  EXPECT_TRUE(dictionary->GetInteger("top", &out_value));
  EXPECT_EQ(50, out_value);

  ASSERT_TRUE(pref_store->GetValue(
      prefs::kAppWindowPlacement + std::string(".") + kAppName, &value));
  ASSERT_TRUE(value->GetAsDictionary(&dictionary));
  EXPECT_TRUE(dictionary->GetInteger("right", &out_value));
  EXPECT_EQ(986, out_value);

  ASSERT_TRUE(pref_store->GetValue(prefs::kAppWindowPlacement +
                                       std::string(".") +
                                       DevToolsWindow::kDevToolsApp,
                                   &value));
  ASSERT_TRUE(value->GetAsDictionary(&dictionary));
  EXPECT_TRUE(dictionary->GetInteger("left", &out_value));
  EXPECT_EQ(700, out_value);
}