OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 #import <Cocoa/Cocoa.h> |
| 6 |
| 7 #import "chrome/browser/cocoa/window_size_autosaver.h" |
| 8 |
| 9 #include "base/scoped_nsobject.h" |
| 10 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 12 #include "chrome/common/pref_service.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/platform_test.h" |
| 15 |
| 16 class WindowSizeAutosaverTest : public CocoaTest { |
| 17 virtual void SetUp() { |
| 18 CocoaTest::SetUp(); |
| 19 path_ = L"WindowSizeAutosaverTest"; |
| 20 window_ = |
| 21 [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 101, 150, 151) |
| 22 styleMask:NSTitledWindowMask |
| 23 backing:NSBackingStoreBuffered |
| 24 defer:NO]; |
| 25 browser_helper_.profile()->GetPrefs()->RegisterDictionaryPref(path_); |
| 26 } |
| 27 |
| 28 virtual void TearDown() { |
| 29 [window_ close]; |
| 30 CocoaTest::TearDown(); |
| 31 } |
| 32 |
| 33 public: |
| 34 BrowserTestHelper browser_helper_; |
| 35 NSWindow* window_; |
| 36 const wchar_t* path_; |
| 37 }; |
| 38 |
| 39 TEST_F(WindowSizeAutosaverTest, RestoresAndSavesPos) { |
| 40 PrefService* pref = browser_helper_.profile()->GetPrefs(); |
| 41 ASSERT_TRUE(pref != NULL); |
| 42 |
| 43 // Check to make sure there is no existing pref for window placement. |
| 44 ASSERT_TRUE(pref->GetDictionary(path_) == NULL); |
| 45 |
| 46 // Ask the window to save its position, then check that a preference |
| 47 // exists. We're technically passing in a pointer to the user prefs |
| 48 // and not the local state prefs, but a PrefService* is a |
| 49 // PrefService*, and this is a unittest. |
| 50 |
| 51 { |
| 52 NSRect frame = [window_ frame]; |
| 53 // Empty state, shouldn't restore: |
| 54 scoped_nsobject<WindowSizeAutosaver> sizeSaver([[WindowSizeAutosaver alloc] |
| 55 initWithWindow:window_ |
| 56 prefService:pref |
| 57 path:path_ |
| 58 state:kSaveWindowPos]); |
| 59 EXPECT_EQ(NSMinX(frame), NSMinX([window_ frame])); |
| 60 EXPECT_EQ(NSMinY(frame), NSMinY([window_ frame])); |
| 61 EXPECT_EQ(NSWidth(frame), NSWidth([window_ frame])); |
| 62 EXPECT_EQ(NSHeight(frame), NSHeight([window_ frame])); |
| 63 |
| 64 // Move and resize window, should store position but not size. |
| 65 [window_ setFrame:NSMakeRect(300, 310, 50, 52) display:NO]; |
| 66 } |
| 67 |
| 68 // Another window movement -- shouldn't be recorded. |
| 69 [window_ setFrame:NSMakeRect(400, 420, 160, 162) display:NO]; |
| 70 |
| 71 { |
| 72 // Should restore last stored position, but not size. |
| 73 scoped_nsobject<WindowSizeAutosaver> sizeSaver([[WindowSizeAutosaver alloc] |
| 74 initWithWindow:window_ |
| 75 prefService:pref |
| 76 path:path_ |
| 77 state:kSaveWindowPos]); |
| 78 EXPECT_EQ(300, NSMinX([window_ frame])); |
| 79 EXPECT_EQ(310, NSMinY([window_ frame])); |
| 80 EXPECT_EQ(160, NSWidth([window_ frame])); |
| 81 EXPECT_EQ(162, NSHeight([window_ frame])); |
| 82 } |
| 83 |
| 84 // ...and it should be in the profile, too. |
| 85 EXPECT_TRUE(pref->GetDictionary(path_) != NULL); |
| 86 int x, y; |
| 87 DictionaryValue* windowPref = pref->GetMutableDictionary(path_); |
| 88 EXPECT_FALSE(windowPref->GetInteger(L"left", &x)); |
| 89 EXPECT_FALSE(windowPref->GetInteger(L"right", &x)); |
| 90 EXPECT_FALSE(windowPref->GetInteger(L"top", &x)); |
| 91 EXPECT_FALSE(windowPref->GetInteger(L"bottom", &x)); |
| 92 ASSERT_TRUE(windowPref->GetInteger(L"x", &x)); |
| 93 ASSERT_TRUE(windowPref->GetInteger(L"y", &y)); |
| 94 EXPECT_EQ(300, x); |
| 95 EXPECT_EQ(310, y); |
| 96 } |
| 97 |
| 98 TEST_F(WindowSizeAutosaverTest, RestoresAndSavesRect) { |
| 99 PrefService* pref = browser_helper_.profile()->GetPrefs(); |
| 100 ASSERT_TRUE(pref != NULL); |
| 101 |
| 102 // Check to make sure there is no existing pref for window placement. |
| 103 ASSERT_TRUE(pref->GetDictionary(path_) == NULL); |
| 104 |
| 105 // Ask the window to save its position, then check that a preference |
| 106 // exists. We're technically passing in a pointer to the user prefs |
| 107 // and not the local state prefs, but a PrefService* is a |
| 108 // PrefService*, and this is a unittest. |
| 109 |
| 110 { |
| 111 NSRect frame = [window_ frame]; |
| 112 // Empty state, shouldn't restore: |
| 113 scoped_nsobject<WindowSizeAutosaver> sizeSaver([[WindowSizeAutosaver alloc] |
| 114 initWithWindow:window_ |
| 115 prefService:pref |
| 116 path:path_ |
| 117 state:kSaveWindowRect]); |
| 118 EXPECT_EQ(NSMinX(frame), NSMinX([window_ frame])); |
| 119 EXPECT_EQ(NSMinY(frame), NSMinY([window_ frame])); |
| 120 EXPECT_EQ(NSWidth(frame), NSWidth([window_ frame])); |
| 121 EXPECT_EQ(NSHeight(frame), NSHeight([window_ frame])); |
| 122 |
| 123 // Move and resize window, should store |
| 124 [window_ setFrame:NSMakeRect(300, 310, 50, 52) display:NO]; |
| 125 } |
| 126 |
| 127 // Another window movement -- shouldn't be recorded. |
| 128 [window_ setFrame:NSMakeRect(400, 420, 160, 162) display:NO]; |
| 129 |
| 130 { |
| 131 // Should restore last stored size |
| 132 scoped_nsobject<WindowSizeAutosaver> sizeSaver([[WindowSizeAutosaver alloc] |
| 133 initWithWindow:window_ |
| 134 prefService:pref |
| 135 path:path_ |
| 136 state:kSaveWindowRect]); |
| 137 EXPECT_EQ(300, NSMinX([window_ frame])); |
| 138 EXPECT_EQ(310, NSMinY([window_ frame])); |
| 139 EXPECT_EQ(50, NSWidth([window_ frame])); |
| 140 EXPECT_EQ(52, NSHeight([window_ frame])); |
| 141 } |
| 142 |
| 143 // ...and it should be in the profile, too. |
| 144 EXPECT_TRUE(pref->GetDictionary(path_) != NULL); |
| 145 int x1, y1, x2, y2; |
| 146 DictionaryValue* windowPref = pref->GetMutableDictionary(path_); |
| 147 EXPECT_FALSE(windowPref->GetInteger(L"x", &x1)); |
| 148 EXPECT_FALSE(windowPref->GetInteger(L"y", &x1)); |
| 149 ASSERT_TRUE(windowPref->GetInteger(L"left", &x1)); |
| 150 ASSERT_TRUE(windowPref->GetInteger(L"right", &x2)); |
| 151 ASSERT_TRUE(windowPref->GetInteger(L"top", &y1)); |
| 152 ASSERT_TRUE(windowPref->GetInteger(L"bottom", &y2)); |
| 153 EXPECT_EQ(300, x1); |
| 154 EXPECT_EQ(310, y1); |
| 155 EXPECT_EQ(300 + 50, x2); |
| 156 EXPECT_EQ(310 + 52, y2); |
| 157 } |
OLD | NEW |