summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tab_contents/thumbnail_generator_unittest.cc
blob: 93a43f21bddfe5d12e92464f8a2bfeb32edf80d1 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2011 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.

#include "base/basictypes.h"
#include "base/stringprintf.h"
#include "chrome/browser/history/top_sites.h"
#include "chrome/browser/tab_contents/thumbnail_generator.h"
#include "chrome/common/render_messages.h"
#include "chrome/test/testing_profile.h"
#include "content/browser/renderer_host/backing_store_manager.h"
#include "content/browser/renderer_host/backing_store_skia.h"
#include "content/browser/renderer_host/mock_render_process_host.h"
#include "content/browser/renderer_host/test_render_view_host.h"
#include "content/browser/tab_contents/render_view_host_manager.h"
#include "content/common/notification_service.h"
#include "content/common/view_messages.h"
#include "skia/ext/platform_canvas.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColorPriv.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/surface/transport_dib.h"

static const int kBitmapWidth = 100;
static const int kBitmapHeight = 100;

// TODO(brettw,satorux) enable this when GetThumbnailForBackingStore is
// implemented for Mac in thumbnail_generator.cc
//
// The test fails on Windows for the following error. Figure it out and fix.
//
// transport_dib_win.cc(71)] Check failed: !memory(). Mapped file twice in
// the same process.
//
#if !defined(OS_MACOSX) && !defined(OS_WIN)

// This test render widget host view uses BackingStoreSkia instead of
// TestBackingStore, so that basic operations like CopyFromBackingStore()
// works. The skia implementation doesn't have any hardware or system
// dependencies.
class TestRenderWidgetHostViewWithBackingStoreSkia
    : public TestRenderWidgetHostView {
 public:
  explicit TestRenderWidgetHostViewWithBackingStoreSkia(RenderWidgetHost* rwh)
      : TestRenderWidgetHostView(rwh), rwh_(rwh) {}

  BackingStore* AllocBackingStore(const gfx::Size& size) {
    return new BackingStoreSkia(rwh_, size);
  }

 private:
  RenderWidgetHost* rwh_;
  DISALLOW_COPY_AND_ASSIGN(TestRenderWidgetHostViewWithBackingStoreSkia);
};

class ThumbnailGeneratorTest : public testing::Test {
 public:
  ThumbnailGeneratorTest() {
    profile_.reset(new TestingProfile());
    process_ = new MockRenderProcessHost(profile_.get());
    widget_.reset(new RenderWidgetHost(process_, 1));
    view_.reset(new TestRenderWidgetHostViewWithBackingStoreSkia(
        widget_.get()));
    // Paiting will be skipped if there's no view.
    widget_->set_view(view_.get());

    // Need to send out a create notification for the RWH to get hooked. This is
    // a little scary in that we don't have a RenderView, but the only listener
    // will want a RenderWidget, so it works out OK.
    NotificationService::current()->Notify(
        NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
        Source<RenderViewHostManager>(NULL),
        Details<RenderViewHost>(reinterpret_cast<RenderViewHost*>(
            widget_.get())));

    transport_dib_.reset(TransportDIB::Create(kBitmapWidth * kBitmapHeight * 4,
                                              1));
  }

  ~ThumbnailGeneratorTest() {
    view_.reset();
    widget_.reset();
    process_ = NULL;
    profile_.reset();

    // Process all pending tasks to avoid leaks.
    message_loop_.RunAllPending();
  }

 protected:
  // Indicates what bitmap should be sent with the paint message. _OTHER will
  // only be retrned by CheckFirstPixel if the pixel is none of the others.
  enum TransportType { TRANSPORT_BLACK, TRANSPORT_WHITE, TRANSPORT_OTHER };

