summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/web_intent_bubble_controller_unittest.mm
blob: 8da099508d36f8d1541a2ac550e83d4b6c341ba0 (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
// 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/cocoa_test_helper.h"
#import "chrome/browser/ui/cocoa/hyperlink_button_cell.h"
#import "chrome/browser/ui/cocoa/info_bubble_window.h"
#import "chrome/browser/ui/cocoa/web_intent_bubble_controller.h"
#include "chrome/browser/ui/cocoa/web_intent_picker_cocoa.h"
#include "chrome/browser/ui/intents/web_intent_picker_delegate.h"

namespace {

class FakeIntentPickerDelegate : public WebIntentPickerDelegate {
 public:
  virtual ~FakeIntentPickerDelegate() {}
  virtual void OnServiceChosen(
      size_t index, Disposition disposition) OVERRIDE {};
  virtual void OnInlineDispositionWebContentsCreated(
      content::WebContents* web_contents) OVERRIDE {}

  // Callback called when the user cancels out of the dialog.
  virtual void OnCancelled() OVERRIDE {};
  virtual void OnClosing() OVERRIDE {};
};

}  // namespace

class WebIntentBubbleControllerTest : public CocoaTest {
 public:
  virtual void TearDown() {
    // Do not animate out because that is hard to test around.
    [window_ setDelayOnClose:NO];
    [controller_ close];
    CocoaTest::TearDown();
  }

  void CreateBubble() {
    picker_.reset(new WebIntentPickerCocoa());
    picker_->delegate_ = &delegate_;
    NSPoint anchor=NSMakePoint(0,0);

    controller_ =
        [[WebIntentBubbleController alloc] initWithPicker:picker_.get()
                                             parentWindow:test_window()
                                               anchoredAt:anchor];
    window_ = static_cast<InfoBubbleWindow*>([controller_ window]);
    [controller_ showWindow:nil];
  }

  // Checks the controller's window for the requisite subviews and icons.
  void CheckWindow(size_t icon_count) {
    NSArray* views = [[window_ contentView] subviews];

    // 4 subviews - Icon, Header text, NSMatrix, CWS link.
    EXPECT_EQ(4U, [views count]);
    EXPECT_TRUE([[views objectAtIndex:0] isKindOfClass:[NSButton class]]);
    EXPECT_TRUE([[views objectAtIndex:1] isKindOfClass:[NSMatrix class]]);
    EXPECT_TRUE([[views objectAtIndex:2] isKindOfClass:[NSTextField class]]);
    EXPECT_TRUE([[views objectAtIndex:3] isKindOfClass:[NSImageView class]]);

    // Verify the Chrome Web Store button.
    NSButton* button = static_cast<NSButton*>([views objectAtIndex:0]);
    EXPECT_TRUE([[button cell] isKindOfClass:[HyperlinkButtonCell class]]);
    CheckButton(button, @selector(showChromeWebStore:));

    // Verify icons/buttons pointing to services.
    NSMatrix* icon_matrix = static_cast<NSMatrix*>([views objectAtIndex:1]);
    NSArray* cells = [icon_matrix cells];
    size_t cell_count = 0;
    for (NSButtonCell* cell in cells) {
      // Skip not populated cells
      if ([cell isKindOfClass:[NSImageCell class]])
        continue;

      ++cell_count;
      CheckButton(cell, @selector(invokeService:));
    }
    // TODO(binji): This check needs to be disabled until the bubble controller
    // is fixed to use the WebIntentPickerModel directly.
    // EXPECT_EQ(icon_count, cell_count);

    EXPECT_EQ([window_ delegate], controller_);
  }

  // Checks that a button is hooked up correctly.
  void CheckButton(id button, SEL action) {
    EXPECT_TRUE([button isKindOfClass:[NSButton class]] ||
      [button isKindOfClass:[NSButtonCell class]]);
    EXPECT_EQ(action, [button action]);
    EXPECT_EQ(controller_, [button target]);
    EXPECT_TRUE([button stringValue]);
  }

  WebIntentBubbleController* controller_;  // Weak, owns self.
  InfoBubbleWindow* window_;  // Weak, owned by controller.
  scoped_ptr<WebIntentPickerCocoa> picker_;
  FakeIntentPickerDelegate delegate_;
};

TEST_F(WebIntentBubbleControllerTest, EmptyBubble) {
  CreateBubble();

  CheckWindow(/*icon_count=*/0);
}

TEST_F(WebIntentBubbleControllerTest, PopulatedBubble) {
  CreateBubble();
  [controller_ replaceImageAtIndex:2 withImage:nil];

  CheckWindow(/*icon_count=*/3);
}