Update To 11.40.268.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/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/grit/generated_resources.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 "components/content_settings/core/common/content_settings_types.h"
22 #include "content/public/common/media_stream_request.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "testing/gtest_mac.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 namespace {
28
29 class DummyContentSettingBubbleModel : public ContentSettingBubbleModel {
30  public:
31   DummyContentSettingBubbleModel(content::WebContents* web_contents,
32                                  Profile* profile,
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);
39     MediaMenu micMenu;
40     micMenu.label = "Microphone:";
41     add_media_menu(content::MEDIA_DEVICE_AUDIO_CAPTURE, micMenu);
42     MediaMenu cameraMenu;
43     cameraMenu.label = "Camera:";
44     add_media_menu(content::MEDIA_DEVICE_VIDEO_CAPTURE, cameraMenu);
45   }
46 };
47
48 class ContentSettingBubbleControllerTest
49     : public ChromeRenderViewHostTestHarness {
50  protected:
51   // Helper function to create the bubble controller.
52   ContentSettingBubbleController* CreateBubbleController(
53       ContentSettingsType settingsType);
54
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();
64   }
65
66   scoped_ptr<ChromeContentClient> content_client_;
67   scoped_ptr<chrome::ChromeContentBrowserClient> browser_content_client_;
68
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_;
72
73   base::scoped_nsobject<NSWindow> parent_;
74
75  private:
76   base::mac::ScopedNSAutoreleasePool pool_;
77 };
78
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
85                                                 defer:NO]);
86   [parent_ setReleasedWhenClosed:NO];
87   [parent_ orderFront:nil];
88
89   ContentSettingBubbleController* controller = [ContentSettingBubbleController
90       showForModel:new DummyContentSettingBubbleModel(web_contents(),
91                                                       profile(),
92                                                       settingsType)
93        webContents:web_contents()
94       parentWindow:parent_
95         anchoredAt:NSMakePoint(50, 20)];
96
97   EXPECT_TRUE(controller);
98   EXPECT_TRUE([[controller window] isVisible]);
99
100   return controller;
101 }
102
103 // Check that the bubble doesn't crash or leak for any settings type
104 TEST_F(ContentSettingBubbleControllerTest, Init) {
105   for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
106     if (i == CONTENT_SETTINGS_TYPE_NOTIFICATIONS ||
107         i == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE ||
108         i == CONTENT_SETTINGS_TYPE_FULLSCREEN ||
109         i == CONTENT_SETTINGS_TYPE_MOUSELOCK ||
110         i == CONTENT_SETTINGS_TYPE_MEDIASTREAM ||
111         i == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC ||
112         i == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA ||
113         i == CONTENT_SETTINGS_TYPE_PPAPI_BROKER ||
114         i == CONTENT_SETTINGS_TYPE_MIDI_SYSEX ||
115         i == CONTENT_SETTINGS_TYPE_PUSH_MESSAGING ||
116         i == CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS) {
117       // These types have no bubble.
118       continue;
119     }
120
121     ContentSettingsType settingsType = static_cast<ContentSettingsType>(i);
122
123     ContentSettingBubbleController* controller =
124         CreateBubbleController(settingsType);
125     EXPECT_EQ(0u, [controller mediaMenus]->size());
126     [parent_ close];
127   }
128 }
129
130 // Check that the bubble works for CONTENT_SETTINGS_TYPE_MEDIASTREAM.
131 TEST_F(ContentSettingBubbleControllerTest, MediaStreamBubble) {
132   MediaCaptureDevicesDispatcher::GetInstance()->
133       DisableDeviceEnumerationForTesting();
134   ContentSettingBubbleController* controller =
135       CreateBubbleController(CONTENT_SETTINGS_TYPE_MEDIASTREAM);
136   content_setting_bubble::MediaMenuPartsMap* mediaMenus =
137       [controller mediaMenus];
138   EXPECT_EQ(2u, mediaMenus->size());
139   NSString* title = l10n_util::GetNSString(IDS_MEDIA_MENU_NO_DEVICE_TITLE);
140   for (content_setting_bubble::MediaMenuPartsMap::const_iterator i =
141        mediaMenus->begin(); i != mediaMenus->end(); ++i) {
142     EXPECT_TRUE((content::MEDIA_DEVICE_AUDIO_CAPTURE == i->second->type) ||
143                 (content::MEDIA_DEVICE_VIDEO_CAPTURE == i->second->type));
144     EXPECT_EQ(0, [i->first numberOfItems]);
145     EXPECT_NSEQ(title, [i->first title]);
146     EXPECT_FALSE([i->first isEnabled]);
147   }
148
149  [parent_ close];
150 }
151
152 }  // namespace