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 <gui/ISurfaceComposer.h>
     21 #include <gui/Surface.h>
     22 #include <gui/SurfaceComposerClient.h>
     23 #include <utils/String8.h>
     24 
     25 #include <private/gui/ComposerService.h>
     26 
     27 namespace android {
     28 
     29 class SurfaceTest : public ::testing::Test {
     30 protected:
     31     virtual void SetUp() {
     32         mComposerClient = new SurfaceComposerClient;
     33         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
     34 
     35         mSurfaceControl = mComposerClient->createSurface(
     36                 String8("Test Surface"), 0, 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
     37 
     38         ASSERT_TRUE(mSurfaceControl != NULL);
     39         ASSERT_TRUE(mSurfaceControl->isValid());
     40 
     41         SurfaceComposerClient::openGlobalTransaction();
     42         ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
     43         ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
     44         SurfaceComposerClient::closeGlobalTransaction();
     45 
     46         mSurface = mSurfaceControl->getSurface();
     47         ASSERT_TRUE(mSurface != NULL);
     48     }
     49 
     50     virtual void TearDown() {
     51         mComposerClient->dispose();
     52     }
     53 
     54     sp<Surface> mSurface;
     55     sp<SurfaceComposerClient> mComposerClient;
     56     sp<SurfaceControl> mSurfaceControl;
     57 };
     58 
     59 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
     60     sp<ANativeWindow> anw(mSurface);
     61     int result = -123;
     62     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
     63             &result);
     64     EXPECT_EQ(NO_ERROR, err);
     65     EXPECT_EQ(1, result);
     66 }
     67 
     68 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
     69     mSurfaceControl.clear();
     70 
     71     sp<ANativeWindow> anw(mSurface);
     72     int result = -123;
     73     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
     74             &result);
     75     EXPECT_EQ(NO_ERROR, err);
     76     EXPECT_EQ(1, result);
     77 }
     78 
     79 // This test probably doesn't belong here.
     80 TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
     81     sp<ANativeWindow> anw(mSurface);
     82 
     83     // Verify the screenshot works with no protected buffers.
     84     sp<IMemoryHeap> heap;
     85     uint32_t w=0, h=0;
     86     PixelFormat fmt=0;
     87     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
     88     ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt, 64, 64, 0,
     89             0x7fffffff));
     90     ASSERT_TRUE(heap != NULL);
     91 
     92     // Set the PROTECTED usage bit and verify that the screenshot fails.  Note
     93     // that we need to dequeue a buffer in order for it to actually get
     94     // allocated in SurfaceFlinger.
     95     ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
     96             GRALLOC_USAGE_PROTECTED));
     97     ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
     98     ANativeWindowBuffer* buf = 0;
     99 
    100     status_t err = anw->dequeueBuffer(anw.get(), &buf);
    101     if (err) {
    102         // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
    103         // that's okay as long as this is the reason for the failure.
    104         // try again without the GRALLOC_USAGE_PROTECTED bit.
    105         ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
    106         ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
    107         return;
    108     }
    109     ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf));
    110 
    111     for (int i = 0; i < 4; i++) {
    112         // Loop to make sure SurfaceFlinger has retired a protected buffer.
    113         ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
    114         ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf));
    115         ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf));
    116     }
    117     heap = 0;
    118     w = h = fmt = 0;
    119     ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt,
    120             64, 64, 0, 0x7fffffff));
    121     ASSERT_TRUE(heap != NULL);
    122 }
    123 
    124 TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
    125     sp<ANativeWindow> anw(mSurface);
    126     int result = -123;
    127     int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
    128     EXPECT_EQ(NO_ERROR, err);
    129     EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
    130 }
    131 
    132 }
    133