File: extension_installed_bubble_controller_unittest.mm

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (385 lines) | stat: -rw-r--r-- 15,144 bytes parent folder | download
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// 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 "chrome/browser/ui/cocoa/extensions/extension_installed_bubble_controller.h"

#import <Cocoa/Cocoa.h>
#include <stddef.h>

#include <memory>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/macros.h"
#include "base/path_service.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/commands/command_service.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/info_bubble_window.h"
#import "chrome/browser/ui/cocoa/test/cocoa_profile_test.h"
#include "chrome/browser/ui/extensions/extension_installed_bubble.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h"
#include "components/crx_file/id_util.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/value_builder.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;
using extensions::DictionaryBuilder;

class ExtensionInstalledBubbleControllerTest : public CocoaProfileTest {
 protected:
  ExtensionInstalledBubbleControllerTest() {}
  ~ExtensionInstalledBubbleControllerTest() override {}

  enum ExtensionType {
    BROWSER_ACTION,
    PAGE_ACTION,
    APP,
  };

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

  // Adds a WebContents to the tab strip.
  void AddWebContents() {
    content::WebContents* web_contents =
        content::WebContents::Create(content::WebContents::CreateParams(
            profile(), content::SiteInstance::Create(profile())));
    browser()->tab_strip_model()->AppendWebContents(web_contents, true);
  }

  // Create a simple extension of the given |type| and manifest |location|, and
  // optionally with an associated keybinding.
  void CreateExtension(ExtensionType type,
                       bool has_keybinding,
                       extensions::Manifest::Location location) {
    DictionaryBuilder manifest;
    manifest.Set("version", "1.0");
    manifest.Set("name", "extension");
    manifest.Set("manifest_version", 2);
    switch (type) {
      case PAGE_ACTION:
        manifest.Set("page_action", DictionaryBuilder().Build());
        break;
      case BROWSER_ACTION:
        manifest.Set("browser_action", DictionaryBuilder().Build());
        break;
      case APP:
        manifest.Set(
            "app",
            DictionaryBuilder()
                .Set("launch", DictionaryBuilder()
                                   .Set("web_url", "http://www.example.com")
                                   .Build())
                .Build());
        break;
    }

    if (has_keybinding) {
      DictionaryBuilder command;
      command.Set(type == PAGE_ACTION ? "_execute_page_action"
                                      : "_execute_browser_action",
                  DictionaryBuilder()
                      .Set("suggested_key", DictionaryBuilder()
                                                .Set("mac", "MacCtrl+Shift+E")
                                                .Set("default", "Ctrl+Shift+E")
                                                .Build())
                      .Build());
      manifest.Set("commands", command.Build());
    }

    extension_ = extensions::ExtensionBuilder()
                     .SetManifest(manifest.Build())
                     .SetID(crx_file::id_util::GenerateId("foo"))
                     .SetLocation(location)
                     .Build();
    extensionService_->AddExtension(extension_.get());
    if (has_keybinding) {
      // Slight hack: manually notify the command service of the extension since
      // it doesn't go through the normal installation flow.
      extensions::CommandService::Get(profile())->UpdateKeybindingsForTest(
          extension_.get());
    }
  }
  void CreateExtension(ExtensionType type, bool has_keybinding) {
    CreateExtension(type, has_keybinding, extensions::Manifest::INTERNAL);
  }

  // Create and return an ExtensionInstalledBubbleController and instruct it to
  // show itself.
  ExtensionInstalledBubbleController* CreateController() {
    extensionBubble_.reset(
        new ExtensionInstalledBubble(extension_.get(), browser(), icon_));
    extensionBubble_->Initialize();
    ExtensionInstalledBubbleController* controller =
        [[ExtensionInstalledBubbleController alloc]
            initWithParentWindow:window_
                 extensionBubble:extensionBubble_.get()];

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

    return controller;
  }

  NSWindow* window() { return window_; }

 private:
  // 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;
  }

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

  // The associated ExtensionService, owned by the ExtensionSystem.
  ExtensionService* extensionService_;

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

  // The bubble that tests are run on.
  std::unique_ptr<ExtensionInstalledBubble> extensionBubble_;

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

  DISALLOW_COPY_AND_ASSIGN(ExtensionInstalledBubbleControllerTest);
};

