Home | History | Annotate | Download | only in ash
      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.
      4 
      5 #include "chrome/browser/ui/ash/screenshot_taker.h"
      6 
      7 #include "ash/shell.h"
      8 #include "ash/test/ash_test_base.h"
      9 #include "base/bind.h"
     10 #include "base/command_line.h"
     11 #include "base/file_util.h"
     12 #include "base/files/scoped_temp_dir.h"
     13 #include "base/message_loop/message_loop.h"
     14 #include "chrome/browser/browser_process.h"
     15 #include "chrome/browser/notifications/notification_ui_manager.h"
     16 #include "chrome/test/base/in_process_browser_test.h"
     17 #include "chrome/test/base/testing_browser_process.h"
     18 #include "chrome/test/base/testing_profile.h"
     19 #include "chrome/test/base/testing_profile_manager.h"
     20 #include "content/public/browser/browser_thread.h"
     21 #include "content/public/test/test_utils.h"
     22 #include "ui/aura/root_window.h"
     23 #include "ui/message_center/message_center_switches.h"
     24 
     25 #if defined(OS_CHROMEOS)
     26 #include "chromeos/login/login_state.h"
     27 #endif
     28 
     29 namespace ash {
     30 namespace test {
     31 
     32 class ScreenshotTakerTest : public AshTestBase,
     33                             public ScreenshotTakerObserver {
     34  public:
     35   ScreenshotTakerTest()
     36       : running_(false),
     37         screenshot_complete_(false),
     38         screenshot_result_(ScreenshotTakerObserver::SCREENSHOT_SUCCESS) {
     39   }
     40 
     41   virtual void SetUp() {
     42     AshTestBase::SetUp();
     43   }
     44 
     45   virtual void TearDown() {
     46     RunAllPendingInMessageLoop();
     47     AshTestBase::TearDown();
     48   }
     49 
     50   // Overridden from ScreenshotTakerObserver
     51   virtual void OnScreenshotCompleted(
     52       ScreenshotTakerObserver::Result screenshot_result,
     53       const base::FilePath& screenshot_path) OVERRIDE {
     54     screenshot_complete_ = true;
     55     screenshot_result_ = screenshot_result;
     56     screenshot_path_ = screenshot_path;
     57     if (!running_)
     58       return;
     59     message_loop_runner_->Quit();
     60     running_ = false;
     61   }
     62 
     63  protected:
     64   // ScreenshotTakerTest is a friend of ScreenshotTaker and therefore
     65   // allowed to set the directory, basename and profile.
     66   void SetScreenshotDirectoryForTest(
     67       ScreenshotTaker* screenshot_taker,
     68       const base::FilePath& screenshot_directory) {
     69     screenshot_taker->SetScreenshotDirectoryForTest(screenshot_directory);
     70   }
     71   void SetScreenshotBasenameForTest(
     72       ScreenshotTaker* screenshot_taker,
     73       const std::string& screenshot_basename) {
     74     screenshot_taker->SetScreenshotBasenameForTest(screenshot_basename);
     75   }
     76   void SetScreenshotProfileForTest(
     77       ScreenshotTaker* screenshot_taker,
     78       Profile* profile) {
     79     screenshot_taker->SetScreenshotProfileForTest(profile);
     80   }
     81 
     82   void Wait() {
     83     if (screenshot_complete_)
     84       return;
     85     running_ = true;
     86     message_loop_runner_ = new content::MessageLoopRunner;
     87     message_loop_runner_->Run();
     88     EXPECT_TRUE(screenshot_complete_);
     89   }
     90 
     91   bool running_;
     92   bool screenshot_complete_;
     93   ScreenshotTakerObserver::Result screenshot_result_;
     94   base::FilePath screenshot_path_;
     95   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
     96 
     97   DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerTest);
     98 };
     99 
    100 TEST_F(ScreenshotTakerTest, TakeScreenshot) {
    101 #if defined(OS_CHROMEOS)
    102   // Note that within the test framework the LoginState object will always
    103   // claim that the user did log in.
    104   ASSERT_FALSE(chromeos::LoginState::IsInitialized());
    105   chromeos::LoginState::Initialize();
    106 #endif
    107   scoped_ptr<TestingProfileManager> profile_manager(
    108       new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
    109   ASSERT_TRUE(profile_manager->SetUp());
    110   TestingProfile* profile =
    111       profile_manager->CreateTestingProfile("test_profile");
    112   ScreenshotTaker screenshot_taker;
    113   screenshot_taker.AddObserver(this);
    114   base::ScopedTempDir directory;
    115   ASSERT_TRUE(directory.CreateUniqueTempDir());
    116   SetScreenshotDirectoryForTest(&screenshot_taker, directory.path());
    117   SetScreenshotBasenameForTest(&screenshot_taker, "Screenshot");
    118   SetScreenshotProfileForTest(&screenshot_taker, profile);
    119 
    120   EXPECT_TRUE(screenshot_taker.CanTakeScreenshot());
    121 
    122   screenshot_taker.HandleTakePartialScreenshot(
    123       Shell::GetPrimaryRootWindow(), gfx::Rect(0, 0, 100, 100));
    124 
    125   EXPECT_FALSE(screenshot_taker.CanTakeScreenshot());
    126 
    127   Wait();
    128 
    129 #if defined(OS_CHROMEOS)
    130   // Screenshot notifications on Windows not yet turned on.
    131   EXPECT_TRUE(g_browser_process->notification_ui_manager()->FindById(
    132       std::string("screenshot")) != NULL);
    133   g_browser_process->notification_ui_manager()->CancelAll();
    134 #endif
    135 
    136   EXPECT_EQ(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, screenshot_result_);
    137 
    138   if (ScreenshotTakerObserver::SCREENSHOT_SUCCESS == screenshot_result_)
    139     EXPECT_TRUE(base::PathExists(screenshot_path_));
    140 
    141 #if defined(OS_CHROMEOS)
    142   chromeos::LoginState::Shutdown();
    143 #endif
    144 }
    145 
    146 }  // namespace test
    147 }  // namespace ash
    148