Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: chrome/browser/tab_contents/thumbnail_generator_unittest.cc

Issue 118420: Adds kind-of-live thumbnail generation for a potential tab switcher. This... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/tab_contents/thumbnail_generator.cc ('k') | chrome/chrome.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 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 "base/basictypes.h"
6 #include "chrome/browser/renderer_host/backing_store_manager.h"
7 #include "chrome/browser/renderer_host/mock_render_process_host.h"
8 #include "chrome/browser/renderer_host/test_render_view_host.h"
9 #include "chrome/browser/tab_contents/thumbnail_generator.h"
10 #include "chrome/common/notification_service.h"
11 #include "chrome/common/render_messages.h"
12 #include "chrome/common/transport_dib.h"
13 #include "chrome/test/testing_profile.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/skia/include/core/SkColorPriv.h"
16
17 static const int kBitmapWidth = 100;
18 static const int kBitmapHeight = 100;
19
20 class ThumbnailGeneratorTest : public testing::Test {
21 public:
22 ThumbnailGeneratorTest()
23 : profile_(),
24 process_(new MockRenderProcessHost(&profile_)),
25 widget_(process_, 1),
26 view_(&widget_) {
27 // Paiting will be skipped if there's no view.
28 widget_.set_view(&view_);
29
30 // Need to send out a create notification for the RWH to get hooked. This is
31 // a little scary in that we don't have a RenderView, but the only listener
32 // will want a RenderWidget, so it works out OK.
33 NotificationService::current()->Notify(
34 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
35 Source<RenderViewHostManager>(NULL),
36 Details<RenderViewHost>(reinterpret_cast<RenderViewHost*>(&widget_)));
37
38 transport_dib_.reset(TransportDIB::Create(kBitmapWidth * kBitmapHeight * 4,
39 1));
40
41 // We don't want to be sensitive to timing.
42 generator_.set_no_timeout(true);
43 }
44
45 protected:
46 // Indicates what bitmap should be sent with the paint message. _OTHER will
47 // only be retrned by CheckFirstPixel if the pixel is none of the others.
48 enum TransportType { TRANSPORT_BLACK, TRANSPORT_WHITE, TRANSPORT_OTHER };
49
50 void SendPaint(TransportType type) {
51 ViewHostMsg_PaintRect_Params params;
52 params.bitmap_rect = gfx::Rect(0, 0, kBitmapWidth, kBitmapHeight);
53 params.view_size = params.bitmap_rect.size();
54 params.flags = 0;
55
56 scoped_ptr<skia::PlatformCanvas> canvas(
57 transport_dib_->GetPlatformCanvas(kBitmapWidth, kBitmapHeight));
58 switch (type) {
59 case TRANSPORT_BLACK:
60 canvas->getTopPlatformDevice().accessBitmap(true).eraseARGB(
61 0xFF, 0, 0, 0);
62 break;
63 case TRANSPORT_WHITE:
64 canvas->getTopPlatformDevice().accessBitmap(true).eraseARGB(
65 0xFF, 0xFF, 0xFF, 0xFF);
66 break;
67 case TRANSPORT_OTHER:
68 default:
69 NOTREACHED();
70 break;
71 }
72
73 params.bitmap = transport_dib_->id();
74
75 ViewHostMsg_PaintRect msg(1, params);
76 widget_.OnMessageReceived(msg);
77 }
78
79 TransportType ClassifyFirstPixel(const SkBitmap& bitmap) {
80 // Returns the color of the first pixel of the bitmap. The bitmap must be
81 // non-empty.
82 SkAutoLockPixels lock(bitmap);
83 uint32 pixel = *bitmap.getAddr32(0, 0);
84
85 if (SkGetPackedA32(pixel) != 0xFF)
86 return TRANSPORT_OTHER; // All values expect an opqaue alpha channel
87
88 if (SkGetPackedR32(pixel) == 0 &&
89 SkGetPackedG32(pixel) == 0 &&
90 SkGetPackedB32(pixel) == 0)
91 return TRANSPORT_BLACK;
92
93 if (SkGetPackedR32(pixel) == 0xFF &&
94 SkGetPackedG32(pixel) == 0xFF &&
95 SkGetPackedB32(pixel) == 0xFF)
96 return TRANSPORT_WHITE;
97
98 return TRANSPORT_OTHER;
99 }
100
101 MessageLoopForUI message_loop_;
102
103 TestingProfile profile_;
104
105 // This will get deleted when the last RHWH associated with it is destroyed.
106 MockRenderProcessHost* process_;
107
108 RenderWidgetHost widget_;
109 TestRenderWidgetHostView view_;
110 ThumbnailGenerator generator_;
111
112 scoped_ptr<TransportDIB> transport_dib_;
113
114 private:
115 // testing::Test implementation.
116 void SetUp() {
117 }
118 void TearDown() {
119 }
120 };
121
122 TEST_F(ThumbnailGeneratorTest, NoThumbnail) {
123 // This is the case where there is no thumbnail available on the tab and
124 // there is no backing store. There should be no image returned.
125 SkBitmap result = generator_.GetThumbnailForRenderer(&widget_);
126 EXPECT_TRUE(result.isNull());
127 }
128
129 // Tests basic thumbnail generation when a backing store is discarded.
130 TEST_F(ThumbnailGeneratorTest, DiscardBackingStore) {
131 // First set up a backing store and then discard it.
132 SendPaint(TRANSPORT_BLACK);
133 widget_.WasHidden();
134 ASSERT_TRUE(BackingStoreManager::ExpireBackingStoreForTest(&widget_));
135 ASSERT_FALSE(widget_.GetBackingStore(false));
136
137 // The thumbnail generator should have stashed a thumbnail of the page.
138 SkBitmap result = generator_.GetThumbnailForRenderer(&widget_);
139 ASSERT_FALSE(result.isNull());
140 EXPECT_EQ(TRANSPORT_BLACK, ClassifyFirstPixel(result));
141 }
142
143 TEST_F(ThumbnailGeneratorTest, QuickShow) {
144 // Set up a hidden widget with a black cached thumbnail and an expired
145 // backing store.
146 SendPaint(TRANSPORT_BLACK);
147 widget_.WasHidden();
148 ASSERT_TRUE(BackingStoreManager::ExpireBackingStoreForTest(&widget_));
149 ASSERT_FALSE(widget_.GetBackingStore(false));
150
151 // Now show the widget and paint white.
152 widget_.WasRestored();
153 SendPaint(TRANSPORT_WHITE);
154
155 // The black thumbnail should still be cached because it hasn't processed the
156 // timer message yet.
157 SkBitmap result = generator_.GetThumbnailForRenderer(&widget_);
158 ASSERT_FALSE(result.isNull());
159 EXPECT_EQ(TRANSPORT_BLACK, ClassifyFirstPixel(result));
160
161 // Running the message loop will process the timer, which should expire the
162 // cached thumbnail. Asking again should give us a new one computed from the
163 // backing store.
164 message_loop_.RunAllPending();
165 result = generator_.GetThumbnailForRenderer(&widget_);
166 ASSERT_FALSE(result.isNull());
167 EXPECT_EQ(TRANSPORT_WHITE, ClassifyFirstPixel(result));
168 }
OLDNEW
« no previous file with comments | « chrome/browser/tab_contents/thumbnail_generator.cc ('k') | chrome/chrome.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698