Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / window_size_autosaver_unittest.mm
blob38d94ad0ba20077e3bdf34c1bd819f698e2c8a93
1 // Copyright (c) 2011 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 #import <Cocoa/Cocoa.h>
7 #import "chrome/browser/ui/cocoa/window_size_autosaver.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
18 namespace {
20 class WindowSizeAutosaverTest : public CocoaProfileTest {
21   void SetUp() override {
22     CocoaProfileTest::SetUp();
23     path_ = "WindowSizeAutosaverTest";
24     window_ =
25         [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 101, 150, 151)
26                                     styleMask:NSTitledWindowMask|
27                                               NSResizableWindowMask
28                                       backing:NSBackingStoreBuffered
29                                         defer:NO];
30     // TODO(joi): Do all registration up front.
31     static_cast<user_prefs::PrefRegistrySyncable*>(
32         profile()->GetPrefs()->DeprecatedGetPrefRegistry())
33         ->RegisterDictionaryPref(path_);
34   }
36   void TearDown() override {
37     [window_ close];
38     CocoaProfileTest::TearDown();
39   }
41  public:
42   NSWindow* window_;
43   const char* path_;
46 TEST_F(WindowSizeAutosaverTest, RestoresAndSavesPos) {
47   PrefService* pref = profile()->GetPrefs();
48   ASSERT_TRUE(pref != NULL);
50   // Check to make sure there is no existing pref for window placement.
51   const base::DictionaryValue* placement = pref->GetDictionary(path_);
52   ASSERT_TRUE(placement);
53   EXPECT_TRUE(placement->empty());
55   // Replace the window with one that doesn't have resize controls.
56   [window_ close];
57   window_ =
58       [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 101, 150, 151)
59                                   styleMask:NSTitledWindowMask
60                                     backing:NSBackingStoreBuffered
61                                       defer:NO];
63   // Ask the window to save its position, then check that a preference
64   // exists.  We're technically passing in a pointer to the user prefs
65   // and not the local state prefs, but a PrefService* is a
66   // PrefService*, and this is a unittest.
68   {
69     NSRect frame = [window_ frame];
70     // Empty state, shouldn't restore:
71     base::scoped_nsobject<WindowSizeAutosaver> sizeSaver(
72         [[WindowSizeAutosaver alloc] initWithWindow:window_
73                                         prefService:pref
74                                                path:path_]);
75     EXPECT_EQ(NSMinX(frame), NSMinX([window_ frame]));
76     EXPECT_EQ(NSMinY(frame), NSMinY([window_ frame]));
77     EXPECT_EQ(NSWidth(frame), NSWidth([window_ frame]));
78     EXPECT_EQ(NSHeight(frame), NSHeight([window_ frame]));
80     // Move and resize window, should store position but not size.
81     [window_ setFrame:NSMakeRect(300, 310, 250, 252) display:NO];
82   }
84   // Another window movement -- shouldn't be recorded.
85   [window_ setFrame:NSMakeRect(400, 420, 160, 162) display:NO];
87   {
88     // Should restore last stored position, but not size.
89     base::scoped_nsobject<WindowSizeAutosaver> sizeSaver(
90         [[WindowSizeAutosaver alloc] initWithWindow:window_
91                                         prefService:pref
92                                                path:path_]);
93     EXPECT_EQ(300, NSMinX([window_ frame]));
94     EXPECT_EQ(310, NSMinY([window_ frame]));
95     EXPECT_EQ(160, NSWidth([window_ frame]));
96     EXPECT_EQ(162, NSHeight([window_ frame]));
97   }
99   // ...and it should be in the profile, too.
100   EXPECT_TRUE(pref->GetDictionary(path_) != NULL);
101   int x, y;
102   const base::DictionaryValue* windowPref = pref->GetDictionary(path_);
103   EXPECT_FALSE(windowPref->GetInteger("left", &x));
104   EXPECT_FALSE(windowPref->GetInteger("right", &x));
105   EXPECT_FALSE(windowPref->GetInteger("top", &x));
106   EXPECT_FALSE(windowPref->GetInteger("bottom", &x));
107   ASSERT_TRUE(windowPref->GetInteger("x", &x));
108   ASSERT_TRUE(windowPref->GetInteger("y", &y));
109   EXPECT_EQ(300, x);
110   EXPECT_EQ(310, y);
113 TEST_F(WindowSizeAutosaverTest, RestoresAndSavesRect) {
114   PrefService* pref = profile()->GetPrefs();
115   ASSERT_TRUE(pref != NULL);
117   // Check to make sure there is no existing pref for window placement.
118   const base::DictionaryValue* placement = pref->GetDictionary(path_);
119   ASSERT_TRUE(placement);
120   EXPECT_TRUE(placement->empty());
122   // Ask the window to save its position, then check that a preference
123   // exists.  We're technically passing in a pointer to the user prefs
124   // and not the local state prefs, but a PrefService* is a
125   // PrefService*, and this is a unittest.
127   {
128     NSRect frame = [window_ frame];
129     // Empty state, shouldn't restore:
130     base::scoped_nsobject<WindowSizeAutosaver> sizeSaver(
131         [[WindowSizeAutosaver alloc] initWithWindow:window_
132                                         prefService:pref
133                                                path:path_]);
134     EXPECT_EQ(NSMinX(frame), NSMinX([window_ frame]));
135     EXPECT_EQ(NSMinY(frame), NSMinY([window_ frame]));
136     EXPECT_EQ(NSWidth(frame), NSWidth([window_ frame]));
137     EXPECT_EQ(NSHeight(frame), NSHeight([window_ frame]));
139     // Move and resize window, should store
140     [window_ setFrame:NSMakeRect(300, 310, 250, 252) display:NO];
141   }
143   // Another window movement -- shouldn't be recorded.
144   [window_ setFrame:NSMakeRect(400, 420, 160, 162) display:NO];
146   {
147     // Should restore last stored size
148     base::scoped_nsobject<WindowSizeAutosaver> sizeSaver(
149         [[WindowSizeAutosaver alloc] initWithWindow:window_
150                                         prefService:pref
151                                                path:path_]);
152     EXPECT_EQ(300, NSMinX([window_ frame]));
153     EXPECT_EQ(310, NSMinY([window_ frame]));
154     EXPECT_EQ(250, NSWidth([window_ frame]));
155     EXPECT_EQ(252, NSHeight([window_ frame]));
156   }
158   // ...and it should be in the profile, too.
159   EXPECT_TRUE(pref->GetDictionary(path_) != NULL);
160   int x1, y1, x2, y2;
161   const base::DictionaryValue* windowPref = pref->GetDictionary(path_);
162   EXPECT_FALSE(windowPref->GetInteger("x", &x1));
163   EXPECT_FALSE(windowPref->GetInteger("y", &x1));
164   ASSERT_TRUE(windowPref->GetInteger("left", &x1));
165   ASSERT_TRUE(windowPref->GetInteger("right", &x2));
166   ASSERT_TRUE(windowPref->GetInteger("top", &y1));
167   ASSERT_TRUE(windowPref->GetInteger("bottom", &y2));
168   EXPECT_EQ(300, x1);
169   EXPECT_EQ(310, y1);
170   EXPECT_EQ(300 + 250, x2);
171   EXPECT_EQ(310 + 252, y2);
174 // http://crbug.com/39625
175 TEST_F(WindowSizeAutosaverTest, DoesNotRestoreButClearsEmptyRect) {
176   PrefService* pref = profile()->GetPrefs();
177   ASSERT_TRUE(pref != NULL);
179   DictionaryPrefUpdate update(pref, path_);
180   base::DictionaryValue* windowPref = update.Get();
181   windowPref->SetInteger("left", 50);
182   windowPref->SetInteger("right", 50);
183   windowPref->SetInteger("top", 60);
184   windowPref->SetInteger("bottom", 60);
186   {
187     // Window rect shouldn't change...
188     NSRect frame = [window_ frame];
189     base::scoped_nsobject<WindowSizeAutosaver> sizeSaver(
190         [[WindowSizeAutosaver alloc] initWithWindow:window_
191                                         prefService:pref
192                                                path:path_]);
193     EXPECT_EQ(NSMinX(frame), NSMinX([window_ frame]));
194     EXPECT_EQ(NSMinY(frame), NSMinY([window_ frame]));
195     EXPECT_EQ(NSWidth(frame), NSWidth([window_ frame]));
196     EXPECT_EQ(NSHeight(frame), NSHeight([window_ frame]));
197   }
199   // ...and it should be gone from the profile, too.
200   EXPECT_TRUE(pref->GetDictionary(path_) != NULL);
201   int x1, y1, x2, y2;
202   EXPECT_FALSE(windowPref->GetInteger("x", &x1));
203   EXPECT_FALSE(windowPref->GetInteger("y", &x1));
204   ASSERT_FALSE(windowPref->GetInteger("left", &x1));
205   ASSERT_FALSE(windowPref->GetInteger("right", &x2));
206   ASSERT_FALSE(windowPref->GetInteger("top", &y1));
207   ASSERT_FALSE(windowPref->GetInteger("bottom", &y2));
210 }  // namespace