OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #import "base/memory/scoped_nsobject.h" | 7 #import "base/mac/scoped_nsobject.h" |
8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
9 #import "chrome/browser/app_controller_mac.h" | 9 #import "chrome/browser/app_controller_mac.h" |
10 #import "chrome/browser/chrome_browser_application_mac.h" | 10 #import "chrome/browser/chrome_browser_application_mac.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h" | 12 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h" |
13 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h" | 13 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h" |
14 #import "chrome/browser/ui/cocoa/applescript/tab_applescript.h" | 14 #import "chrome/browser/ui/cocoa/applescript/tab_applescript.h" |
15 #import "chrome/browser/ui/cocoa/applescript/window_applescript.h" | 15 #import "chrome/browser/ui/cocoa/applescript/window_applescript.h" |
16 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #import "testing/gtest_mac.h" | 19 #import "testing/gtest_mac.h" |
20 | 20 |
21 typedef InProcessBrowserTest WindowAppleScriptTest; | 21 typedef InProcessBrowserTest WindowAppleScriptTest; |
22 | 22 |
23 // Create a window in default/normal mode. | 23 // Create a window in default/normal mode. |
24 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, DefaultCreation) { | 24 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, DefaultCreation) { |
25 scoped_nsobject<WindowAppleScript> aWindow( | 25 base::scoped_nsobject<WindowAppleScript> aWindow( |
26 [[WindowAppleScript alloc] init]); | 26 [[WindowAppleScript alloc] init]); |
27 EXPECT_TRUE(aWindow.get()); | 27 EXPECT_TRUE(aWindow.get()); |
28 NSString* mode = [aWindow.get() mode]; | 28 NSString* mode = [aWindow.get() mode]; |
29 EXPECT_NSEQ(AppleScript::kNormalWindowMode, | 29 EXPECT_NSEQ(AppleScript::kNormalWindowMode, |
30 mode); | 30 mode); |
31 } | 31 } |
32 | 32 |
33 // Create a window with a |NULL profile|. | 33 // Create a window with a |NULL profile|. |
34 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithNoProfile) { | 34 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithNoProfile) { |
35 scoped_nsobject<WindowAppleScript> aWindow( | 35 base::scoped_nsobject<WindowAppleScript> aWindow( |
36 [[WindowAppleScript alloc] initWithProfile:NULL]); | 36 [[WindowAppleScript alloc] initWithProfile:NULL]); |
37 EXPECT_FALSE(aWindow.get()); | 37 EXPECT_FALSE(aWindow.get()); |
38 } | 38 } |
39 | 39 |
40 // Create a window with a particular profile. | 40 // Create a window with a particular profile. |
41 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithProfile) { | 41 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithProfile) { |
42 Profile* lastProfile = [[NSApp delegate] lastProfile]; | 42 Profile* lastProfile = [[NSApp delegate] lastProfile]; |
43 scoped_nsobject<WindowAppleScript> aWindow( | 43 base::scoped_nsobject<WindowAppleScript> aWindow( |
44 [[WindowAppleScript alloc] initWithProfile:lastProfile]); | 44 [[WindowAppleScript alloc] initWithProfile:lastProfile]); |
45 EXPECT_TRUE(aWindow.get()); | 45 EXPECT_TRUE(aWindow.get()); |
46 EXPECT_TRUE([aWindow.get() uniqueID]); | 46 EXPECT_TRUE([aWindow.get() uniqueID]); |
47 } | 47 } |
48 | 48 |
49 // Create a window with no |Browser*|. | 49 // Create a window with no |Browser*|. |
50 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithNoBrowser) { | 50 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithNoBrowser) { |
51 scoped_nsobject<WindowAppleScript> aWindow( | 51 base::scoped_nsobject<WindowAppleScript> aWindow( |
52 [[WindowAppleScript alloc] initWithBrowser:NULL]); | 52 [[WindowAppleScript alloc] initWithBrowser:NULL]); |
53 EXPECT_FALSE(aWindow.get()); | 53 EXPECT_FALSE(aWindow.get()); |
54 } | 54 } |
55 | 55 |
56 // Create a window with |Browser*| already present. | 56 // Create a window with |Browser*| already present. |
57 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithBrowser) { | 57 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithBrowser) { |
58 scoped_nsobject<WindowAppleScript> aWindow( | 58 base::scoped_nsobject<WindowAppleScript> aWindow( |
59 [[WindowAppleScript alloc] initWithBrowser:browser()]); | 59 [[WindowAppleScript alloc] initWithBrowser:browser()]); |
60 EXPECT_TRUE(aWindow.get()); | 60 EXPECT_TRUE(aWindow.get()); |
61 EXPECT_TRUE([aWindow.get() uniqueID]); | 61 EXPECT_TRUE([aWindow.get() uniqueID]); |
62 } | 62 } |
63 | 63 |
64 // Tabs within the window. | 64 // Tabs within the window. |
65 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, Tabs) { | 65 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, Tabs) { |
66 scoped_nsobject<WindowAppleScript> aWindow( | 66 base::scoped_nsobject<WindowAppleScript> aWindow( |
67 [[WindowAppleScript alloc] initWithBrowser:browser()]); | 67 [[WindowAppleScript alloc] initWithBrowser:browser()]); |
68 NSArray* tabs = [aWindow.get() tabs]; | 68 NSArray* tabs = [aWindow.get() tabs]; |
69 EXPECT_EQ(1U, [tabs count]); | 69 EXPECT_EQ(1U, [tabs count]); |
70 TabAppleScript* tab1 = [tabs objectAtIndex:0]; | 70 TabAppleScript* tab1 = [tabs objectAtIndex:0]; |
71 EXPECT_EQ([tab1 container], aWindow.get()); | 71 EXPECT_EQ([tab1 container], aWindow.get()); |
72 EXPECT_NSEQ(AppleScript::kTabsProperty, | 72 EXPECT_NSEQ(AppleScript::kTabsProperty, |
73 [tab1 containerProperty]); | 73 [tab1 containerProperty]); |
74 } | 74 } |
75 | 75 |
76 // Insert a new tab. | 76 // Insert a new tab. |
77 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTab) { | 77 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTab) { |
78 // Emulate what applescript would do when creating a new tab. | 78 // Emulate what applescript would do when creating a new tab. |
79 // Emulates a script like |set var to make new tab with | 79 // Emulates a script like |set var to make new tab with |
80 // properties URL:"http://google.com"}|. | 80 // properties URL:"http://google.com"}|. |
81 scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]); | 81 base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]); |
82 scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]); | 82 base::scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]); |
83 [aTab.get() setURL:@"http://google.com"]; | 83 [aTab.get() setURL:@"http://google.com"]; |
84 scoped_nsobject<WindowAppleScript> aWindow( | 84 base::scoped_nsobject<WindowAppleScript> aWindow( |
85 [[WindowAppleScript alloc] initWithBrowser:browser()]); | 85 [[WindowAppleScript alloc] initWithBrowser:browser()]); |
86 [aWindow.get() insertInTabs:aTab.get()]; | 86 [aWindow.get() insertInTabs:aTab.get()]; |
87 | 87 |
88 // Represents the tab after it is inserted. | 88 // Represents the tab after it is inserted. |
89 TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:1]; | 89 TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:1]; |
90 EXPECT_EQ(GURL("http://google.com"), | 90 EXPECT_EQ(GURL("http://google.com"), |
91 GURL(base::SysNSStringToUTF8([tab URL]))); | 91 GURL(base::SysNSStringToUTF8([tab URL]))); |
92 EXPECT_EQ([tab container], aWindow.get()); | 92 EXPECT_EQ([tab container], aWindow.get()); |
93 EXPECT_NSEQ(AppleScript::kTabsProperty, | 93 EXPECT_NSEQ(AppleScript::kTabsProperty, |
94 [tab containerProperty]); | 94 [tab containerProperty]); |
95 EXPECT_NSEQ(var.get(), [tab uniqueID]); | 95 EXPECT_NSEQ(var.get(), [tab uniqueID]); |
96 } | 96 } |
97 | 97 |
98 // Insert a new tab at a particular position | 98 // Insert a new tab at a particular position |
99 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTabAtPosition) { | 99 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTabAtPosition) { |
100 // Emulate what applescript would do when creating a new tab. | 100 // Emulate what applescript would do when creating a new tab. |
101 // Emulates a script like |set var to make new tab with | 101 // Emulates a script like |set var to make new tab with |
102 // properties URL:"http://google.com"} at before tab 1|. | 102 // properties URL:"http://google.com"} at before tab 1|. |
103 scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]); | 103 base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]); |
104 scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]); | 104 base::scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]); |
105 [aTab.get() setURL:@"http://google.com"]; | 105 [aTab.get() setURL:@"http://google.com"]; |
106 scoped_nsobject<WindowAppleScript> aWindow( | 106 base::scoped_nsobject<WindowAppleScript> aWindow( |
107 [[WindowAppleScript alloc] initWithBrowser:browser()]); | 107 [[WindowAppleScript alloc] initWithBrowser:browser()]); |
108 [aWindow.get() insertInTabs:aTab.get() atIndex:0]; | 108 [aWindow.get() insertInTabs:aTab.get() atIndex:0]; |
109 | 109 |
110 // Represents the tab after it is inserted. | 110 // Represents the tab after it is inserted. |
111 TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:0]; | 111 TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:0]; |
112 EXPECT_EQ(GURL("http://google.com"), | 112 EXPECT_EQ(GURL("http://google.com"), |
113 GURL(base::SysNSStringToUTF8([tab URL]))); | 113 GURL(base::SysNSStringToUTF8([tab URL]))); |
114 EXPECT_EQ([tab container], aWindow.get()); | 114 EXPECT_EQ([tab container], aWindow.get()); |
115 EXPECT_NSEQ(AppleScript::kTabsProperty, [tab containerProperty]); | 115 EXPECT_NSEQ(AppleScript::kTabsProperty, [tab containerProperty]); |
116 EXPECT_NSEQ(var.get(), [tab uniqueID]); | 116 EXPECT_NSEQ(var.get(), [tab uniqueID]); |
117 } | 117 } |
118 | 118 |
119 // Inserting and deleting tabs. | 119 // Inserting and deleting tabs. |
120 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertAndDeleteTabs) { | 120 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertAndDeleteTabs) { |
121 scoped_nsobject<WindowAppleScript> aWindow( | 121 base::scoped_nsobject<WindowAppleScript> aWindow( |
122 [[WindowAppleScript alloc] initWithBrowser:browser()]); | 122 [[WindowAppleScript alloc] initWithBrowser:browser()]); |
123 scoped_nsobject<TabAppleScript> aTab; | 123 base::scoped_nsobject<TabAppleScript> aTab; |
124 int count; | 124 int count; |
125 for (int i = 0; i < 5; ++i) { | 125 for (int i = 0; i < 5; ++i) { |
126 for (int j = 0; j < 3; ++j) { | 126 for (int j = 0; j < 3; ++j) { |
127 aTab.reset([[TabAppleScript alloc] init]); | 127 aTab.reset([[TabAppleScript alloc] init]); |
128 [aWindow.get() insertInTabs:aTab.get()]; | 128 [aWindow.get() insertInTabs:aTab.get()]; |
129 } | 129 } |
130 count = 3 * i + 4; | 130 count = 3 * i + 4; |
131 EXPECT_EQ((int)[[aWindow.get() tabs] count], count); | 131 EXPECT_EQ((int)[[aWindow.get() tabs] count], count); |
132 } | 132 } |
133 | 133 |
134 count = (int)[[aWindow.get() tabs] count]; | 134 count = (int)[[aWindow.get() tabs] count]; |
135 for (int i = 0; i < 5; ++i) { | 135 for (int i = 0; i < 5; ++i) { |
136 for(int j = 0; j < 3; ++j) { | 136 for(int j = 0; j < 3; ++j) { |
137 [aWindow.get() removeFromTabsAtIndex:0]; | 137 [aWindow.get() removeFromTabsAtIndex:0]; |
138 } | 138 } |
139 count = count - 3; | 139 count = count - 3; |
140 EXPECT_EQ((int)[[aWindow.get() tabs] count], count); | 140 EXPECT_EQ((int)[[aWindow.get() tabs] count], count); |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 // Getting and setting values from the NSWindow. | 144 // Getting and setting values from the NSWindow. |
145 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, NSWindowTest) { | 145 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, NSWindowTest) { |
146 scoped_nsobject<WindowAppleScript> aWindow( | 146 base::scoped_nsobject<WindowAppleScript> aWindow( |
147 [[WindowAppleScript alloc] initWithBrowser:browser()]); | 147 [[WindowAppleScript alloc] initWithBrowser:browser()]); |
148 [aWindow.get() setValue:[NSNumber numberWithBool:YES] | 148 [aWindow.get() setValue:[NSNumber numberWithBool:YES] |
149 forKey:@"isMiniaturized"]; | 149 forKey:@"isMiniaturized"]; |
150 EXPECT_TRUE([[aWindow.get() valueForKey:@"isMiniaturized"] boolValue]); | 150 EXPECT_TRUE([[aWindow.get() valueForKey:@"isMiniaturized"] boolValue]); |
151 [aWindow.get() setValue:[NSNumber numberWithBool:NO] | 151 [aWindow.get() setValue:[NSNumber numberWithBool:NO] |
152 forKey:@"isMiniaturized"]; | 152 forKey:@"isMiniaturized"]; |
153 EXPECT_FALSE([[aWindow.get() valueForKey:@"isMiniaturized"] boolValue]); | 153 EXPECT_FALSE([[aWindow.get() valueForKey:@"isMiniaturized"] boolValue]); |
154 } | 154 } |
155 | 155 |
156 // Getting and setting the active tab. | 156 // Getting and setting the active tab. |
157 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, ActiveTab) { | 157 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, ActiveTab) { |
158 scoped_nsobject<WindowAppleScript> aWindow( | 158 base::scoped_nsobject<WindowAppleScript> aWindow( |
159 [[WindowAppleScript alloc] initWithBrowser:browser()]); | 159 [[WindowAppleScript alloc] initWithBrowser:browser()]); |
160 scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]); | 160 base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]); |
161 [aWindow.get() insertInTabs:aTab.get()]; | 161 [aWindow.get() insertInTabs:aTab.get()]; |
162 [aWindow.get() setActiveTabIndex:[NSNumber numberWithInt:2]]; | 162 [aWindow.get() setActiveTabIndex:[NSNumber numberWithInt:2]]; |
163 EXPECT_EQ(2, [[aWindow.get() activeTabIndex] intValue]); | 163 EXPECT_EQ(2, [[aWindow.get() activeTabIndex] intValue]); |
164 TabAppleScript* tab2 = [[aWindow.get() tabs] objectAtIndex:1]; | 164 TabAppleScript* tab2 = [[aWindow.get() tabs] objectAtIndex:1]; |
165 EXPECT_NSEQ([[aWindow.get() activeTab] uniqueID], | 165 EXPECT_NSEQ([[aWindow.get() activeTab] uniqueID], |
166 [tab2 uniqueID]); | 166 [tab2 uniqueID]); |
167 } | 167 } |
168 | 168 |
169 // Order of windows. | 169 // Order of windows. |
170 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, WindowOrder) { | 170 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, WindowOrder) { |
171 scoped_nsobject<WindowAppleScript> window2( | 171 base::scoped_nsobject<WindowAppleScript> window2( |
172 [[WindowAppleScript alloc] initWithBrowser:browser()]); | 172 [[WindowAppleScript alloc] initWithBrowser:browser()]); |
173 scoped_nsobject<WindowAppleScript> window1( | 173 base::scoped_nsobject<WindowAppleScript> window1( |
174 [[WindowAppleScript alloc] init]); | 174 [[WindowAppleScript alloc] init]); |
175 EXPECT_EQ([window1.get() windowComparator:window2.get()], NSOrderedAscending); | 175 EXPECT_EQ([window1.get() windowComparator:window2.get()], NSOrderedAscending); |
176 EXPECT_EQ([window2.get() windowComparator:window1.get()], | 176 EXPECT_EQ([window2.get() windowComparator:window1.get()], |
177 NSOrderedDescending); | 177 NSOrderedDescending); |
178 } | 178 } |
OLD | NEW |