summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/extensions/extension_installed_bubble_controller_unittest.mm
blob: 1ae61bcad12d81c7e8e437ffc502608c80277e76 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) 2012 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>

#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#import "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#import "chrome/browser/ui/cocoa/extensions/extension_installed_bubble_controller.h"
#import "chrome/browser/ui/cocoa/info_bubble_window.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#include "ui/gfx/codec/png_codec.h"

using extensions::Extension;

// ExtensionInstalledBubbleController with removePageActionPreview overridden
// to a no-op, because pageActions are not yet hooked up in the test browser.
@interface ExtensionInstalledBubbleControllerForTest :
    ExtensionInstalledBubbleController {
}

// Do nothing, because browser window is not set up with page actions
// for unit testing.
- (void)removePageActionPreview;

@end

@implementation ExtensionInstalledBubbleControllerForTest

- (void)removePageActionPreview { }

@end

namespace keys = extensions::manifest_keys;

class ExtensionInstalledBubbleControllerTest : public CocoaProfileTest {

 public:
  virtual void SetUp() {
    CocoaProfileTest::SetUp();
    ASSERT_TRUE(browser());
    window_ = browser()->window()->GetNativeWindow();
    icon_ = LoadTestIcon();
    CommandLine command_line(CommandLine::NO_PROGRAM);
    extension_service_ = static_cast<extensions::TestExtensionSystem*>(
        extensions::ExtensionSystem::Get(profile()))->CreateExtensionService(
            &command_line, base::FilePath(), false);
  }

  // Load test icon from extension test directory.
  SkBitmap LoadTestIcon() {
    base::FilePath path;
    PathService::Get(chrome::DIR_TEST_DATA, &path);
    path = path.AppendASCII("extensions").AppendASCII("icon1.png");

    std::string file_contents;
    base::ReadFileToString(path, &file_contents);
    const unsigned char* data =
        reinterpret_cast<const unsigned char*>(file_contents.data());

    SkBitmap bitmap;
    gfx::PNGCodec::Decode(data, file_contents.length(), &bitmap);
    return bitmap;
  }

  // Create a skeletal framework of either page action or browser action
  // type.  This extension only needs to have a type and a name to initialize
  // the ExtensionInstalledBubble for unit testing.
  scoped_refptr<Extension> CreateExtension(
        extension_installed_bubble::ExtensionType type) {
    base::FilePath path;
    PathService::Get(chrome::DIR_TEST_DATA, &path);
    path = path.AppendASCII("extensions").AppendASCII("dummy");

    DictionaryValue extension_input_value;
    extension_input_value.SetString(keys::kVersion, "1.0.0.0");
    if (type == extension_installed_bubble::kPageAction) {
      extension_input_value.SetString(keys::kName, "page action extension");
      DictionaryValue* action = new DictionaryValue;
      action->SetString(keys::kPageActionId, "ExtensionActionId");
      action->SetString(keys::kPageActionDefaultTitle, "ExtensionActionTitle");
      action->SetString(keys::kPageActionDefaultIcon, "image1.png");
      ListValue* action_list = new ListValue;
      action_list->Append(action);
      extension_input_value.Set(keys::kPageActions, action_list);
    } else if (type == extension_installed_bubble::kBrowserAction) {
      extension_input_value.SetString(keys::kName, "browser action extension");
      DictionaryValue* browser_action = new DictionaryValue;
      // An empty dictionary is enough to create a Browser Action.
      extension_input_value.Set(keys::kBrowserAction, browser_action);
    } else if (type == extension_installed_bubble::kApp) {
      extension_input_value.SetString(keys::kName, "test app");
      extension_input_value.SetString(keys::kLaunchWebURL,
                                      "http://www.example.com");
    }

    std::string error;
    scoped_refptr<Extension> extension =
        Extension::Create(path, extensions::Manifest::INVALID_LOCATION,
                          extension_input_value, Extension::NO_FLAGS, &error);
    extension_service_->AddExtension(extension.get());
    return extension;
  }

  // Required to initialize the extension installed bubble.
  NSWindow* window_;  // weak, owned by CocoaProfileTest.

  ExtensionService* extension_service_;

