1// Copyright (c) 2010 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.
4
5#import <Cocoa/Cocoa.h>
6
7#include "chrome/browser/profiles/profile.h"
8#include "chrome/browser/ui/browser.h"
9#import "chrome/browser/ui/cocoa/applescript/browsercrapplication+applescript.h"
10#import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
11#import "chrome/browser/ui/cocoa/applescript/window_applescript.h"
12#include "chrome/test/in_process_browser_test.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "testing/gtest_mac.h"
15#include "ui/gfx/size.h"
16
17typedef InProcessBrowserTest BrowserCrApplicationAppleScriptTest;
18
19// Create windows of different |Type|.
20IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest, Creation) {
21  // Create additional |Browser*| objects of different type.
22  Profile* profile = browser()->profile();
23  Browser* b1 = Browser::CreateForType(Browser::TYPE_POPUP, profile);
24  Browser* b2 = Browser::CreateForApp("", gfx::Size(), profile, true);
25  Browser* b3 = Browser::CreateForApp("", gfx::Size(), profile, false);
26
27  EXPECT_EQ(4U, [[NSApp appleScriptWindows] count]);
28  for (WindowAppleScript* window in [NSApp appleScriptWindows]) {
29    EXPECT_NSEQ(AppleScript::kWindowsProperty,
30                [window containerProperty]);
31    EXPECT_NSEQ(NSApp, [window container]);
32  }
33
34  // Close the additional browsers.
35  b1->CloseAllTabs();
36  b2->CloseAllTabs();
37  b3->CloseAllTabs();
38}
39
40// Insert a new window.
41IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest, InsertWindow) {
42  // Emulate what applescript would do when creating a new window.
43  // Emulate a script like |set var to make new window with properties
44  // {visible:false}|.
45  scoped_nsobject<WindowAppleScript> aWindow([[WindowAppleScript alloc] init]);
46  scoped_nsobject<NSNumber> var([[aWindow.get() uniqueID] copy]);
47  [aWindow.get() setValue:[NSNumber numberWithBool:YES] forKey:@"isVisible"];
48
49  [NSApp insertInAppleScriptWindows:aWindow.get()];
50
51  // Represents the window after it is added.
52  WindowAppleScript* window = [[NSApp appleScriptWindows] objectAtIndex:0];
53  EXPECT_NSEQ([NSNumber numberWithBool:YES],
54              [aWindow.get() valueForKey:@"isVisible"]);
55  EXPECT_EQ([window container], NSApp);
56  EXPECT_NSEQ(AppleScript::kWindowsProperty,
57              [window containerProperty]);
58  EXPECT_NSEQ(var, [window uniqueID]);
59}
60
61// Inserting and deleting windows.
62IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest,
63                       InsertAndDeleteWindows) {
64  scoped_nsobject<WindowAppleScript> aWindow;
65  int count;
66  // Create a bunch of windows.
67  for (int i = 0; i < 5; ++i) {
68    for (int j = 0; j < 3; ++j) {
69      aWindow.reset([[WindowAppleScript alloc] init]);
70      [NSApp insertInAppleScriptWindows:aWindow.get()];
71    }
72    count = 3 * i + 4;
73    EXPECT_EQ(count, (int)[[NSApp appleScriptWindows] count]);
74  }
75
76  // Remove all the windows, just created.
77  count = (int)[[NSApp appleScriptWindows] count];
78  for (int i = 0; i < 5; ++i) {
79    for(int j = 0; j < 3; ++j) {
80      [NSApp removeFromAppleScriptWindowsAtIndex:0];
81    }
82    count = count - 3;
83    EXPECT_EQ(count, (int)[[NSApp appleScriptWindows] count]);
84  }
85}
86
87// Check for objectSpecifer of the root scripting object.
88IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest, ObjectSpecifier) {
89  // Should always return nil to indicate its the root scripting object.
90  EXPECT_EQ(nil, [NSApp objectSpecifier]);
91}
92
93// Bookmark folders at the root level.
94IN_PROC_BROWSER_TEST_F(BrowserCrApplicationAppleScriptTest, BookmarkFolders) {
95  NSArray* bookmarkFolders = [NSApp bookmarkFolders];
96  EXPECT_EQ(2U, [bookmarkFolders count]);
97
98  for (BookmarkFolderAppleScript* bookmarkFolder in bookmarkFolders) {
99    EXPECT_EQ(NSApp,
100              [bookmarkFolder container]);
101    EXPECT_NSEQ(AppleScript::kBookmarkFoldersProperty,
102                [bookmarkFolder containerProperty]);
103  }
104
105  EXPECT_NSEQ(@"Other Bookmarks", [[NSApp otherBookmarks] title]);
106  EXPECT_NSEQ(@"Bookmarks Bar", [[NSApp bookmarksBar] title]);
107}
108