summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/feedback_ui_unittest.cc
blob: 6bcf7d3614c18581ece8d1d53d908c2355718be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// The tests in this file are chromeos only.

#include "chrome/browser/ui/webui/feedback_ui.h"

#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "testing/gtest/include/gtest/gtest.h"

// This macro helps avoid wrapped lines in the test structs.
#define FPL(x) FILE_PATH_LITERAL(x)

namespace {

// Simple function to create a file with |filename|.
void CreateFile(const base::FilePath& filename) {
  FILE* fp = file_util::OpenFile(filename, "w");
  ASSERT_TRUE(fp != NULL);
  file_util::CloseFile(fp);
}

std::string GetScreenshotFilename(const std::string timestamp) {
  return std::string("Screenshot ") + timestamp + std::string(".png");
}

std::string GetScreenshotUrl(const std::string timestamp) {
  return std::string("chrome://screenshots/saved/") +
      GetScreenshotFilename(timestamp);
}

class FeedbackUITest : public testing::Test {
 public:
  FeedbackUITest() {}
  virtual ~FeedbackUITest() {}
  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  }
 protected:
  void CreateScreenshotFile(const std::string& timestamp) {
    base::FilePath filepath = temp_dir_.path().Append(
        FILE_PATH_LITERAL(GetScreenshotFilename(timestamp)));
    ASSERT_NO_FATAL_FAILURE(CreateFile(filepath));
  }

  base::ScopedTempDir temp_dir_;
  std::vector<std::string> saved_screenshots_;

 private:
  DISALLOW_COPY_AND_ASSIGN(FeedbackUITest);
};

TEST_F(FeedbackUITest, GetMostRecentScreenshotsNoScreenShot) {
  // Create a random file.
  base::FilePath filepath =
      temp_dir_.path().Append(FILE_PATH_LITERAL("garbage.png"));
  ASSERT_NO_FATAL_FAILURE(CreateFile(filepath));
  // Expect getting no screenshot.
  FeedbackUI::GetMostRecentScreenshots(
      temp_dir_.path(), &saved_screenshots_, 2);
  ASSERT_TRUE(saved_screenshots_.empty());
}

TEST_F(FeedbackUITest, GetMostRecentScreenshotsOneScreenShot) {
  // Create 1 screenshot.
  ASSERT_NO_FATAL_FAILURE(CreateScreenshotFile("20120416-152908"));
  // Expect getting 1 screenshot.
  FeedbackUI::GetMostRecentScreenshots(
      temp_dir_.path(), &saved_screenshots_, 2);
  ASSERT_EQ(1U, saved_screenshots_.size());
  ASSERT_EQ(GetScreenshotUrl("20120416-152908"), saved_screenshots_[0]);
}

TEST_F(FeedbackUITest, GetMostRecentScreenshotsManyScreenShots) {
  // Create 2 screenshots.
  ASSERT_NO_FATAL_FAILURE(CreateScreenshotFile("20120416-152908"));
  ASSERT_NO_FATAL_FAILURE(CreateScreenshotFile("20120416-162908"));
  ASSERT_NO_FATAL_FAILURE(CreateScreenshotFile("20120413-152908"));
  // Expect getting most recent 2 screenshots.
  FeedbackUI::GetMostRecentScreenshots(
      temp_dir_.path(), &saved_screenshots_, 2);
  ASSERT_EQ(2U, saved_screenshots_.size());
  ASSERT_EQ(GetScreenshotUrl("20120416-162908"), saved_screenshots_[0]);
  ASSERT_EQ(GetScreenshotUrl("20120416-152908"), saved_screenshots_[1]);
}

}  // namespace