  void SendPaint(TransportType type) {
    ViewHostMsg_UpdateRect_Params params;
    params.bitmap_rect = gfx::Rect(0, 0, kBitmapWidth, kBitmapHeight);
    params.view_size = params.bitmap_rect.size();
    params.copy_rects.push_back(params.bitmap_rect);
    params.flags = 0;

    scoped_ptr<skia::PlatformCanvas> canvas(
        transport_dib_->GetPlatformCanvas(kBitmapWidth, kBitmapHeight));
    switch (type) {
      case TRANSPORT_BLACK:
        canvas->getTopPlatformDevice().accessBitmap(true).eraseARGB(
            0xFF, 0, 0, 0);
        break;
      case TRANSPORT_WHITE:
        canvas->getTopPlatformDevice().accessBitmap(true).eraseARGB(
            0xFF, 0xFF, 0xFF, 0xFF);
        break;
      case TRANSPORT_OTHER:
      default:
        NOTREACHED();
        break;
    }

    params.bitmap = transport_dib_->id();

    ViewHostMsg_UpdateRect msg(1, params);
    widget_->OnMessageReceived(msg);
  }

  TransportType ClassifyFirstPixel(const SkBitmap& bitmap) {
    // Returns the color of the first pixel of the bitmap. The bitmap must be
    // non-empty.
    SkAutoLockPixels lock(bitmap);
    uint32 pixel = *bitmap.getAddr32(0, 0);

    if (SkGetPackedA32(pixel) != 0xFF)
      return TRANSPORT_OTHER;  // All values expect an opqaue alpha channel

    if (SkGetPackedR32(pixel) == 0 &&
        SkGetPackedG32(pixel) == 0 &&
        SkGetPackedB32(pixel) == 0)
      return TRANSPORT_BLACK;

    if (SkGetPackedR32(pixel) == 0xFF &&
        SkGetPackedG32(pixel) == 0xFF &&
        SkGetPackedB32(pixel) == 0xFF)
      return TRANSPORT_WHITE;

    EXPECT_TRUE(false) << "Got weird color: " << pixel;
    return TRANSPORT_OTHER;
  }

  MessageLoopForUI message_loop_;

  scoped_ptr<TestingProfile> profile_;

  // Deleted automatically by widget_, but the deletion is done by
  // DeleteSoon(), hence the message loop needs to run pending tasks.
  MockRenderProcessHost* process_;

  scoped_ptr<RenderWidgetHost> widget_;
  scoped_ptr<TestRenderWidgetHostViewWithBackingStoreSkia> view_;
  ThumbnailGenerator generator_;

  scoped_ptr<TransportDIB> transport_dib_;

 private:
  // testing::Test implementation.
  void SetUp() {
  }
  void TearDown() {
  }
};

TEST_F(ThumbnailGeneratorTest, NoThumbnail) {
  // This is the case where there is no thumbnail available on the tab and
  // there is no backing store. There should be no image returned.
  SkBitmap result = generator_.GetThumbnailForRenderer(widget_.get());
  EXPECT_TRUE(result.isNull());
}

// Tests basic thumbnail generation when a backing store is discarded.
TEST_F(ThumbnailGeneratorTest, DiscardBackingStore) {
  // First set up a backing store.
  SendPaint(TRANSPORT_BLACK);
  ASSERT_TRUE(widget_->GetBackingStore(false));

  // The thumbnail generator should be able to retrieve a thumbnail.
  SkBitmap result = generator_.GetThumbnailForRenderer(widget_.get());
  ASSERT_FALSE(result.isNull());
  // TODO(satorux): Figure out why valgrind complains about this and fix.
  // See crbug.com/80458.
  // EXPECT_EQ(TRANSPORT_BLACK, ClassifyFirstPixel(result));

  // Discard the backing store.
  ASSERT_TRUE(BackingStoreManager::ExpireBackingStoreForTest(widget_.get()));
  ASSERT_FALSE(widget_->GetBackingStore(false));

  // The thumbnail generator should not be able to retrieve a thumbnail,
  // as the backing store is now gone.
  result = generator_.GetThumbnailForRenderer(widget_.get());
  ASSERT_TRUE(result.isNull());
}

#endif  // !defined(OS_MAC)

