Home | History | Annotate | Download | only in tabs
      1 // Copyright (c) 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.
      4 
      5 #include "chrome/browser/ui/tabs/tab_audio_indicator.h"
      6 
      7 #include "base/message_loop/message_loop.h"
      8 #include "grit/theme_resources.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 #include "ui/base/animation/linear_animation.h"
     11 #include "ui/base/resource/resource_bundle.h"
     12 #include "ui/gfx/canvas.h"
     13 #include "ui/gfx/rect.h"
     14 
     15 class TabAudioIndicatorTest : public TabAudioIndicator::Delegate,
     16                               public testing::Test {
     17  protected:
     18   TabAudioIndicatorTest() : schedule_paint_count_(0) {}
     19 
     20   virtual void ScheduleAudioIndicatorPaint() OVERRIDE {
     21     ++schedule_paint_count_;
     22   }
     23 
     24   int schedule_paint_count_;
     25   base::MessageLoopForUI message_loop_;  // Needed for ui::LinearAnimation.
     26 
     27  private:
     28   DISALLOW_COPY_AND_ASSIGN(TabAudioIndicatorTest);
     29 };
     30 
     31 TEST_F(TabAudioIndicatorTest, AnimationState) {
     32   // Start animating.
     33   TabAudioIndicator indicator(this);
     34   indicator.SetIsPlayingAudio(true);
     35   EXPECT_EQ(TabAudioIndicator::STATE_ANIMATING, indicator.state_);
     36   EXPECT_TRUE(indicator.IsAnimating());
     37 
     38   // Once the audio stops the indicator should switch to ending animation.
     39   indicator.SetIsPlayingAudio(false);
     40   EXPECT_EQ(TabAudioIndicator::STATE_ANIMATION_ENDING, indicator.state_);
     41   EXPECT_TRUE(indicator.IsAnimating());
     42 
     43   // Once the ending animation is complete animation should stop.
     44   indicator.animation_->End();
     45   EXPECT_EQ(TabAudioIndicator::STATE_NOT_ANIMATING, indicator.state_);
     46   EXPECT_FALSE(indicator.IsAnimating());
     47 }
     48 
     49 TEST_F(TabAudioIndicatorTest, Paint) {
     50   TabAudioIndicator indicator(this);
     51   indicator.SetIsPlayingAudio(true);
     52 
     53   gfx::Rect rect(0, 0, 16, 16);
     54   gfx::Canvas canvas(rect.size(), ui::SCALE_FACTOR_100P, true);
     55 
     56   // Nothing to test here. Just exercise the paint code to verify that nothing
     57   // leaks or crashes.
     58   indicator.Paint(&canvas, rect);
     59 
     60   // Paint with a favicon.
     61   ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance();
     62   indicator.set_favicon(*rb.GetImageSkiaNamed(IDR_PRODUCT_LOGO_16));
     63   indicator.Paint(&canvas, rect);
     64 }
     65 
     66 TEST_F(TabAudioIndicatorTest, SchedulePaint) {
     67   TabAudioIndicator indicator(this);
     68   indicator.SetIsPlayingAudio(true);
     69 
     70   indicator.animation_->SetCurrentValue(1.0);
     71   schedule_paint_count_ = 0;
     72   indicator.AnimationProgressed(NULL);
     73   EXPECT_EQ(1, schedule_paint_count_);
     74 }
     75