summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-21 14:19:09 +0000
committerpinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-21 14:19:09 +0000
commit61e76d9bf46423e8dcb6f77e8e91262af55b7a03 (patch)
treea02f4e6e9812331d7026705f4f08ba740607fa90
parent1c9b152007f05e1767b05986e5cb69fbb2629e43 (diff)
downloadchromium_src-61e76d9bf46423e8dcb6f77e8e91262af55b7a03.zip
chromium_src-61e76d9bf46423e8dcb6f77e8e91262af55b7a03.tar.gz
chromium_src-61e76d9bf46423e8dcb6f77e8e91262af55b7a03.tar.bz2
Make the page info window remember its placement origin on the Mac
* Store x/y origin in the local state PrefService * Offset new windows when creating them so they don't overlap * Add a unit test for remembering window placement * Refactor PageInfoWindowControllerTest to make use of SetUp() Patch from Robert Sesek (rsesek@chromium.org) BUG=none TEST=Open a page info window and move it. Open another and it should open in the new location. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23954 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/cocoa/page_info_window_controller.h9
-rw-r--r--chrome/browser/cocoa/page_info_window_controller.mm49
-rw-r--r--chrome/browser/cocoa/page_info_window_controller_unittest.mm52
3 files changed, 92 insertions, 18 deletions
diff --git a/chrome/browser/cocoa/page_info_window_controller.h b/chrome/browser/cocoa/page_info_window_controller.h
index 132c684..c62893b 100644
--- a/chrome/browser/cocoa/page_info_window_controller.h
+++ b/chrome/browser/cocoa/page_info_window_controller.h
@@ -8,6 +8,7 @@
#include "base/scoped_ptr.h"
class PageInfoWindowMac;
+class PrefService;
// This NSWindowController subclass implements the Cocoa window for
// PageInfoWindow. This creates and owns the PageInfoWindowMac subclass.
@@ -58,3 +59,11 @@ class PageInfoWindowMac;
- (void)setShowHistoryBox:(BOOL)show;
@end
+
+@interface PageInfoWindowController (ExposedForTesting)
+
+// Saves the window's origin into the given PrefService. Caller is responsible
+// for making sure |prefs| is not NULL.
+- (void)saveWindowPositionToPrefs:(PrefService*)prefs;
+
+@end
diff --git a/chrome/browser/cocoa/page_info_window_controller.mm b/chrome/browser/cocoa/page_info_window_controller.mm
index afbdea8..9f3b813 100644
--- a/chrome/browser/cocoa/page_info_window_controller.mm
+++ b/chrome/browser/cocoa/page_info_window_controller.mm
@@ -5,7 +5,16 @@
#import "chrome/browser/cocoa/page_info_window_controller.h"
#include "base/mac_util.h"
+#include "base/values.h"
+#include "chrome/browser/browser_process.h"
#include "chrome/browser/cocoa/page_info_window_mac.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/common/pref_service.h"
+
+@interface PageInfoWindowController (Private)
+// Saves the window preference to the local state.
+- (void)saveWindowPositionToLocalState;
+@end
@implementation PageInfoWindowController
@synthesize identityImg = identityImg_;
@@ -33,6 +42,22 @@
}
- (void)awakeFromNib {
+ if (g_browser_process && g_browser_process->local_state()) {
+ // Get the positioning information.
+ PrefService* prefs = g_browser_process->local_state();
+ DictionaryValue* windowPrefs =
+ prefs->GetMutableDictionary(prefs::kPageInfoWindowPlacement);
+ int x = 0, y = 0;
+ windowPrefs->GetInteger(L"x", &x);
+ windowPrefs->GetInteger(L"y", &y);
+ // Turn the origin (lower-left) into an upper-left window point.
+ NSPoint upperLeft = NSMakePoint(x, y + [[self window] frame].size.height);
+ NSPoint cascadePoint = [[self window] cascadeTopLeftFromPoint:upperLeft];
+ // Cascade again to get the offset when opening new windows.
+ [[self window] cascadeTopLeftFromPoint:cascadePoint];
+ [self saveWindowPositionToLocalState]; // Force a save of the pref.
+ }
+
// By default, assume we have no history information.
[self setShowHistoryBox:NO];
}
@@ -88,4 +113,28 @@
[self autorelease];
}
+// The last page info window that was moved will determine the location of the
+// next new one.
+- (void)windowDidMove:(NSNotification*)notif {
+ [self saveWindowPositionToLocalState];
+}
+
+// Saves the window preference to the local state.
+- (void)saveWindowPositionToLocalState {
+ if (!g_browser_process || !g_browser_process->local_state())
+ return;
+ [self saveWindowPositionToPrefs:g_browser_process->local_state()];
+}
+
+// Saves the window's origin into the given PrefService. Caller is responsible
+// for making sure |prefs| is not NULL.
+- (void)saveWindowPositionToPrefs:(PrefService*)prefs {
+ // Save the origin of the window.
+ DictionaryValue* windowPrefs = prefs->GetMutableDictionary(
+ prefs::kPageInfoWindowPlacement);
+ NSRect frame = [[self window] frame];
+ windowPrefs->SetInteger(L"x", frame.origin.x);
+ windowPrefs->SetInteger(L"y", frame.origin.y);
+}
+
@end
diff --git a/chrome/browser/cocoa/page_info_window_controller_unittest.mm b/chrome/browser/cocoa/page_info_window_controller_unittest.mm
index ef73e6b..66018a2 100644
--- a/chrome/browser/cocoa/page_info_window_controller_unittest.mm
+++ b/chrome/browser/cocoa/page_info_window_controller_unittest.mm
@@ -8,46 +8,62 @@
#import "chrome/browser/cocoa/page_info_window_controller.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
+#include "chrome/common/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
class PageInfoWindowControllerTest : public PlatformTest {
+ virtual void SetUp() {
+ controller_.reset([[PageInfoWindowController alloc] init]);
+ }
+
public:
CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc...
BrowserTestHelper helper_;
+ scoped_nsobject<PageInfoWindowController> controller_;
};
TEST_F(PageInfoWindowControllerTest, TestImages) {
- scoped_nsobject<PageInfoWindowController>
- controller([[PageInfoWindowController alloc] init]);
- [controller window]; // Force nib load.
- EXPECT_TRUE([controller goodImg]);
- EXPECT_TRUE([controller badImg]);
+ [controller_ window]; // Force nib load.
+ EXPECT_TRUE([controller_ goodImg]);
+ EXPECT_TRUE([controller_ badImg]);
}
TEST_F(PageInfoWindowControllerTest, TestGrow) {
- scoped_nsobject<PageInfoWindowController>
- controller([[PageInfoWindowController alloc] init]);
- [controller window]; // Force nib load.
- NSRect frame = [[controller window] frame];
- [controller setShowHistoryBox:YES];
- NSRect newFrame = [[controller window] frame];
+ [controller_ window]; // Force nib load.
+ NSRect frame = [[controller_ window] frame];
+ [controller_ setShowHistoryBox:YES];
+ NSRect newFrame = [[controller_ window] frame];
EXPECT_GE(newFrame.size.height, frame.size.height);
EXPECT_LE(newFrame.origin.y, frame.origin.y);
}
TEST_F(PageInfoWindowControllerTest, TestShrink) {
- scoped_nsobject<PageInfoWindowController>
- controller([[PageInfoWindowController alloc] init]);
- [controller window]; // Force nib to load.
- [controller setShowHistoryBox:YES];
- NSRect frame = [[controller window] frame];
- [controller setShowHistoryBox:NO];
- NSRect newFrame = [[controller window] frame];
+ [controller_ window]; // Force nib to load.
+ [controller_ setShowHistoryBox:YES];
+ NSRect frame = [[controller_ window] frame];
+ [controller_ setShowHistoryBox:NO];
+ NSRect newFrame = [[controller_ window] frame];
EXPECT_LE(newFrame.size.height, frame.size.height);
EXPECT_GE(newFrame.origin.y, frame.origin.y);
}
+
+TEST_F(PageInfoWindowControllerTest, TestSaveWindowPlacement) {
+ PrefService* prefs = helper_.profile()->GetPrefs();
+ ASSERT_TRUE(prefs != NULL);
+
+ // Check to make sure there is no existing pref for window placement.
+ ASSERT_TRUE(prefs->GetDictionary(prefs::kPageInfoWindowPlacement) == NULL);
+
+ // Ask the window to save its position, then check that a preference
+ // exists. We're technically passing in a pointer to the user prefs
+ // and not the local state prefs, but a PrefService* is a
+ // PrefService*, and this is a unittest.
+ [controller_ saveWindowPositionToPrefs:prefs];
+ EXPECT_TRUE(prefs->GetDictionary(prefs::kPageInfoWindowPlacement) != NULL);
+}
+