TEST(ThumbnailGeneratorSimpleTest, CalculateBoringScore_Empty) {
  SkBitmap bitmap;
  EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(&bitmap));
}

TEST(ThumbnailGeneratorSimpleTest, CalculateBoringScore_SingleColor) {
  const SkColor kBlack = SkColorSetRGB(0, 0, 0);
  const gfx::Size kSize(20, 10);
  gfx::CanvasSkia canvas(kSize.width(), kSize.height(), true);
  // Fill all pixesl in black.
  canvas.FillRectInt(kBlack, 0, 0, kSize.width(), kSize.height());

  SkBitmap bitmap = canvas.getTopPlatformDevice().accessBitmap(false);
  // The thumbnail should deserve the highest boring score.
  EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(&bitmap));
}

TEST(ThumbnailGeneratorSimpleTest, CalculateBoringScore_TwoColors) {
  const SkColor kBlack = SkColorSetRGB(0, 0, 0);
  const SkColor kWhite = SkColorSetRGB(0xFF, 0xFF, 0xFF);
  const gfx::Size kSize(20, 10);

  gfx::CanvasSkia canvas(kSize.width(), kSize.height(), true);
  // Fill all pixesl in black.
  canvas.FillRectInt(kBlack, 0, 0, kSize.width(), kSize.height());
  // Fill the left half pixels in white.
  canvas.FillRectInt(kWhite, 0, 0, kSize.width() / 2, kSize.height());

  SkBitmap bitmap = canvas.getTopPlatformDevice().accessBitmap(false);
  ASSERT_EQ(kSize.width(), bitmap.width());
  ASSERT_EQ(kSize.height(), bitmap.height());
  // The thumbnail should be less boring because two colors are used.
  EXPECT_DOUBLE_EQ(0.5, ThumbnailGenerator::CalculateBoringScore(&bitmap));
}

TEST(ThumbnailGeneratorSimpleTest, GetClippedBitmap_TallerThanWide) {
  // The input bitmap is vertically long.
  gfx::CanvasSkia canvas(40, 90, true);
  const SkBitmap bitmap = canvas.getTopPlatformDevice().accessBitmap(false);

  // The desired size is square.
  ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped;
  SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap(
      bitmap, 10, 10, &clip_result);
  // The clipped bitmap should be square.
  EXPECT_EQ(40, clipped_bitmap.width());
  EXPECT_EQ(40, clipped_bitmap.height());
  // The input was taller than wide.
  EXPECT_EQ(ThumbnailGenerator::kTallerThanWide, clip_result);
}

TEST(ThumbnailGeneratorSimpleTest, GetClippedBitmap_WiderThanTall) {
  // The input bitmap is horizontally long.
  gfx::CanvasSkia canvas(90, 40, true);
  const SkBitmap bitmap = canvas.getTopPlatformDevice().accessBitmap(false);

  // The desired size is square.
  ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped;
  SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap(
      bitmap, 10, 10, &clip_result);
  // The clipped bitmap should be square.
  EXPECT_EQ(40, clipped_bitmap.width());
  EXPECT_EQ(40, clipped_bitmap.height());
  // The input was wider than tall.
  EXPECT_EQ(ThumbnailGenerator::kWiderThanTall, clip_result);
}

TEST(ThumbnailGeneratorSimpleTest, GetClippedBitmap_NotClipped) {
  // The input bitmap is square.
  gfx::CanvasSkia canvas(40, 40, true);
  const SkBitmap bitmap = canvas.getTopPlatformDevice().accessBitmap(false);

  // The desired size is square.
  ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped;
  SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap(
      bitmap, 10, 10, &clip_result);
  // The clipped bitmap should be square.
  EXPECT_EQ(40, clipped_bitmap.width());
  EXPECT_EQ(40, clipped_bitmap.height());
  // There was no need to clip.
  EXPECT_EQ(ThumbnailGenerator::kNotClipped, clip_result);
}

