Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / content_settings / content_setting_bubble_cocoa_unittest.mm
blob078f4548a64b5de210389c21da41ab0df03cd4ff
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/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"
27 namespace {
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   }
48 class ContentSettingBubbleControllerTest
49     : public ChromeRenderViewHostTestHarness {
50  protected:
51   // Helper function to create the bubble controller.
52   ContentSettingBubbleController* CreateBubbleController(
53       ContentSettingsType settingsType);
55   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 ChromeContentBrowserClient());
61     content::SetBrowserClientForTesting(browser_content_client_.get());
62     initializer_.reset(new TestingBrowserProcessInitializer);
63     ChromeRenderViewHostTestHarness::SetUp();
64   }
66   scoped_ptr<ChromeContentClient> content_client_;
67   scoped_ptr<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_;
75  private:
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
85                                                 defer:NO]);
86   [parent_ setReleasedWhenClosed:NO];
87   [parent_ orderFront:nil];
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)];
97   EXPECT_TRUE(controller);
98   EXPECT_TRUE([[controller window] isVisible]);
100   return controller;
103 // Check that the bubble doesn't crash or leak for any settings type
104 TEST_F(ContentSettingBubbleControllerTest, Init) {
105   for (ContentSettingsType type :
106        ContentSettingBubbleModel::GetSupportedBubbleTypes()) {
107     // Media stream is tested in the MediaStreamBubble test below.
108     if (type == CONTENT_SETTINGS_TYPE_MEDIASTREAM)
109       continue;
110     ContentSettingBubbleController* controller = CreateBubbleController(type);
111     EXPECT_EQ(0u, [controller mediaMenus]->size());
112     [parent_ close];
113   }
116 // Check that the bubble works for CONTENT_SETTINGS_TYPE_MEDIASTREAM.
117 TEST_F(ContentSettingBubbleControllerTest, MediaStreamBubble) {
118   MediaCaptureDevicesDispatcher::GetInstance()->
119       DisableDeviceEnumerationForTesting();
120   ContentSettingBubbleController* controller =
121       CreateBubbleController(CONTENT_SETTINGS_TYPE_MEDIASTREAM);
122   content_setting_bubble::MediaMenuPartsMap* mediaMenus =
123       [controller mediaMenus];
124   EXPECT_EQ(2u, mediaMenus->size());
125   NSString* title = l10n_util::GetNSString(IDS_MEDIA_MENU_NO_DEVICE_TITLE);
126   for (content_setting_bubble::MediaMenuPartsMap::const_iterator i =
127        mediaMenus->begin(); i != mediaMenus->end(); ++i) {
128     EXPECT_TRUE((content::MEDIA_DEVICE_AUDIO_CAPTURE == i->second->type) ||
129                 (content::MEDIA_DEVICE_VIDEO_CAPTURE == i->second->type));
130     EXPECT_EQ(0, [i->first numberOfItems]);
131     EXPECT_NSEQ(title, [i->first title]);
132     EXPECT_FALSE([i->first isEnabled]);
133   }
135  [parent_ close];
138 }  // namespace