package com.fuzz.indicator;

import android.database.DataSetObserver;
import android.view.ViewTreeObserver;

import com.fuzz.indicator.proxy.StateProxy;
import com.fuzz.indicator.proxy.UnavailableProxy;

import org.junit.Test;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Test of whether calls to {@link StateProxy#associateWith} are always mirrored by
 * calls to {@link StateProxy#disassociateFrom}.
 *
 * @author Philip Cohn-Cort (Fuzz)
 */
public class StateProxyBalancingTest {

    @Test
    public void associateThenDisassociateWithNoChanges() throws Exception {
        CutoutViewIndicator indicator = mock(CutoutViewIndicator.class);


        // Setup

        indicator.dataSetObserver = mock(DataSetObserver.class);
        indicator.stateProxy = new UnavailableProxy();
        ViewTreeObserver vtoMock = mock(ViewTreeObserver.class);

        when(indicator.getViewTreeObserver()).thenReturn(vtoMock);
        doCallRealMethod().when(indicator).setStateProxy(any(StateProxy.class));

        StateProxy proxyA = mock(StateProxy.class);
        when(proxyA.canObserve(any(DataSetObserver.class))).thenReturn(false);

        StateProxy proxyB = mock(StateProxy.class);
        when(proxyB.canObserve(any(DataSetObserver.class))).thenReturn(true);


        // The test

        indicator.setStateProxy(proxyA);

        verify(proxyA).canObserve(any(DataSetObserver.class));
        verify(proxyA, never()).associateWith(any(DataSetObserver.class));

        indicator.setStateProxy(proxyB);

        verify(proxyA, never()).disassociateFrom(any(DataSetObserver.class));

        verify(proxyB).canObserve(any(DataSetObserver.class));
        verify(proxyB).associateWith(any(DataSetObserver.class));
    }
}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2017 Philip Cohn-Cort%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -798,16 +798,41 @@
 Proxy;%0A%0A
+import org.junit.Before;%0A
 import o
@@ -1122,16 +1122,17 @@
 %0A * Test
+s
  of whet
@@ -1339,68 +1339,233 @@
     
-@Test%0A    public void associateThenDisassociateWithNoChanges
+protected CutoutViewIndicator indicator = mock(CutoutViewIndicator.class);%0A    protected StateProxy proxyA = mock(StateProxy.class);%0A    protected StateProxy proxyB = mock(StateProxy.class);%0A%0A    @Before%0A    public void setUp
 () t
@@ -1582,24 +1582,25 @@
 ption %7B%0A
+%0A
         
 CutoutVi
@@ -1595,91 +1595,64 @@
     
-CutoutViewIndicator indicator = mock(CutoutViewIndicator.class);%0A%0A%0A        // Setup
+ViewTreeObserver vtoMock = mock(ViewTreeObserver.class);
 %0A%0A  
@@ -1772,73 +1772,8 @@
 y();
-%0A        ViewTreeObserver vtoMock = mock(ViewTreeObserver.class);
 %0A%0A  
@@ -1923,60 +1923,8 @@
 );%0A%0A
-        StateProxy proxyA = mock(StateProxy.class);%0A
     
@@ -2003,60 +2003,8 @@
 );%0A%0A
-        StateProxy proxyB = mock(StateProxy.class);%0A
     
@@ -2081,29 +2081,794 @@
 e);%0A
-%0A%0A        // The test
+    %7D%0A%0A    @Test%0A    public void associateThenDisassociateWithNoChanges() throws Exception %7B%0A        when(proxyA.canObserve(any(DataSetObserver.class))).thenReturn(false);%0A%0A        indicator.setStateProxy(proxyA);%0A%0A        verify(proxyA).canObserve(any(DataSetObserver.class));%0A        verify(proxyA, never()).associateWith(any(DataSetObserver.class));%0A%0A        indicator.setStateProxy(proxyB);%0A%0A        verify(proxyA, never()).disassociateFrom(any(DataSetObserver.class));%0A%0A        verify(proxyB).canObserve(any(DataSetObserver.class));%0A        verify(proxyB).associateWith(any(DataSetObserver.class));%0A    %7D%0A%0A    @Test%0A    public void associateThenDisassociateWithChangedObservability() throws Exception %7B%0A        when(proxyA.canObserve(any(DataSetObserver.class))).thenReturn(false);
 %0A%0A  
@@ -3038,32 +3038,164 @@
 erver.class));%0A%0A
+        // Change whether this proxy can be observed%0A        when(proxyA.canObserve(any(DataSetObserver.class))).thenReturn(true);%0A%0A
         indicato

Test whether StateProxies' observability is ignored during disassociation