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