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 // The tests in this file are chromeos only. 6 7 #include "chrome/browser/ui/webui/feedback_ui.h" 8 9 #include "base/file_util.h" 10 #include "base/files/scoped_temp_dir.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 // This macro helps avoid wrapped lines in the test structs. 14 #define FPL(x) FILE_PATH_LITERAL(x) 15 16 namespace { 17 18 // Simple function to create a file with |filename|. 19 void CreateFile(const base::FilePath& filename) { 20 FILE* fp = file_util::OpenFile(filename, "w"); 21 ASSERT_TRUE(fp != NULL); 22 file_util::CloseFile(fp); 23 } 24 25 std::string GetScreenshotFilename(const std::string timestamp) { 26 return std::string("Screenshot ") + timestamp + std::string(".png"); 27 } 28 29 std::string GetScreenshotUrl(const std::string timestamp) { 30 return std::string("chrome://screenshots/saved/") + 31 GetScreenshotFilename(timestamp); 32 } 33 34 class FeedbackUITest : public testing::Test { 35 public: 36 FeedbackUITest() {} 37 virtual ~FeedbackUITest() {} 38 virtual void SetUp() OVERRIDE { 39 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 40 } 41 protected: 42 void CreateScreenshotFile(const std::string& timestamp) { 43 base::FilePath filepath = temp_dir_.path().Append( 44 FILE_PATH_LITERAL(GetScreenshotFilename(timestamp))); 45 ASSERT_NO_FATAL_FAILURE(CreateFile(filepath)); 46 } 47 48 base::ScopedTempDir temp_dir_; 49 std::vector<std::string> saved_screenshots_; 50 51 private: 52 DISALLOW_COPY_AND_ASSIGN(FeedbackUITest); 53 }; 54 55 TEST_F(FeedbackUITest, GetMostRecentScreenshotsNoScreenShot) { 56 // Create a random file. 57 base::FilePath filepath = 58 temp_dir_.path().Append(FILE_PATH_LITERAL("garbage.png")); 59 ASSERT_NO_FATAL_FAILURE(CreateFile(filepath)); 60 // Expect getting no screenshot. 61 FeedbackUI::GetMostRecentScreenshots( 62 temp_dir_.path(), &saved_screenshots_, 2); 63 ASSERT_TRUE(saved_screenshots_.empty()); 64 } 65 66 TEST_F(FeedbackUITest, GetMostRecentScreenshotsOneScreenShot) { 67 // Create 1 screenshot. 68 ASSERT_NO_FATAL_FAILURE(CreateScreenshotFile("20120416-152908")); 69 // Expect getting 1 screenshot. 70 FeedbackUI::GetMostRecentScreenshots( 71 temp_dir_.path(), &saved_screenshots_, 2); 72 ASSERT_EQ(1U, saved_screenshots_.size()); 73 ASSERT_EQ(GetScreenshotUrl("20120416-152908"), saved_screenshots_[0]); 74 } 75 76 TEST_F(FeedbackUITest, GetMostRecentScreenshotsManyScreenShots) { 77 // Create 2 screenshots. 78 ASSERT_NO_FATAL_FAILURE(CreateScreenshotFile("20120416-152908")); 79 ASSERT_NO_FATAL_FAILURE(CreateScreenshotFile("20120416-162908")); 80 ASSERT_NO_FATAL_FAILURE(CreateScreenshotFile("20120413-152908")); 81 // Expect getting most recent 2 screenshots. 82 FeedbackUI::GetMostRecentScreenshots( 83 temp_dir_.path(), &saved_screenshots_, 2); 84 ASSERT_EQ(2U, saved_screenshots_.size()); 85 ASSERT_EQ(GetScreenshotUrl("20120416-162908"), saved_screenshots_[0]); 86 ASSERT_EQ(GetScreenshotUrl("20120416-152908"), saved_screenshots_[1]); 87 } 88 89 } // namespace 90