summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/applescript/window_applescript_test.mm
blob: 9ebba04726b0dd4bbaa0d09712f15e68f412472d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#import "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#import "chrome/browser/app_controller_mac.h"
#import "chrome/browser/chrome_browser_application_mac.h"
#include "chrome/browser/profiles/profile.h"
#import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
#import "chrome/browser/ui/cocoa/applescript/error_applescript.h"
#import "chrome/browser/ui/cocoa/applescript/tab_applescript.h"
#import "chrome/browser/ui/cocoa/applescript/window_applescript.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "url/gurl.h"

typedef InProcessBrowserTest WindowAppleScriptTest;

// Create a window in default/normal mode.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, DefaultCreation) {
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] init]);
  EXPECT_TRUE(aWindow.get());
  NSString* mode = [aWindow.get() mode];
  EXPECT_NSEQ(AppleScript::kNormalWindowMode,
              mode);
}

// Create a window with a |NULL profile|.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithNoProfile) {
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithProfile:NULL]);
  EXPECT_FALSE(aWindow.get());
}

// Create a window with a particular profile.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithProfile) {
  Profile* lastProfile = [[NSApp delegate] lastProfile];
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithProfile:lastProfile]);
  EXPECT_TRUE(aWindow.get());
  EXPECT_TRUE([aWindow.get() uniqueID]);
}

// Create a window with no |Browser*|.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithNoBrowser) {
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithBrowser:NULL]);
  EXPECT_FALSE(aWindow.get());
}

// Create a window with |Browser*| already present.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, CreationWithBrowser) {
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithBrowser:browser()]);
  EXPECT_TRUE(aWindow.get());
  EXPECT_TRUE([aWindow.get() uniqueID]);
}

// Tabs within the window.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, Tabs) {
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithBrowser:browser()]);
  NSArray* tabs = [aWindow.get() tabs];
  EXPECT_EQ(1U, [tabs count]);
  TabAppleScript* tab1 = [tabs objectAtIndex:0];
  EXPECT_EQ([tab1 container], aWindow.get());
  EXPECT_NSEQ(AppleScript::kTabsProperty,
              [tab1 containerProperty]);
}

// Insert a new tab.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTab) {
  // Emulate what applescript would do when creating a new tab.
  // Emulates a script like |set var to make new tab with
  // properties URL:"http://google.com"}|.
  base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
  base::scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]);
  [aTab.get() setURL:@"http://google.com"];
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithBrowser:browser()]);
  [aWindow.get() insertInTabs:aTab.get()];

  // Represents the tab after it is inserted.
  TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:1];
  EXPECT_EQ(GURL("http://google.com"),
            GURL(base::SysNSStringToUTF8([tab URL])));
  EXPECT_EQ([tab container], aWindow.get());
  EXPECT_NSEQ(AppleScript::kTabsProperty,
              [tab containerProperty]);
  EXPECT_NSEQ(var.get(), [tab uniqueID]);
}

// Insert a new tab at a particular position
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTabAtPosition) {
  // Emulate what applescript would do when creating a new tab.
  // Emulates a script like |set var to make new tab with
  // properties URL:"http://google.com"} at before tab 1|.
  base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
  base::scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]);
  [aTab.get() setURL:@"http://google.com"];
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithBrowser:browser()]);
  [aWindow.get() insertInTabs:aTab.get() atIndex:0];

  // Represents the tab after it is inserted.
  TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:0];
  EXPECT_EQ(GURL("http://google.com"),
            GURL(base::SysNSStringToUTF8([tab URL])));
  EXPECT_EQ([tab container], aWindow.get());
  EXPECT_NSEQ(AppleScript::kTabsProperty, [tab containerProperty]);
  EXPECT_NSEQ(var.get(), [tab uniqueID]);
}

// Inserting and deleting tabs.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertAndDeleteTabs) {
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithBrowser:browser()]);
  base::scoped_nsobject<TabAppleScript> aTab;
  int count;
  for (int i = 0; i < 5; ++i) {
    for (int j = 0; j < 3; ++j) {
      aTab.reset([[TabAppleScript alloc] init]);
      [aWindow.get() insertInTabs:aTab.get()];
    }
    count = 3 * i + 4;
    EXPECT_EQ((int)[[aWindow.get() tabs] count], count);
  }

  count = (int)[[aWindow.get() tabs] count];
  for (int i = 0; i < 5; ++i) {
    for(int j = 0; j < 3; ++j) {
      [aWindow.get() removeFromTabsAtIndex:0];
    }
    count = count - 3;
    EXPECT_EQ((int)[[aWindow.get() tabs] count], count);
  }
}

// Getting and setting values from the NSWindow.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, NSWindowTest) {
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithBrowser:browser()]);
  [aWindow.get() setValue:[NSNumber numberWithBool:YES]
                   forKey:@"isMiniaturized"];
  EXPECT_TRUE([[aWindow.get() valueForKey:@"isMiniaturized"] boolValue]);
  [aWindow.get() setValue:[NSNumber numberWithBool:NO]
                   forKey:@"isMiniaturized"];
  EXPECT_FALSE([[aWindow.get() valueForKey:@"isMiniaturized"] boolValue]);
}

// Getting and setting the active tab.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, ActiveTab) {
  base::scoped_nsobject<WindowAppleScript> aWindow(
      [[WindowAppleScript alloc] initWithBrowser:browser()]);
  base::scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
  [aWindow.get() insertInTabs:aTab.get()];
  [aWindow.get() setActiveTabIndex:[NSNumber numberWithInt:2]];
  EXPECT_EQ(2, [[aWindow.get() activeTabIndex] intValue]);
  TabAppleScript* tab2 = [[aWindow.get() tabs] objectAtIndex:1];
  EXPECT_NSEQ([[aWindow.get() activeTab] uniqueID],
              [tab2 uniqueID]);
}

// Order of windows.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, WindowOrder) {
  base::scoped_nsobject<WindowAppleScript> window2(
      [[WindowAppleScript alloc] initWithBrowser:browser()]);
  base::scoped_nsobject<WindowAppleScript> window1(
      [[WindowAppleScript alloc] init]);
  EXPECT_EQ([window1.get() windowComparator:window2.get()], NSOrderedAscending);
  EXPECT_EQ([window2.get() windowComparator:window1.get()],
            NSOrderedDescending);
}