diff options
-rw-r--r-- | chrome/browser/cocoa/page_info_window_controller.h | 9 | ||||
-rw-r--r-- | chrome/browser/cocoa/page_info_window_controller.mm | 49 | ||||
-rw-r--r-- | chrome/browser/cocoa/page_info_window_controller_unittest.mm | 52 |
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); +} + |