Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / applescript / window_applescript_test.mm
blob013ce958013d39331af808c87e51e90476b6c79c
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/browser/ui/cocoa/run_loop_testing.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #import "testing/gtest_mac.h"
20 #include "url/gurl.h"
22 typedef InProcessBrowserTest WindowAppleScriptTest;
24 // Create a window in default/normal mode.
25 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, DefaultCreation) {
26   base::scoped_nsobject<WindowAppleScript> aWindow(
27       [[WindowAppleScript alloc] init]);
28   EXPECT_TRUE(aWindow.get());
29   NSString* mode = [aWindow.get() mode];
30   EXPECT_NSEQ(AppleScript::kNormalWindowMode,
31               mode);
34 // Create a window with a |NULL profile|.
35 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithNoProfile) {
36   base::scoped_nsobject<WindowAppleScript> aWindow(
37       [[WindowAppleScript alloc] initWithProfile:NULL]);
38   EXPECT_FALSE(aWindow.get());
41 // Create a window with a particular profile.
42 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithProfile) {
43   Profile* lastProfile = [[NSApp delegate] lastProfile];
44   base::scoped_nsobject<WindowAppleScript> aWindow(
45       [[WindowAppleScript alloc] initWithProfile:lastProfile]);
46   EXPECT_TRUE(aWindow.get());
47   EXPECT_TRUE([aWindow.get() uniqueID]);
50 // Create a window with no |Browser*|.
51 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithNoBrowser) {
52   base::scoped_nsobject<WindowAppleScript> aWindow(
53       [[WindowAppleScript alloc] initWithBrowser:NULL]);
54   EXPECT_FALSE(aWindow.get());
57 // Create a window with |Browser*| already present.
58 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithBrowser) {
59   base::scoped_nsobject<WindowAppleScript> aWindow(
60       [[WindowAppleScript alloc] initWithBrowser:browser()]);
61   EXPECT_TRUE(aWindow.get());
62   EXPECT_TRUE([aWindow.get() uniqueID]);
65 // Tabs within the window.
66 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, Tabs) {
67   base::scoped_nsobject<WindowAppleScript> aWindow(
68       [[WindowAppleScript alloc] initWithBrowser:browser()]);
69   NSArray* tabs = [aWindow.get() tabs];
70   EXPECT_EQ(1U, [tabs count]);
71   TabAppleScript* tab1 = [tabs objectAtIndex:0];
72   EXPECT_EQ([tab1 container], aWindow.get());
73   EXPECT_NSEQ(AppleScript::kTabsProperty,
74               [tab1 containerProperty]);
77 // Insert a new tab.
78 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTab) {
79   // Emulate what applescript would do when creating a new tab.
80   // Emulates a script like |set var to make new tab with
81   // properties URL:"http://google.com"}|.
82   base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
83   base::scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]);
84   [aTab.get() setURL:@"http://google.com"];
85   base::scoped_nsobject<WindowAppleScript> aWindow(
86       [[WindowAppleScript alloc] initWithBrowser:browser()]);
87   [aWindow.get() insertInTabs:aTab.get()];
89   // Represents the tab after it is inserted.
90   TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:1];
91   EXPECT_EQ(GURL("http://google.com"),
92             GURL(base::SysNSStringToUTF8([tab URL])));
93   EXPECT_EQ([tab container], aWindow.get());
94   EXPECT_NSEQ(AppleScript::kTabsProperty,
95               [tab containerProperty]);
96   EXPECT_NSEQ(var.get(), [tab uniqueID]);
99 // Insert a new tab at a particular position
100 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTabAtPosition) {
101   // Emulate what applescript would do when creating a new tab.
102   // Emulates a script like |set var to make new tab with
103   // properties URL:"http://google.com"} at before tab 1|.
104   base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
105   base::scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]);
106   [aTab.get() setURL:@"http://google.com"];
107   base::scoped_nsobject<WindowAppleScript> aWindow(
108       [[WindowAppleScript alloc] initWithBrowser:browser()]);
109   [aWindow.get() insertInTabs:aTab.get() atIndex:0];
111   // Represents the tab after it is inserted.
112   TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:0];
113   EXPECT_EQ(GURL("http://google.com"),
114             GURL(base::SysNSStringToUTF8([tab URL])));
115   EXPECT_EQ([tab container], aWindow.get());
116   EXPECT_NSEQ(AppleScript::kTabsProperty, [tab containerProperty]);
117   EXPECT_NSEQ(var.get(), [tab uniqueID]);
120 // Inserting and deleting tabs.
121 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertAndDeleteTabs) {
122   base::scoped_nsobject<WindowAppleScript> aWindow(
123       [[WindowAppleScript alloc] initWithBrowser:browser()]);
124   base::scoped_nsobject<TabAppleScript> aTab;
125   int count;
126   for (int i = 0; i < 5; ++i) {
127     for (int j = 0; j < 3; ++j) {
128       aTab.reset([[TabAppleScript alloc] init]);
129       [aWindow.get() insertInTabs:aTab.get()];
130     }
131     count = 3 * i + 4;
132     EXPECT_EQ((int)[[aWindow.get() tabs] count], count);
133   }
135   count = (int)[[aWindow.get() tabs] count];
136   for (int i = 0; i < 5; ++i) {
137     for(int j = 0; j < 3; ++j) {
138       [aWindow.get() removeFromTabsAtIndex:0];
139     }
140     count = count - 3;
141     EXPECT_EQ((int)[[aWindow.get() tabs] count], count);
142   }
145 // Getting and setting values from the NSWindow.
146 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, NSWindowTest) {
147   base::scoped_nsobject<WindowAppleScript> aWindow(
148       [[WindowAppleScript alloc] initWithBrowser:browser()]);
149   [aWindow.get() setValue:[NSNumber numberWithBool:YES]
150                    forKey:@"isMiniaturized"];
151   EXPECT_TRUE([[aWindow.get() valueForKey:@"isMiniaturized"] boolValue]);
152   [aWindow.get() setValue:[NSNumber numberWithBool:NO]
153                    forKey:@"isMiniaturized"];
154   EXPECT_FALSE([[aWindow.get() valueForKey:@"isMiniaturized"] boolValue]);
157 // Getting and setting the active tab.
158 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, ActiveTab) {
159   base::scoped_nsobject<WindowAppleScript> aWindow(
160       [[WindowAppleScript alloc] initWithBrowser:browser()]);
161   base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
162   [aWindow.get() insertInTabs:aTab.get()];
163   [aWindow.get() setActiveTabIndex:[NSNumber numberWithInt:2]];
164   EXPECT_EQ(2, [[aWindow.get() activeTabIndex] intValue]);
165   TabAppleScript* tab2 = [[aWindow.get() tabs] objectAtIndex:1];
166   EXPECT_NSEQ([[aWindow.get() activeTab] uniqueID],
167               [tab2 uniqueID]);
170 // Order of windows.
171 IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, WindowOrder) {
172   base::scoped_nsobject<WindowAppleScript> window2(
173       [[WindowAppleScript alloc] initWithBrowser:browser()]);
174   base::scoped_nsobject<WindowAppleScript> window1(
175       [[WindowAppleScript alloc] init]);
176   chrome::testing::NSRunLoopRunAllPending();
177   EXPECT_EQ([window1.get() windowComparator:window2.get()], NSOrderedAscending);
178   EXPECT_EQ([window2.get() windowComparator:window1.get()],
179             NSOrderedDescending);