// We don't want to just test the bounds of these frames, because that results
// in a change detector test (and just duplicates the logic in the class).
// Instead, we do a few sanity checks.
void SanityCheckFrames(NSRect frames[], size_t size) {
  for (size_t i = 0; i < size; ++i) {
    // Check 1: Non-hidden views should have a non-empty frame.
    EXPECT_FALSE(NSIsEmptyRect(frames[i])) <<
        "Frame at index " << i << " is empty";
    // Check 2: No frames should overlap.
    for (size_t j = 0; j < i; ++j) {
      EXPECT_FALSE(NSIntersectsRect(frames[i], frames[j])) <<
          "Frame at index " << i << " intersects frame at index " << j;
    }
  }
}

// Test the basic layout of the bubble for an extension that is from the store.
TEST_F(ExtensionInstalledBubbleControllerTest,
       BubbleLayoutFromStoreNoKeybinding) {
  CreateExtension(BROWSER_ACTION, false);
  ExtensionInstalledBubbleController* controller = CreateController();
  ASSERT_TRUE(controller);

  // The extension bubble should have the "how to use", "how to manage", and
  // "sign in promo" areas. Since it doesn't have an associated keybinding, it
  // shouldn't have the "manage shortcut" view.
  EXPECT_FALSE([[controller howToUse] isHidden]);
  EXPECT_FALSE([[controller howToManage] isHidden]);
  EXPECT_FALSE([[controller promoContainer] isHidden]);
  EXPECT_TRUE([[controller manageShortcutLink] isHidden]);

  NSRect headingFrame = [[controller heading] frame];
  NSRect closeFrame = [[controller closeButton] frame];
  NSRect howToUseFrame = [[controller howToUse] frame];
  NSRect howToManageFrame = [[controller howToManage] frame];
  NSRect syncPromoFrame = [[controller promoContainer] frame];
  NSRect iconFrame = [[controller iconImage] frame];

  NSRect frames[] = {headingFrame, closeFrame, howToUseFrame, howToManageFrame,
                     syncPromoFrame, iconFrame};
  SanityCheckFrames(frames, arraysize(frames));

  // Check the overall layout of the bubble; it should be:
  // |------| | Heading        |
  // | icon | | How to Use     |
  // |------| | How to Manage  |
  // |-------------------------|
  // |       Sync Promo        |
  // |-------------------------|
  EXPECT_GT(NSMinY(headingFrame), NSMinY(howToUseFrame));
  EXPECT_GT(NSMinY(howToUseFrame), NSMinY(howToManageFrame));
  EXPECT_GT(NSMinY(howToManageFrame), NSMinY(syncPromoFrame));
  EXPECT_GT(NSMinY(iconFrame), NSMinY(syncPromoFrame));
  EXPECT_GT(NSMinY(iconFrame), 0);
  EXPECT_EQ(NSMinY(syncPromoFrame), 0);

  [controller close];
}

// Test the layout of a bubble for an extension that is from the store with an
// associated keybinding.
TEST_F(ExtensionInstalledBubbleControllerTest,
       BubbleLayoutFromStoreWithKeybinding) {
  CreateExtension(BROWSER_ACTION, true);
  ExtensionInstalledBubbleController* controller = CreateController();
  ASSERT_TRUE(controller);

  // Since the extension has a keybinding, the "how to manage" section is
  // hidden. The other fields are present.
  EXPECT_FALSE([[controller howToUse] isHidden]);
  EXPECT_TRUE([[controller howToManage] isHidden]);
  EXPECT_FALSE([[controller manageShortcutLink] isHidden]);
  EXPECT_FALSE([[controller promoContainer] isHidden]);

  NSRect headingFrame = [[controller heading] frame];
  NSRect closeFrame = [[controller closeButton] frame];
  NSRect howToUseFrame = [[controller howToUse] frame];
  NSRect manageShortcutFrame = [[controller manageShortcutLink] frame];
  NSRect syncPromoFrame = [[controller promoContainer] frame];
  NSRect iconFrame = [[controller iconImage] frame];

  NSRect frames[] = {headingFrame, closeFrame, howToUseFrame,
                     manageShortcutFrame, syncPromoFrame, iconFrame};
  SanityCheckFrames(frames, arraysize(frames));

  // Layout should be:
  // |------| | Heading         |
  // | icon | | How to Use      |
  // |------| | Manage shortcut |
  // |--------------------------|
  // |       Sync Promo         |
  // |--------------------------|
  EXPECT_GT(NSMinY(headingFrame), NSMinY(howToUseFrame));
  EXPECT_GT(NSMinY(howToUseFrame), NSMinY(manageShortcutFrame));
  EXPECT_GT(NSMinY(manageShortcutFrame), NSMinY(syncPromoFrame));
  EXPECT_GT(NSMinY(iconFrame), NSMinY(syncPromoFrame));
  EXPECT_GT(NSMinY(iconFrame), 0);
  EXPECT_EQ(NSMinY(syncPromoFrame), 0);

  [controller close];
}