  // Skeleton extension to be tested; reinitialized for each test.
  scoped_refptr<Extension> extension_;

  // The icon_ to be loaded into the bubble window.
  SkBitmap icon_;
};

// Confirm that window sizes are set correctly for a page action extension.
TEST_F(ExtensionInstalledBubbleControllerTest, PageActionTest) {
  extension_ = CreateExtension(extension_installed_bubble::kPageAction);
  ExtensionInstalledBubbleControllerForTest* controller =
      [[ExtensionInstalledBubbleControllerForTest alloc]
          initWithParentWindow:window_
                     extension:extension_.get()
                        bundle:NULL
                       browser:browser()
                          icon:icon_];
  EXPECT_TRUE(controller);

  // Initialize window without having to calculate tabstrip locations.
  [controller initializeWindow];
  EXPECT_TRUE([controller window]);

  int height = [controller calculateWindowHeight];
  // Height should equal the vertical padding + height of all messages.
  int correctHeight = 2 * extension_installed_bubble::kOuterVerticalMargin +
      2 * extension_installed_bubble::kInnerVerticalMargin +
      NSHeight([controller headingFrame]) +
      NSHeight([controller frameOfHowToUse]) +
      NSHeight([controller frameOfHowToManage]);
  if ([controller showSyncPromo]) {
    correctHeight += NSHeight([controller frameOfSigninPromo]) +
                     extension_installed_bubble::kInnerVerticalMargin;
  }
  EXPECT_EQ(height, correctHeight);
  [controller setMessageFrames:height];

  NSRect msg1Frame = [controller headingFrame];
  NSRect msg2Frame = [controller frameOfHowToUse];
  NSRect msg3Frame = [controller frameOfHowToManage];
  NSRect msg4Frame = [controller frameOfSigninPromo];

  int next_y = extension_installed_bubble::kOuterVerticalMargin;
  if ([controller showSyncPromo]) {
    // Bottom message should be kOuterVerticalMargin pixels above window edge.
    EXPECT_EQ(next_y, NSMinY(msg4Frame));
    next_y = NSMinY(msg4Frame) + NSHeight(msg4Frame) +
            extension_installed_bubble::kInnerVerticalMargin;
  }

  // HowToManage frame should be kInnerVerticalMargin pixels above sync promo,
  // unless sync promo is hidden, then kOuterVerticalMargin pixels above.
  EXPECT_EQ(next_y, NSMinY(msg3Frame));

  // Page action message should be kInnerVerticalMargin pixels above bottom msg.
  EXPECT_EQ(NSMinY(msg3Frame) + NSHeight(msg3Frame) +
                extension_installed_bubble::kInnerVerticalMargin,
            NSMinY(msg2Frame));

  // Top message should be kInnerVerticalMargin pixels above Page action msg.
  EXPECT_EQ(NSMinY(msg2Frame) + NSHeight(msg2Frame) +
                extension_installed_bubble::kInnerVerticalMargin,
            NSMinY(msg1Frame));

  [controller setPageActionPreviewShowing:NO];
  [controller close];
}

