Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / tabs / media_indicator_button_cocoa_unittest.mm
blob2cba5de352a2cd3d6046cdfbab3dbaa19555aff7
1 // Copyright 2013 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/tabs/media_indicator_button_cocoa.h"
7 #include <string>
9 #include "base/command_line.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/message_loop/message_loop.h"
12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
17 // A simple target to confirm an action was invoked.
18 @interface MediaIndicatorButtonTestTarget : NSObject {
19  @private
20   int count_;
22 @property(readonly, nonatomic) int count;
23 - (void)incrementCount:(id)sender;
24 @end
26 @implementation MediaIndicatorButtonTestTarget
27 @synthesize count = count_;
28 - (void)incrementCount:(id)sender {
29   ++count_;
31 @end
33 namespace {
35 class MediaIndicatorButtonTest : public CocoaTest {
36  public:
37   MediaIndicatorButtonTest() {
38     base::CommandLine::ForCurrentProcess()->AppendSwitch(
39         std::string("--") + switches::kEnableTabAudioMuting);
41     // Create the MediaIndicatorButton and add it to a view.
42     button_.reset([[MediaIndicatorButton alloc] init]);
43     EXPECT_TRUE(button_ != nil);
44     [[test_window() contentView] addSubview:button_.get()];
46     // Initially the button is disabled and showing no indicator.
47     EXPECT_EQ(TAB_MEDIA_STATE_NONE, [button_ showingMediaState]);
48     EXPECT_FALSE([button_ isEnabled]);
50     // Register target to be notified of clicks.
51     base::scoped_nsobject<MediaIndicatorButtonTestTarget> clickTarget(
52         [[MediaIndicatorButtonTestTarget alloc] init]);
53     EXPECT_EQ(0, [clickTarget count]);
54     [button_ setClickTarget:clickTarget withAction:@selector(incrementCount:)];
56     // Transition to audio indicator mode, and expect button is enabled.
57     [button_ transitionToMediaState:TAB_MEDIA_STATE_AUDIO_PLAYING];
58     EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING, [button_ showingMediaState]);
59     EXPECT_TRUE([button_ isEnabled]);
61     // Click, and expect one click notification.
62     EXPECT_EQ(0, [clickTarget count]);
63     [button_ performClick:button_];
64     EXPECT_EQ(1, [clickTarget count]);
66     // Transition to audio muting mode, and expect button is still enabled.  A
67     // click should result in another click notification.
68     [button_ transitionToMediaState:TAB_MEDIA_STATE_AUDIO_MUTING];
69     EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_MUTING, [button_ showingMediaState]);
70     EXPECT_TRUE([button_ isEnabled]);
71     [button_ performClick:button_];
72     EXPECT_EQ(2, [clickTarget count]);
74     // Transition to capturing mode.  Now, the button is disabled since it
75     // should only be drawing the indicator icon (i.e., there is nothing to
76     // mute).  A click should NOT result in another click notification.
77     [button_ transitionToMediaState:TAB_MEDIA_STATE_CAPTURING];
78     EXPECT_EQ(TAB_MEDIA_STATE_CAPTURING, [button_ showingMediaState]);
79     EXPECT_FALSE([button_ isEnabled]);
80     [button_ performClick:button_];
81     EXPECT_EQ(2, [clickTarget count]);
82   }
84   base::scoped_nsobject<MediaIndicatorButton> button_;
85   base::MessageLoopForUI message_loop_;  // Needed for gfx::Animation.
88 TEST_VIEW(MediaIndicatorButtonTest, button_)
90 }  // namespace