// Test the layout of a bubble for an unpacked extension (which is not syncable)
// and verify that the page action preview is enabled.
TEST_F(ExtensionInstalledBubbleControllerTest, BubbleLayoutPageActionUnpacked) {
  // Tests legacy behavior.
  extensions::FeatureSwitch::ScopedOverride extension_action_override(
      extensions::FeatureSwitch::extension_action_redesign(), false);
  // Page actions need a web contents (for the location bar to not break).
  AddWebContents();

  LocationBarTesting* locationBar =
      browser()->window()->GetLocationBar()->GetLocationBarForTesting();
  // To start, there should be no visible page actions.
  EXPECT_EQ(0, locationBar->PageActionVisibleCount());

  CreateExtension(PAGE_ACTION, true, extensions::Manifest::UNPACKED);
  ExtensionInstalledBubbleController* controller = CreateController();
  ASSERT_TRUE(controller);

  // The extension has a keybinding (so the "how to manage" view is hidden) and
  // is an unpacked extension (so the "sign in promo" view is also hidden).
  EXPECT_FALSE([[controller howToUse] isHidden]);
  EXPECT_TRUE([[controller howToManage] isHidden]);
  EXPECT_TRUE([[controller promoContainer] isHidden]);
  EXPECT_FALSE([[controller manageShortcutLink] isHidden]);

  NSRect headingFrame = [[controller heading] frame];
  NSRect closeFrame = [[controller closeButton] frame];
  NSRect howToUseFrame = [[controller howToUse] frame];
  NSRect howToManageFrame = [[controller howToManage] frame];
  NSRect iconFrame = [[controller iconImage] frame];

  NSRect frames[] = {headingFrame, closeFrame, howToUseFrame, howToManageFrame,
                     iconFrame};
  SanityCheckFrames(frames, arraysize(frames));

  // Layout should be:
  // |------| | Heading         |
  // | icon | | How to Use      |
  // |------| | Manage shortcut |
  EXPECT_FALSE(NSIntersectsRect(howToUseFrame, howToManageFrame));
  EXPECT_GT(NSMinY(headingFrame), NSMinY(howToManageFrame));
  EXPECT_GT(NSMinY(howToUseFrame), NSMinY(howToManageFrame));
  EXPECT_GT(NSMinY(iconFrame), 0);

  // The page action preview should be visible.
  EXPECT_TRUE([controller pageActionPreviewShowing]);
  EXPECT_EQ(1, locationBar->PageActionVisibleCount());

  [controller close];
  // The page action preview should have ended.
  EXPECT_EQ(0, locationBar->PageActionVisibleCount());
}

TEST_F(ExtensionInstalledBubbleControllerTest, ParentClose) {
  CreateExtension(BROWSER_ACTION, false);
  ExtensionInstalledBubbleController* controller = CreateController();
  EXPECT_TRUE(controller);

  NSWindow* bubbleWindow = [controller window];

  // 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) {
  CreateExtension(APP, false);
  ExtensionInstalledBubbleController* controller = CreateController();
  EXPECT_TRUE(controller);

  int height = NSHeight([[controller window] frame]);

  // 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];
}