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