TEST(ThumbnailGeneratorSimpleTest, GetClippedBitmap_NonSquareOutput) {
  // The input bitmap is square.
  gfx::CanvasSkia canvas(40, 40, true);
  const SkBitmap bitmap = canvas.getTopPlatformDevice().accessBitmap(false);

  // The desired size is horizontally long.
  ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped;
  SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap(
      bitmap, 20, 10, &clip_result);
  // The clipped bitmap should have the same aspect ratio of the desired size.
  EXPECT_EQ(40, clipped_bitmap.width());
  EXPECT_EQ(20, clipped_bitmap.height());
  // The input was taller than wide.
  EXPECT_EQ(ThumbnailGenerator::kTallerThanWide, clip_result);
}

// A mock version of TopSites, used for testing ShouldUpdateThumbnail().
class MockTopSites : public history::TopSites {
 public:
  explicit MockTopSites(Profile* profile)
      : history::TopSites(profile),
        capacity_(1) {
  }

  // history::TopSites overrides.
  virtual bool IsFull() {
    return known_url_map_.size() >= capacity_;
  }
  virtual bool IsKnownURL(const GURL& url) {
    return known_url_map_.find(url.spec()) != known_url_map_.end();
  }
  virtual bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) {
    std::map<std::string, ThumbnailScore>::const_iterator iter =
        known_url_map_.find(url.spec());
    if (iter == known_url_map_.end()) {
      return false;
    } else {
      *score = iter->second;
      return true;
    }
  }

  // Adds a known URL with the associated thumbnail score.
  void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
    known_url_map_[url.spec()] = score;
  }

 private:
  virtual ~MockTopSites() {}
  size_t capacity_;
  std::map<std::string, ThumbnailScore> known_url_map_;
};

TEST(ThumbnailGeneratorSimpleTest, ShouldUpdateThumbnail) {
  const GURL kGoodURL("http://www.google.com/");
  const GURL kBadURL("chrome://newtab");

  // Set up the profile.
  TestingProfile profile;

  // Set up the top sites service.
  ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  scoped_refptr<MockTopSites> top_sites(new MockTopSites(&profile));

  // Should be false because it's a bad URL.
  EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail(
      &profile, top_sites.get(), kBadURL));

  // Should be true, as it's a good URL.
  EXPECT_TRUE(ThumbnailGenerator::ShouldUpdateThumbnail(
      &profile, top_sites.get(), kGoodURL));

  // Should be false, if it's in the incognito mode.
  profile.set_incognito(true);
  EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail(
      &profile, top_sites.get(), kGoodURL));

  // Should be true again, once turning off the incognito mode.
  profile.set_incognito(false);
  EXPECT_TRUE(ThumbnailGenerator::ShouldUpdateThumbnail(
      &profile, top_sites.get(), kGoodURL));

  // Add a known URL. This makes the top sites data full.
  ThumbnailScore bad_score;
  bad_score.time_at_snapshot = base::Time::UnixEpoch();  // Ancient time stamp.
  top_sites->AddKnownURL(kGoodURL, bad_score);
  ASSERT_TRUE(top_sites->IsFull());

  // Should be false, as the top sites data is full, and the new URL is
  // not known.
  const GURL kAnotherGoodURL("http://www.youtube.com/");
  EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail(
      &profile, top_sites.get(), kAnotherGoodURL));

  // Should be true, as the existing thumbnail is bad (i.e need a better one).
  EXPECT_TRUE(ThumbnailGenerator::ShouldUpdateThumbnail(
      &profile, top_sites.get(), kGoodURL));

  // Replace the thumbnail score with a really good one.
  ThumbnailScore good_score;
  good_score.time_at_snapshot = base::Time::Now();  // Very new.
  good_score.at_top = true;
  good_score.good_clipping = true;
  good_score.boring_score = 0.0;
  top_sites->AddKnownURL(kGoodURL, good_score);

  // Should be false, as the existing thumbnail is good enough (i.e. don't
  // need to replace the existing thumbnail which is new and good).
  EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail(
      &profile, top_sites.get(), kGoodURL));
}