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 #include "base/mac/scoped_nsobject.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_tabstrip.h"
11 #import "chrome/browser/ui/cocoa/applescript/browsercrapplication+applescript.h"
12 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
13 #import "chrome/browser/ui/cocoa/applescript/window_applescript.h"
14 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/gtest_mac.h"
19 #include "ui/gfx/geometry/size.h"
21 typedef InProcessBrowserTest BrowserCrApplicationAppleScriptTest;
23 // Create windows of different |Type|.
24 IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest, Creation) {
25 // Create additional |Browser*| objects of different type.
26 Profile* profile = browser()->profile();
28 new Browser(Browser::CreateParams(Browser::TYPE_POPUP, profile,
29 browser()->host_desktop_type()));
30 Browser* b2 = new Browser(
31 Browser::CreateParams::CreateForApp(
32 "Test", true /* trusted_source */, gfx::Rect(), profile,
33 browser()->host_desktop_type()));
35 EXPECT_EQ(3U, [[NSApp appleScriptWindows] count]);
36 for (WindowAppleScript* window in [NSApp appleScriptWindows]) {
37 EXPECT_NSEQ(AppleScript::kWindowsProperty,
38 [window containerProperty]);
39 EXPECT_NSEQ(NSApp, [window container]);
42 // Close the additional browsers.
43 b1->tab_strip_model()->CloseAllTabs();
44 b2->tab_strip_model()->CloseAllTabs();
47 // Insert a new window.
48 IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest, InsertWindow) {
49 // Emulate what applescript would do when creating a new window.
50 // Emulate a script like |set var to make new window with properties
52 base::scoped_nsobject<WindowAppleScript> aWindow(
53 [[WindowAppleScript alloc] init]);
54 base::scoped_nsobject<NSNumber> var([[aWindow.get() uniqueID] copy]);
55 [aWindow.get() setValue:[NSNumber numberWithBool:YES] forKey:@"isVisible"];
57 [NSApp insertInAppleScriptWindows:aWindow.get()];
58 chrome::testing::NSRunLoopRunAllPending();
60 // Represents the window after it is added.
61 WindowAppleScript* window = [[NSApp appleScriptWindows] objectAtIndex:0];
62 EXPECT_NSEQ([NSNumber numberWithBool:YES],
63 [aWindow.get() valueForKey:@"isVisible"]);
64 EXPECT_EQ([window container], NSApp);
65 EXPECT_NSEQ(AppleScript::kWindowsProperty,
66 [window containerProperty]);
67 EXPECT_NSEQ(var, [window uniqueID]);
70 // Inserting and deleting windows.
71 IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest,
72 InsertAndDeleteWindows) {
73 base::scoped_nsobject<WindowAppleScript> aWindow;
75 // Create a bunch of windows.
76 for (int i = 0; i < 5; ++i) {
77 for (int j = 0; j < 3; ++j) {
78 aWindow.reset([[WindowAppleScript alloc] init]);
79 [NSApp insertInAppleScriptWindows:aWindow.get()];
82 EXPECT_EQ(count, (int)[[NSApp appleScriptWindows] count]);
85 // Remove all the windows, just created.
86 count = (int)[[NSApp appleScriptWindows] count];
87 for (int i = 0; i < 5; ++i) {
88 for(int j = 0; j < 3; ++j) {
89 [NSApp removeFromAppleScriptWindowsAtIndex:0];
92 EXPECT_EQ(count, (int)[[NSApp appleScriptWindows] count]);
96 // Check for objectSpecifer of the root scripting object.
97 IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest, ObjectSpecifier) {
98 // Should always return nil to indicate its the root scripting object.
99 EXPECT_EQ(nil, [NSApp objectSpecifier]);
102 // Bookmark folders at the root level.
103 // http://code.google.com/p/chromium/issues/detail?id=84299
104 IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest,
105 DISABLED_BookmarkFolders) {
106 NSArray* bookmarkFolders = [NSApp bookmarkFolders];
107 EXPECT_EQ(2U, [bookmarkFolders count]);
109 for (BookmarkFolderAppleScript* bookmarkFolder in bookmarkFolders) {
111 [bookmarkFolder container]);
112 EXPECT_NSEQ(AppleScript::kBookmarkFoldersProperty,
113 [bookmarkFolder containerProperty]);
116 EXPECT_NSEQ(@"Other Bookmarks", [[NSApp otherBookmarks] title]);
117 EXPECT_NSEQ(@"Bookmarks Bar", [[NSApp bookmarksBar] title]);