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