OLD | NEW |
(Empty) | |
| 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 #include <windows.h> |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/win/scoped_hdc.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 |
| 12 namespace { |
| 13 // Helper class that allows testing ScopedDC<T>. |
| 14 class TestScopedDC : public base::win::ScopedDC<TestScopedDC> { |
| 15 public: |
| 16 explicit TestScopedDC(HDC hdc) |
| 17 : ScopedDC(hdc) { |
| 18 } |
| 19 |
| 20 private: |
| 21 friend class base::win::ScopedDC<TestScopedDC>; |
| 22 |
| 23 void DisposeDC(HDC hdc) { |
| 24 // We leak the DC, so we can test its state. The test itself |
| 25 // will dispose of the dc later. |
| 26 } |
| 27 |
| 28 DISALLOW_COPY_AND_ASSIGN(TestScopedDC); |
| 29 }; |
| 30 |
| 31 bool IsValidDC(HDC hdc) { |
| 32 // The theory here is that any (cheap) GDI operation should fail for |
| 33 // an invalid dc. |
| 34 return GetCurrentObject(hdc, OBJ_BITMAP) != NULL; |
| 35 } |
| 36 |
| 37 } // namespace. |
| 38 |
| 39 TEST(BaseWinScopedDC, CreateDestroy) { |
| 40 HDC hdc1; |
| 41 { |
| 42 base::win::ScopedGetDC dc1(NULL); |
| 43 hdc1 = dc1.Get(); |
| 44 EXPECT_TRUE(IsValidDC(hdc1)); |
| 45 } |
| 46 EXPECT_FALSE(IsValidDC(hdc1)); |
| 47 |
| 48 HDC hdc2 = CreateDC(L"DISPLAY", NULL, NULL, NULL); |
| 49 ASSERT_TRUE(IsValidDC(hdc2)); |
| 50 { |
| 51 base::win::ScopedCreateDC dc2(hdc2); |
| 52 EXPECT_TRUE(IsValidDC(hdc2)); |
| 53 } |
| 54 EXPECT_FALSE(IsValidDC(hdc2)); |
| 55 } |
| 56 |
| 57 TEST(BaseWinScopedDC, SelectObjects) { |
| 58 HDC hdc = CreateCompatibleDC(NULL); |
| 59 ASSERT_TRUE(IsValidDC(hdc)); |
| 60 HGDIOBJ bitmap = GetCurrentObject(hdc, OBJ_BITMAP); |
| 61 HGDIOBJ brush = GetCurrentObject(hdc, OBJ_BRUSH); |
| 62 HGDIOBJ pen = GetCurrentObject(hdc, OBJ_PEN); |
| 63 HGDIOBJ font = GetCurrentObject(hdc, OBJ_FONT); |
| 64 |
| 65 HBITMAP compat_bitmap = CreateCompatibleBitmap(hdc, 24, 24); |
| 66 ASSERT_TRUE(compat_bitmap != NULL); |
| 67 HBRUSH solid_brush = CreateSolidBrush(RGB(22, 33, 44)); |
| 68 ASSERT_TRUE(solid_brush != NULL); |
| 69 |
| 70 { |
| 71 TestScopedDC dc2(hdc); |
| 72 dc2.SelectBitmap(compat_bitmap); |
| 73 dc2.SelectBrush(solid_brush); |
| 74 EXPECT_TRUE(bitmap != GetCurrentObject(hdc, OBJ_BITMAP)); |
| 75 EXPECT_TRUE(brush != GetCurrentObject(hdc, OBJ_BRUSH)); |
| 76 } |
| 77 |
| 78 EXPECT_TRUE(bitmap == GetCurrentObject(hdc, OBJ_BITMAP)); |
| 79 EXPECT_TRUE(brush == GetCurrentObject(hdc, OBJ_BRUSH)); |
| 80 EXPECT_TRUE(pen == GetCurrentObject(hdc, OBJ_PEN)); |
| 81 EXPECT_TRUE(font == GetCurrentObject(hdc, OBJ_FONT)); |
| 82 |
| 83 EXPECT_TRUE(DeleteDC(hdc)); |
| 84 EXPECT_TRUE(DeleteObject(compat_bitmap)); |
| 85 EXPECT_TRUE(DeleteObject(solid_brush)); |
| 86 } |
OLD | NEW |