TEST_F(ExtensionInstalledBubbleControllerTest, BrowserActionTest) {
  extension_ = CreateExtension(extension_installed_bubble::kBrowserAction);
  ExtensionInstalledBubbleControllerForTest* controller =
      [[ExtensionInstalledBubbleControllerForTest alloc]
          initWithParentWindow:window_
                     extension:extension_.get()
                        bundle:NULL
                       browser:browser()
                          icon:icon_];
  EXPECT_TRUE(controller);

  // Initialize window without having to calculate tabstrip locations.
  [controller initializeWindow];
  EXPECT_TRUE([controller window]);

  int height = [controller calculateWindowHeight];
  // Height should equal the vertical padding + height of all messages.
  int correctHeight = 2 * extension_installed_bubble::kOuterVerticalMargin +
      2 * extension_installed_bubble::kInnerVerticalMargin +
      NSHeight([controller headingFrame]) +
      NSHeight([controller frameOfHowToUse]) +
      NSHeight([controller frameOfHowToManage]);
  if ([controller showSyncPromo]) {
    correctHeight += NSHeight([controller frameOfSigninPromo]) +
                     extension_installed_bubble::kInnerVerticalMargin;
  }
  EXPECT_EQ(height, correctHeight);
  [controller setMessageFrames:height];

  NSRect msg1Frame = [controller headingFrame];
  NSRect msg2Frame = [controller frameOfHowToUse];
  NSRect msg3Frame = [controller frameOfHowToManage];
  NSRect msg4Frame = [controller frameOfSigninPromo];

  int next_y = extension_installed_bubble::kOuterVerticalMargin;
  if ([controller showSyncPromo]) {
    // Bottom message should be kOuterVerticalMargin pixels above window edge.
    EXPECT_EQ(next_y, NSMinY(msg4Frame));
    next_y = NSMinY(msg4Frame) + NSHeight(msg4Frame) +
            extension_installed_bubble::kInnerVerticalMargin;
  }

  // HowToManage frame should be kInnerVerticalMargin pixels above sync promo,
  // unless sync promo is hidden, then kOuterVerticalMargin pixels above.
  EXPECT_EQ(next_y, NSMinY(msg3Frame));

  // Browser action message should be kInnerVerticalMargin pixels above bottom
  // msg.
  EXPECT_EQ(NSMinY(msg3Frame) + NSHeight(msg3Frame) +
                extension_installed_bubble::kInnerVerticalMargin,
            NSMinY(msg2Frame));

  // Top message should be kInnerVerticalMargin pixels above Browser action msg.
  EXPECT_EQ(NSMinY(msg2Frame) + NSHeight(msg2Frame) +
                extension_installed_bubble::kInnerVerticalMargin,
            NSMinY(msg1Frame));

  [controller close];
}

TEST_F(ExtensionInstalledBubbleControllerTest, ParentClose) {
  extension_ = CreateExtension(extension_installed_bubble::kBrowserAction);
  ExtensionInstalledBubbleControllerForTest* controller =
      [[ExtensionInstalledBubbleControllerForTest alloc]
          initWithParentWindow:window_
                     extension:extension_.get()
                        bundle:NULL
                       browser:browser()
                          icon:icon_];
  EXPECT_TRUE(controller);

  // Bring up the window and disable close animation.
  [controller showWindow:nil];
  NSWindow* bubbleWindow = [controller window];
  ASSERT_TRUE([bubbleWindow isKindOfClass:[InfoBubbleWindow class]]);
  [static_cast<InfoBubbleWindow*>(bubbleWindow)
      setAllowedAnimations:info_bubble::kAnimateNone];

  // Observe whether the bubble window closes.
  NSString* notification = NSWindowWillCloseNotification;
  id observer = [OCMockObject observerMock];
  [[observer expect] notificationWithName:notification object:bubbleWindow];
  [[NSNotificationCenter defaultCenter]
    addMockObserver:observer name:notification object:bubbleWindow];

  // The bubble window goes from visible to not-visible.
  EXPECT_TRUE([bubbleWindow isVisible]);
  [window_ close];
  EXPECT_FALSE([bubbleWindow isVisible]);

  [[NSNotificationCenter defaultCenter] removeObserver:observer];

  // Check that the appropriate notification was received.
  EXPECT_OCMOCK_VERIFY(observer);
}

TEST_F(ExtensionInstalledBubbleControllerTest, AppTest) {
  extension_ = CreateExtension(extension_installed_bubble::kApp);
  ExtensionInstalledBubbleControllerForTest* controller =
      [[ExtensionInstalledBubbleControllerForTest alloc]
          initWithParentWindow:window_
                     extension:extension_.get()
                        bundle:NULL
                       browser:browser()
                          icon:icon_];
  EXPECT_TRUE(controller);
  [controller initializeWindow];
  EXPECT_TRUE([controller window]);

  int height = [controller calculateWindowHeight];

  // Make sure there is always enough room for the icon and margin.
  int minHeight = extension_installed_bubble::kIconSize +
    (2 * extension_installed_bubble::kOuterVerticalMargin);
  EXPECT_GT(height, minHeight);

  // Make sure the "show me" link is visible.
  EXPECT_FALSE([[controller appInstalledShortcutLink] isHidden]);

  [controller close];
}