content_setting_bubble_cocoa_unittest.mm revision 558790d6acca3451cf3a6b497803a5f07d0bec58
1// Copyright (c) 2012 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 "chrome/browser/ui/cocoa/content_settings/content_setting_bubble_cocoa.h"
6
7#import <Cocoa/Cocoa.h>
8
9#include "base/mac/scoped_nsautorelease_pool.h"
10#include "base/mac/scoped_nsobject.h"
11#include "chrome/browser/media/media_capture_devices_dispatcher.h"
12#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13#include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
14#include "chrome/common/content_settings_types.h"
15#include "chrome/test/base/chrome_render_view_host_test_harness.h"
16#include "chrome/test/base/testing_profile.h"
17#include "content/public/common/media_stream_request.h"
18#include "grit/generated_resources.h"
19#include "testing/gtest/include/gtest/gtest.h"
20#include "testing/gtest_mac.h"
21#include "ui/base/l10n/l10n_util.h"
22
23namespace {
24
25class DummyContentSettingBubbleModel : public ContentSettingBubbleModel {
26 public:
27  DummyContentSettingBubbleModel(content::WebContents* web_contents,
28                                 Profile* profile,
29                                 ContentSettingsType content_type)
30      : ContentSettingBubbleModel(web_contents, profile, content_type) {
31    RadioGroup radio_group;
32    radio_group.default_item = 0;
33    radio_group.radio_items.resize(2);
34    set_radio_group(radio_group);
35    MediaMenu micMenu;
36    micMenu.label = "Microphone:";
37    add_media_menu(content::MEDIA_DEVICE_AUDIO_CAPTURE, micMenu);
38    MediaMenu cameraMenu;
39    cameraMenu.label = "Camera:";
40    add_media_menu(content::MEDIA_DEVICE_VIDEO_CAPTURE, cameraMenu);
41  }
42};
43
44class ContentSettingBubbleControllerTest
45    : public ChromeRenderViewHostTestHarness {
46 protected:
47  // Helper function to create the bubble controller.
48  ContentSettingBubbleController* CreateBubbleController(
49      ContentSettingsType settingsType);
50
51  base::scoped_nsobject<NSWindow> parent_;
52
53 private:
54  base::mac::ScopedNSAutoreleasePool pool_;
55};
56
57ContentSettingBubbleController*
58ContentSettingBubbleControllerTest::CreateBubbleController(
59    ContentSettingsType settingsType) {
60  parent_.reset([[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
61                                            styleMask:NSBorderlessWindowMask
62                                              backing:NSBackingStoreBuffered
63                                                defer:NO]);
64  [parent_ setReleasedWhenClosed:NO];
65  [parent_ orderFront:nil];
66
67  ContentSettingBubbleController* controller = [ContentSettingBubbleController
68      showForModel:new DummyContentSettingBubbleModel(web_contents(),
69                                                      profile(),
70                                                      settingsType)
71      parentWindow:parent_
72        anchoredAt:NSMakePoint(50, 20)];
73
74  EXPECT_TRUE(controller);
75  EXPECT_TRUE([[controller window] isVisible]);
76
77  return controller;
78}
79
80// Check that the bubble doesn't crash or leak for any settings type
81TEST_F(ContentSettingBubbleControllerTest, Init) {
82  for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
83    if (i == CONTENT_SETTINGS_TYPE_NOTIFICATIONS ||
84        i == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE ||
85        i == CONTENT_SETTINGS_TYPE_FULLSCREEN ||
86        i == CONTENT_SETTINGS_TYPE_MOUSELOCK ||
87        i == CONTENT_SETTINGS_TYPE_MEDIASTREAM ||
88        i == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC ||
89        i == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA ||
90        i == CONTENT_SETTINGS_TYPE_PPAPI_BROKER ||
91        i == CONTENT_SETTINGS_TYPE_MIDI_SYSEX) {
92      // These types have no bubble.
93      continue;
94    }
95
96    ContentSettingsType settingsType = static_cast<ContentSettingsType>(i);
97
98    ContentSettingBubbleController* controller =
99        CreateBubbleController(settingsType);
100    EXPECT_EQ(0u, [controller mediaMenus]->size());
101    [parent_ close];
102  }
103}
104
105// Check that the bubble works for CONTENT_SETTINGS_TYPE_MEDIASTREAM.
106TEST_F(ContentSettingBubbleControllerTest, MediaStreamBubble) {
107  MediaCaptureDevicesDispatcher::GetInstance()->
108      DisableDeviceEnumerationForTesting();
109  ContentSettingBubbleController* controller =
110      CreateBubbleController(CONTENT_SETTINGS_TYPE_MEDIASTREAM);
111  content_setting_bubble::MediaMenuPartsMap* mediaMenus =
112      [controller mediaMenus];
113  EXPECT_EQ(2u, mediaMenus->size());
114  NSString* title = l10n_util::GetNSString(IDS_MEDIA_MENU_NO_DEVICE_TITLE);
115  for (content_setting_bubble::MediaMenuPartsMap::const_iterator i =
116       mediaMenus->begin(); i != mediaMenus->end(); ++i) {
117    EXPECT_TRUE((content::MEDIA_DEVICE_AUDIO_CAPTURE == i->second->type) ||
118                (content::MEDIA_DEVICE_VIDEO_CAPTURE == i->second->type));
119    EXPECT_EQ(0, [i->first numberOfItems]);
120    EXPECT_NSEQ(title, [i->first title]);
121    EXPECT_FALSE([i->first isEnabled]);
122  }
123
124 [parent_ close];
125}
126
127}  // namespace
128