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 #include "base/scoped_nsobject.h" | |
8 #include "chrome/browser/cocoa/browser_test_helper.h" | |
9 #import "chrome/browser/cocoa/cocoa_test_helper.h" | |
10 #import "chrome/browser/cocoa/nswindow_local_state.h" | |
11 #include "chrome/common/pref_service.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "testing/platform_test.h" | |
14 | |
15 class NSWindowLocalStateTest : public CocoaTest { | |
16 virtual void SetUp() { | |
17 CocoaTest::SetUp(); | |
18 path_ = L"NSWindowLocalStateTest"; | |
19 window_ = | |
20 [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 20, 20) | |
21 styleMask:NSTitledWindowMask | |
22 backing:NSBackingStoreBuffered | |
23 defer:NO]; | |
24 browser_helper_.profile()->GetPrefs()->RegisterDictionaryPref(path_); | |
25 } | |
26 | |
27 virtual void TearDown() { | |
28 [window_ close]; | |
29 CocoaTest::TearDown(); | |
30 } | |
31 | |
32 public: | |
33 BrowserTestHelper browser_helper_; | |
34 NSWindow* window_; | |
35 const wchar_t* path_; | |
36 }; | |
37 | |
38 TEST_F(NSWindowLocalStateTest, SaveWindowPlacement) { | |
39 PrefService* prefs = browser_helper_.profile()->GetPrefs(); | |
40 ASSERT_TRUE(prefs != NULL); | |
41 | |
42 // Check to make sure there is no existing pref for window placement. | |
43 ASSERT_TRUE(prefs->GetDictionary(path_) == NULL); | |
44 | |
45 // Ask the window to save its position, then check that a preference | |
46 // exists. We're technically passing in a pointer to the user prefs | |
47 // and not the local state prefs, but a PrefService* is a | |
48 // PrefService*, and this is a unittest. | |
49 [window_ saveWindowPositionToPrefs:prefs withPath:path_]; | |
50 EXPECT_TRUE(prefs->GetDictionary(path_) != NULL); | |
51 int x, y; | |
52 DictionaryValue* windowPrefs = prefs->GetMutableDictionary(path_); | |
53 windowPrefs->GetInteger(L"x", &x); | |
54 windowPrefs->GetInteger(L"y", &y); | |
55 EXPECT_EQ(x, [window_ frame].origin.x); | |
56 EXPECT_EQ(y, [window_ frame].origin.y); | |
57 } | |
58 | |
59 TEST_F(NSWindowLocalStateTest, RestoreWindowPlacement) { | |
60 PrefService* prefs = browser_helper_.profile()->GetPrefs(); | |
61 DictionaryValue* windowPrefs = prefs->GetMutableDictionary(path_); | |
62 | |
63 // Large enough so that the window is on screen without cascasding to a | |
64 // totally new location. | |
65 const int value = 420; | |
66 windowPrefs->SetInteger(L"x", value); | |
67 windowPrefs->SetInteger(L"y", value); | |
68 [window_ restoreWindowPositionFromPrefs:prefs withPath:path_]; | |
69 EXPECT_LT(value, [window_ frame].origin.x); | |
70 EXPECT_GT(value, [window_ frame].origin.y); | |
71 } | |
OLD | NEW |