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.
5 #import "chrome/browser/ui/cocoa/content_settings/content_setting_bubble_cocoa.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/scoped_nsautorelease_pool.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "chrome/browser/chrome_content_browser_client.h"
12 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
13 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
14 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
15 #include "chrome/common/chrome_content_client.h"
16 #include "chrome/common/content_settings_types.h"
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
18 #include "chrome/test/base/chrome_unit_test_suite.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/common/media_stream_request.h"
22 #include "grit/generated_resources.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "testing/gtest_mac.h"
25 #include "ui/base/l10n/l10n_util.h"
29 class DummyContentSettingBubbleModel : public ContentSettingBubbleModel {
31 DummyContentSettingBubbleModel(content::WebContents* web_contents,
33 ContentSettingsType content_type)
34 : ContentSettingBubbleModel(web_contents, profile, content_type) {
35 RadioGroup radio_group;
36 radio_group.default_item = 0;
37 radio_group.radio_items.resize(2);
38 set_radio_group(radio_group);
40 micMenu.label = "Microphone:";
41 add_media_menu(content::MEDIA_DEVICE_AUDIO_CAPTURE, micMenu);
43 cameraMenu.label = "Camera:";
44 add_media_menu(content::MEDIA_DEVICE_VIDEO_CAPTURE, cameraMenu);
48 class ContentSettingBubbleControllerTest
49 : public ChromeRenderViewHostTestHarness {
51 // Helper function to create the bubble controller.
52 ContentSettingBubbleController* CreateBubbleController(
53 ContentSettingsType settingsType);
55 virtual void SetUp() OVERRIDE {
56 ChromeUnitTestSuite::InitializeProviders();
57 ChromeUnitTestSuite::InitializeResourceBundle();
58 content_client_.reset(new ChromeContentClient);
59 content::SetContentClient(content_client_.get());
60 browser_content_client_.reset(new chrome::ChromeContentBrowserClient());
61 content::SetBrowserClientForTesting(browser_content_client_.get());
62 initializer_.reset(new TestingBrowserProcessInitializer);
63 ChromeRenderViewHostTestHarness::SetUp();
66 scoped_ptr<ChromeContentClient> content_client_;
67 scoped_ptr<chrome::ChromeContentBrowserClient> browser_content_client_;
69 // This is a unit test running in the browser_tests suite, so we must create
70 // the TestingBrowserProcess manually. Must be first member.
71 scoped_ptr<TestingBrowserProcessInitializer> initializer_;
73 base::scoped_nsobject<NSWindow> parent_;
76 base::mac::ScopedNSAutoreleasePool pool_;
79 ContentSettingBubbleController*
80 ContentSettingBubbleControllerTest::CreateBubbleController(
81 ContentSettingsType settingsType) {
82 parent_.reset([[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
83 styleMask:NSBorderlessWindowMask
84 backing:NSBackingStoreBuffered
86 [parent_ setReleasedWhenClosed:NO];
87 [parent_ orderFront:nil];
89 ContentSettingBubbleController* controller = [ContentSettingBubbleController
90 showForModel:new DummyContentSettingBubbleModel(web_contents(),
94 anchoredAt:NSMakePoint(50, 20)];
96 EXPECT_TRUE(controller);
97 EXPECT_TRUE([[controller window] isVisible]);
102 // Check that the bubble doesn't crash or leak for any settings type
103 TEST_F(ContentSettingBubbleControllerTest, Init) {
104 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
105 if (i == CONTENT_SETTINGS_TYPE_NOTIFICATIONS ||
106 i == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE ||
107 i == CONTENT_SETTINGS_TYPE_FULLSCREEN ||
108 i == CONTENT_SETTINGS_TYPE_MOUSELOCK ||
109 i == CONTENT_SETTINGS_TYPE_MEDIASTREAM ||
110 i == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC ||
111 i == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA ||
112 i == CONTENT_SETTINGS_TYPE_PPAPI_BROKER ||
113 i == CONTENT_SETTINGS_TYPE_MIDI_SYSEX) {
114 // These types have no bubble.
118 ContentSettingsType settingsType = static_cast<ContentSettingsType>(i);
120 ContentSettingBubbleController* controller =
121 CreateBubbleController(settingsType);
122 EXPECT_EQ(0u, [controller mediaMenus]->size());
127 // Check that the bubble works for CONTENT_SETTINGS_TYPE_MEDIASTREAM.
128 TEST_F(ContentSettingBubbleControllerTest, MediaStreamBubble) {
129 MediaCaptureDevicesDispatcher::GetInstance()->
130 DisableDeviceEnumerationForTesting();
131 ContentSettingBubbleController* controller =
132 CreateBubbleController(CONTENT_SETTINGS_TYPE_MEDIASTREAM);
133 content_setting_bubble::MediaMenuPartsMap* mediaMenus =
134 [controller mediaMenus];
135 EXPECT_EQ(2u, mediaMenus->size());
136 NSString* title = l10n_util::GetNSString(IDS_MEDIA_MENU_NO_DEVICE_TITLE);
137 for (content_setting_bubble::MediaMenuPartsMap::const_iterator i =
138 mediaMenus->begin(); i != mediaMenus->end(); ++i) {
139 EXPECT_TRUE((content::MEDIA_DEVICE_AUDIO_CAPTURE == i->second->type) ||
140 (content::MEDIA_DEVICE_VIDEO_CAPTURE == i->second->type));
141 EXPECT_EQ(0, [i->first numberOfItems]);
142 EXPECT_NSEQ(title, [i->first title]);
143 EXPECT_FALSE([i->first isEnabled]);