Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <gtest/gtest.h>
     18 
     19 #include <binder/IMemory.h>
     20 #include <surfaceflinger/ISurfaceComposer.h>
     21 #include <surfaceflinger/Surface.h>
     22 #include <surfaceflinger/SurfaceComposerClient.h>
     23 #include <utils/String8.h>
     24 
     25 namespace android {
     26 
     27 class SurfaceTest : public ::testing::Test {
     28 protected:
     29     virtual void SetUp() {
     30         mComposerClient = new SurfaceComposerClient;
     31         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
     32 
     33         mSurfaceControl = mComposerClient->createSurface(
     34                 String8("Test Surface"), 0, 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
     35 
     36         ASSERT_TRUE(mSurfaceControl != NULL);
     37         ASSERT_TRUE(mSurfaceControl->isValid());
     38 
     39         SurfaceComposerClient::openGlobalTransaction();
     40         ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
     41         ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
     42         SurfaceComposerClient::closeGlobalTransaction();
     43 
     44         mSurface = mSurfaceControl->getSurface();
     45         ASSERT_TRUE(mSurface != NULL);
     46     }
     47 
     48     virtual void TearDown() {
     49         mComposerClient->dispose();
     50     }
     51 
     52     sp<Surface> mSurface;
     53     sp<SurfaceComposerClient> mComposerClient;
     54     sp<SurfaceControl> mSurfaceControl;
     55 };
     56 
     57 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
     58     sp<ANativeWindow> anw(mSurface);
     59     int result = -123;
     60     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
     61             &result);
     62     EXPECT_EQ(NO_ERROR, err);
     63     EXPECT_EQ(1, result);
     64 }
     65 
     66 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
     67     mSurfaceControl.clear();
     68 
     69     sp<ANativeWindow> anw(mSurface);
     70     int result = -123;
     71     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
     72             &result);
     73     EXPECT_EQ(NO_ERROR, err);
     74     EXPECT_EQ(1, result);
     75 }
     76 
     77 // This test probably doesn't belong here.
     78 TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
     79     sp<ANativeWindow> anw(mSurface);
     80 
     81     // Verify the screenshot works with no protected buffers.
     82     sp<IMemoryHeap> heap;
     83     uint32_t w=0, h=0;
     84     PixelFormat fmt=0;
     85     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
     86     ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt, 64, 64, 0,
     87             0x7fffffff));
     88     ASSERT_TRUE(heap != NULL);
     89 
     90     // Set the PROTECTED usage bit and verify that the screenshot fails.  Note
     91     // that we need to dequeue a buffer in order for it to actually get
     92     // allocated in SurfaceFlinger.
     93     ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
     94             GRALLOC_USAGE_PROTECTED));
     95     ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
     96     ANativeWindowBuffer* buf = 0;
     97 
     98     status_t err = anw->dequeueBuffer(anw.get(), &buf);
     99     if (err) {
    100         // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
    101         // that's okay as long as this is the reason for the failure.
    102         // try again without the GRALLOC_USAGE_PROTECTED bit.
    103         ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
    104         ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
    105         return;
    106     }
    107     ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf));
    108 
    109     for (int i = 0; i < 4; i++) {
    110         // Loop to make sure SurfaceFlinger has retired a protected buffer.
    111         ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
    112         ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf));
    113         ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf));
    114     }
    115     heap = 0;
    116     w = h = fmt = 0;
    117     ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt,
    118             64, 64, 0, 0x7fffffff));
    119     ASSERT_TRUE(heap != NULL);
    120 }
    121 
    122 TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
    123     sp<ANativeWindow> anw(mSurface);
    124     int result = -123;
    125     int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
    126     EXPECT_EQ(NO_ERROR, err);
    127     EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
    128 }
    129 
    130 }
    131