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